Python example: mrpt_hwdrivers_example.py

Creates and configures MRPT sensor drivers by class name with mrpt.hwdrivers.

Modules: mrpt.config, mrpt.hwdrivers

 1#!/usr/bin/env python3
 2"""
 3Creates and configures MRPT sensor drivers by class name with mrpt.hwdrivers.
 4
 5Any MRPT sensor driver can be created by class name and configured from an
 6.ini section, like the rawlog-grabber application does. This example only
 7configures a driver (no hardware needed); see hwdriver-tao-imu-usb.py for a
 8capture loop.
 9
10Run:
11    python3 mrpt_hwdrivers_example.py
12"""
13
14from mrpt.config import CConfigFileMemory
15from mrpt.hwdrivers import CGenericSensor, CJoystick
16
17# ---------------------------------------------------------------------------
18# Create and configure a driver by its class name
19# ---------------------------------------------------------------------------
20gps = CGenericSensor.createSensor("CGPSInterface")
21gps.loadConfig(CConfigFileMemory("""
22[GPS]
23sensorLabel  = GPS_RTK
24COM_port_LIN = /dev/ttyUSB0
25COM_port_WIN = COM3
26baudRate     = 115200
27"""), "GPS")
28print(f"{gps!r}: port={gps.getSerialPortName()}, rate={gps.getProcessRate()} Hz")
29
30# With a device connected, the capture loop would be:
31#   gps.initialize()
32#   while True:
33#       gps.doProcess()
34#       for timestamp, obs in gps.getObservations():
35#           print(obs)
36
37# ---------------------------------------------------------------------------
38# Joysticks / gamepads
39# ---------------------------------------------------------------------------
40n_joy = CJoystick.getJoysticksCount()
41print(f"\n{n_joy} joystick(s) connected")
42if n_joy > 0:
43    state = CJoystick().getJoystickPosition(0)
44    if state is not None:
45        print(f"  axes={state.axes} buttons={state.buttons}")