As part of my ongoing rover project, I have started writing C++ libraries for the various sensors and actuators. The first device I have begun to tackle writing a library for is the Adafruit 10DOF sensor. This sensor has libraries available for Arduino boards and in Python. Writing library code for the sensor hasn’t been without issues but has been a great learning experience. There are a lots of online tutorials but I have found that Derek Molloy’s tutorials have been consistently helpful. I recently bought Derek Molloy’s book Exploring BeagleBone: Tools and Techniques for Building with Embedded Linux to aid me in working with the BeagleBone Black. I did so because I found his freely available tutorials extremely thorough and have had the most success following them compared to other material online. His book includes the material in his tutorials plus more including introductory topics such as administering Linux and basic electronic components to advanced topics such as interacting with the onboard Programmable Real-time Units (PRUs).
To start writing the driver for the 10DOF, I focused on one sensor at a time. I took the Arduino code for each sensor provided by Adafruit and went about replacing all calls to the Wire library to appropriate calls to the libmraa library. The 10DOF sensor uses the I2C bus to communicate with all the sensors and, initially, I used Derek Molloy’s I2CDevice class to handle communication. After some success with this class, I decided to use Intel’s libmraa for handling all communication between the BeagleBone Black and peripherals. I made this switch because Molloy’s class is not as complete as libmraa and only deals with I2C communication whereas the libmraa api includes SPI, GPIO, UART and PWM functionality. This means I can use libmraa for controlling the motors and other sensors instead of having to write my own code or relying on separate libraries for sensors using different communication interfaces.
Writing the driver for each sensor was not as simple as replacing some functions in the Arduino code. The datatypes in the Arduino code had to be changed to match those recognized by the g++ compiler while staying true to the datatype used to store the data in the sensor’s registers. This required a close reading of each sensor’s data sheet to make sure I was storing the register data in variables with the same datatype. I won’t go into fine detail as to what changes I made since you can see the code yourself and compare it to the original Adafruit Arduino code.
There were a few times while working with the 10DOF sensor when I lost connection to it. This can occur when a register on a sensor is mishandled causing the sensor lock up becoming unresponsive. To deal with these lock ups when working remotely, I wired a P2N2222 transistor into the 10DOF sensor circuit so that I could power cycle the sensor if it became unresponsive. This has happened a couple of times while developing the driver software. I’ve been careful to make sure I’m properly working with the registers but mistakes still happen. Thankfully no damage was done and I haven’t needed to use this feature yet.
I’m still working on the driver and will post another update when it’s completed. The code for the BMP180, L3GD20 and LSM303 sensors can be found on my github page. Just make sure you have the libmraa library installed on your BeagleBone Black. This code is still under development and is provided with no warranty so use it at your own risk. If you have any comments on the code, please use the github issues page. My plan is to roll these separate libraries into one library for use with the 10DOF sensor but I will keep the repos for each sensor for those who buy the sensors individually.