Skip to content

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}')
or
# Print the raw sensor data packet as a hex string
def handle_raw(m):
    print(m.payload.hex())

See the SentiSystems Protobuf Documentation for message IDs and message descriptions.

Tip

When the RAW_MSG type is received, the data contained in the message's payload can be fed into your own sensor data parser. E.g when dealing with raw data from uBlox receivers, the open source pyubx2 can be used by feeding the payload data into the pyubx2 module.