Subscribing to a telemetry message
The drone publishes all of its telemetry data as protobuf encoded messages transmitted via a ZeroMQ socket. You can find the protobuf message definitions in the Protocol Definitions repository, and the generated python definitions are located in the blueye.protocol repository.
Upon connection the Drone object will instantiate an instance of the Telemetry class as the telemetry attribute. Using this attribute one can control various telemetry function and add/remove new callbacks.
Adding a callback
To add a callback we need to use the add_msg_callback function, and provide it with a list of telemetry messages types we want it to trigger on, as well as a function handle to call. All available telemetry messages can be found in telemetry.proto
Removing a callback
A callback is removed with remove_msg_callback using the ID returned when creating the callback.
Adjusting the publishing frequency of a telemetry message
set_msg_publish_frequency changes how often the drone publishes a telemetry message. The frequency must be greater than 0 and at most 100 Hz.
The drone has a single telemetry publisher shared by every connected client, so the new rate also applies to the Blueye app and any other SDK client, and it stays in effect until the drone reboots. There is no request for restoring the default, so set the rate back yourself before your program exits if other clients depend on it, as the example at the bottom of this page does.
Not every message has a publish frequency. The messages in EVENT_DRIVEN_TELEMETRY are published when their data arrives, such as a sonar ping or a detection, rather than on a timer. Messages from peripherals, like the thickness gauge, only exist while that peripheral is connected. The drone refuses to change the frequency in both cases, and the SDK raises a RuntimeError that names the message and the reason.
On Blunux versions older than 5.1, changing a rate could stop the drone from publishing that message until it restarts, while the request still reported success. If a message goes quiet after a rate change on such a drone, a reboot brings it back.
Adjusting the logging frequency of a telemetry message
The drone writes telemetry to the dive log at its own rate, separate from the publish rate. set_msg_log_frequency changes how often a message is logged, for example to get IMU or DVL data at a higher rate in the log without sending it to every client at that rate. It requires Blunux 5.1 or newer and takes the same frequency range as the publish frequency.
On the X3 Ultra and X7 a multibeam recording raises the log rate of six messages while it runs (PositionEstimateTel, AttitudeTel, CalibratedImuTel, CalibratedImuHpTel, DepthTel and AltitudeTel) and restores their defaults when it stops. A rate set for one of those is replaced both times.
IMU telemetry
The drone publishes raw and calibrated data from its inertial measurement units (IMUs):
| Message | Data | Models |
|---|---|---|
Imu1Tel, Imu2Tel |
Raw data from the drone's two IMUs | All |
CalibratedImuTel |
Calibrated IMU data | All |
ImuHpTel |
Raw data from the high-precision IMU | X3 Ultra and X7 |
CalibratedImuHpTel |
Calibrated high-precision IMU data | X3 Ultra and X7 |
Each message carries an imu field with the accelerometer in g, the gyroscope in rad/s, the magnetometer in μT and the temperature in °C. The axes are the drone's body frame: x forward, y right and z down. A drone at rest reads about +1 g on z. The high-precision IMU has no magnetometer, and its magnetometer fields stay 0.
All five are published at 1 Hz by default. Raise the rate with set_msg_publish_frequency, which changes it for every client, or the log rate with set_msg_log_frequency. This example prints all of them; on models without the high-precision IMU the high-precision callback never runs:
"""print_imu.py
Print the drone's IMU telemetry for a few seconds. Every model publishes Imu1Tel, Imu2Tel and
CalibratedImuTel. Only the X3 Ultra and X7 have the high-precision IMU behind ImuHpTel and
CalibratedImuHpTel, so on other models that callback never runs.
"""
import time
import blueye.protocol as bp
from blueye.sdk import Drone
def callback_imu_raw(msg_type, msg):
print(f"Raw {msg_type}\n{msg}")
def callback_imu_calibrated(msg_type, msg):
print(f"{msg_type}:\n{msg}")
def callback_imu_high_precision(msg_type, msg):
print(f"High-precision {msg_type}:\n{msg}")
if __name__ == "__main__":
with Drone(connect_as_observer=True) as my_drone:
# Use the current telemetry rates without changing them for the pilot.
my_drone.telemetry.add_msg_callback([bp.Imu1Tel, bp.Imu2Tel], callback_imu_raw)
my_drone.telemetry.add_msg_callback([bp.CalibratedImuTel], callback_imu_calibrated)
my_drone.telemetry.add_msg_callback(
[bp.ImuHpTel, bp.CalibratedImuHpTel], callback_imu_high_precision
)
time.sleep(3)
Example
The following example illustrates how can use a callback to print the depth reported by the drone.
"""print_depth.py
This example program demonstrates how one can add a callback function to a telemetry message, as
well as how to adjust the frequency of that telemetry message, and how to remove the callback.
"""
import time
import blueye.protocol as bp
from blueye.sdk import Drone
def callback_depth(msg_type: str, msg: bp.DepthTel):
"""Callback for the depth telemetry message
This function is called once for every DepthTel message received by the telemetry watcher
"""
print(f"Got a {msg_type} message with depth: {msg.depth.value:.3f}")
if __name__ == "__main__":
with Drone(connect_as_observer=True) as my_drone:
# Add a callback for the DepthTel message, storing the ID for later use
callback_id = my_drone.telemetry.add_msg_callback([bp.DepthTel], callback_depth)
try:
# This demonstration changes a drone-wide rate even though we are an observer.
# Omit the rate changes if another client needs its current rate preserved.
my_drone.telemetry.set_msg_publish_frequency(bp.DepthTel, 5)
# Callback is triggered by a separate thread while we sleep here
time.sleep(5)
finally:
my_drone.telemetry.remove_msg_callback(callback_id)
# Restore the default, also on Ctrl-C. This is not necessarily the rate another
# client had set before this example ran.
my_drone.telemetry.set_msg_publish_frequency(bp.DepthTel, 10)