SimpleSensorSync icon indicating copy to clipboard operation
SimpleSensorSync copied to clipboard

设置触发频率高于30hz后,获取的触发时间有延迟

Open linyicheng1 opened this issue 1 year ago • 4 comments

在相机和外部IMU触发的情况下,会出现如下的问题。

main.cpp

// 加入SDK头文件
#include "infinite_sense.h"
// 加入工业相机头文件
#include "mv_cam.h"
using namespace infinite_sense;
// 自定义回调函数
void ImuCallback(const void *msg, size_t) {
  const auto *imu_data = static_cast<const ImuData *>(msg);
  LOG(INFO) << imu_data->time_stamp_us << " " << "Accel: " << imu_data->a[0] << " " << imu_data->a[1] << " "
            << imu_data->a[2] << " Gyro: " << imu_data->g[0] << " " << imu_data->g[1] << " " << imu_data->g[2]
            << " Temp: " << imu_data->temperature;
  // 处理IMU数据
  std::cout<<imu_data->time_stamp_us<<std::endl;
}

// 自定义回调函数
void ImageCallback(const void *msg, size_t) {
  const auto *cam_data = static_cast<const CamData *>(msg);
  // 处理图像数据
}

int main() {
  // 1.创建同步器
  Synchronizer synchronizer;
  synchronizer.SetUsbLink("/dev/ttyACM0", 921600);
  // synchronizer.SetNetLink("192.168.1.188", 8888);
  // 2.配置同步接口
  auto mv_cam = std::make_shared<MvCam>();
  mv_cam->SetParams({{"cam_1", CAM_1}});
  mv_cam->SetParams({{"custom_imu", CAM_4}});
  synchronizer.UseSensor(mv_cam);

  // 3.开启同步
  synchronizer.Start();

  // 4.接收数据
  Messenger::GetInstance().SubStruct("custom_imu", ImuCallback);
  Messenger::GetInstance().SubStruct("cam_1", ImageCallback);

  while (true) {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }
  // 5.停止同步
  synchronizer.Stop();
  return 0;}

mv_cam.h

#pragma once
#include "infinite_sense.h"
namespace infinite_sense {
class MvCam final : public Sensor {
 public:
  ~MvCam() override;

  bool Initialization() override;
  void Stop() override;
  void Start() override;

 private:
  void Receive(void* handle, const std::string&) override;

  // 添加IMU数据读取函数
  void IMUReceive(void* handle, const std::string&);

  std::vector<int> rets_;
  std::vector<void*> handles_;
};
}  // namespace infinite_sense

mv_cam.cpp

#include "mv_cam.h"
#include "infinite_sense.h"
#include "MvCameraControl.h"
#include <chrono>

