Skip to content

Commit ed05833

Browse files
committed
mavros: use shared io_service in router endpoints
1 parent 0e8d8ba commit ed05833

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mavros/include/mavros/mavros_router.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <Eigen/Eigen> // NOLINT
3232

3333
#include "mavconn/interface.hpp"
34+
#include "mavconn/io_context_runner.hpp"
3435
#include "mavconn/mavlink_dialect.hpp"
3536
#include "mavros/utils.hpp"
3637
#include "rclcpp/macros.hpp"
@@ -145,6 +146,7 @@ class Router : public rclcpp::Node
145146
const std::string & node_name = "mavros_router")
146147
: rclcpp::Node(node_name,
147148
options /* rclcpp::NodeOptions(options).use_intra_process_comms(true) */),
149+
router_io_runner(),
148150
endpoints{}, stat_msg_routed(0), stat_msg_sent(0), stat_msg_dropped(0),
149151
diagnostic_updater(this, 1.0)
150152
{
@@ -192,6 +194,8 @@ class Router : public rclcpp::Node
192194
RCLCPP_INFO(get_logger(), "Known MAVLink dialects:%s", ss.str().c_str());
193195
RCLCPP_INFO(get_logger(), "MAVROS Router started");
194196

197+
router_io_runner.start([this]() {this->router_io_runner.io().run();});
198+
195199
// Delay parameter callback initialization because
196200
// add/del endpoints calls have to use shared_from_this(),
197201
// which cannot be used before we leave the constructor.
@@ -202,13 +206,24 @@ class Router : public rclcpp::Node
202206
});
203207
}
204208

209+
~Router() override
210+
{
211+
router_io_runner.shutdown_owned();
212+
}
213+
205214
void route_message(Endpoint::SharedPtr src, const mavlink_message_t * msg, const Framing framing);
206215

216+
[[nodiscard]] asio::io_service * get_shared_io()
217+
{
218+
return &router_io_runner.io();
219+
}
220+
207221
private:
208222
friend class Endpoint;
209223
friend class TestRouter;
210224

211225
static std::atomic<id_t> id_counter;
226+
mavconn::IoContextRunner router_io_runner;
212227

213228
std::shared_mutex mu;
214229

mavros/src/lib/mavros_router.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ bool MAVConnEndpoint::is_open()
403403

404404
std::pair<bool, std::string> MAVConnEndpoint::open()
405405
{
406+
auto nh = this->parent;
407+
if (!nh) {
408+
return {false, "parent not set"};
409+
}
410+
406411
try {
407412
auto weak_self = weak_from_this();
408413
this->link = mavconn::MAVConnInterface::open_url(
@@ -411,7 +416,9 @@ std::pair<bool, std::string> MAVConnEndpoint::open()
411416
if (auto self = weak_self.lock()) {
412417
self->recv_message(msg, framing);
413418
}
414-
});
419+
},
420+
mavconn::MAVConnInterface::ClosedCb(),
421+
nh->get_shared_io());
415422
} catch (mavconn::DeviceError & ex) {
416423
return {false, ex.what()};
417424
}

0 commit comments

Comments
 (0)