Python / C++ source code for i2c sensors: RM-G144 and RM-G185

All of the source code from this post can be found here.

Part of my goal for working on the blimp project at PyCon this year is to make it easy for other developers to bypass the same stumbling blocks that I dealt with. While my last few posts have somehow incorporated Boost.Python, the real reason that I needed to understand how to use the library was so that I could interface with sensors that were readily accessible using C or C++. For dealing with flight, the main sensors that I needed included an accelerometer, a compass, and an altimeter (I have a separate USB GPS module).

Please note that I haven’t tested this with 100% accuracy at this point

RoBoard RM-G144 Magnetic Compass

rmg144Code for this module is here. The boost library additions are optional and only needed if you intend to reference the module from Python.

The code I have written will return an angle in degrees. Both an accelerometer and a digital compass are embedded onto the same device, and since we’re using i2c, we can read from both modules since it’s a protocol that uses addresses.

RoBoard RM-G144 ±16g 3-Axis Accelerometer Board

rmg144Code for this module is here. The boost library additions are optional and only needed if you intend to reference the module from Python.

The actual magnitude of the resultant vector for the forces in the x, y, and z direction never actually summed to 1g, and even at rest at various angles and positions, the magnitude of the resultant vector also varied. However, the relative values can be used to get the pitch and roll from the accelerometer. Yaw can be determined from the compass.

Roboard RM-G185 Temperature, Humidity and Pressure Sensor

rmg185Code for this module is here. The boost library additions are optional and only needed if you intend to reference the module from Python.

Altitude is calculated in relative terms by measuring humidity, temperature, and pressure. Sorcery is then applied to those values to obtain an altitude. Note that because the obtained altitude is only a relative value, you’ll need to keep track of or zeroize the value you obtain while grounded.

Other Random Stumbling Blocks

  • When dealing with the Roboard, ensure that you have root privileges when executing the code. Otherwise you can’t read from the Roboard.
  • I didn’t add any sleep() functions directly into the modules. Ensure that you wait at least 100 ms between initializing each read of the sensor.