From de0f0d0639ed03583afb7f83770b1a8c332a452c Mon Sep 17 00:00:00 2001 From: John Zhao Date: Tue, 15 May 2018 22:01:15 +0800 Subject: [PATCH] Log error and return if no devices --- include/mynteye/utils.h | 12 +++++++-- samples/api/camera.cc | 2 ++ samples/device/camera.cc | 2 ++ samples/tutorials/control/auto_exposure.cc | 2 ++ samples/tutorials/control/framerate.cc | 2 ++ samples/tutorials/control/infrared.cc | 2 ++ samples/tutorials/control/manual_exposure.cc | 2 ++ samples/tutorials/data/get_depth.cc | 2 ++ samples/tutorials/data/get_device_info.cc | 2 ++ samples/tutorials/data/get_disparity.cc | 2 ++ samples/tutorials/data/get_from_callbacks.cc | 2 ++ samples/tutorials/data/get_img_params.cc | 2 ++ samples/tutorials/data/get_imu.cc | 2 ++ samples/tutorials/data/get_imu_params.cc | 2 ++ samples/tutorials/data/get_points.cc | 2 ++ samples/tutorials/data/get_stereo.cc | 2 ++ .../tutorials/data/get_stereo_rectified.cc | 2 ++ samples/tutorials/data/get_with_plugin.cc | 2 ++ src/api/api.cc | 9 ++++++- src/api/api.h | 26 +++++++++++++++++++ src/public/utils.cc | 5 +++- tools/dataset/record.cc | 2 ++ tools/writer/device_info_writer.cc | 2 ++ tools/writer/img_params_writer.cc | 2 ++ tools/writer/imu_params_writer.cc | 2 ++ tools/writer/save_all_infos.cc | 2 ++ 26 files changed, 92 insertions(+), 4 deletions(-) diff --git a/include/mynteye/utils.h b/include/mynteye/utils.h index 0b8f474..7ca41bd 100644 --- a/include/mynteye/utils.h +++ b/include/mynteye/utils.h @@ -25,14 +25,22 @@ class Device; namespace device { -/** Detecting MYNT EYE devices and prompt user to select one */ +/** + * Detecting MYNT EYE devices and prompt user to select one. + * @return the selected device, or `nullptr` if none. + */ MYNTEYE_API std::shared_ptr select(); } // namespace device namespace utils { -/** Get real exposure time in ms from virtual value, according to its frame rate +/** + * Get real exposure time in ms from virtual value, according to its frame rate. + * @param frame_rate the frame rate of the device. + * @param exposure_time the virtual exposure time. + * @return the real exposure time in ms, or the virtual value if frame rate is + * invalid. */ MYNTEYE_API float get_real_exposure_time( std::int32_t frame_rate, std::uint16_t exposure_time); diff --git a/samples/api/camera.cc b/samples/api/camera.cc index d5aff62..5cddd85 100644 --- a/samples/api/camera.cc +++ b/samples/api/camera.cc @@ -22,6 +22,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // api->SetOptionValue(Option::FRAME_RATE, 25); // api->SetOptionValue(Option::IMU_FREQUENCY, 500); diff --git a/samples/device/camera.cc b/samples/device/camera.cc index d9b210f..bcd9695 100644 --- a/samples/device/camera.cc +++ b/samples/device/camera.cc @@ -27,6 +27,8 @@ int main(int argc, char *argv[]) { glog_init _(argc, argv); auto &&device = device::select(); + if (!device) + return 1; /* { // auto-exposure device->SetOptionValue(Option::EXPOSURE_MODE, 0); diff --git a/samples/tutorials/control/auto_exposure.cc b/samples/tutorials/control/auto_exposure.cc index c517256..f1f5c13 100644 --- a/samples/tutorials/control/auto_exposure.cc +++ b/samples/tutorials/control/auto_exposure.cc @@ -23,6 +23,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // auto-exposure: 0 api->SetOptionValue(Option::EXPOSURE_MODE, 0); diff --git a/samples/tutorials/control/framerate.cc b/samples/tutorials/control/framerate.cc index 882fa1f..55cfd30 100644 --- a/samples/tutorials/control/framerate.cc +++ b/samples/tutorials/control/framerate.cc @@ -24,6 +24,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // Attention: must set FRAME_RATE and IMU_FREQUENCY together, otherwise won't // succeed. diff --git a/samples/tutorials/control/infrared.cc b/samples/tutorials/control/infrared.cc index da0e12b..7ed6286 100644 --- a/samples/tutorials/control/infrared.cc +++ b/samples/tutorials/control/infrared.cc @@ -21,6 +21,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // Detect infrared add-ons LOG(INFO) << "Support infrared: " << std::boolalpha diff --git a/samples/tutorials/control/manual_exposure.cc b/samples/tutorials/control/manual_exposure.cc index 74c4b68..12aef0e 100644 --- a/samples/tutorials/control/manual_exposure.cc +++ b/samples/tutorials/control/manual_exposure.cc @@ -23,6 +23,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // manual-exposure: 1 api->SetOptionValue(Option::EXPOSURE_MODE, 1); diff --git a/samples/tutorials/data/get_depth.cc b/samples/tutorials/data/get_depth.cc index 2b5e2a9..274c8ee 100644 --- a/samples/tutorials/data/get_depth.cc +++ b/samples/tutorials/data/get_depth.cc @@ -21,6 +21,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; api->EnableStreamData(Stream::DEPTH); diff --git a/samples/tutorials/data/get_device_info.cc b/samples/tutorials/data/get_device_info.cc index 1701373..ee41348 100644 --- a/samples/tutorials/data/get_device_info.cc +++ b/samples/tutorials/data/get_device_info.cc @@ -19,6 +19,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; LOG(INFO) << "Device name: " << api->GetInfo(Info::DEVICE_NAME); LOG(INFO) << "Serial number: " << api->GetInfo(Info::SERIAL_NUMBER); diff --git a/samples/tutorials/data/get_disparity.cc b/samples/tutorials/data/get_disparity.cc index a96196d..5a9f798 100644 --- a/samples/tutorials/data/get_disparity.cc +++ b/samples/tutorials/data/get_disparity.cc @@ -21,6 +21,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // api->EnableStreamData(Stream::DISPARITY); api->EnableStreamData(Stream::DISPARITY_NORMALIZED); diff --git a/samples/tutorials/data/get_from_callbacks.cc b/samples/tutorials/data/get_from_callbacks.cc index f73b358..cb51875 100644 --- a/samples/tutorials/data/get_from_callbacks.cc +++ b/samples/tutorials/data/get_from_callbacks.cc @@ -28,6 +28,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // Attention: must not block the callbacks. diff --git a/samples/tutorials/data/get_img_params.cc b/samples/tutorials/data/get_img_params.cc index e43be7d..4298c38 100644 --- a/samples/tutorials/data/get_img_params.cc +++ b/samples/tutorials/data/get_img_params.cc @@ -19,6 +19,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; LOG(INFO) << "Intrinsics left: {" << api->GetIntrinsics(Stream::LEFT) << "}"; LOG(INFO) << "Intrinsics right: {" << api->GetIntrinsics(Stream::RIGHT) diff --git a/samples/tutorials/data/get_imu.cc b/samples/tutorials/data/get_imu.cc index b52cb5f..6ed0552 100644 --- a/samples/tutorials/data/get_imu.cc +++ b/samples/tutorials/data/get_imu.cc @@ -23,6 +23,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; // Enable this will cache the motion datas until you get them. api->EnableMotionDatas(); diff --git a/samples/tutorials/data/get_imu_params.cc b/samples/tutorials/data/get_imu_params.cc index 8ab5ac2..ddcfd49 100644 --- a/samples/tutorials/data/get_imu_params.cc +++ b/samples/tutorials/data/get_imu_params.cc @@ -19,6 +19,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; LOG(INFO) << "Motion intrinsics: {" << api->GetMotionIntrinsics() << "}"; LOG(INFO) << "Motion extrinsics left to imu: {" diff --git a/samples/tutorials/data/get_points.cc b/samples/tutorials/data/get_points.cc index 246c10b..828418d 100644 --- a/samples/tutorials/data/get_points.cc +++ b/samples/tutorials/data/get_points.cc @@ -23,6 +23,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; api->EnableStreamData(Stream::POINTS); diff --git a/samples/tutorials/data/get_stereo.cc b/samples/tutorials/data/get_stereo.cc index 284a778..a82a9e9 100644 --- a/samples/tutorials/data/get_stereo.cc +++ b/samples/tutorials/data/get_stereo.cc @@ -21,6 +21,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; api->Start(Source::VIDEO_STREAMING); diff --git a/samples/tutorials/data/get_stereo_rectified.cc b/samples/tutorials/data/get_stereo_rectified.cc index f36899e..04c4eae 100644 --- a/samples/tutorials/data/get_stereo_rectified.cc +++ b/samples/tutorials/data/get_stereo_rectified.cc @@ -21,6 +21,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; api->EnableStreamData(Stream::LEFT_RECTIFIED); api->EnableStreamData(Stream::RIGHT_RECTIFIED); diff --git a/samples/tutorials/data/get_with_plugin.cc b/samples/tutorials/data/get_with_plugin.cc index 1fd0e57..a2201ba 100644 --- a/samples/tutorials/data/get_with_plugin.cc +++ b/samples/tutorials/data/get_with_plugin.cc @@ -21,6 +21,8 @@ MYNTEYE_USE_NAMESPACE int main(int argc, char *argv[]) { auto &&api = API::Create(argc, argv); + if (!api) + return 1; api->EnablePlugin("plugins/linux-x86_64/libplugin_g_cuda9.1_opencv3.4.0.so"); diff --git a/src/api/api.cc b/src/api/api.cc index e10184c..a8ae126 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -41,17 +41,24 @@ std::shared_ptr API::Create() { } std::shared_ptr API::Create(std::shared_ptr device) { + if (!device) + return nullptr; return std::make_shared(device); } std::shared_ptr API::Create(int argc, char *argv[]) { static glog_init _(argc, argv); - return std::make_shared(device::select()); + auto &&device = device::select(); + if (!device) + return nullptr; + return std::make_shared(device); } std::shared_ptr API::Create( int argc, char *argv[], std::shared_ptr device) { static glog_init _(argc, argv); + if (!device) + return nullptr; return std::make_shared(device); } diff --git a/src/api/api.h b/src/api/api.h index 285e604..a172b05 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -51,9 +51,35 @@ class MYNTEYE_API API { explicit API(std::shared_ptr device); /*virtual*/ ~API(); + /** + * Create the API instance. + * @return the API instance. + * @note This will call device::select() to select a device. + */ static std::shared_ptr Create(); + /** + * Create the API instance. + * @param device the selected device. + * @return the API instance. + */ static std::shared_ptr Create(std::shared_ptr device); + /** + * Create the API instance. + * @param argc the arg count. + * @param argv the arg values. + * @return the API instance. + * @note This will init glog with args and call device::select() to select a + * device. + */ static std::shared_ptr Create(int argc, char *argv[]); + /** + * Create the API instance. + * @param argc the arg count. + * @param argv the arg values. + * @param device the selected device. + * @return the API instance. + * @note This will init glog with args. + */ static std::shared_ptr Create( int argc, char *argv[], std::shared_ptr device); diff --git a/src/public/utils.cc b/src/public/utils.cc index af33e53..f4ad5b3 100644 --- a/src/public/utils.cc +++ b/src/public/utils.cc @@ -28,7 +28,10 @@ std::shared_ptr select() { auto &&devices = context.devices(); size_t n = devices.size(); - LOG_IF(FATAL, n <= 0) << "No MYNT EYE devices :("; + if (n <= 0) { + LOG(ERROR) << "No MYNT EYE devices :("; + return nullptr; + } LOG(INFO) << "MYNT EYE devices:"; for (size_t i = 0; i < n; i++) { diff --git a/tools/dataset/record.cc b/tools/dataset/record.cc index 8e19057..e3de362 100644 --- a/tools/dataset/record.cc +++ b/tools/dataset/record.cc @@ -29,6 +29,8 @@ int main(int argc, char *argv[]) { glog_init _(argc, argv); auto &&device = device::select(); + if (!device) + return 1; /* { // auto-exposure device->SetOptionValue(Option::EXPOSURE_MODE, 0); diff --git a/tools/writer/device_info_writer.cc b/tools/writer/device_info_writer.cc index 3eaf394..1625b7a 100644 --- a/tools/writer/device_info_writer.cc +++ b/tools/writer/device_info_writer.cc @@ -32,6 +32,8 @@ int main(int argc, char *argv[]) { } auto &&device = device::select(); + if (!device) + return 1; tools::DeviceWriter writer(device); writer.WriteDeviceInfo(filepath); diff --git a/tools/writer/img_params_writer.cc b/tools/writer/img_params_writer.cc index 0afcf54..77647a5 100644 --- a/tools/writer/img_params_writer.cc +++ b/tools/writer/img_params_writer.cc @@ -32,6 +32,8 @@ int main(int argc, char *argv[]) { } auto &&device = device::select(); + if (!device) + return 1; tools::DeviceWriter writer(device); writer.WriteImgParams(filepath); diff --git a/tools/writer/imu_params_writer.cc b/tools/writer/imu_params_writer.cc index 29d33b1..9f2034b 100644 --- a/tools/writer/imu_params_writer.cc +++ b/tools/writer/imu_params_writer.cc @@ -32,6 +32,8 @@ int main(int argc, char *argv[]) { } auto &&device = device::select(); + if (!device) + return 1; tools::DeviceWriter writer(device); writer.WriteImuParams(filepath); diff --git a/tools/writer/save_all_infos.cc b/tools/writer/save_all_infos.cc index dcfa97f..a4ff051 100644 --- a/tools/writer/save_all_infos.cc +++ b/tools/writer/save_all_infos.cc @@ -29,6 +29,8 @@ int main(int argc, char *argv[]) { } auto &&device = device::select(); + if (!device) + return 1; dir.append(OS_SEP "SN").append(device->GetInfo()->serial_number);