diff --git a/CMakeLists.txt b/CMakeLists.txt index 23fdace..2d85cca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,9 @@ add_library (robocad-cpp SHARED "src/internal/common/updaters.cpp" ) -target_link_options(robocad-cpp PRIVATE "LINKER:/NODEFAULTLIB:library") +if (MSVC) + target_link_options(robocad-cpp PRIVATE "LINKER:/NODEFAULTLIB:library") +endif() target_link_libraries(robocad-cpp PRIVATE ${OpenCV_LIBS}) target_link_libraries(robocad-cpp PRIVATE Threads::Threads) diff --git a/include/internal/common/robot_configuration.hpp b/include/internal/common/robot_configuration.hpp index 06484eb..69066aa 100644 --- a/include/internal/common/robot_configuration.hpp +++ b/include/internal/common/robot_configuration.hpp @@ -30,7 +30,7 @@ class DefaultAlgaritmConfiguration : public RobotConfiguration { public: DefaultAlgaritmConfiguration() { - camera_index = 2; + camera_index = 0; with_pi_blaster = false; } diff --git a/include/shufflecad.hpp b/include/shufflecad.hpp index eac41d4..34bfcfb 100644 --- a/include/shufflecad.hpp +++ b/include/shufflecad.hpp @@ -34,7 +34,7 @@ class Shufflecad void print_to_log(std::string message, std::string message_type = LOG_INFO, - std::string color = "#cccccc"); + std::string color = "#808080"); std::vector variables_array; std::vector camera_variables_array; @@ -156,8 +156,17 @@ class CameraVariable std::vector get_value() { std::lock_guard lock(this->data_mutex); + // No frame yet (e.g. robot has no camera, or none captured so far): + // cv::imencode throws on an empty Mat, which would propagate out of the + // camera thread and terminate the whole program. Send a "null" placeholder. + if (this->value.empty()) + return { 'n', 'u', 'l', 'l' }; std::vector result; - cv::imencode(".jpg", this->value, result); + try { + cv::imencode(".jpg", this->value, result); + } catch (const cv::Exception&) { + return { 'n', 'u', 'l', 'l' }; + } return result; } private: diff --git a/src/internal/common/connection_real.cpp b/src/internal/common/connection_real.cpp index c297e36..741bdfa 100644 --- a/src/internal/common/connection_real.cpp +++ b/src/internal/common/connection_real.cpp @@ -12,6 +12,11 @@ ConnectionReal::ConnectionReal(Robot* robot, Updater* updater, RobotConfiguratio try { camera_instance = new cv::VideoCapture(conf->camera_index); + if (camera_instance->isOpened()) + robot->write_log("Camera opened on index " + std::to_string(conf->camera_index)); + else + robot->write_log("Camera FAILED to open on index " + std::to_string(conf->camera_index) + + " (VideoCapture did not throw, but device is not available)"); } catch (const std::exception& e) {