Tutorial - Retrieving Sensors Data

Open in ClaudeOpen in ChatGPT

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.

1// Create a ZED camera object
2Camera zed;
3
4// Set configuration parameters
5InitParameters init_parameters;
6// No depth computation required here
7init_parameters.depth_mode = DEPTH_MODE::NONE;
8
9// Open the camera
10ERROR_CODE err = zed.open(init_parameters);
11if (err != ERROR_CODE::SUCCESS) {
12 cout << "Error " << err << ", exit program.\n";
13 return -1;
14}

Capture Sensor Data

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

1SensorsData sensors_data;
2while(zed.grab() == ERROR_CODE::SUCCESS){
3 zed.getSensorsData(sensors_data, TIME_REFERENCE::CURRENT);
4}

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.

1TimestampHandler ts;
2if (ts.isNew(sensors_data.imu)) {
3 // sensors_data.imu contains new data
4}

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

1// Check if IMU data has been updated
2if (ts.isNew(sensors_data.imu)) {
3 cout << "IMU Orientation: {" << sensors_data.imu.pose.getOrientation() << "}";
4 cout << "IMU Linear Acceleration: {" << sensors_data.imu.linear_acceleration << "} [m/sec^2]";
5 cout << "IMU Angular Velocity: {" << sensors_data.imu.angular_velocity << "} [deg/sec]";
6}
7
8// Check if Magnetometer data has been updated
9if (ts.isNew(sensors_data.magnetometer)) {
10 cout << " Magnetometer Magnetic Field: {" << sensors_data.magnetometer.magnetic_field_calibrated << "} [uT]";
11}
12
13// Check if Barometer data has been updated
14if (ts.isNew(sensors_data.barometer)) {
15 cout << " Barometer Atmospheric pressure:" << sensors_data.barometer.pressure << " [hPa]";
16}

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.

1// Close the camera
2zed.close();