Using SentiUtils with Python
Preparations
Before you start integrating SentiUtil's data stream into Python, please make sure your system has SentiUtils and the Protobuf compiler installed.
To work with SentiUtil's Protobuf message definitions, please clone or download the SentiSystems senti-proto repository.
git clone https://gitlab.senti.no/senti/senti-proto.git
To generate the needed Python file, proceed to run the following command
protoc --python_out=<insert location for output file> senti.proto
This should generate a senti_pb2.py
file, which should be placed in the same folder as your Python software.
Warning
For the newest versions of Protobuf you need to run export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
in your environment (e.g from the terminal used to start the python software) to use Python to receive SentiUtil's Protobuf messages.
SentiMessageParser for Python
The file scripts/sentipy.py
contains the class SentiMessageParser
, which will read and parse the SentiUtil's data stream.
Example usage is described in scripts/udp_message_receiver.py
, but in short works as follows:
import socket
import senti_pb2 as senti
import sentipy
# Please make sure senti_pb2.py and sentipy.py
# is located in the same folder as this code
UDP_IP = ""
UDP_PORT = 21314
parser = sentipy.SentiMessageParser(UDP_IP, UDP_PORT)
# Register callbacks based on MessageID
# create your own functions to decide what should happen with the data
parser.register_callback(senti.RAW_MSG, handle_raw)
parser.register_callback(senti.IMU_MAG_ORIENTATION_MSG, handle_imu)
parser.connect()
while True:
parser.receive_message()
The callback functions are functions which takes a Protobuf message of MessageID type as input.
E.g:
# Print linear acceleration and angular velocity of incoming IMU_MSG.
def handle_imu(m):
print(f'lin_acc=[{m.lin_acc[0]:7.2f}, {m.lin_acc[1]:7.2f}, {m.lin_acc[2]:7.2f}]
ang_vel=[{m.ang_vel[0]:7.2f}, {m.ang_vel[1]:7.2f}, {m.ang_vel[2]:7.2f}]
temp={m.temperature:.1f}')
See the SentiSystems Protobuf Documentation for message IDs and message descriptions.