9DoF Razor IMU with ROS (Part I)

Luis Ortiz F
7 min readSep 9, 2019

--

In this first part, I show how setup and calibrate the "SparkFun 9DoF Razor IMU M0" board with Arduino IDE and ROS. I also share some useful tips that will save you time 😉.

1. Hardware

SparkFun 9DoF Razor IMU M0.
micro USB to USB 3.0 cable.

𝕋𝕀ℙ →(Do not use cables designed to charge cell phones.)

2. Software

Ubuntu 16.04.
Arduino IDE 1.8.9.
ROS Kinetic Kame.
Visual Python. Required for the 3D visualization ROS node.

$ sudo apt-get install python-visual

— Download and install Processing last version. We will use it to compile and run the magnetometer calibration program. Tested with version 3.5.3.
— Download EJML library. Get the library here (you only need the .jar file), find your Processing libraries folder (normally in “/home/user/sketchbook/libraries”). Create a folder “EJML” inside “libraries”,
create a folder “library” inside “EJML” and put the .jar inside. Rename to EJML.jar. So you should have “libraries/EJML/library/EJML.jar”. Restart Processing and you’re good.¹

𝕋𝕀ℙ →(The magnetometer calibration program only run with EJML 0.17 and 0.23.)

This guide assumes that you have correctly installed Ubuntu, Arduino IDE and ROS on your workstation. Most of the information presented below was obtained from existing tutorials. For more details consult the References section.

3. Steps

In Arduino IDE²:

  • Install Arduino SAMD Board:
    ToolsBoardBoards Manager, then find an entry for Arduino SAMD Boards (32-bits ARM Cortex-M0+). Select it, and install the latest version.
  • Install SparkFun Board Definition:
    FilePreferences. Then find the Additional Board Manager URLs text box, and paste the below link in:
https://raw.githubusercontent.com/sparkfun/Arduino_Boards/master/IDE_Board_Manager/package_sparkfun_index.json
  • Select the Board and Serial Port:
    ToolsBoard list, including SparkFun 9DoF Razor IMU M0, under the “SparkFun SAMD (32-bits ARM Cortex-M0+) Boards” menu. Finally, select your 9DoF Razor’s port, by navigating back up to the ToolsPort menu.

In ROS³

To use the SparkFun 9DoF Razor IMU M0 with ROS it is necessary to provide it with a driver and then calibrate the IMU. For this, we use the package razor_imu_9dof². A node which displays the attitude (roll, pitch, and yaw) of the Razor board is provided for testing.

  • Install ROS razor_imu_9dof Package:
    ▫️ From Source: Clone the ROS source repository and build it:
$ cd catkin_ws/src
$ git clone https://github.com/KristofRobot/razor_imu_9dof.git
$ cd ..
$ catkin_make

or from repository:

$ sudo apt-get install ros-kinetic-razor-imu-9dof
  • Load firmware into Razor board:
    Copy the firmware from the “razor_imu_9dof/src” directory to your Arduino source directory “/home/user/Arduino”.
$ roscd razor_imu_9dof
$ cp -r src/Razor_AHRS ~/Arduino
  • Plug the board into your workstation.
  • Open the “Razor_AHRS.ino” sketch in Arduino IDE, search the section labeled “USER SETUP AREA” → “HARDWARE OPTIONS” and uncomment this line:
#define HW__VERSION_CODE 14001 // SparkFun “9DoF Razor IMU M0” version “SEN-14001”.
  • Go to ToolsBoard, and select “SparkFun 9DoF Razor IMU M0”.
  • Go to ToolsPort and select the port used with the Razor.
  • Upload sketch to board.

4. Coordinate Frames

The definition of the coordinate frame used by the Razor_AHRS firmware differs from what is printed on the board and also from the ROS coordinate frame.

𝕋𝕀ℙ →(For the SparkFun 9DoF Razor IMU M0 do not use the coordinate frames describe into razor_imu_9dof package guide. Also ignore the labeling on the board.)

The Razor_AHRS firmware uses⁴:

The razor_imu_9dof node transforms the Razor_AHRS measurements into the ROS coordinate frame.

𝕋𝕀ℙ →(It is easy to get confused when viewing data from the Razor_AHRS on a serial monitor and from the ROS /imu topic. Remember that these are different.)

