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 C++ or Python sample code.

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 is accessible with the SensorsData class. To retrieve sensor data, use getSensorsData() as shown below.

SensorsData sensors_data;

while(zed.grab() == 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 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 which are used as unique identifiers. If they are the same, then a sensor data has not been updated.

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

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 for sensors availability depending on your 3D camera model. If the sensors are not available for your camera, its data will contain NAN values and timestamp will be 0.

Close the camera

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

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:

  • Read the Sensors Overview to learn about what sensors data can be retrieved from the 3D cameras.
  • Read the Using the Sensors API section to see code examples on how to access sensors data.