Hi
I’m trying to get the AbsoluteIMU-ACG sensor from MindSensors working with the Lego Ultrasonic Sensor. So far here is the code:
!/usr/bin/env python
from BrickPi import *
import math
import time
# Connection information. For this test program the AbsoluteIMU-ACG should be
# connected to port 1 of the BrickPi, and it should be the only device on that port.
GYRO_PORT = PORT_1 # I2C port the AbsoluteIMU-ACG is connected to
GYRO_SPEED = 0 # Delay for as little time as possible. Usually about 100k baud
GYRO_DEVICE_INDEX = 0 # AbsoluteIMU-ACG is device 0 on this I2C bus
GYRO_ADDR = 0x22 # Default I2C address of MindSensors AbsoluteIMU-ACG
GYRO_TILT = 0x42 # Tilt data is 3 bytes
ULTRASONIC_PORT = PORT_2 # I2C port the Lego Ultrasonic Sensor is connected to
# Setup the communication with BrickPi
BrickPiSetup()
# Type of sensors and connection details
BrickPi.SensorType[GYRO_PORT] = TYPE_SENSOR_I2C
BrickPi.SensorI2CSpeed[GYRO_PORT] = GYRO_SPEED
BrickPi.SensorI2CDevices [GYRO_PORT] = 1
BrickPi.SensorSettings[GYRO_PORT][GYRO_DEVICE_INDEX] = 0
BrickPi.SensorI2CAddr[GYRO_PORT][GYRO_DEVICE_INDEX] = GYRO_ADDR
BrickPi.SensorType[ULTRASONIC_PORT] = TYPE_SENSOR_ULTRASONIC_CONT
BrickPiSetupSensors() # Send the properties of sensors to BrickPi
# The sensor user manual says that we have to wait at least 50 milliseconds
# for the sensor to reconfigure itself after changing the sensitivity.
time.sleep(0.06) # 60ms
# Read the tilt information from the sensor
BrickPi.SensorSettings[GYRO_PORT][GYRO_DEVICE_INDEX] = BIT_I2C_SAME
BrickPi.SensorI2CWrite[GYRO_PORT][GYRO_DEVICE_INDEX] = 1
BrickPi.SensorI2CRead[GYRO_PORT][GYRO_DEVICE_INDEX] = 3
BrickPi.SensorI2COut[GYRO_PORT][GYRO_DEVICE_INDEX][0] = GYRO_TILT
while True:
tx = 999 # Invalid tilt value. -127 <= tilt <= 128
ty = 999
tz = 999
if not BrickPiUpdateValues():
distance = BrickPi.Sensor[ULTRASONIC_PORT]
if not BrickPiUpdateValues():
if BrickPi.Sensor[GYRO_PORT] & (0x01 << GYRO_DEVICE_INDEX):
tx = BrickPi.SensorI2CIn[GYRO_PORT][GYRO_DEVICE_INDEX][0] - 128
ty = BrickPi.SensorI2CIn[GYRO_PORT][GYRO_DEVICE_INDEX][1] - 128
tz = BrickPi.SensorI2CIn[GYRO_PORT][GYRO_DEVICE_INDEX][2] - 128
if tx != 999:
txd = math.degrees(math.asin(tx/128.0))
if ty != 999:
tyd = math.degrees(math.asin(ty/128.0))
if tz != 999:
tzd = math.degrees(math.asin(tz/128.0))
print("tx: %3.2f ty: %3.2f tz: %3.2f" % (txd, tyd, tzd))
# NOTE: The tilt reading is the angle of an axis with the horizontal
# plane. It is stable but not very accurate.
print("distance: %d" % (distance))
print("==========================================================")
time.sleep(1) # Sleep for 1 s
Output:
tx: -36.42 ty: -90.00 tz: -90.00
distance: -1
==========================================================
tx: -45.31 ty: -90.00 tz: -90.00
distance: -1
==========================================================
...
When I comment out the AbsoluteIMU-ACG sensor part, I get distances that make sense:
distance: 21
==========================================================
distance: 18
==========================================================
...
Any ideas about what is causing that?