5. Testing the board

  • On Arduino IDE:
    ToolsSerial Monitor. Set it to 57600 baud and send the string “#o1” to the board. You should get output that looks like this:

The three numbers represent YPR (Yaw, Pitch, and Roll) in degrees.

  • On ROS:
    Run the launch file:
$ roslaunch razor_imu_9dof razor-pub-and-display.launch

6. Calibration

Power up the Razor a few minutes before calibration, so the sensors can warm up.

  • Open “/home/user/catkin_ws/src/razor_imu_9dof/src/Razor_AHRSRazor_AHRS.ino” using Arduino IDE.
  • Connect the board to your computer, set the correct serial port in Arduino IDE and open the Serial Monitor.
  • You should see lots of output like this:
#YPR=-155.73,-76.48,-129.51
  • Set the firmware output mode to calibration by sending the string “#oc”. You should now see output like this:
accel x,y,z (min/max) = -5.00/-1.00  25.00/29.00  225.00/232.00

Calibrating the Accelerometer

𝕋𝕀ℙ →(When you move the board, move it real slow, so the acceleration you apply to it is as small as possible.)

  • Take the board and point straight down with the x-axis (see Coordinate Frames section). While you do that, you can see the x-maximum (the second value) getting bigger.
  • Hold the board very still and reset the measurement by sending “#oc” again.
  • Now carefully tilt the board a little in every direction until the value does not get bigger any more and write down the x-maximum value.
  • Do the same thing for the opposite side (x-axis pointing up) to get the x-minimum: bring into position, send #oc to reset measurement, find x-minimum value and write it down.
  • Do the same thing for the z-axis (down and up) and the y-axis (right and left).
  • If you think you messed up the measurement by shaking or moving the board too fast, you can always reset by sending “#oc”.
  • You should now have all the min/max values. Put them into Razor_AHRS.ino in the section “USER SETUP AREA”“SENSOR CALIBRATION”.

𝕋𝕀ℙ →(Even slightly tapping the board with the finger messes up the measurement and leads to the wrong calibration. Use “#oc” very often and double-check your min/max values.)

Calibrating the gyroscope

  • Lay the Razor AHRS still on the table.
  • We’re still in calibration mode for the accelerometer. Send “#on” twice, which will move calibration past the magnetometer to the gyroscope.
  • Wait for 10 seconds, and do not move the Razor AHRS. It will collect and average the noise of the gyroscope on all three axes.
  • You should now have an output that looks like this:
gyro x,y,z (current/average) = -29.00/-27.98  102.00/100.51  -5.00/-5.85
  • If you think you messed up the measurement by shaking or moving the board, you can reset by sending “#oc”.
  • Take the second values of each pair and put them into Razor_AHRS.ino in the section “USER SETUP AREA”“SENSOR CALIBRATION”.

Calibrating the magnetometer

  • Put the sensor in the magnetic environment where it will be used later — e.g. in the exact spot on your robot.
  • We’re still in calibration mode for the gyroscope. Send “#on” twice, which will move calibration past the gyroscope to the magnetometer.
  • Quit all applications that read from the sensor (e.g. Serial Monitor, 3D Visualization GUI, …) and run the Processing magnetometer calibration sketch located in “/home/user/catkin_ws/src/razor_imu_9dof/magnetometer_calibration”.
  • Try to rotate the sensor in a way so that you cover all orientations so you produce dots that more or less evenly cover the sphere.
  • In a mostly undistorted environment this could look something like this:
  • Hit SPACE and watch the Processing console — you’ll find some lines of code that you have to put into the firmware under “USER SETUP AREA”“SENSOR CALIBRATION” and then you’re done.

7. Additional Material

I have designed a very nice case for the SparkFun 9DoF Razor IMU M0 board. If you would like to have it you can buy it below:

8. References

[1] https://www.youtube.com/watch?v=n7l41bs94To

[2] https://learn.sparkfun.com/tutorials/9dof-razor-imu-m0-hookup-guide?_ga=2.115712326.1582924489.1567892458-180161161.1563824471

[3] http://wiki.ros.org/razor_imu_9dof

[4] https://github.com/Razor-AHRS/razor-9dof-ahrs/issues/57

--

--

Luis Ortiz F

“A smart person learns from his mistakes, but a truly wise person learns from the mistakes of others.” (Voltaire) — Ph.D. Candidate in Computer Engineering.