Python example: hwdriver-tao-imu-usb.py

Reads a Taobotics USB IMU sensor with mrpt.hwdrivers.

Modules: mrpt.core, mrpt.hwdrivers, mrpt.obs | Requires: hardware

 1#!/usr/bin/env python3
 2"""
 3Reads a Taobotics USB IMU sensor with mrpt.hwdrivers.
 4
 5Usage:
 6  . install/setup.bash
 7  ./mrpt_examples_py/hwdriver-tao-imu-usb.py [/dev/ttyUSB0]
 8
 9Any other sensor driver can be used the same way, configured from an .ini
10file section instead of with setters:
11  sensor = CGenericSensor.createSensor("CGPSInterface")
12  sensor.loadConfig(mrpt.config.CConfigFile("sensors.ini"), "GPS")
13"""
14# mrpt-example: requires=hardware; video=qaaP9BmZYmo
15
16import sys
17import time
18
19from mrpt.core import Clock
20from mrpt.hwdrivers import CGenericSensor, CTaoboticsIMU
21from mrpt.obs import CObservationIMU, TIMUDataIndex
22
23serial_port = sys.argv[1] if len(sys.argv) > 1 else "/dev/ttyUSB0"
24
25imu = CTaoboticsIMU()
26imu.setSerialPort(serial_port)
27imu.initialize()
28if imu.getState() == CGenericSensor.ssError:
29    sys.exit(f"Could not open the IMU at {serial_port}")
30
31while True:
32    imu.doProcess()
33    for timestamp, obs in imu.getObservations():
34        if not isinstance(obs, CObservationIMU):
35            continue
36        print(f"t={Clock.toDouble(timestamp):.3f} "
37              f"wz={obs.get(TIMUDataIndex.IMU_WZ):+.4f} rad/s "
38              f"acc_z={obs.get(TIMUDataIndex.IMU_Z_ACC):+.3f} m/s2")
39    time.sleep(0.005)  # poll often: data is buffered by the driver between calls