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

This example shows how to read a Taobotics USB IMU with the pymrpt hwdrivers module.

 1#!/usr/bin/env python3
 2
 3# ---------------------------------------------------------------------
 4# Install python3-pymrpt, ros-$ROS_DISTRO-python-mrpt,
 5# ros-$ROS_DISTRO-mrpt2, or test with a local build with:
 6# export PYTHONPATH=$HOME/code/mrpt/build-Release/:$PYTHONPATH
 7# ---------------------------------------------------------------------
 8
 9from mrpt.pymrpt import mrpt
10
11imu = mrpt.hwdrivers.CTaoboticsIMU()
12
13imu.setSerialPort('/dev/ttyUSB0')
14
15# Opens the serial port and start listening
16imu.initialize()
17
18while True:
19    # Process serial port data queue:
20    imu.doProcess()
21
22    # Get observations:
23    obsList = imu.getObservations()
24
25    # Process them:
26    if not obsList.empty():
27        print('Read {} observations'.format(obsList.size()))
28        for t, obs in obsList:
29            timestamp = mrpt.Clock.toDouble(t)
30            print('Time={} obs={}'.format(timestamp, obs.asString()))
31
32            # You can access individual IMU readings with:
33            # o = mrpt.obs.CObservationIMU(obs)
34            # angVel_z = o.get(mrpt.obs.TIMUDataIndex.IMU_WZ)
35            #
36            # See: https://docs.mrpt.org/reference/latest/enum_mrpt_obs_TIMUDataIndex.html