Timer and Ticker issues

10 Dec 2012

Hello Everyone

I'm having trouble with sampling PPM signals properly. I'm already sampling my sensors at a 5ms rate using a ticker and there is no problem with that, but sampling PPM signals at that rate allows noise to massively disrupt my signals so i thought i'd create another ticker to sample it at 50Hz but after modifying my code to do that i found out that it's sampled at the same rate as my sensors although it has a different ticker with a different interrupt rate. so i had another idea to use a timer in a while loop and update both methods accordingly. This worked..... but after a while my mbed freezes.

so could someone tell me what i'm doing wrong

My code with Timer :

int main(){
 // initialization procedures 
 t.start();
 while(true){
 if (t.read_ms()-lastSensorUpdate>=5){
  lastSensorUpdate = t.read_ms();
  // get sensor data and calculate euler angles
} 
 if (t.read_ms()-latsRadioUpdate>=20){
 lastRadioUpdate = t.read_ms();
 // update radio signals procedures
}
}
}

my Code With Ticker :

void rx(void){
      getcmd(); // update PPM signals
}
void FlightControl(void){
      
      GyroRate();
      getAccAngle();
        kalman_predict(&filter_pitch, GyroX,RATE); 
        kalman_update(&filter_pitch, accangle[0]);
        kalman_predict(&filter_roll, GyroY,RATE); 
        kalman_update(&filter_roll, accangle[1]); 
        angle[0] = kalman_get_angle(&filter_pitch);
        angle[1] = kalman_get_angle(&filter_roll);
} 
int main() {
    
     //initialization procedures 
     rc.attach(&rx,0.02);
     t.attach(&FlightControl,0.005);
 }
10 Dec 2012

Timers are based on 32-bit int microsecond counters, so they can only time up to a maximum of 2^31-1 microseconds i.e. 30 minutes. That means your Timer code could run into a freeze situation when the timer overflows and your saved 'lasttime' was sampled just before the overflow occurred.

The main code in your second example may not run as expected because it may finish and stop.

int main() {
    
     //initialization procedures 
     rc.attach(&rx,0.02); // 20ms ticker
     t.attach(&FlightControl,0.005); // 5ms ticker
}

Try this instead:

int main() {

     //initialization procedures 
     rc.attach(&rx,0.02); // 20ms ticker
     t.attach(&FlightControl,0.005); // 5ms ticker

     while (1) {}; // endless loop
}

Also note that things go wrong when the called functions take longer than the tickerrate Could getcmd() take longer than 20ms to complete? The two functions might also interfere: the 5ms call overlaps with the 20ms interrupt every so often.

10 Dec 2012

Thank you for your response

my code only stops when using the timer method and it stops after 20 seconds of run time.

The issue with the Ticker is that both functions run at the same rate, i don't know how this happens. but if i run the rx method by itself it works perfectly

Siomon Ford said in the Ticker handbook (in the case of multiple Tickers):

Quote:

Hendrick; they don't execute in parallel, as one will get there first and they both have the same priority. So you should be fine

John; thanks for the pointer. I'll note that to be updated at the next natural point.

Simon

10 Dec 2012

Karim Azzouz wrote:

The issue with the Ticker is that both functions run at the same rate, i don't know how this happens. but if i run the rx method by itself it works perfectly

I assume the functions are somehow linked by using the same shared variables? That could explain dependance. You may have to declare (some of the) shared variables as 'volatile' to prevent problems. Can you try debugging by setting and monitoring output pins with a scope or analyser to find out what is going on.

10 Dec 2012

i had the same thought, but after checking i found out that they're not linked.

I'm thinking about using mbed RTOS to solve this problem, i'll modify my code to include RTOS and see if it will fix the problem