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 - (http://www.sparkfun.com/products/10724), 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:

 

while(1)

  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 –

Replies

  • 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. 

    protocol.at - This website is for sale! - protocol Resources and Information.
    This website is for sale! protocol.at 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.

Activity

DIY Drones via Twitter
RT @chr1sa: After more than a year of only virtual races, @DIYRobocars returns to the newly renovated @circuitlaunch on May 22 for the resu…
yesterday
DIY Robocars via Twitter
RT @DAVGtech: And now available with LiDAR 🔥 https://twitter.com/Heavy02011/status/1381137016381964293
yesterday
DIY Robocars via Twitter
RT @Heavy02011: #VirtualRaceLeague: @DIYRobocars Race #9 - #ParkingLotNerds #thunderhillracetrack, CA Join us for the next race April 24th,…
yesterday
DIY Robocars via Twitter
RT @DWalmroth: Weather's finally cooperating, looking forward to racing 1:10 scale autonomous cars outdoors again! @diyrobocars, @NVIDIAEm…
Wednesday
DIY Robocars via Twitter
RT @AIDRI_: I finally succeeded in optimizing the trajectory and speed of a car on a #racetrack. Next step: implement a 2d controller and…
Apr 7
DIY Robocars via Twitter
@jetdillo @circuitlaunch Actually the second *in person* event in a year. We do virtual races every month
Apr 2
DIY Robocars via Twitter
Update: we're moving it back one day to Sunday (the 4th) at 11:00am instead
Apr 2
DIY Robocars via Twitter
@GrantEMoe @circuitlaunch Update: we're doing it on Sunday (4th) at 11:00am instead
Apr 2
Laurie J. Troy liked Jasper Kueppers's profile
Apr 1
DIY Robocars via Twitter
RT @chr1sa: Maybe we should have a mini @DIYRobocars race in our lower school's playground https://t.co/xLFeua1R6X
Mar 29
DIY Robocars via Twitter
If anybody wants to join us for an informal outdoors hack/race we're going to be meeting at the @CircuitLaunch park… https://twitter.com/i/web/status/1375907409223249923
Mar 27
DIY Robocars via Twitter
RT @SmallpixelCar: Ready to reopen, innovation has to continue. Inside/outside, LiDAR/GPS, race/delivery https://t.co/jpmvttoHEd
Mar 26
DIY Drones via Twitter
RT @DAVGtech: By far best race yet! Congratulations to the winner @Heavy02011 🥇🏆🍾👏👏👏 @diyrobocars @donkey_car @NVIDIAEmbedded https://t.co/…
Mar 20
DIY Robocars via Twitter
RT @Heavy02011: #VirtualRaceLeague: @DIYRobocars Race #8 - #ParkingLotNerds ⁦@DAVGtech⁩ ⁦@DWalmroth⁩ ⁦@OttawaAVGroup⁩ - join us tomorrow h…
Mar 20
DIY Drones via Twitter
RT @mrpollo: 11 years ago, the pxIMU was announced to the world on @DIYDrones, and it changed the life of many (mine included). The followi…
Mar 18
DIY Drones via Twitter
RT @ishcahealth: This is awesome! ☘️ @DroneDJ @DIYDrones @WorkerDrones @DroneMedia_UK @dublinaviation https://twitter.com/media_ireland/status/1372077878536462336
Mar 18
More…