help please with some software design.

hey guys, 

wasn't quite sure where to post this so i'm putting it here : please refer me if i misplaced this post.

im building  a quadrotor on my own from scratch. now- im at a quite advanced state.

my problem is software related mostly. my quad is also arduino based and has a arduinoMega2560 at its hart. the sensors(IMU) i use are the ones on the 9dof sensor stick by sparkfun - (, communication is done by i2c. here comes the problem:

from what i understand the PID control loop needs to be running at at least 100Hz, more so i read that the frequency stability is also really important.  right now my problem is that my control isn't tight enough and doesn't respond quickly enough to my inputs. i think the cause to the problem is the fact that my control loop is running at ~150Hz but very unstable. (by that i mean lots of bouncing in the frequencies)

in my program i have 2 loops each have a separate timer setup for them: the Control loop and the Coms loop. the control loop does the following: read the sensors (i2c),read the RC remote,calculate PID,update motor. the Coms loop handles communication with the ground station computer. the 2 timers i set up each have a separate handler each setting a flag and then the main loop looks something like:



  if(ControlLoopFlag)  -> execute control loop, lower the flag.

  if(ComsLoopFlag)    -> execute Coms Loop, lower the flag.

at the end of each loop if the execution time is slower than a certain number then i wait. (to prevent spikes in the frequency). i was forced to adapt this design because the Arduino framework in which i work doesn't allow nested interrupts meaning i cant read the i2c sensors and handle serial info in an interrupt handler( which is what i would like to do : put the whole loop in a ISR thus causing a steady frequency). i also tried enabling the interrupts inside the ISR and then reading the sensors but that crashed the controller. if such a thing was possible i would be able to stick the whole loops inside separate ISR's and solve the problem. (if u still dont understand then ill try to explain. since the control loop isnt inside an ISR (only the flag setting is) i cant stop the com loop in the middle thus causing unstable run frequency on the control loop. the same goes for the com loop)

my question to you guys is what design do i need to use(or how to modify mine) in order to ensure a stable frequency?

thanks in advance for all the comments.

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • Hi Edi,

    You should use Finite State Machines. This kind of structure, will let you handle task, like some kind of "non-blocking Task".    State Machines are well situated  to be implemented on I2C protocols, "without Interrupts". This could make, your code run faster!

    I don't understand very well, why do you wait?  may be, that it is the problem! "there are some times where your micro it is doing Nothing!". Your main loop, it looks well. Another thing.... It is not right "not recommended" write all the code on the ISR handler. This will prevent that the program will not miss another "important" interrupt. A good program, will use a simple Flag in the ISR handler and the same structure  that you have in the main Loop.

    By the way, I use the 9DOF sensor stick Too. I implemented a State machine, to read the all three sensors. For me it is important since the microcontroller it is running at 50Mhz without Interrupt service on the I2C protocol.At 50 Mhz, wait for something it's completed, it means thousands of cycles are wasted.

    Try to implement Finite State Machines.
    Regards. - This website is for sale! - protocol Resources and Information.
    This website is for sale! is your first and best source for all of the information you’re looking for. From general topics to more of wha…
  • Why do you invent the wheel by your own .... again?

    Why not use the working Arducopter setup and improve that?

  • anyone care to share there opinion?

This reply was deleted.