From 190d066ddf13905a92542f94cbd2c908532ef57f Mon Sep 17 00:00:00 2001 From: Osenberg Date: Thu, 8 Aug 2019 15:01:32 +0800 Subject: [PATCH] feat(src): added feature of syncing timestamp for 200B --- include/mynteye/api/api.h | 5 ++ include/mynteye/device/device.h | 4 ++ samples/tutorials/CMakeLists.txt | 1 + samples/tutorials/control/sync_timestamp.cc | 56 +++++++++++++++++++++ src/mynteye/api/api.cc | 4 ++ src/mynteye/device/channel/channels.cc | 27 +++++++++- src/mynteye/device/channel/channels.h | 1 + src/mynteye/device/device.cc | 8 +++ 8 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 samples/tutorials/control/sync_timestamp.cc diff --git a/include/mynteye/api/api.h b/include/mynteye/api/api.h index 27e2ab3..1fc69bd 100644 --- a/include/mynteye/api/api.h +++ b/include/mynteye/api/api.h @@ -252,6 +252,11 @@ class MYNTEYE_API API { */ void SetOptionValue(const Option &option, std::int32_t value); + /** + * Set the option value. + */ + bool SetOptionValue(const Option &option, std::uint64_t value); + /** * Run the option action. */ diff --git a/include/mynteye/device/device.h b/include/mynteye/device/device.h index 19fbc1c..05c94e9 100644 --- a/include/mynteye/device/device.h +++ b/include/mynteye/device/device.h @@ -223,6 +223,10 @@ class MYNTEYE_API Device { * Set the option value. */ void SetOptionValue(const Option &option, std::int32_t value); + /** + * Set the option value. + */ + bool SetOptionValue(const Option &option, std::uint64_t value); /** * Run the option action. diff --git a/samples/tutorials/CMakeLists.txt b/samples/tutorials/CMakeLists.txt index f11c497..f0523c4 100644 --- a/samples/tutorials/CMakeLists.txt +++ b/samples/tutorials/CMakeLists.txt @@ -125,6 +125,7 @@ make_executable2(ctrl_imu_low_pass_filter SRCS control/imu_low_pass_filter.cc WI make_executable2(ctrl_imu_range SRCS control/imu_range.cc WITH_OPENCV) make_executable2(ctrl_infrared SRCS control/infrared.cc WITH_OPENCV) make_executable2(ctrl_iic_adress SRCS control/iic_address.cc WITH_OPENCV) +make_executable2(ctrl_sync_timestamp SRCS control/sync_timestamp.cc WITH_OPENCV) make_executable2(ctrl_auto_exposure SRCS control/auto_exposure.cc util/cv_painter.cc WITH_OPENCV diff --git a/samples/tutorials/control/sync_timestamp.cc b/samples/tutorials/control/sync_timestamp.cc new file mode 100644 index 0000000..4a52fb0 --- /dev/null +++ b/samples/tutorials/control/sync_timestamp.cc @@ -0,0 +1,56 @@ +#include + +#include "mynteye/logger.h" +#include "mynteye/api/api.h" + +MYNTEYE_USE_NAMESPACE + +int main(int argc, char *argv[]) { + auto &&api = API::Create(argc, argv); + if (!api) return 1; + + bool ok; + auto &&request = api->SelectStreamRequest(&ok); + if (!ok) return 1; + api->ConfigStreamRequest(request); + + Model model = api->GetModel(); + + if (model != Model::STANDARD200B) { + LOG(INFO) << "Sorry, This device don't support sync timestamp"; + return 0; + } + + if (model == Model::STANDARD200B) { + std::uint64_t value = 10; + auto ok = api->SetOptionValue(Option::SYNC_TIMESTAMP, value); + if (ok) { + LOG(INFO) << "Set timestamp is successful."; + } else { + LOG(INFO) << "Set timestamp is failed."; + } + } + + api->Start(Source::VIDEO_STREAMING); + + cv::namedWindow("frame"); + + while (true) { + api->WaitForStreams(); + + auto &&left_data = api->GetStreamData(Stream::LEFT); + auto &&right_data = api->GetStreamData(Stream::RIGHT); + + cv::Mat img; + cv::hconcat(left_data.frame, right_data.frame, img); + cv::imshow("frame", img); + + char key = static_cast(cv::waitKey(1)); + if (key == 27 || key == 'q' || key == 'Q') { // ESC/Q + break; + } + } + + api->Stop(Source::VIDEO_STREAMING); + return 0; +} diff --git a/src/mynteye/api/api.cc b/src/mynteye/api/api.cc index e69dec9..6d58ea8 100644 --- a/src/mynteye/api/api.cc +++ b/src/mynteye/api/api.cc @@ -368,6 +368,10 @@ void API::SetOptionValue(const Option &option, std::int32_t value) { device_->SetOptionValue(option, value); } +bool API::SetOptionValue(const Option &option, std::uint64_t value) { + return device_->SetOptionValue(option, value); +} + bool API::RunOptionAction(const Option &option) const { return device_->RunOptionAction(option); } diff --git a/src/mynteye/device/channel/channels.cc b/src/mynteye/device/channel/channels.cc index 09cccc4..2348a52 100644 --- a/src/mynteye/device/channel/channels.cc +++ b/src/mynteye/device/channel/channels.cc @@ -97,7 +97,7 @@ int XuHalfDuplexId(Option option) { return 1; break; case Option::SYNC_TIMESTAMP: - return 2; + return 3; break; default: LOG(FATAL) << "No half duplex id for " << option; @@ -332,7 +332,7 @@ bool Channels::SetControlValue(const Option &option, std::uint64_t value) { LOG(WARNING) << option << " set value useless"; break; case Option::SYNC_TIMESTAMP: - return XuHalfDuplexSet(option, XU_SYNC_TIMESTAMP); + return XuHalfDuplexSet(option, value); break; default: LOG(ERROR) << "Unsupported option " << option; @@ -719,6 +719,29 @@ bool Channels::XuHalfDuplexSet(Option option, xu_cmd_t cmd) const { } } +bool Channels::XuHalfDuplexSet(Option option, std::uint64_t value) const { + int id = XuHalfDuplexId(option); + std::uint8_t data[20] = {static_cast(id & 0xFF), + static_cast(value & 0xFF), + static_cast((value >> 8) & 0xFF), + static_cast((value >> 16) & 0xFF), + static_cast((value >> 24) & 0xFF), + static_cast((value >> 32) & 0xFF), + static_cast((value >> 40) & 0xFF), + static_cast((value >> 48) & 0xFF), + static_cast((value >> 56) & 0xFF)}; + + if (XuControlQuery(CHANNEL_HALF_DUPLEX, uvc::XU_QUERY_SET, 20, data)) { + VLOG(2) << "XuHalfDuplexSet value (0x" << std::hex << std::uppercase << value + << ") of " << option << " success"; + return true; + } else { + LOG(WARNING) << "XuHalfDuplexSet value (0x" << std::hex << std::uppercase + << value << ") of " << option << " failed"; + return false; + } +} + bool Channels::XuImuWrite(const ImuReqPacket &req) const { auto &&data = req.to_data(); if (XuControlQuery( diff --git a/src/mynteye/device/channel/channels.h b/src/mynteye/device/channel/channels.h index bbe5acb..d7c43ad 100644 --- a/src/mynteye/device/channel/channels.h +++ b/src/mynteye/device/channel/channels.h @@ -112,6 +112,7 @@ class MYNTEYE_API Channels { void XuCamCtrlSet(Option option, std::int32_t value) const; bool XuHalfDuplexSet(Option option, xu_cmd_t cmd) const; + bool XuHalfDuplexSet(Option option, std::uint64_t value) const; bool XuImuWrite(const ImuReqPacket &req) const; bool XuImuRead(ImuResPacket *res) const; diff --git a/src/mynteye/device/device.cc b/src/mynteye/device/device.cc index 73cb39d..9284aeb 100644 --- a/src/mynteye/device/device.cc +++ b/src/mynteye/device/device.cc @@ -381,6 +381,14 @@ void Device::SetOptionValue(const Option &option, std::int32_t value) { channels_->SetControlValue(option, value); } +bool Device::SetOptionValue(const Option &option, std::uint64_t value) { + if (!Supports(option)) { + LOG(WARNING) << "Unsupported option: " << option; + return false; + } + return channels_->SetControlValue(option, value); +} + bool Device::RunOptionAction(const Option &option) const { if (!Supports(option)) { LOG(WARNING) << "Unsupported option: " << option;