diff --git a/toid_control/src/toid_control.cpp b/toid_control/src/toid_control.cpp index af6f261..5534873 100644 --- a/toid_control/src/toid_control.cpp +++ b/toid_control/src/toid_control.cpp @@ -82,6 +82,15 @@ hardware_interface::return_type toid::StepperInterface::write(const rclcpp::Time boost::system::error_code ec; ec = serial_port_.close(ec); ec = serial_port_.open(serial_port_name_, ec); + if(!ec.failed()) { + try { + for(int i = 0; i < 64; i++) { + asio::write(serial_port_, asio::buffer("\x00")); + } + } catch(const std::runtime_error& err) { + + } + } } return hardware_interface::return_type::OK; } diff --git a/toid_odometry/src/odometry.cpp b/toid_odometry/src/odometry.cpp index 95b5a50..891109e 100644 --- a/toid_odometry/src/odometry.cpp +++ b/toid_odometry/src/odometry.cpp @@ -139,6 +139,23 @@ private: tf_static_broadcaster_->sendTransform(t); } + void serial_ec_recovery(boost::system::error_code ec) { + if(ec.failed()) { + boost::system::error_code ec; + ec = this->serial_.close(ec); + ec = this->serial_.open(serial_port_path_, ec); + if(!ec.failed()) { + try { + for(int i = 0; i < 64; i++) { + asio::write(this->serial_, asio::buffer("\x00")); + } + } catch(const std::runtime_error& err) { + + } + } + } + } + void set_width(const std::shared_ptr req) { boost::system::error_code ec; const std::array cmd{TMSG_SET_WIDTH, TMSG_DELIM}; @@ -146,6 +163,7 @@ private: msg.crc = crcFooter(msg); asio::write(this->serial_, asio::buffer(cmd), ec); asio::write(this->serial_, asio::buffer(&msg, sizeof(msg)), ec); + serial_ec_recovery(ec); } void set_ratio(const std::shared_ptr req) { @@ -155,6 +173,7 @@ private: msg.crc = crcFooter(msg); asio::write(this->serial_, asio::buffer(cmd), ec); asio::write(this->serial_, asio::buffer(&msg, sizeof(msg)), ec); + serial_ec_recovery(ec); } void end_calib(const std::shared_ptr) { @@ -166,12 +185,15 @@ private: if(!ec.failed()) { RCLCPP_INFO(this->get_logger(), "Calculated ratio: %lf, %lf", resp.left_gain, resp.right_gain); } + serial_ec_recovery(ec); + } void zero(const std::shared_ptr) { boost::system::error_code ec; const std::array cmd{TMSG_ZERO, TMSG_DELIM}; asio::write(this->serial_, asio::buffer(cmd), ec); + serial_ec_recovery(ec); } void publish_robot_state() { @@ -245,6 +267,17 @@ private: ec.what().c_str() ); } + + if(!ec.failed()) { + try { + for(int i = 0; i < 64; i++) { + asio::write(serial_, asio::buffer("\x00")); + } + } catch(const std::runtime_error& err) { + + } + } + return false; } return state.crc == crcFooter(state);