namespace infinite_sense {
bool IsColor(const MvGvspPixelType type) {
  switch (type) {
    case PixelType_Gvsp_BGR8_Packed:
    case PixelType_Gvsp_YUV422_Packed:
    case PixelType_Gvsp_YUV422_YUYV_Packed:
    case PixelType_Gvsp_BayerGR8:
    case PixelType_Gvsp_BayerRG8:
    case PixelType_Gvsp_BayerGB8:
    case PixelType_Gvsp_BayerBG8:
    case PixelType_Gvsp_BayerGB10:
    case PixelType_Gvsp_BayerGB10_Packed:
    case PixelType_Gvsp_BayerBG10:
    case PixelType_Gvsp_BayerBG10_Packed:
    case PixelType_Gvsp_BayerRG10:
    case PixelType_Gvsp_BayerRG10_Packed:
    case PixelType_Gvsp_BayerGR10:
    case PixelType_Gvsp_BayerGR10_Packed:
    case PixelType_Gvsp_BayerGB12:
    case PixelType_Gvsp_BayerGB12_Packed:
    case PixelType_Gvsp_BayerBG12:
    case PixelType_Gvsp_BayerBG12_Packed:
    case PixelType_Gvsp_BayerRG12:
    case PixelType_Gvsp_BayerRG12_Packed:
    case PixelType_Gvsp_BayerGR12:
    case PixelType_Gvsp_BayerGR12_Packed:
      return true;
    default:
      return false;
  }
}
bool IsMono(const MvGvspPixelType type) {
  switch (type) {
    case PixelType_Gvsp_Mono8:
    case PixelType_Gvsp_Mono10:
    case PixelType_Gvsp_Mono10_Packed:
    case PixelType_Gvsp_Mono12:
    case PixelType_Gvsp_Mono12_Packed:
      return true;
    default:
      return false;
  }
}

bool PrintDeviceInfo(const MV_CC_DEVICE_INFO *info) {
  if (info == nullptr) {
    LOG(WARNING) << "[WARNING] Failed to get camera details. Skipping...";
    return false;
  }
  LOG(INFO) << "-------------------------------------------------------------";
  LOG(INFO) << "----------------------Camera Device Info---------------------";
  if (info->nTLayerType == MV_GIGE_DEVICE) {
    const unsigned int n_ip1 = ((info->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
    const unsigned int n_ip2 = ((info->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
    const unsigned int n_ip3 = ((info->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
    const unsigned int n_ip4 = (info->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);
    LOG(INFO) << "  Device Model Name    : " << info->SpecialInfo.stGigEInfo.chModelName;
    LOG(INFO) << "  Current IP Address   : " << n_ip1 << "." << n_ip2 << "." << n_ip3 << "." << n_ip4;
    LOG(INFO) << "  User Defined Name    : " << info->SpecialInfo.stGigEInfo.chUserDefinedName;
  } else if (info->nTLayerType == MV_USB_DEVICE) {
    LOG(INFO) << "  Device Model Name    : " << info->SpecialInfo.stUsb3VInfo.chModelName;
    LOG(INFO) << "  User Defined Name    : " << info->SpecialInfo.stUsb3VInfo.chUserDefinedName;
  } else {
    LOG(ERROR) << "[ERROR] Unsupported camera type!";
    return false;
  }
  LOG(INFO) << "-------------------------------------------------------------";
  return true;
}

MvCam::~MvCam() { Stop(); }

bool MvCam::Initialization() {
  int n_ret = MV_OK;
  MV_CC_DEVICE_INFO_LIST st_device_list{};
  memset(&st_device_list, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
  n_ret = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &st_device_list);
  if (MV_OK != n_ret) {
    LOG(ERROR) << "MV_CC_EnumDevices fail! n_ret [" << n_ret << "]";
  }
  if (st_device_list.nDeviceNum > 0) {
    for (size_t i = 0; i < st_device_list.nDeviceNum; i++) {
      MV_CC_DEVICE_INFO *p_device_info = st_device_list.pDeviceInfo[i];
      if (nullptr == p_device_info) {
        continue;
      }
      PrintDeviceInfo(p_device_info);
    }
  } else {
    LOG(ERROR) << "Not find GIGE or USB Cam Devices!";
    return false;
  }
  LOG(INFO) << "Number of cameras detected : " << st_device_list.nDeviceNum;
  for (size_t i = 0; i < st_device_list.nDeviceNum; i++) {
    handles_.emplace_back(nullptr);
    rets_.push_back(MV_OK);
  }
  // 一共找到多少个设备,分别对所有的设备进行初始化
  for (unsigned int i = 0; i < st_device_list.nDeviceNum; ++i) {
    // 检测到有多少个相机要全部进行初始化。
    // 选择设备并创建句柄
    rets_[i] = MV_CC_CreateHandleWithoutLog(&handles_[i], st_device_list.pDeviceInfo[i]);
    if (MV_OK != rets_[i]) {
      LOG(ERROR) << "MV_CC_CreateHandle fail! n_ret [" << rets_[i] << "]";
    }
    // open device
    rets_[i] = MV_CC_OpenDevice(handles_[i]);
    if (MV_OK != rets_[i]) {
      LOG(ERROR) << "MV_CC_OpenDevice fail! n_ret [" << rets_[i] << "]";
    }
    // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal
    if (st_device_list.pDeviceInfo[i]->nTLayerType == MV_GIGE_DEVICE) {
      int n_packet_size = MV_CC_GetOptimalPacketSize(handles_[i]);
      if (n_packet_size > 0) {
        rets_[i] = MV_CC_SetIntValue(handles_[i], "GevSCPSPacketSize", n_packet_size);
        if (rets_[i] != MV_OK) {
          LOG(WARNING) << "Set Packet Size fail n_ret [0x" << std::hex << rets_[i] << "]";
        }
      } else {
        LOG(WARNING) << "Get Packet Size fail n_ret [0x" << std::hex << n_packet_size << "]";
      }
    }
    // 设置触发模式为off
    // rets_[i] = MV_CC_SetEnumValue(handles_[i], "TriggerMode", 1);
    // if (MV_OK != rets_[i]) {
    //   printf("MV_CC_SetTriggerMode fail! n_ret [%x]\n", rets_[i]);
    // }
    // 设置SDK内部图像缓存节点个数,大于等于1,在抓图前调用 1-30个缓存
    rets_[i] = MV_CC_SetImageNodeNum(handles_[i], 100);
    if (MV_OK != rets_[i]) {
      LOG(ERROR) << "MV_CC_SetImageNodeNum fail! n_ret [" << rets_[i] << "]";
    }
    // start grab image
    rets_[i] = MV_CC_StartGrabbing(handles_[i]);
    if (MV_OK != rets_[i]) {
      LOG(ERROR) << "MV_CC_StartGrabbing fail! n_ret [" << rets_[i] << "]";
    }
    LOG(INFO) << "Camera " << i << " opens to completion.";
  }
  if (0 == st_device_list.nDeviceNum) {
    return true;
  } else {
    return false;
  }
}

void MvCam::Stop() {
  Disable();
  std::this_thread::sleep_for(std::chrono::milliseconds{500});
  for (auto &cam_thread : cam_threads) {
    while (cam_thread.joinable()) {
      cam_thread.join();
    }
  }
  cam_threads.clear();
  cam_threads.shrink_to_fit();
  for (size_t i = 0; i < handles_.size(); ++i) {
    int n_ret = MV_OK;
    n_ret = MV_CC_StopGrabbing(handles_[i]);
    if (MV_OK != n_ret) {
      LOG(ERROR) << "MV_CC_StopGrabbing fail! n_ret [" << n_ret << "]";
    }
    n_ret = MV_CC_CloseDevice(handles_[i]);
    if (MV_OK != n_ret) {
      LOG(ERROR) << "MV_CC_CloseDevice fail! n_ret [" << n_ret << "]";
    }
    if (n_ret != MV_OK) {
      if (!handles_[i]) {
        MV_CC_DestroyHandle(handles_[i]);
        handles_[i] = nullptr;
      }
    }
    LOG(INFO) << "Exit  " << i << "  cam ";
  }
}

void MvCam::IMUReceive(void* handle, const std::string& name) {
  // 自定义的IMU类,包含1. IMU数据 2. 时间戳 3. IMU名称 
  ImuData imu_data;
  // 统一的消息管理类
  Messenger &messenger = Messenger::GetInstance();

  // 初始化完成,运行的状态中
  std::chrono::steady_clock::time_point last_time = std::chrono::steady_clock::now();
  while (is_running) {
    // 1. 读取IMU数据 TODO,根据不同IMU自行实现
    
    // TODO: 这里通过时间是否更新来产生一个虚拟的IMU数据用于测试。
    static uint64_t last_time_us = 0;

    // 2. 读取时间戳信号
    // 这里的time_stamp_us是IMU触发时间,调用 GET_LAST_TRIGGER_STATUS(params[name], time)) 获取name 对应设备最新触发时间
    if (params.find(name) == params.end()) {
      LOG(ERROR) << "IMU: " << name << " not found!";
    } else {
      if (uint64_t time; GET_LAST_TRIGGER_STATUS(params[name], time)) {
        imu_data.time_stamp_us = time;
      } else {
        LOG(ERROR) << "Trigger cam: " << name << " not found!";
      }
    }
    // 时间戳没更新,认为IMU数据也没更新
    if (last_time_us + 1 > imu_data.time_stamp_us) {
      continue;
    }
    
    // 时间戳更新了,产生虚拟IMU数据更新
    last_time_us = imu_data.time_stamp_us;
    // IMU的名称
    imu_data.name = name;
    // IMU数据
    imu_data.a[0] = 0;
    imu_data.a[1] = 0;
    imu_data.a[2] = 9.8;
    imu_data.g[0] = 0;
    imu_data.g[1] = 0;
    imu_data.g[2] = 0;
    imu_data.temperature = 28;
    
    // 计算间隔时间
    auto now = std::chrono::steady_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_time);
    last_time = now;  // 更新 last_time
    std::cout<<" cost ms: "<<duration.count()<<std::endl;
    
    messenger.PubStruct(name, &imu_data, sizeof(imu_data));
    std::this_thread::sleep_for(std::chrono::milliseconds{1});
  }
}



void MvCam::Receive(void *handle, const std::string &name) {
  unsigned int last_count = 0;
  MV_FRAME_OUT st_out_frame;
  CamData cam_data;
  Messenger &messenger = Messenger::GetInstance();
  while (is_running) {
    memset(&st_out_frame, 0, sizeof(MV_FRAME_OUT));
    int n_ret = MV_CC_GetImageBuffer(handle, &st_out_frame, 10);
    if (n_ret == MV_OK) {
      MVCC_FLOATVALUE expose_time;
      n_ret = MV_CC_GetExposureTime(handle, &expose_time);
      if (n_ret != MV_OK) {
        LOG(ERROR) << "Get ExposureTime fail! n_ret [0x" << std::hex << n_ret << "]";
      }
      // 这里的time_stamp_us是相机触发时间,需要加上曝光时间的一半,以获得相机拍摄的时间
      if (params.find(name) == params.end()) {
        LOG(ERROR) << "cam: " << name << " not found!";
      } else {
        if (uint64_t time; GET_LAST_TRIGGER_STATUS(params[name], time)) {
          cam_data.time_stamp_us = time + static_cast<uint64_t>(expose_time.fCurValue / 2.);
        } else {
          LOG(ERROR) << "Trigger cam: " << name << " not found!";
        }
      }
      MvGvspPixelType en_dst_pixel_type = PixelType_Gvsp_Undefined;
      unsigned int n_channel_num = 0;
      // 如果是彩色则转成RGB8
      if (IsColor(st_out_frame.stFrameInfo.enPixelType)) {
        n_channel_num = 3;
        en_dst_pixel_type = PixelType_Gvsp_RGB8_Packed;
      }
      // 如果是黑白则转换成Mono8
      else if (IsMono(st_out_frame.stFrameInfo.enPixelType)) {
        n_channel_num = 1;
        en_dst_pixel_type = PixelType_Gvsp_Mono8;
      }
      if (n_channel_num != 0) {
        cam_data.name = name;
        if (en_dst_pixel_type == PixelType_Gvsp_Mono8) {
          cam_data.image = GMat(st_out_frame.stFrameInfo.nHeight, st_out_frame.stFrameInfo.nWidth,
                                GMatType<uint8_t, 1>::Type, st_out_frame.pBufAddr)
                               .Clone();
        }
        if (en_dst_pixel_type == PixelType_Gvsp_RGB8_Packed) {
          cam_data.image = GMat(st_out_frame.stFrameInfo.nHeight, st_out_frame.stFrameInfo.nWidth,
                                GMatType<uint8_t, 3>::Type, st_out_frame.pBufAddr)
                               .Clone();
        }
        messenger.PubStruct(name, &cam_data, sizeof(cam_data));
        if (last_count == 0) {
          last_count = st_out_frame.stFrameInfo.nFrameNum;
        } else {
          last_count++;
          if (last_count != st_out_frame.stFrameInfo.nFrameNum) {
            LOG(WARNING) << "Loss of data from cam connection : " << last_count;
            last_count = 0;
          }
        }
      }
    }
    if (nullptr != st_out_frame.pBufAddr) {
      n_ret = MV_CC_FreeImageBuffer(handle, &st_out_frame);
      if (n_ret != MV_OK) {
        LOG(ERROR) << "Free Image Buffer fail! n_ret [0x" << std::hex << n_ret << "]";
      }
    }
    std::this_thread::sleep_for(std::chrono::milliseconds{2});
  }
}




void MvCam::Start() {
  for (const auto &handle : handles_) {
    int n_ret = MV_OK;
    MVCC_STRINGVALUE pst_value;
    n_ret = MV_CC_GetDeviceUserID(handle, &pst_value);
    if (n_ret != MV_OK) {
      LOG(ERROR) << "Get DeviceUserID fail! n_ret [0x" << std::hex << n_ret << "]";
    }
    Enable();
    auto name = std::string(pst_value.chCurValue);
    if (name.empty()) {
      // 相机名称默认从1开始
      static int cam_index{1};
      name = "cam_" + std::to_string(cam_index++);
      LOG(WARNING) << "Camera name is empty,Create new name: " << name;
    } else {
      LOG(INFO) << "Camera name is " << name;
    }
    cam_threads.emplace_back(&MvCam::Receive, this, handle, name);
    LOG(INFO) << "Camera " << name << " start.";
  }

  // 添加IMU数据的读取线程

  // 开启一个线程并告诉其IMU名称
  std::string name = "custom_imu";
  cam_threads.emplace_back(&MvCam::IMUReceive, this, nullptr, name);
  std::cout << "[CustomIMU] IMU capture started." << std::endl;
  Enable();
}
}  // namespace infinite_sense

在程序中设置触发频率为100hz,然后计算两次时间更新之间的间隔时间得到结果如下:

1756125368149775
 cost ms: 1
main.cpp:9 1756125368159775 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368159775
 cost ms: 1
main.cpp:9 1756125368169775 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368169775
 cost ms: 2
main.cpp:9 1756125368179776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368179776
 cost ms: 91
main.cpp:9 1756125368189776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368189776
 cost ms: 1
main.cpp:9 1756125368199776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368199776
 cost ms: 2
main.cpp:9 1756125368209776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368209776
 cost ms: 1
main.cpp:9 1756125368219776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368219776
 cost ms: 1
main.cpp:9 1756125368229776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368229776
 cost ms: 1
main.cpp:9 1756125368239776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368239776
 cost ms: 1
main.cpp:9 1756125368249776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368249776
 cost ms: 1
main.cpp:9 1756125368259776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368259776
 cost ms: 2
main.cpp:9 1756125368269776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368269776
 cost ms: 1
main.cpp:9 1756125368279776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368279776
 cost ms: 2
main.cpp:9 1756125368289776 Accel: 0 0 9.8 Gyro: 0 0 0 Temp: 28
1756125368289776
 cost ms: 91

中间会有90ms以上的间隔,这可能是ptp同步要求快速响应与发送时间戳要求的实时性之间冲突,导致的。因此触发时间并不总是最新的。

linyicheng1 avatar Aug 25 '25 12:08 linyicheng1

@KingRichord

linyicheng1 avatar Aug 25 '25 12:08 linyicheng1

目前临时解决方案为使用V4版本的网口进行同步,在100Hz下测试是没有问题的,但是串口同步依旧存在问题

linyicheng1 avatar Sep 03 '25 07:09 linyicheng1

后续这种情况就使用网口同步吧。串口本来就是不得已的办法。没问题就close吧

KingRichord avatar Sep 12 '25 09:09 KingRichord

IMU 数据由于发送延迟,发送的时间分布不均匀

Image

linyicheng1 avatar Sep 14 '25 09:09 linyicheng1