Monday, November 26, 2012

Orientation sensing with the Raspberry Pi and MinIMU-9 v2

The Raspberry Pi is an ideal platform for experimenting with inertial measurement units (IMUs). These IMUs are usually I²C devices, so it is straightforward to access them using the Raspberry Pi's I²C bus. The integrated 3D graphics on the Raspberry Pi are useful for visualizing the results.

I have made some software that allows you to connect the MinIMU-9 v2 from Pololu Robotics & Electronics to your Raspberry Pi and calculate the orientation of the IMU. I also wrote a visualization program that shows the results in 3D. Check out the demo in this video:

The software is open source and I have written a tutorial for it. The only items needed besides your Raspberry Pi setup are:

Soldering is required: the male header pins that come with the MinIMU-9 v2 need to be soldered in.

The MinIMU-9 v2 can be conveniently powered from the Raspberry Pi's 3V3 power line. The photos below show how to connect a MinIMU-9 v2 to a Raspberry Pi:

Wiring

The software consists mainly of two programs, both written in C++. The minimu9-ahrs program reads data from the MinIMU-9's sensors via I²C, calculates the orientation of the IMU, and outputs data to the standard output. Internally, it represents the orientation with a quaternion, but several different output options are available: direction cosine matrix, quaternion, euler angles, and raw sensor data. The ahrs-visualizer program reads direction cosine matrix data from its standard input and displays a 3D representation of the MinIMU-9 v2 using OpenGL. I also used libi2c, libpng, the Boost program_options library, and Eigen. There is a Python script that helps calibrate the magnetometer. For easy installation of the software, I have created Debian packages.

For more information, see the tutorial! I would love to see what people build with this, so please post comments below!

