Skip to content

Commit

Permalink
✨ examples for bmi088 [#1]
Browse files Browse the repository at this point in the history
Examples for BMO088 shuttle:
simple I2C / SPI read / write
polling streaming over I2C / SPI
interrupt streaming over I2C / SPI
  • Loading branch information
selyunin committed Nov 21, 2024
1 parent 1d92c8c commit f3c68c6
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 12 deletions.
Empty file added examples/bmi088/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions examples/bmi088/bmi088_i2c_interrupt_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmi088.bmi088_shuttle import BMI088Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# This example is for Application Board 3.1 hardware
shuttle = BMI088Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_i2c()
logger.info(f"acc_chip_id=0x{shuttle.sensor.acc_chip_id:02X}")
logger.info(f"gyro_chip_id=0x{shuttle.sensor.gyro_chip_id:02X}")
assert shuttle.sensor.acc_chip_id == 0x1E
assert shuttle.sensor.gyro_chip_id == 0x0F
shuttle.configure_interrupt_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_interrupt_streaming_multiple(includes_mcu_timestamp=False):
sensor_id, packet, time_stamp, payload = streaming
d_x, d_y, d_z = struct.unpack("<hhh", payload)
if sensor_id == 1:
logger.info(f"[{idx}][a] {packet=:06d} a_x={d_x:04d}, a_y={d_y:04d}, a_z={d_z:04d}")
elif sensor_id == 2:
logger.info(f"[{idx}][g] {packet=:06d} g_x={d_x:04d}, g_y={d_y:04d}, g_z={d_z:04d}")
time.sleep(0.05)
shuttle.board.stop_interrupt_streaming()
48 changes: 48 additions & 0 deletions examples/bmi088/bmi088_i2c_polling_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmi088.bmi088_shuttle import BMI088Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# This example is for Application Board 3.1 hardware
shuttle = BMI088Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_i2c()
logger.info(f"acc_chip_id=0x{shuttle.sensor.acc_chip_id:02X}")
logger.info(f"gyro_chip_id=0x{shuttle.sensor.gyro_chip_id:02X}")
assert shuttle.sensor.acc_chip_id == 0x1E
assert shuttle.sensor.gyro_chip_id == 0x0F
shuttle.configure_polling_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_polling_streaming_multiple():
sensor_id, payload = streaming
data_x, data_y, data_z = struct.unpack("<hhh", payload)
if sensor_id == 1:
logger.info(f"[{idx}][a] a_x={data_x:04d}, a_y={data_y:04d}, a_z={data_z:04d} ")
elif sensor_id == 2:
logger.info(f"[{idx}][g] g_x={data_x:04d}, g_y={data_y:04d}, g_z={data_z:04d} ")
time.sleep(0.05)
shuttle.board.stop_polling_streaming()
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
from umrx_app_v3.shuttle_board.bmi088.bmi088_shuttle import BMI088Shuttle


def setup_logging() -> logging.Logger:
def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_handler.setFormatter(
logging.Formatter("(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s")
)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# For using with Application board 3.0, replace the line below with:
# shuttle = BMI088Shuttle.on_hardware_v3_rev0()
shuttle = BMI088Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()
Expand All @@ -29,13 +32,6 @@ def setup_logging() -> logging.Logger:
logger.info(f"acc_chip_id=0x{shuttle.sensor.acc_chip_id:02X}")
logger.info(f"gyro_chip_id=0x{shuttle.sensor.gyro_chip_id:02X}")

time.sleep(0.1)
shuttle.configure_spi()
_ = shuttle.board.read_spi(shuttle.CSB1, 0, 1) # first read is required, do not delete

logger.info(f"acc_chip_id=0x{shuttle.sensor.acc_chip_id:02X}")
logger.info(f"gyro_chip_id=0x{shuttle.sensor.gyro_chip_id:02X}")

logger.info(f"acc_status=0x{shuttle.sensor.acc_status:02X}")
logger.info(f"acc_fifo_config_1=0x{shuttle.sensor.acc_fifo_config_1:02X}")

Expand Down
51 changes: 51 additions & 0 deletions examples/bmi088/bmi088_spi_interrupt_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmi088.bmi088_shuttle import BMI088Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# This example is for Application Board 3.1 hardware
shuttle = BMI088Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_spi()
_ = shuttle.board.read_spi(shuttle.CSB1, 0, 1) # dummy read is required, do not delete

logger.info(f"acc_chip_id=0x{shuttle.sensor.acc_chip_id:02X}")
logger.info(f"gyro_chip_id=0x{shuttle.sensor.gyro_chip_id:02X}")
assert shuttle.sensor.acc_chip_id == 0x1E
assert shuttle.sensor.gyro_chip_id == 0x0F
shuttle.configure_interrupt_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_interrupt_streaming_multiple(includes_mcu_timestamp=False):
sensor_id, packet, time_stamp, payload = streaming
if sensor_id == 1:
a_x, a_y, a_z = struct.unpack("<xhhh", payload)
logger.info(f"[{idx}][{packet}][a] a_x={a_x:04d}, a_y={a_y:04d}, a_z={a_z:04d} ")
elif sensor_id == 2:
g_x, g_y, g_z = struct.unpack("<hhh", payload)
logger.info(f"[{idx}][{packet}][g] g_x={g_x:04d}, g_y={g_y:04d}, g_z={g_z:04d} ")
time.sleep(0.05)
shuttle.board.stop_interrupt_streaming()
51 changes: 51 additions & 0 deletions examples/bmi088/bmi088_spi_polling_streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import logging
import struct
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmi088.bmi088_shuttle import BMI088Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# This example is for Application Board 3.1 hardware
shuttle = BMI088Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_spi()
_ = shuttle.board.read_spi(shuttle.CSB1, 0, 1) # dummy read is required, do not delete

logger.info(f"acc_chip_id=0x{shuttle.sensor.acc_chip_id:02X}")
logger.info(f"gyro_chip_id=0x{shuttle.sensor.gyro_chip_id:02X}")
assert shuttle.sensor.acc_chip_id == 0x1E
assert shuttle.sensor.gyro_chip_id == 0x0F
shuttle.configure_polling_streaming()
shuttle.start_streaming()
time.sleep(0.1)
for idx in range(1000):
for streaming in shuttle.board.receive_polling_streaming_multiple():
sensor_id, payload = streaming
if sensor_id == 1:
a_x, a_y, a_z = struct.unpack("<xhhh", payload)
logger.info(f"[{idx}][a] a_x={a_x:04d}, a_y={a_y:04d}, a_z={a_z:04d} ")
elif sensor_id == 2:
g_x, g_y, g_z = struct.unpack("<hhh", payload)
logger.info(f"[{idx}][g] g_x={g_x:04d}, g_y={g_y:04d}, g_z={g_z:04d} ")
time.sleep(0.05)
shuttle.board.stop_polling_streaming()
44 changes: 44 additions & 0 deletions examples/bmi088/bmi088_spi_read_write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import logging
import sys
import time
from pathlib import Path

from umrx_app_v3.shuttle_board.bmi088.bmi088_shuttle import BMI088Shuttle


def setup_logging(level: int = logging.DEBUG) -> logging.Logger:
logger = logging.getLogger()
logger.setLevel(level)
stdout_handler = logging.StreamHandler(sys.stdout)
log_format = "(%(asctime)s) [%(levelname)-8s] %(filename)s:%(lineno)d: %(message)s"
log_formatter = logging.Formatter(log_format)
stdout_handler.setFormatter(log_formatter)
file_handler = logging.FileHandler(f"{Path(__file__).parent / Path(__file__).stem}.log", mode="w")
file_handler.setFormatter(log_formatter)
logger.addHandler(stdout_handler)
logger.addHandler(file_handler)
return logger


if __name__ == "__main__":
logger = setup_logging()
# For using with Application board 3.0, replace the line below with:
# shuttle = BMI088Shuttle.on_hardware_v3_rev0()
shuttle = BMI088Shuttle.on_hardware_v3_rev1()
shuttle.initialize()
shuttle.check_connected_hw()

shuttle.configure_spi()
_ = shuttle.board.read_spi(shuttle.CSB1, 0, 1) # dummy read is required, do not delete

logger.info(f"acc_chip_id=0x{shuttle.sensor.acc_chip_id:02X}")
logger.info(f"gyro_chip_id=0x{shuttle.sensor.gyro_chip_id:02X}")

logger.info(f"acc_status=0x{shuttle.sensor.acc_status:02X}")
logger.info(f"acc_fifo_config_1=0x{shuttle.sensor.acc_fifo_config_1:02X}")

logger.info(f"acc_conf=0x{shuttle.sensor.acc_conf:02X}")
shuttle.switch_on_accel()
time.sleep(0.1)
logger.info(f"acceleration={shuttle.sensor.acceleration}")
logger.info(f"gyro={shuttle.sensor.gyro_rate}")

0 comments on commit f3c68c6

Please sign in to comment.