CircuitPython のライブラリを探していたところ、9軸センサー(LSM9DS1)用のライブラリがあることを発見しました。LSM9DS1は、昔自作アドオンボードを作ったので試すことができます。





動かすには、adafruit のライブラリバンドルのZIPをダウンロードしてくると便利です。最新の VERSION 5.x 版を持ってきましょう。



https://circuitpython.org/libraries


ダウンロードして展開すると様々なライブラリがありますが、今回重要なのは次の3つです。


adafruit-circuitpython-bundle-5.x-mpy-20200516

├── README.txt
├── VERSIONS.txt
├── examples
│   ├── lsm9ds1_simpletest.py

└── lib
├── adafruit_bus_device
│   ├── __init__.py
│   ├── i2c_device.mpy
│   └── spi_device.mpy
├── adafruit_lsm9ds1.mpy



CircuitPython はあらかじめ SPRESENSE に書き込んでおきましょう。書き込み方は次のブログに記載されています。


SPRESENSE で "CircuitPython" を動かしてみた!
https://makers-with-myson.blog.ss-blog.jp/2020-05-08


SPRESENSEの拡張ボード側のUSBコネクタに接続すると、"CIRCUITPY:"ドライブが見えます。







"adafruit_lsm9ds1.mpy" "adafruit_bus_device" はフォルダごと、"CIRCUITPY:" ドライブの ”lib” フォルダーにコピーしちゃいます。





次に、example のコード "lsm9ds1_simpletest.py" を”CIRCUITPY:”ドライブのルートに "code.py" として保存します。





"code.py" は次のように非常にシンプルです。RTOS で Python でセンサー処理って新鮮ですね。


# Simple demo of the LSM9DS1 accelerometer, magnetometer, gyroscope.
# Will print the acceleration, magnetometer, and gyroscope values every second.
import time
import board
import busio
import adafruit_lsm9ds1

# I2C connection:
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)

# SPI connection:
# from digitalio import DigitalInOut, Direction
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
# csag = DigitalInOut(board.D5)
# csag.direction = Direction.OUTPUT
# csag.value = True
# csm = DigitalInOut(board.D6)
# csm.direction = Direction.OUTPUT
# csm.value = True
# sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)

# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
# values every second and print them out.
while True:
# Read acceleration, magnetometer, gyroscope, temperature.
accel_x, accel_y, accel_z = sensor.acceleration
mag_x, mag_y, mag_z = sensor.magnetic
gyro_x, gyro_y, gyro_z = sensor.gyro
temp = sensor.temperature
# Print values.
print(
"Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
accel_x, accel_y, accel_z
)
)
print(
"Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
mag_x, mag_y, mag_z)
)
print(
"Gyroscope (degrees/sec): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
gyro_x, gyro_y, gyro_z
)
)
print("Temperature: {0:0.3f}C".format(temp))
# Delay for a second.
time.sleep(1.0)



Pythonコードを保存すると、Teraterm 等のターミナルにセンサー出力がプリントされます。これは、お手軽ですねー。





".mpy" はPythonコードなのかなと思ったのですが、開いてみるとバイナリーコードでした。そのうちどうやって作るのか探ってみたいと思います。😃
(^^)/~





SONY SPRESENSE メインボード CXD5602PWBMAIN1

  • 出版社/メーカー: スプレッセンス(Spresense)
  • メディア: Tools & Hardware



Adafruit Trinket M0(CircuitPython/Arduino IDE用)

  • 出版社/メーカー: スイッチサイエンス
  • メディア: