Tutorial - Retrieving Sensors Data

Open in ClaudeOpen in ChatGPT

This tutorial shows how to retrieve onboard sensor data from ZED 3D cameras: IMU (accelerometer and gyroscope) data on any camera with an integrated IMU (ZED 2/2i, ZED Mini, ZED X family), plus barometer and magnetometer data on ZED 2/2i. The sample loops and retrieves 800 sensor data samples, printing the updated 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;
}

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);
}

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
}

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]";
}

You do not need to check sensor availability based on your 3D camera model. If a sensor is not available on your camera, its data will contain NAN values and its 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();