From 7312ead23a1ddc8ee2fb9a93723a502f67dc8755 Mon Sep 17 00:00:00 2001 From: maehw Date: Sat, 27 Nov 2021 20:35:57 +0100 Subject: [PATCH] Bugfix/build with newer boost and opencv libs (#20) * Fix build for newer boost lib versions * Fix build for newer versions of OpenCV * Use std::string instead of std::__cxx11::string * Fix boost package dependencies * Fix path to app executable * Revert "Fix path to app executable" This reverts commit 4be17f3680ce6f2eb4d88a1f5bba5a5ca75500c4 as this breaks the original self-o-mat boxes as described in #20. --- CMakeLists.txt | 9 ++++++++- src/camera/NopCamera.h | 14 ++++++++++++++ src/camera/OpenCVCamera.cpp | 16 ++++++++++++++-- src/tools/blocking_reader.h | 26 ++++++++++++++++++++------ 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70de57d..57cd6f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,14 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf/api.pb.cc ${C find_package(OpenCV REQUIRED) -find_package(Boost REQUIRED COMPONENTS system thread filesystem) +find_package(Boost REQUIRED) +if(Boost_FOUND) + if(${Boost_VERSION} VERSION_GREATER_EQUAL 1.70) + find_package(Boost REQUIRED COMPONENTS system thread filesystem chrono) + else() + find_package(Boost REQUIRED COMPONENTS system thread filesystem) + endif() +endif() find_package(Cups REQUIRED) find_package(Protobuf REQUIRED) diff --git a/src/camera/NopCamera.h b/src/camera/NopCamera.h index 3f17a00..665e601 100644 --- a/src/camera/NopCamera.h +++ b/src/camera/NopCamera.h @@ -10,6 +10,20 @@ #include #include +/* Some OpenCV definitions are outdated/ have been renamed and moved into the `cv` namespace; + * see also: https://stackoverflow.com/questions/57982505/opencv-4-cap-prop-pos-frames-was-not-declared-in-this-scope + * Conditionally defining the macros by the preprocessor if they don't already exist shouldn't + * break the build for older OpenCV versions but fix it for newer ones. + */ +#ifndef CV_CAP_PROP_POS_FRAMES + #define CV_CAP_PROP_POS_FRAMES cv::CAP_PROP_POS_FRAMES +#endif +#ifndef CV_CAP_PROP_FRAME_COUNT + #define CV_CAP_PROP_FRAME_COUNT cv::CAP_PROP_FRAME_COUNT +#endif +#ifndef CV_CAP_PROP_FPS + #define CV_CAP_PROP_FPS cv::CAP_PROP_FPS +#endif namespace selfomat { namespace camera { diff --git a/src/camera/OpenCVCamera.cpp b/src/camera/OpenCVCamera.cpp index d4c15db..2e03077 100644 --- a/src/camera/OpenCVCamera.cpp +++ b/src/camera/OpenCVCamera.cpp @@ -4,6 +4,18 @@ #include "OpenCVCamera.h" +/* Some OpenCV definitions are outdated/ have been renamed and moved into the `cv` namespace; + * see also: https://stackoverflow.com/questions/57982505/opencv-4-cap-prop-pos-frames-was-not-declared-in-this-scope + * Conditionally defining the macros by the preprocessor if they don't already exist shouldn't + * break the build for older OpenCV versions but fix it for newer ones. + */ +#ifndef CV_CAP_PROP_FRAME_WIDTH + #define CV_CAP_PROP_FRAME_WIDTH cv::CAP_PROP_FRAME_WIDTH +#endif +#ifndef CV_CAP_PROP_FRAME_HEIGHT + #define CV_CAP_PROP_FRAME_HEIGHT cv::CAP_PROP_FRAME_HEIGHT +#endif + using namespace selfomat::camera; std::string OpenCVCamera::TAG = "OPENCV_CAMERA"; @@ -133,11 +145,11 @@ bool OpenCVCamera::autofocusBlocking() { string OpenCVCamera::getCameraName() { - return std::__cxx11::string(); + return std::string(); } string OpenCVCamera::getLensName() { - return std::__cxx11::string(); + return std::string(); } int OpenCVCamera::getExposureCorrection() { diff --git a/src/tools/blocking_reader.h b/src/tools/blocking_reader.h index 651af5b..07e6c14 100644 --- a/src/tools/blocking_reader.h +++ b/src/tools/blocking_reader.h @@ -28,6 +28,18 @@ #include #include +/* Use the following macro to allow builds for different boost library versions; + * inspired by: https://stackoverflow.com/a/67773642/1627585 + */ +#if BOOST_VERSION >= 107000 +#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context()) +#else +#define GET_IO_SERVICE(s) ((s).get_io_service()) +#endif + +#define BRDR_TAG "BLOCKING_READER" + + using namespace selfomat::tools; class blocking_reader @@ -41,10 +53,11 @@ class blocking_reader // Called when an async read completes or has been cancelled void read_complete(const boost::system::error_code& error, size_t bytes_transferred) { + LOG_D(BRDR_TAG, "read complete: bytes_transferred=" + std::to_string(bytes_transferred)); read_error = (error || bytes_transferred == 0); if(read_error) { - LOG_E("BLOCKING_READER", "error: ", error.message()); + LOG_E(BRDR_TAG, "boost error message: ", error.message()); } // Read has finished, so cancel the @@ -54,11 +67,12 @@ class blocking_reader // Called when the timer's deadline expires. void time_out(const boost::system::error_code& error) { - // Was the timeout was cancelled? + // Was the timeout cancelled? if (error) { // yes return; } + LOG_D(BRDR_TAG, "timeout"); // no, we have timed out, so kill // the read operation @@ -73,7 +87,7 @@ class blocking_reader // a timeout in milliseconds. blocking_reader(boost::asio::serial_port& port, size_t timeout) : port(port), timeout(timeout), - timer(port.get_io_service()), + timer( GET_IO_SERVICE(port) ), read_error(true) { } @@ -86,8 +100,7 @@ class blocking_reader // After a timeout & cancel it seems we need // to do a reset for subsequent reads to work. - port.get_io_service().reset(); - + GET_IO_SERVICE(port).reset(); // Asynchronously read 1 character. boost::asio::async_read(port, boost::asio::buffer(&c, 1), @@ -97,6 +110,7 @@ class blocking_reader boost::asio::placeholders::bytes_transferred)); // send the request + LOG_D(BRDR_TAG, "Sending request (size: " + std::to_string(request_size) + ")"); port.write_some(boost::asio::buffer(request, request_size)); // Setup a deadline time to implement our timeout. @@ -106,7 +120,7 @@ class blocking_reader // This will block until a character is read // or until the it is cancelled. - port.get_io_service().run(); + GET_IO_SERVICE(port).run(); if (!read_error) val = c;