In one of the projects my team was working on at IIT Kanpur, we were supposed to conduct some ‘on-the-road’ tests on a parafoil. To measure the inertial parameters, we had to find out some cheap alternative to the costly Inertial Measurement Units (IMU) we had in our lab as rugged road test could have rendered them inaccurate. As Sparkfun Electronics happens to be our vendor of choice for this kind of stuff, it didn’t take us long to narrow down on the 9DoF Razor IMU. The Razor IMU was cheap, very light, small in size and most important of all, it was Open Source. The on-board sensors were of pretty high quality too. Perfect Choice!
Once we received the IMUs, testing them for functionality was pretty easy with the on-board firmware. Simply power them up, connect them to the PC through a USB to Serial converter and that’s pretty much it. I faced some problems with the magnetometer output and the on-board power supply, which I sorted out eventually, the technical support staff from Sparkfun told me that there was some inherent problem with the power supply design due to which the magnetometer malfunctioned sometimes. I have discussed the problem at length and the workaround in the following two videos on YouTube (read the description) :-
The IMU’s firmware (not talking about the factory burnt firmware for testing here) has been written in the Arduino environment. Being an Arduino illiterate, it took me quite some time to get it up and running. To help others like me, I wrote a step-by-step tutorial. I have also discussed about some modifications I did in the original source code. You might want to have a look……
SparkFun’s 9DoF Razor IMU Made Easy
If you are reading this then you probably don’t know anything about Arduino and Python(like me!) and have already spent a few frustrating hours trying to get this IMU up and running. I can understand that the python graphic interface video on the product page looks elusive now and is making you pull your hair off! No more headbanging! Simply follow the steps given below while enjoying a cheese pizza with coke and you’ll be just fine.
Uploading the AHRS code on the Razor IMU
- Download the latest AHRS code from here.
- Download and install the latest Arduino IDE(arduino 0018) from here.
- Now you need to find a file called ‘boards.txt‘ in the installed Arduino directory. The path to this file is, Root Directory → arduino-0018 → hardware → arduino → boards.txt.
- Assuming that you have the latest Razor IMU boards from SparkFun which come fitted with an ATMega328p, open the boards.txt file and add the following lines at the bottom and save it.
atmega328.name=Sparkfun 9DOF Razor IMU with ATmega328p
You need to perform this underground file manipulation because the Razor IMU has the ATMega328p deriving its clock from an 8MHz crystal oscillator while the preset board definitions assume the processor to be running at 16MHz. Note the second last line above,
If you don’t add the above stuff to the boards.txt file, you’ll still be able to upload the AHRS code on the IMU board but you’ll receive nothing but some weird ASCII characters on the terminal (like I did for two days!).
- Now connect your Razor IMU to the computer through the SparkFun’s FTDI Breakout board or some other USB-to-serial converter stuff (you are lucky if you have that thing called serial port still there).
- Uploading the AHRS code on the Razor IMU through Arduino IDE is pretty simple. Open all the files present in the SF9DOF_AHRS_1_0.zip (I am using version 1.0 for the time being) in the Arduino environment.
- Click on Tools → Boards → Sparkfun 9DOF Razor IMU with Atmega328p
- Click on Tools → Serial Port → Select the serial port on which Razor IMU has been connected.
- To upload the code on the board, click on File → Upload to I/O Board.
- You should then see the IDE compiling code and then burning it on the IMU board. Once ‘Done Uploading’, you can see the Razor IMU in action by opening a terminal software like Hyperterminal or RealTerm. Set the baud rate at 57600.
Python Graphic Interface for the Razor 9DoF IMU
I know you were waiting waiting for this! Its pretty simple…..
- Download and Install Python 2.6.5 and VPython from here.
- Download and Install PyWin from here.
- Download and Install PySerial from here.
- Download and Unzip (WHEW!!!) the graphic interface code from here.
- Open the IMU_Razor9DOF file in Notepad and set your COM port and the baud rate(57600 already) and save changes.
- Double Click IMU_Razor9DOF to run it and brace yourself for some cool graphical display of the IMU data. :-)
Some playing around with the AHRS code
I have always been a WINAVR chap, but Arduino is fun and easy once you get familiar with it. I am not! But still, I thought why not play around with the AHRS code. I wanted to see the accelerometer readings apart from the Euler Angles which are shown on the terminal. Code is very much self-explanatory and anyone can easily modify it.
1. Open the file Output.pde in the Arduino IDE.
2. Replace the following piece of code
#if PRINT_EULER == 1
With this code
#if PRINT_EULER == 1
Serial.print( Accel_Scale(accel_x) );
Serial.print( Accel_Scale(accel_y) );
Serial.print( Accel_Scale(accel_z) );
3. In the file SF9DOF_AHRS_1_0.pde the Accel_Scale macro has been erroneously defined as
#define Accel_Scale(x) x*(GRAVITY/9.81)
Change it to
#define Accel_Scale(x) x*(9.81/GRAVITY)
4. Compile and upload the program on the Razor IMU. Now you should see the accelerometer readings in meters per second per second on the terminal. Making these modifications does not affect the functionality of the Python Graphic Interface.
Thats all folks…..I hope you enjoyed your cheese pizza and coke! :-)