Skip to content

Commit 1563e2b

Browse files
authored
Add option to disable signal handlers (#65)
This gives the user the option to create their own signal handler without having create::Create interfere. They can disable the sigint/sigterm handler and be responsible for disconnecting from the robot themselves. Signed-off-by: Jacob Perron <jacobmperron@gmail.com>
1 parent fbc87de commit 1563e2b

8 files changed

Lines changed: 35 additions & 19 deletions

File tree

include/create/create.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace create {
9292
float requestedLeftVel;
9393
float requestedRightVel;
9494

95-
void init();
95+
void init(bool install_signal_handler);
9696
// Add two matrices and handle overflow case
9797
Matrix addMatrices(const Matrix &A, const Matrix &B) const;
9898
void onData();
@@ -109,8 +109,10 @@ namespace create {
109109
* Calling this constructor Does not attempt to establish a serial connection to the robot.
110110
*
111111
* \param model the type of the robot. See RobotModel to determine the value for your robot.
112-
*/
113-
Create(RobotModel model = RobotModel::CREATE_2);
112+
* \param install_signal_handler if true, then register a signal handler to disconnect from
113+
* the robot on SIGINT or SIGTERM.
114+
*/
115+
Create(RobotModel model = RobotModel::CREATE_2, bool install_signal_handler = true);
114116

115117
/**
116118
* \brief Attempts to establish serial connection to Create.
@@ -119,8 +121,10 @@ namespace create {
119121
* \param baud rate to communicate with Create. Typically,
120122
* 115200 for Create 2 and 57600 for Create 1.
121123
* \param model type of robot. See RobotModel to determine the value for your robot.
124+
* \param install_signal_handler if true, then register a signal handler to disconnect from
125+
* the robot on SIGINT or SIGTERM.
122126
*/
123-
Create(const std::string& port, const int& baud, RobotModel model = RobotModel::CREATE_2);
127+
Create(const std::string& port, const int& baud, RobotModel model = RobotModel::CREATE_2, bool install_signal_handler = true);
124128

125129
/**
126130
* \brief Attempts to disconnect from serial.

include/create/serial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace create {
8686
void notifyDataReady();
8787

8888
public:
89-
Serial(std::shared_ptr<Data> data);
89+
Serial(std::shared_ptr<Data> data, bool install_signal_handler);
9090
~Serial();
9191
bool connect(const std::string& port, const int& baud = 115200, std::function<void()> cb = 0);
9292
void disconnect();

include/create/serial_query.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace create {
6767
void processByte(uint8_t byteRead);
6868

6969
public:
70-
SerialQuery(std::shared_ptr<Data> data);
70+
SerialQuery(std::shared_ptr<Data> data, bool install_signal_handler = true);
7171
};
7272
} // namespace create
7373

include/create/serial_stream.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ namespace create {
6969
void processByte(uint8_t byteRead);
7070

7171
public:
72-
SerialStream(std::shared_ptr<Data> data, const uint8_t& header = create::util::STREAM_HEADER);
72+
SerialStream(
73+
std::shared_ptr<Data> data,
74+
const uint8_t& header = create::util::STREAM_HEADER,
75+
bool install_signal_handler = true);
7376

7477
};
7578
} // namespace create

src/create.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace create {
1414

1515
namespace ublas = boost::numeric::ublas;
1616

17-
void Create::init() {
17+
void Create::init(bool install_signal_handler) {
1818
mainMotorPower = 0;
1919
sideMotorPower = 0;
2020
vacuumMotorPower = 0;
@@ -44,18 +44,21 @@ namespace create {
4444
dtHistoryLength = 100;
4545
data = std::shared_ptr<Data>(new Data(model.getVersion()));
4646
if (model.getVersion() == V_1) {
47-
serial = std::make_shared<SerialQuery>(data);
47+
serial = std::make_shared<SerialQuery>(data, install_signal_handler);
4848
} else {
49-
serial = std::make_shared<SerialStream>(data);
49+
serial = std::make_shared<SerialStream>(
50+
data, create::util::STREAM_HEADER, install_signal_handler);
5051
}
5152
}
5253

53-
Create::Create(RobotModel m) : model(m) {
54-
init();
54+
Create::Create(RobotModel m, bool install_signal_handler) : model(m) {
55+
init(install_signal_handler);
5556
}
5657

57-
Create::Create(const std::string& dev, const int& baud, RobotModel m) : model(m) {
58-
init();
58+
Create::Create(const std::string& dev, const int& baud, RobotModel m, bool install_signal_handler)
59+
: model(m)
60+
{
61+
init(install_signal_handler);
5962
serial->connect(dev, baud);
6063
}
6164

src/serial.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77

88
namespace create {
99

10-
Serial::Serial(std::shared_ptr<Data> d) :
11-
signals(io, SIGINT, SIGTERM),
10+
Serial::Serial(std::shared_ptr<Data> d, bool install_signal_handler) :
11+
signals(io),
1212
port(io),
1313
dataReady(false),
1414
isReading(false),
1515
data(d),
1616
corruptPackets(0),
17-
totalPackets(0) {
17+
totalPackets(0)
18+
{
19+
if (install_signal_handler) {
20+
signals.add(SIGINT);
21+
signals.add(SIGTERM);
22+
}
1823
}
1924

2025
Serial::~Serial() {

src/serial_query.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
namespace create {
1010

11-
SerialQuery::SerialQuery(std::shared_ptr<Data> d) : Serial(d),
11+
SerialQuery::SerialQuery(std::shared_ptr<Data> d, bool install_signal_handler)
12+
: Serial(d, install_signal_handler),
1213
streamRecoveryTimer(io),
1314
packetID(ID_BUMP_WHEELDROP),
1415
packetByte(0),

src/serial_stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace create {
88

9-
SerialStream::SerialStream(std::shared_ptr<Data> d, const uint8_t& header) : Serial(d), readState(READ_HEADER), headerByte(header) {
9+
SerialStream::SerialStream(std::shared_ptr<Data> d, const uint8_t& header, bool install_signal_handler) : Serial(d, install_signal_handler), readState(READ_HEADER), headerByte(header) {
1010
}
1111

1212
bool SerialStream::startSensorStream() {

0 commit comments

Comments
 (0)