diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 77852b5..7fb7d18 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,7 +13,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-18.04, ubuntu-20.04] + os: [ubuntu-20.04, ubuntu-22.04] steps: - name: Install dependencies diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 77ce1e7..0af0f5b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,12 @@ Changelog for package libcreate ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +3.1.0 (2023-05-09) +------------------ +* Address warnings and errors +* Catch boost exceptions in Serial.h +* Contributors: Swapnil Patel + 3.0.0 (2022-04-06) ------------------ * Add option to workaround bug where firmware reports unexpected OI mode (`#67 `_) diff --git a/README.md b/README.md index 2d85f07..b1eb336 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # libcreate # -C++ library for interfacing with iRobot's [Create 1 and 2](http://www.irobot.com/About-iRobot/STEM/Create-2.aspx) as well as most models of Roomba. [create_autonomy](http://wiki.ros.org/create_autonomy) is a [ROS](http://www.ros.org/) wrapper for this library. +C++ library for interfacing with iRobot's Create 1 and 2 as well as most models of Roomba. [create_robot](http://wiki.ros.org/create_robot) is a [ROS](http://www.ros.org/) wrapper for this library. -* [Code API](http://docs.ros.org/kinetic/api/libcreate/html/index.html) +* [Code API](http://docs.ros.org/noetic/api/libcreate/html/index.html) * Protocol documentation: - - [`V_1`](https://drive.google.com/file/d/0B9O4b91VYXMdUHlqNklDU09NU0k) (Roomba 400 series ) - - [`V_2`](https://drive.google.com/file/d/0B9O4b91VYXMdMmFPMVNDUEZ6d0U) (Create 1, Roomba 500 series) - - [`V_3`](https://drive.google.com/file/d/0B9O4b91VYXMdSVk4amw1N09mQ3c) (Create 2, Roomba 600-800 series) -* Author: [Jacob Perron](http://jacobperron.ca) ([Autonomy Lab](http://autonomylab.org), [Simon Fraser University](http://www.sfu.ca)) + - [`V_1`](https://drive.google.com/file/d/0B9O4b91VYXMdUHlqNklDU09NU0k/view?usp=sharing&resourcekey=0-KxMpRPBMsGAj7eSYC_9ewA) (Roomba 400 series ) + - [`V_2`](https://drive.google.com/file/d/0B9O4b91VYXMdMmFPMVNDUEZ6d0U/view?usp=sharing&resourcekey=0-bqKH8xhtWdYtTik_LLWo9Q) (Create 1, Roomba 500 series) + - [`V_3`](https://drive.google.com/file/d/0B9O4b91VYXMdSVk4amw1N09mQ3c/view?usp=sharing&resourcekey=0-rKvug2IzC7nj4zV31EJtww) (Create 2, Roomba 600-800 series) +* Author: [Jacob Perron](http://jacobperron.ca) ([Autonomy Lab](https://autonomy.cs.sfu.ca), [Simon Fraser University](http://www.sfu.ca)) * Contributors: [Mani Monajjemi](http:mani.im), [Ben Wolsieffer](https://github.com/lopsided98), [Josh Gadeken](https://github.com/process1183) ## Build Status ## diff --git a/include/create/packet.h b/include/create/packet.h index d1e7928..60ff0f2 100644 --- a/include/create/packet.h +++ b/include/create/packet.h @@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. #define CREATE_PACKET_H #include +#include namespace create { class Packet { diff --git a/include/create/serial.h b/include/create/serial.h index 05ac648..ccfdaea 100644 --- a/include/create/serial.h +++ b/include/create/serial.h @@ -71,6 +71,8 @@ namespace create { // Start and stop reading data from Create bool startReading(); void stopReading(); + bool openPort(const std::string& portName, const int& baud); + bool closePort(); protected: std::shared_ptr data; diff --git a/include/create/serial_query.h b/include/create/serial_query.h index b6b372b..ebd876a 100644 --- a/include/create/serial_query.h +++ b/include/create/serial_query.h @@ -68,6 +68,7 @@ namespace create { public: SerialQuery(std::shared_ptr data, bool install_signal_handler = true); + virtual ~SerialQuery() = default; }; } // namespace create diff --git a/include/create/serial_stream.h b/include/create/serial_stream.h index 2f21892..eeb2cb6 100644 --- a/include/create/serial_stream.h +++ b/include/create/serial_stream.h @@ -73,6 +73,7 @@ namespace create { std::shared_ptr data, const uint8_t& header = create::util::STREAM_HEADER, bool install_signal_handler = true); + virtual ~SerialStream() = default; }; } // namespace create diff --git a/package.xml b/package.xml index 9704efe..85101b3 100644 --- a/package.xml +++ b/package.xml @@ -1,7 +1,7 @@ libcreate - 3.0.0 + 3.1.0 C++ library for interfacing with iRobot's Create 1 and Create 2 Jacob Perron diff --git a/src/create.cpp b/src/create.cpp index 21f71be..0517b4a 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -160,10 +160,10 @@ namespace create { prevTicksRight = totalTicksRight; // Handle wrap around - if (fabs(ticksLeft) > 0.9 * util::V_3_MAX_ENCODER_TICKS) { + if (std::abs(ticksLeft) > 0.9 * util::V_3_MAX_ENCODER_TICKS) { ticksLeft = (ticksLeft % util::V_3_MAX_ENCODER_TICKS) + 1; } - if (fabs(ticksRight) > 0.9 * util::V_3_MAX_ENCODER_TICKS) { + if (std::abs(ticksRight) > 0.9 * util::V_3_MAX_ENCODER_TICKS) { ticksRight = (ticksRight % util::V_3_MAX_ENCODER_TICKS) + 1; } @@ -373,7 +373,7 @@ namespace create { min > 59) return false; - uint8_t cmd[4] = { OC_DATE, day, hour, min }; + uint8_t cmd[4] = { OC_DATE, static_cast(day), hour, min }; return serial->send(cmd, 4); } diff --git a/src/serial.cpp b/src/serial.cpp index 97c6085..6617e50 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -40,26 +40,26 @@ namespace create { bool Serial::connect(const std::string& portName, const int& baud, std::function cb) { using namespace boost::asio; - port.open(portName); - port.set_option(serial_port::baud_rate(baud)); - port.set_option(serial_port::character_size(8)); - port.set_option(serial_port::parity(serial_port::parity::none)); - port.set_option(serial_port::stop_bits(serial_port::stop_bits::one)); - port.set_option(serial_port::flow_control(serial_port::flow_control::none)); + if (!openPort(portName, baud)) { + return false; + } signals.async_wait(std::bind(&Serial::signalHandler, this, std::placeholders::_1, std::placeholders::_2)); usleep(1000000); - if (port.is_open()) { - callback = cb; - bool startReadSuccess = startReading(); - if (!startReadSuccess) { - port.close(); - } - return startReadSuccess; + if (!port.is_open()) { + return false; + } + + callback = cb; + bool startReadSuccess = startReading(); + if (!startReadSuccess) { + closePort(); + return false; } - return false; + + return true; } void Serial::disconnect() { @@ -76,6 +76,33 @@ namespace create { } } + bool Serial::openPort(const std::string& portName, const int& baud) { + using namespace boost::asio; + try { + port.open(portName); + port.set_option(serial_port::baud_rate(baud)); + port.set_option(serial_port::character_size(8)); + port.set_option(serial_port::parity(serial_port::parity::none)); + port.set_option(serial_port::stop_bits(serial_port::stop_bits::one)); + port.set_option(serial_port::flow_control(serial_port::flow_control::none)); + } catch (const boost::system::system_error& /*e*/) { + CERR("[create::Serial] ", "failed to open port: " << portName); + return false; + } + return true; + } + + bool Serial::closePort() { + using namespace boost::asio; + try { + port.close(); + } catch (const boost::system::system_error& /*e*/) { + CERR("[create::Serial] ", "failed to close port"); + return false; + } + return true; + } + bool Serial::startReading() { if (!connected()) return false; @@ -186,8 +213,13 @@ namespace create { CERR("[create::Serial] ", "send failed, not connected."); return false; } - // TODO: catch boost exceptions - boost::asio::write(port, boost::asio::buffer(bytes, numBytes)); + + try { + boost::asio::write(port, boost::asio::buffer(bytes, numBytes)); + } catch (const boost::system::system_error & e) { + CERR("[create::Serial] ", "failed to write bytes to port"); + return false; + } return true; }