Tutorial - Retrieving Sensors Data

This tutorial shows how to retrieve IMU, barometer and magnetometer data from ZED 3D cameras (ZED 2, ZED Mini). The sample will loop and retrieve sensor data samples for 5 seconds, printing the values in the console.

Getting Started #

  • First, download the latest version of the ZED SDK.
  • Download the Sensor Data sample code in C++, Python or C#.

Code Overview #

Open the camera #

As in previous tutorials, we create, configure and open the ZED 3D camera. Since we do not need depth information in this tutorial, we can disable depth capture using DEPTH_MODE::NONE.

// Create a ZED camera object
Camera zed;

// Set configuration parameters
InitParameters init_parameters;
// No depth computation required here
init_parameters.depth_mode = DEPTH_MODE::NONE;

// Open the camera
ERROR_CODE err = zed.open(init_parameters);
if (err != ERROR_CODE::SUCCESS) {
    cout << "Error " << err << ", exit program.\n";
    return -1;
}
# Create a ZED camera object
zed = sl.Camera()

# Set configuration parameters
init_params = sl.InitParameters()
# No depth computation is required here
init_params.depth_mode = sl.DEPTH_MODE.NONE

# Open the camera
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS :
    print(repr(err))
    zed.close()
    exit(1)
// Create a ZED camera object
Camera zed = new Camera(0);

// Set configuration parameters
InitParameters init_params = new InitParameters();
// No depth computation required here
init_parameters.depthMode = DEPTH_MODE.NONE;

// Open the camera
ERROR_CODE err = zed.Open(ref init_params);
if (err != ERROR_CODE.SUCCESS)

Capture Sensor Data #

The data from the different sensors are accessible with the SensorsData class. To retrieve sensor data, use getSensorsData() as shown below.

SensorsData sensors_data;
while(zed.grab() == ERROR_CODE::SUCCESS){
    zed.getSensorsData(sensors_data, TIME_REFERENCE::CURRENT);
}
sensors_data = sl.SensorsData()
while zed.grab() == sl.ERROR_CODE_SUCCESS:
    zed.get_sensors_data(sensors_data, sl.TIME_REFERENCE.CURRENT)
SensorsData sensors_data = new SensorsData();
RuntimeParameters runtimeParameters = new RuntimeParameters();
while(zed.Grab(ref runtimeParameters) == ERROR_CODE.SUCCESS){
    zed.GetSensorsData(ref sensors_data, TIME_REFERENCE.CURRENT);
}

Here we use TIME_REFERENCE::CURRENT to extract the most recent data available for each sensor. We can also extract sensor data that was captured at the same time or close to the camera’s latest image using TIME_REFERENCE::IMAGE.

For more information on reference time, see the Time Synchronization section.

Update Sensor Data #

Since the sensors on the 3D camera have different sampling rates, their data is not updated at the same frequency. To know if a given sensor data has been updated, we compare timestamps that are used as unique identifiers. If they are the same, then the sensor data has not been updated.

In this tutorial, we use a basic class TimestampHandler to store timestamps and check for data updates.

TimestampHandler ts;
if (ts.isNew(sensors_data.imu)) {
    // sensors_data.imu contains new data
}
ts_handler = TimestampHandler()
if ts_handler.is_new(sensors_data.get_imu_data()):
    # sensors_data.get_imu_data() contains new data
ulong last_imu_timestamp = 0;
if (sensors_data.imu.timestamp > last_imu_timestamp){
    // sensors_data.imu contains new data
    last_imu_timestamp = sensors_data.imu.timestamp;
}

If new data is available, we display it in the console:

// Check if IMU data has been updated
if (ts.isNew(sensors_data.imu)) {
    cout << "IMU Orientation: {" << sensors_data.imu.pose.getOrientation() << "}";
    cout << "IMU Linear Acceleration: {" << sensors_data.imu.linear_acceleration << "} [m/sec^2]";
    cout << "IMU Angular Velocity: {" << sensors_data.imu.angular_velocity << "} [deg/sec]";
}

// Check if Magnetometer data has been updated
if (ts.isNew(sensors_data.magnetometer)) {
    cout << " Magnetometer Magnetic Field: {" << sensors_data.magnetometer.magnetic_field_calibrated << "} [uT]";
}

// Check if Barometer data has been updated
if (ts.isNew(sensors_data.barometer)) {
    cout << " Barometer Atmospheric pressure:" << sensors_data.barometer.pressure << " [hPa]";
}
# Check if IMU data has been updated
if ts_handler.is_new(sensors_data.get_imu_data()):
    quaternion = sensors_data.get_imu_data().get_pose().get_orientation().get()
    print("IMU Orientation: {}".format(quaternion))
    linear_acceleration = sensors_data.get_imu_data().get_linear_acceleration()
    print("IMU Acceleration: {} [m/sec^2]".format(linear_acceleration))
    angular_velocity = sensors_data.get_imu_data().get_angular_velocity()
    print("IMU Angular Velocity: {} [deg/sec]".format(angular_velocity))

# Check if Magnetometer data has been updated
if ts_handler.is_new(sensors_data.get_magnetometer_data()):
    magnetic_field_calibrated = sensors_data.get_magnetometer_data().get_magnetic_field_calibrated()
    print("Magnetometer Magnetic Field: {} [uT]".format(magnetic_field_calibrated))

# Check if Barometer data has been updated 
if ts_handler.is_new(sensors_data.get_barometer_data()):
    magnetic_field_calibrated = sensors_data.get_barometer_data().pressure
    print("Barometer Atmospheric pressure: {} [hPa]".format(sensors_data.get_barometer_data().pressure))
// Check if IMU data has been updated
if (sensors_data.imu.timestamp > last_imu_timestamp){
    Console.WriteLine("IMU Orientation : " + sensors_data.imu.fusedOrientation);
    Console.WriteLine("IMU Angular Velocity : " + sensors_data.imu.angularVelocity + "m/sec^2");
    Console.WriteLine("IMU Linear Acceleration : " + sensors_data.imu.linearAcceleration + "deg/sec");
    last_imu_timestamp = sensors_data.imu.timestamp;
}
// Check if Magnetometer data has been updated
if (sensors_data.magnetometer.timestamp > last_magnetometer_timestamp){
    Console.WriteLine("Magnetometer Magnetic field : " + sensors_data.magnetometer.magneticField + "uT");
    last_magnetometer_timestamp = sensors_data.magnetometer.timestamp;
}
// Check if Barometer data has been updated
if (sensors_data.barometer.timestamp > last_barometer_timestamp){
    Console.WriteLine("Barometer Atmospheric pressure : " + sensors_data.barometer.pressure + "hPa");
    last_barometer_timestamp = sensors_data.barometer.timestamp;
}

You do not need to check for sensor availability depending on your 3D camera model. If the sensors are not available for your camera, its data will contain NAN values and the timestamp will be 0.

Close the camera #

Once sensor data has been extracted, don’t forget to close the camera before exiting the program.

// Close the camera
zed.close();
# Close the camera
zed.close()
// Close the camera
zed.Close();

Next Steps #

At this point, you know how to retrieve sensor data from ZED 2 and ZED Mini stereo cameras. For more information on the camera sensor stack and API: