要略
Raspberry Pi3BでBNO005をi2Cで動作させる機会があり、躓いたので備忘録として残します。
使用言語はPythonです。
開発環境の設定はこちらの通りに進めました。
こちらの公式サンプルを動かしたところ、そのままではI2Cで動作しませんでした。
解決策
30行目当たりにある次のコードを変更しました。
Befor
# Create and configure the BNO sensor connection. Make sure only ONE of the
# below 'bno = ...' lines is uncommented:
# Raspberry Pi configuration with serial UART and RST connected to GPIO 18:
bno = BNO055.BNO055(serial_port='/dev/serial0', rst=18)
# BeagleBone Black configuration with default I2C connection (SCL=P9_19, SDA=P9_20),
# and RST connected to pin P9_12:
# bno = BNO055.BNO055(rst='P9_12')
After
# Create and configure the BNO sensor connection. Make sure only ONE of the
# below 'bno = ...' lines is uncommented:
# Raspberry Pi configuration with serial UART and RST connected to GPIO 18:
bno = BNO055.BNO055(rst=18)
# BeagleBone Black configuration with default I2C connection (SCL=P9_19, SDA=P9_20),
# and RST connected to pin P9_12:
# bno = BNO055.BNO055(rst='P9_12')
何をしたかというと、BNO005の初期化時に通信インターフェースを指定しないようにしました。こうする事で自動的にI2Cの設定がなされるようになっていました。
BNO005.pyライブラリの次の箇所を見ると分かりました。
if serial_port is not None:
# Use serial communication if serial_port name is provided.
# Open the serial port at 115200 baud, 8N1. Add a 5 second timeout
# to prevent hanging if device is disconnected.
self._serial = serial.Serial(serial_port, 115200, timeout=serial_timeout_sec,
writeTimeout=serial_timeout_sec)
else:
# Use I2C if no serial port is provided.
# Assume we're using platform's default I2C bus if none is specified.
if i2c is None:
import Adafruit_GPIO.I2C as I2C
i2c = I2C
# Save a reference to the I2C device instance for later communication.
self._i2c_device = i2c.get_i2c_device(address, **kwargs)
おわりに
I2Cを使う場合も、サンプルコードの説明に書いといてほしかったです...。