some coding help

hi guys- just a quick question.

i want in my program to run 2 separate loops - a control loop - running at 120Hz with a runtime of ~6ms, and a communications loop - running at 25Hz with a runtime of ~3ms.

to do this i set up 2 timers (using the Atmega2560) to run at these speeds... now here comes the tricky part- i cant run the loops inside the ISR since the arduino framework doesnt allow nested interrupts which is required to read the digital sensors and receive incoming serial data and PPM data... the only solution i found was to raise flags in the ISR's and then run the loops inside the main Loop... this causes some problems such as inconsistent run frequencies (problematic for the PID).

how can i design the infrastructure of my program so that i run these loop at fixed constant speeds? ( please elaborate with code, i'm no AVR expert...)

Thanks, Edi

Just set flags in the two ISRs and test for them in a main loop.

OOps, I just realized you mentioned that.

this causes some problems such as inconsistent run frequencies

Not sure why that would happen but it depends a lot on you code. Can you post it?

What does your "communications loop" do?

If it does a lot of serial work that could easily block other stuff.


Rob

that what i did but because the loops take 2 different times this results in bouncing frequencies at which the loops run. just to be clear, you prepose? this is what i did: loop() { if(flag 1) -> loop 1 if(flag 2) -> loop 2 } ISR1 : set flag 1 ISR2: set flag 2

the runtime result is something like this: loop1,loop1,loop1,loop2,loop1,loop1,loop1,loop2.... because its not in interrupts there are pauses in the loop1 frequency when loop2 runs.

loop2 does some serial work...

void setup() {  
  Serial.begin(38400);
  Wire.begin();
  tasker.init();   
  attachInterrupt(1, ppmInt , CHANGE);  // setup Interrupt 
}
void ppmInt() { tasker.triggerRCevent();}
void loop() {

     if(conLoopFlag) {
         tasker.conLoop();
         conLoopFlag=false;
     }
     if(comLoopFlag) {
         tasker.comLoop();
         comLoopFlag=false;
     }    
}
ISR(TIMER1_OVF_vect) {
// fast timer= conLoop
   conLoopFlag=true;
}
ISR(TIMER5_OVF_vect) {
// slow timer= comLoop
comLoopFlag=true;
}

What in the two xxxLoop() funcs?

At a freqs of 120 and 25Hz that will clash on occasion and that will cause jitter.

I suspect you may have to do all your comms using interrupts.


Rob

that exactly my problem! how to i fix that???

the loop functions are out of the scope of this question- the only relevant data is the fact that in the conLoop() I2C is being used, and in the comLoop() Serial is being used. in general, in the background incoming ppm data is always being analyzed (external interrupt) and serial data is constantly received...