OrbbecSDK_v2

C++ Sample: 3.lidar_record

Overview

This sample demonstrates how to record LiDAR and IMU sensor data to a bag file format, providing real-time data capture with pause/resume functionality for flexible recording sessions.

Knowledge

Code overview

  1. Initialize context and device selection

     ob::Context context;
     auto deviceList = context.queryDeviceList();
     auto device = selectDevice(deviceList);
    
  2. Frame counting with thread-safe monitoring

     std::mutex                      frameMutex;
     std::map<OBFrameType, uint64_t> frameCountMap;
     pipe->start(config, [&](std::shared_ptr<ob::FrameSet> frameSet) {
         if(frameSet == nullptr) {
             return;
         }
    
         std::lock_guard<std::mutex> lock(frameMutex);
         auto                        count = frameSet->getCount();
         for(uint32_t i = 0; i < count; i++) {
             auto frame = frameSet->getFrameByIndex(i);
             if(frame) {
                 auto type = frame->getType();
                 frameCountMap[type]++;
             }
         }
     });
    
  3. Recording setup

     // Initialize recording with user-specified filename
     auto recordDevice = std::make_shared<ob::RecordDevice>(device, filePath);
    
  4. Real-time FPS calculation and display

     std::cout << "Recording... Current FPS: ";
     for(const auto &item: tempCountMap) {
         auto  name = ob::TypeHelper::convertOBFrameTypeToString(item.first);
         float rate = item.second / (duration / 1000.0f);
    
         std::cout << std::fixed << std::setprecision(2) << std::showpoint;
         std::cout << seperate << name << "=" << rate;
         seperate = ", ";
     }
     std::cout << std::endl;
    
  5. Safe recording termination

     do {
         auto key = ob_smpl::waitForKeyPressed(50);
         if(key == ESC_KEY || key == 'q' || key == 'Q') {
             break;  // Safe exit
         }
     } while(true);
        
     pipe->stop();
     recordDevice = nullptr;  // Flush and save
    

Run Sample

Operation Instructions

  1. Start Program: Enter the desired output filename with .bag extension
  2. Automatic Setup: Program validates LiDAR device and enables all sensors
  3. Real-time Monitoring: Watch FPS statistics for each sensor type
  4. Safe Exit: Use ESC, Q, or q to stop recording properly

Safety Features

Monitoring Capabilities

Result

The program creates a complete sensor data recording with:

Device list:
0. name: LiDAR ME450, vid: 0x2bc5, pid: 0x1302, uid: 0x20:4b:5e:00:43:09, sn: T0H6851001Z
1. name: LiDAR ME450, vid: 0x2bc5, pid: 0x1302, uid: 0x20:4b:5e:13:64:30, sn: T0H6851000Z
Select a device: 1

------------------------------------------------------------------------
Please enter the output filename (with .bag extension) and press Enter to start recording: test.bag
Streams and recorder have started!
Press ESC, 'q', or 'Q' to stop recording and exit safely.
IMPORTANT: Always use ESC/q/Q to stop! Otherwise, the bag file will be corrupted and unplayable.

Recording... Current FPS: Accel=50.14, Gyro=50.14, LiDAR Points=19.87
Recording... Current FPS: Accel=49.93, Gyro=49.93, LiDAR Points=20.36
Recording... Current FPS: Accel=50.46, Gyro=50.46, LiDAR Points=20.38
Recording... Current FPS: Accel=49.95, Gyro=49.95, LiDAR Points=20.37
Recording... Current FPS: Accel=49.95, Gyro=49.95, LiDAR Points=20.37
Recording... Current FPS: Accel=48.98, Gyro=48.98, LiDAR Points=19.99
Recording... Current FPS: Accel=50.41, Gyro=50.41, LiDAR Points=20.36
Recording... Current FPS: Accel=50.41, Gyro=50.41, LiDAR Points=20.36