Disconnect from serial cleanly on SIGINT

Send the STOP opcode before exiting the program to ensure the robot is not left in a state that could potentially drain the battery.

Signed-off-by: Jacob Perron <jacobmperron@gmail.com>
This commit is contained in:
Jacob Perron 2019-08-27 00:11:29 -07:00 committed by Jacob Perron
parent 04ab2ccd80
commit f5044c7ec8
3 changed files with 17 additions and 3 deletions

View file

@ -15,8 +15,6 @@ namespace create {
namespace ublas = boost::numeric::ublas;
// TODO: Handle SIGINT to do clean disconnect
void Create::init() {
mainMotorPower = 0;
sideMotorPower = 0;

View file

@ -8,6 +8,7 @@ namespace create {
Serial::Serial(boost::shared_ptr<Data> d) :
data(d),
port(io),
signals(io, SIGINT, SIGTERM),
isReading(false),
dataReady(false),
corruptPackets(0),
@ -18,6 +19,18 @@ namespace create {
disconnect();
}
void Serial::signalHandler(const boost::system::error_code& error, int signal_number) {
if (!error) {
if (connected()) {
// Ensure not in Safe/Full modes
sendOpcode(OC_START);
// Stop OI
sendOpcode(OC_STOP);
exit(signal_number);
}
}
}
bool Serial::connect(const std::string& portName, const int& baud, boost::function<void()> cb) {
using namespace boost::asio;
port.open(portName);
@ -27,6 +40,8 @@ namespace create {
port.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
port.set_option(serial_port::flow_control(serial_port::flow_control::none));
signals.async_wait(boost::bind(&Serial::signalHandler, this, _1, _2));
usleep(1000000);
if (port.is_open()) {