Pages: [1]   Go Down
Author Topic: reading RC signal and timer affect unpleasant!!  (Read 785 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hallo I have the arduino 2009 with ATmega328.
in my projekt i read the RC Signal (PWM)from my receiver. from this data I generate a frequenze between 0 - 15 kHz for a Steppercontroll (clock and Direction).
I read the time in millis() between High and High. on Pin 2.
then I make a lowpass filter to smoth the Result.
in next step i interpolate the range 0 to 500 to 1500 on left and 1500 on right in an "nonlinear shape."("Xsmoth" in my projekt)
then fast PWM port (9) to 65535,
and create an timeroverflow evry 65535 counts.
Xsmoth is the speed how fast the counter counts.on pin 9 I create a squarewave for my Steppercontroll (smc11 (www.nanotec.de))

It works fine but there are 2 problems.
first the reading of my RC Signal "jumps"somtimes to impossible values. bevore i use the timeroverflow it works fine.... maybe its a timing problem?

the second is, if I initialize the arduino or reset it at first i have a high frequence on pin 9 for a small moment. its not good because my stepper will turn some steps unintended.

can sombody help me please.
this is my code.....
Code:
/////////////////////////////////for the timer////////////////////////////////
float Xsmoth;                            //final smoothstepped value
int taktPin = 9;
void writetakt( uint8_t val ) {
  OCR1A = (val);
}
uint16_t phase = 0;
uint16_t speed = 0;
uint8_t sample = 0;

SIGNAL(TIMER1_OVF_vect){
  writetakt(sample);
  phase += speed;

  if(phase >= 32768)
  sample = 255;
  else
  sample = 0;
  if (Xsmoth < 1)
   sample = 0;
}
/////////////////////////////////reading PWM////////////////////////////////
int RCPin = 2;                                                               //input rc signal
int Motor_drehrichtung = 13;                                                // direction for steppercontroll
int timeX = 0;                                                             // rc signal
int timeX_old = 0;                                                        // old rc signal from last loop
int timeX_final = 0;                                                     // lowpassed RC Signal
int Zero_delay_timeLED = 250;                                           // zeropoint of Remotecontroll (not used)

long lastPulseX = 0;                                                  // last pulse in millis
long mstime = 0;                                                     // read time Millisecunden
long hptime = 0;                                                    //  read time Microsecunden

extern volatile unsigned long timer0_overflow_count;
unsigned long hpticks (void)
{
 return (timer0_overflow_count << 8) + TCNT0;
}

/////////////////////////////////non linear interpolation////////////////////////////////

#define SMOOTHSTEP( Xsmoth) (( Xsmoth) * ( Xsmoth) * (3 - 2 * ( Xsmoth)))
#define SMOOTHSTEP_plus( Xsmoth_plus) (( Xsmoth_plus) * ( Xsmoth_plus) * (3 - 2 * ( Xsmoth_plus)))

float TimeX_Min_Val_minus = 1500.0;            //Input Min Value ,hier wird die zu Interpolierende Range festgelegt
float TimeX_MAX_Val_minus = 0.0;              //Input Max Value, hier wird die zu Interpolierende Range festgelegt
float TimeX_Min_Val_plus = 0.0;              //Input Min Value ,hier wird die zu Interpolierende Range festgelegt
float TimeX_MAX_Val_plus = 1500.0;          //Input Max Value ,hier wird die zu Interpolierende Range festgelegt
float N = 250.0;  
float Nplus = 250.0;                      //limit input
float v;                                 //smoothstep expression variable


                                          
float Xsmoth_plus = (Xsmoth - 250);     //final smoothstepped value for left turning

/////////////////////////////////////

void setup() {
 Serial.begin(115200);                                      // Kommunikation PC

 pinMode (taktPin, OUTPUT);                                 / /Frequenze output
 pinMode(RCPin, INPUT);                                    //RC Pin as Input
 pinMode(Motor_drehrichtung, OUTPUT);                  //Direction Steppercontroll

 TCCR1A = _BV(WGM10) | _BV(COM1A1);
 TCCR1B = _BV(CS10) | _BV(WGM12);

 TIMSK1 |= _BV(TOIE1);

}
void loop() {

 if(millis() - lastPulseX >= 5)                               //read every 5 millis
 {
   while(!digitalRead(RCPin) == HIGH)                         //wait for next RC high
   {
     continue;
   }
   mstime = millis();
   hptime = hpticks();                                        //an recive RC Signal, record
   while(!digitalRead(RCPin) == LOW){
     continue;
   }
   mstime = millis();
   timeX = (hpticks()) - hptime - 220;                       //diference bitween is the result (my PWM)

////////////////////////////////////////Lowpass///////////////////////////////////////////////

   timeX_final = (0.1 * timeX) + (0.9 * timeX_old);       //10 percent from first value add to 90 percent from second value
  
  /////////////////////////////////// border from 0 to 500////////////////////////////////////////////////////////////////////////////
if (timeX_final > 500)                                  
   {timeX_final = 500;}

if (timeX_final < 0)
   {timeX_final = 0;}
///////////////////////////////////////value CW and CCW translation///////////////////////////////////////////////////////////////////
if (timeX_final < 250)
  {
    v = timeX_final / N;                                                            // Unterteilungen geteilt durch die nummer an schritten.
    v = SMOOTHSTEP(v);                                                             // Starten der smoothstep expression on v.
    Xsmoth = (TimeX_MAX_Val_minus * v) + (TimeX_Min_Val_minus * (1 - v));         // Lineare Interpolation Ausführen auf basis der zuvor gesetzten werte
   digitalWrite (Motor_drehrichtung,HIGH);
  }



if (timeX_final > 250)
  {
    v = (timeX_final - 250) / Nplus;                                                         // Unterteilungen geteilt durch die nummer an schritten.
    v = SMOOTHSTEP(v);                                                                      // Starten der smoothstep expression on v.
    Xsmoth = ((TimeX_MAX_Val_plus * v) + (TimeX_Min_Val_plus * (1 - v)));                  // Lineare Interpolation Ausführen auf basis der zuvor gesetzten werte
    digitalWrite (Motor_drehrichtung,LOW);
  }

////////////////////////////////////////////for next loop//////////////////////////////////////
 timeX_old = timeX_final;
 
 speed = Xsmoth;

 Serial.println(Xsmoth);  //PC out




///////////////////////////////////////////for next loop////////////////////////////////////////////////
   hptime = 0;
   lastPulseX = millis();
 }  

}

 
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://de.nanotec.com/start.html is the right link...sorry..
Logged

Pages: [1]   Go Up
Jump to: