Thursday, 12 February 2015

Which way is up??

Another significant component of the datalogger is an Inertial Measurement Unit (IMU).  There are many many models out there, from the really expensive to the really cheap.  I decided to start with a 10DOF (degrees of freedom) board based on these chipsets: L3GD20 LSM303D BMP180.  It has a temperature and barometric pressure sensor in addition to the usual compass, accelerometer and gyro sensors. Delivered from HK for £5.88 in a couple of weeks, not bad!  I have since ordered an MPU-9150 board for £4.90 to see how that behaves. The choice of both boards was based on the support matrix for RTIMULib, which is probably going to be the library of choice.

The board came with an unsoldered header, but I'm getting the hang of this soldering malarkey, so it was soon a soldered header!  These boards all use the I2C, so the wiring is simple: power (+3.3v), ground and the I2C lines (SDA/SCL).  I2C is on GPIO 2 (SDA) and GPIO 3 (SCL).









To check that the RPI is seeing it, use i2cdetect on I2C bus 1:

pi@pi0 ~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- UU -- 1d -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- 39 -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- UU -- -- 6b -- -- -- -- 
70: -- -- -- -- -- -- -- 77                         

There are quite a few things on the bus, most of them are the chips on this board, but there's also a RTC that I added to help out with the GPS timing.

After downloading and installing RTIMULib, it was time for a bit of python:
import sys, getopt

sys.path.append('.')
import RTIMU
import os.path
import os
import time
import math
import datetime

SETTINGS_FILE = "RTIMULib"
print("Using settings file " + SETTINGS_FILE + ".ini")
if not os.path.exists(SETTINGS_FILE + ".ini"):
  print("Settings file does not exist, will be created")

s = RTIMU.Settings(SETTINGS_FILE)
imu = RTIMU.RTIMU(s)
pressure = RTIMU.RTPressure(s)

print("IMU Name: " + imu.IMUName())
print("Pressure Name: " + pressure.pressureName())

if (not imu.IMUInit()):
    print("IMU Init Failed");
    sys.exit(1)
else:
    print("IMU Init Succeeded");

if (not pressure.pressureInit()):
    print("Pressure sensor Init Failed");
else:
    print("Pressure sensor Init Succeeded");

poll_interval = imu.IMUGetPollInterval()
print("Recommended Poll Interval: %dmS\n" % poll_interval)

raw_input("Press Enter when ready\n>")

while True:
  if imu.IMURead():
    (x, y, z) = imu.getFusionData()
    data = imu.getIMUData()
    (data["pressureValid"], data["pressure"], data["temperatureValid"], data["temperature"]) = pressure.pressureRead()
    fusionPose = data["fusionPose"]
    os.system('clear')

    print "timestamp: " + str(data["timestamp"])

    print "accelValid: " + str(data["accelValid"])
    print "accel: " + str(data["accel"])
    print "accel x: " + str(data["accel"][0])
    print "accel y: " + str(data["accel"][1])

    print "compassValid: " + str(data["compassValid"])
    print "compass: " + str(data["compass"])

    print "fusionPoseValid: " + str(data["fusionPoseValid"])
    print "fusionPose: " + str(data["fusionPose"])
    print("r: %f p: %f y: %f" % (math.degrees(fusionPose[0]), math.degrees(fusionPose[1]), math.degrees(fusionPose[2])))
    print("r: %i p: %i y: %i" % (int(math.degrees(fusionPose[0])), int(math.degrees(fusionPose[1])), int(math.degrees(fusionPose[2]))))
    print("x: %f y: %f z: %f" % (x,y,z))

    print "fusionQPoseValid: " + str(data["fusionQPoseValid"])
    print "fusionQPose: " + str(data["fusionQPose"])

    print "gyroValid: " + str(data["gyroValid"])
    print "gyro: " + str(data["gyro"])

    print "pressureValid: " + str(data["pressureValid"])
    print("Pressure: %f" % (data["pressure"]))

    print "temperatureValid: " + str(data["temperatureValid"])
    print("Temperature: %f" % (data["temperature"]))

    time.sleep(0.2)



pi@pi0 ~ $ python imu_fields.py
Using settings file RTIMULib.ini
IMU Name: LSM9DS0
Pressure Name: BMP180
IMU Init Succeeded
Pressure sensor Init Succeeded
Recommended Poll Interval: 4mS

Press Enter when ready
>
timestamp: 1423748093614330
accelValid: True
accel: (1.0306559801101685, 0.04684799909591675, -0.16396799683570862)
accel x: 1.03065598011
accel y: 0.0468479990959
compassValid: True
compass: (43.95816421508789, 25.756946563720703, -5.119741439819336)
fusionPoseValid: True
fusionPose: (2.216735363006592, -1.4253621101379395, 2.5034217834472656)
r: 127.009581 p: -81.667233 y: 143.435503
r: 127 p: -81 y: 143
x: 2.216735 y: -1.425362 z: 2.503422
fusionQPoseValid: True
fusionQPose: (-0.44976526498794556, 0.4893990159034729, 0.551458477973938, 0.5040767192840576)
gyroValid: True
gyro: (0.15618298947811127, 0.15292565524578094, -0.16467183828353882)
pressureValid: 1
Pressure: 1013.469971
temperatureValid: 1
Temperature: 21.500000

That seems to work nicely, and is easily capable of 50Hz updates, and may well do 100Hz if I decide to go to that level.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home