aiyangsky
Here is my test code.
#include "depthai/depthai.hpp"
#include <chrono>
#include <opencv2/opencv.hpp>
#include <time.h>
#include <iostream>
#include <thread>
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
#include <time.h>
std::mutex timerMutex;
std::condition_variable_any timerMutexEmpty;
struct timespec current_time_s;
static void timerCallback(int signo, siginfo_t *si, void *data)
{
std::lock_guard<std::mutex> locker(timerMutex);
clock_gettime(CLOCK_REALTIME, ¤t_time_s);
timerMutexEmpty.notify_all();
}
void getSystemTime()
{
std::ofstream File("systime.csv", std::ios::app);
File << "system_time" << std::endl;
struct sigevent evp;
struct itimerspec ts;
timer_t timer;
int ret;
evp.sigev_value.sival_ptr = &timer;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGALRM;
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerCallback;
sigaction(SIGALRM, &sa, NULL);
ret = timer_create(CLOCK_REALTIME, &evp, &timer);
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 100000000;
ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 100000000;
ret = timer_settime(timer, 0, &ts, NULL);
while (true)
{
std::lock_guard<std::mutex> locker(timerMutex);
timerMutexEmpty.wait(timerMutex);
uint64_t current_utc_time = current_time_s.tv_sec * 1000000000ULL + current_time_s.tv_nsec;
File << std::fixed << std::setprecision(9) << current_utc_time / 1e9 << std::endl;
}
timer_delete(timer);
return;
}
int main()
{
std::ofstream File("OAK_time.csv", std::ios::app);
File << "CameraC_time" << std::endl;
// Create pipeline
dai::Pipeline pipeline;
// Define sources and outputs
// cam_b
auto camB = pipeline.create<dai::node::MonoCamera>();
camB->setBoardSocket(dai::CameraBoardSocket::CAM_B);
camB->setResolution(dai::MonoCameraProperties::SensorResolution::THE_400_P);
camB->setFps(10);
auto BOut = pipeline.create<dai::node::XLinkOut>();
BOut->setStreamName("CAM_B");
camB->out.link(BOut->input);
dai::Device device(pipeline);
std::thread thread_systemtime(&getSystemTime);
cv::Mat BMat;
auto imageB = device.getOutputQueue("CAM_B", 1, false);
bool is_firt_frame = true;
uint64_t boot_utc_time;
while (true)
{
auto B = imageB->get<dai::ImgFrame>();
BMat = B->getCvFrame();
auto tsB = B->getTimestamp().time_since_epoch().count();
if (is_firt_frame)
{
is_firt_frame = true;
struct timespec boot_time;
struct timespec current_time;
// 获取系统启动后的时间
if (clock_gettime(CLOCK_BOOTTIME, &boot_time) != 0)
{
throw std::runtime_error("Failed to get boot time");
}
// 获取当前时间
if (clock_gettime(CLOCK_REALTIME, ¤t_time) != 0)
{
throw std::runtime_error("Failed to get current time");
}
boot_utc_time = current_time.tv_sec * 1000000000ULL + current_time.tv_nsec - (boot_time.tv_sec * 1000000000ULL + boot_time.tv_nsec);
}
uint64_t oak_utc_timestamp = boot_utc_time + tsB;
File << std::fixed << std::setprecision(9) << oak_utc_timestamp / 1e9 << std::endl;
}
return 0;
}