38 comments:

  1. Dear Mr.David, I did all the steps accordingly but I am still encountering a problem when I tried to install the package referring to 'dpkg -i minimu9-ahrs_VERSION_armel.deb' and I cannot find out what the problem is. Do you know where did I do wrong?

    ReplyDelete
  2. Shahirah: Please create a new github issue here and give me all of the details about what you are doing what you are seeing: https://github.com/DavidEGrayson/minimu9-ahrs/issues?state=open

    ReplyDelete
  3. Dear Mr. I want to try this code with a microcontroller PIC, but I can´t written correctly can you help to write this code in C. for CCS ??? thank you.

    ReplyDelete
    Replies
    1. Sorry, this code is not well-suited for a PIC because it uses the Eigen library and lots of fancy features of C++. You would be better off looking at this code, which is similar: https://github.com/pololu/MinIMU-9-Arduino-AHRS

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Just wanted to say thanks for this, it's been very helpful :)

    ReplyDelete
  6. Noobie here. Think I've got everything installed but when running minimu9-ahrs it fails to open ic2 device. I have the MinIMU-9 v2. and the latest pi. i2cdetect -y 1 shows the 3 addresses 19 1e and 6b for the MinIMU-9 v2.. also having trouble installing ahrs-visualizer. Suspect I'm having trouble with the basics. Just got started with the PI and all this about a week ago. Any help would be appreciated.

    ReplyDelete
    Replies
    1. You probably have a newer Raspberry Pi that exposes the pins for /dev/i2c-1 instead of /dev/i2c-0. You will have to provide the right options to my programs to use the correct I2C bus.

      Delete
    2. I kinda figured my device wasn't the 0 but was 1 so I tried to tell it otherwise but this is probably where I have a big hole in my understanding and I'm probably entering the wrong thing. I put sudo minimu9-ahrs -b, /dev/12c-1 after the -b, I've tried many thinks like --12c-1 --/dev/i2c-1 /dev/i2c-1 i2c-1 but I really don't understand the syntax and/or may be using the wrong command all together. I get errors like no such option, no such file or directory etc. Thanks for your come back. This old guy appreciates it!

      Delete
    3. My memory is foggy too. Did you try "--help"? What did it say?

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I've been looking at "man minimu9-ahrs" for help. Just now looked at minimu9-ahrs --help and get similar stuff. I've tried the following but even after looking at the help I'm not really sure of the syntax. like single - or double dash or commas -b or arg just not really sure of what I should use. for example if I type the following:
    "sudo minimu9-ahrs -b, --/dev/i2c-1" gives me
    "Error: unknown option /dev/i2c-1" If I try
    "sudo minimu9-ahrs -b, /dev/i2c-1" gives me
    "Error: Failed to open I2C device.: No such file or directory No such file or directory (system:2)" and the help is similar to the man stuff
    "sudo minimu9-ahrs --help" gives me
    "Allowed options:
    -h [ --help ] produce help message
    -v [ --version ] print version number
    -b [ --i2c-bus ] arg (=/dev/i2c-0) i2c-bus the IMU is connected to
    --mode arg (=normal) specifies what algorithm to use.
    normal: Fuse compass and gyro.
    gyro-only: Use only gyro (drifts).
    compass-only: Use only compass (noisy).
    raw: Just print raw values from sensors.
    --output arg (=matrix) specifies how to output the orientation.
    matrix: Direction Cosine Matrix.
    quaternion: Quaternion.
    euler: Euler angles (yaw, pitch, roll).


    For more information, run: man minimu9-ahrs"
    So am I typing stuff in wrong or just don't have a clue?

    ReplyDelete
    Replies
    1. Except for the --help, the stuff you tried simply doesn't look valid. Why did you put a comma after b? What about "-b /dev/i2c-1" ?

      Delete
    2. This comment has been removed by the author.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. I am facing the same problem here, Is there any known solution?

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Hello David,
    I am using a Raspberry Pi and the MinIMU-9 v2 module for controlling an autonomously flying model airplane.
    I have compiled the program from source code and everything seems to work fine but there is one problem I have: The MinIMU module is mounted into the airplane rotated 180° about the X-axis (X=X, Y=-Y, Z=-Z). What can I do to get the right output? Change the source code? Where?

    I would appreciate your help.
    Best regards
    Wolfgang from Germany




    ReplyDelete
  13. Hi Dave, I'm pretty new to Raspberry Pi. I'm a software engineer by profession and have tinkered with electronics from time to time as well. I'm looking at reverse engineering the Technomancer Hoodie that ThinkGeek sells into a custom Halloween costume with a bit more control and functionality. I haven't sourced or picked up any components yet since I'm still in the research phase. This project looks like a great place to start. I was wondering though if it's possible to connect TWO IMUs to the Raspberry Pi? It looks like GPIO pins 0 and 1 are the only pins suited but I could be wrong.

    The idea is to attach one IMU to each wrist with a wrist band and track the movements of each arm.

    ReplyDelete
    Replies
    1. The Raspberry Pi actually has two I2C busses (/dev/i2c-0 and /dev/i2c-1) but I think one of them is on a different connector or maybe no connector at all. And one annoying thing is that they switched the I2C interfaces between revisions. If you can figure out a physical solution to getting access to the other I2C bus, then you should be able to use two instances of my software running on two different busses.

      Since you and I are a software guys though, maybe it would better to find some library for the Raspberry Pi that does software I2C on two of the other GPIOs. When you are the master in an I2C bus, timing is not very important and you could implement a bit-banging I2C thing from user space in any language. Someone must have done this already.

      Delete
    2. A few Google searches led me to this: http://lwn.net/Articles/230571/

      Delete
  14. I am using two minimu-9 v2 sensor.I use 4052 multiplexer but it didnt work.I didnt solve problem.I controlled two servos with two pot that using multiplexer.Can you any sugesstion?

    ReplyDelete
  15. Hi Mr.David,
    I am an experienced Italian helicopters, Raspberry use to interface the weather station and webcam present in an oil rig.
    I would like to add the ability to display also the pitch and roll and heading of the platform and then access the data via the web in my site .... is it possible using its software generating a web page?
    Thank you for your attention ...

    Christian D.
    Italy
    cris412@libero.it

    ReplyDelete
  16. David,

    Awesome project page! I just got my minimu9 v2 last week from Pololu. I used a 256MB RPi running Raspian and although it took a few hours of compiling source files (I was using armhf EABI), I finally got everything running. I was taking notes along the way so that I could document my steps as they were fairly different from the tutorial. The tutorial was awesome and very helpful! I'll post a page in the next couple days to help others who may be trying with a similar setup.

    Cheers,
    Mike

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Hello David great work!
    how bout a device that i would wear on my wrist or ankle and detect motion for a pedometer?
    my roomate is a mathemetician, so im thinking that perhaps the data from the minimu be crunched to interpret motion.
    also, if i have the ahrs program record raw sensor data to a file, can i run the program later and have it take it's input from the file?

    thanx, mike
    spillikinaerospace@yahoo.com

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Hi David,

    As promised, I've put up a companion tutorial for anybody who might have a slightly different Pi setup.

    http://www.nacionale.com/pololu-minimu9-step-by-step/

    Thanks again for your tutorial!
    Mike

    ReplyDelete
    Replies
    1. Had to update the link since the old link was no longer valid.

      Delete
    2. Hey mike, thanks for writing your tutorial! Hey, I noticed you called me "David Gray" a few times and you have a random "br />" hanging out in the middle of the post. I added a link to your tutorial on https://github.com/DavidEGrayson/minimu9-ahrs/wiki

      Delete
  21. Dear David,

    I am trying to create an AHRS box. I have tested two boards Arduino Uno and Netduino Plus 1 with PololuIMU-9-v2 using Madgwick's algorithm (http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/). Une update of the quaternion took around 20 milliseconds (50Hz) on Arduino and around 30 ms (33 Hz) on Netduino. Do you know what is the frequency of updates in your case?

    ReplyDelete
    Replies
    1. Hello, Tomaz. It's important to make the distinction between the actual CPU time it takes to do the update computations, and the time the program waits between computations. I haven't measured the CPU time carefully, but the minimu9-ahrs software I wrote is designed to do the computations every 20 ms, and this is controlled by the "ahrs" function you can find in minimu9-ahrs.cpp. That source code is here:

      https://github.com/DavidEGrayson/minimu9-ahrs/

      I really doubt that the Netduino would be slower at doing a computation than the Arduino, though I guess it depends what type of Arduino you are talking about; were you talking about one of the newer 32-bit ones? It's more likely that the code you were looking at is just spending some time waiting in a loop before doing the next calculation.

      Delete
    2. Hi! Thank you for quick reply.

      Well, I have been playing around measuring time and I am pretty sure that the calculation itself takes around 15ms on Netduino, but readings and the basic program structure add allmost 15ms. It seems that fancy C# .NET Micro Framework environment has a lot of overhead which makes it even slower than Arduino Uno. I am talking about these two boards:

      http://arduino.cc/en/pmwiki.php?n=Main/arduinoBoardUno
      http://www.netduino.com/netduinoplus/specs.htm

      Delete
  22. Hi david!
    I have a problem, when I put this command for install minimu9-ahrs "wget http://www.davidegrayson.com/minimu9-ahrs/debian/minimu9-ahrs_2.0.0-1_armhf.deb
    dpkg -i minimu9-ahrs_2.0.0-1_armhf.deb" gives me "not urls found in minimu9-ahrs" so when I continue with the next step I type "minimu9-ahrs --mode raw" gives me "bash: minimu9-ahrs: command not found"
    Do you know where did I do wrong please?

    ReplyDelete
    Replies
    1. That error message doesn't even sound like correct English. Could you tell me which of the two commands is causing the error and then copy and paste the full output of that command here?

      Delete