How do I use MinIMU-9 in a wheeled Arduino robot?


I am trying to understand how I should use MinIMU-9 in a larger project. I have gone through and installed the libraries and AHRS code, done the calibration, and the board seems to work as expected.

Now, what I want to do is to be able to tell my robot “turn north” for example. So I will need a loop that monitors input (maybe from a remote control or through serial for example), which will then pass control over to a function that will read the IMU and steer until it gets the compass where I want it.

However, after looking at the AHRS code, it’s not clear for me how to do this. There are readings of the IMU at 50Hz, and the compass at 10Hz. But in the larger loop, I may not be reading the compass this frequently while waiting for instructions on what the robot should do, which would make a long integration time in the DCM algorithm. I don’t know if this will create huge errors, or is the “right way”.

Another approach would be to use some kind of threading, so the microcontroller (Arduino) is always reading the IMU at approximately 50Hz/10Hz and storing it in an internal state, and when my command is given the steering function can request the value. I’m no threading expert but this sounds like it would give what I think it called a race condition or something.

Anyway, in short, I was trying to do something that initially I thought was simple and now I’m not sure. If no one can give advice I’m going to attempt to figure it out and I’ll let you know how it goes. I guess I would maybe be better off with an IMU that will report its estimated R/P/Yaw?

A last resorts approach would be to either make a tiny Arduino using RBBB or something like that to handle the AHRS, and feed that into another Arduino, or possibly get a Propeller controller so I can run it on a separate thread/cog.

Sorry, that’s a lot of reading, but thank you for any help!

Arduino Uno (1.0 software), R1 or R2 board
MinIMU-9 compass/accel/gyro


A tilt compensated compass might be all you need if you want to mount something that is always more or less parallel to the ground and then know how much to yaw.

Hi dougbot,

You could run the main loop at the fastest frequency needed, in your example 50Hz. Then in the main loop, use counters to decrease the frequency for each function that runs slower. The AHRS does this with the Accelerometer and Compass which you could build on.

Each function called in the main loop should not block, but instead restore state, do work, save state and then exit. The work part is tricky. You have decide what is the longest amount of time you can spend and if it is too long then you may need sub states. This can get messy and at some point it may be a better trade off to use a realtime OS such as FreeRTOS:

For example this would be the puedo-code for turning:

If turning flag is true then
  If compass heading matchs new heading then
    reset turning flag

The main loop would set the turning flag, new heading value and would set the motor speeds for each wheel to cause the vehicle to start turning. The main loop would call the read compass function at its frequency and also from the main loop the function above would be called at its frequency. The main loop would reset the motors to straight travel when the turning flag is false.

This is just one of many ways to mimic threading.

Hope this helps,