Skip to content

Commit 586e9b8

Browse files
committed
Using new callback wrapper in AsyncMotionModel
1 parent 0137559 commit 586e9b8

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

fuse_core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pkg_check_modules(libglog REQUIRED)
4040
## fuse_core library
4141
add_library(
4242
${PROJECT_NAME} SHARED
43-
# src/async_motion_model.cpp
43+
src/async_motion_model.cpp
4444
# src/async_publisher.cpp
4545
# src/async_sensor_model.cpp
4646
src/ceres_options.cpp

fuse_core/include/fuse_core/async_motion_model.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
#include <fuse_core/fuse_macros.h>
3939
#include <fuse_core/motion_model.h>
4040
#include <fuse_core/transaction.h>
41-
#include <ros/callback_queue.h>
42-
#include <ros/node_handle.h>
43-
#include <ros/spinner.h>
41+
#include <rclcpp/rclcpp.hpp>
4442

4543
#include <string>
4644

@@ -85,7 +83,7 @@ namespace fuse_core
8583
class AsyncMotionModel : public MotionModel
8684
{
8785
public:
88-
FUSE_SMART_PTR_ALIASES_ONLY(AsyncMotionModel);
86+
FUSE_SMART_PTR_ALIASES_ONLY(AsyncMotionModel)
8987

9088
/**
9189
* @brief Destructor
@@ -170,11 +168,10 @@ class AsyncMotionModel : public MotionModel
170168
void stop() override;
171169

172170
protected:
173-
ros::CallbackQueue callback_queue_; //!< The local callback queue used for all subscriptions
174171
std::string name_; //!< The unique name for this motion model instance
175-
ros::NodeHandle node_handle_; //!< A node handle in the global namespace using the local callback queue
176-
ros::NodeHandle private_node_handle_; //!< A node handle in the private namespace using the local callback queue
177-
ros::AsyncSpinner spinner_; //!< A single/multi-threaded spinner assigned to the local callback queue
172+
rclcpp::Node::SharedPtr node_; //!< The node for this motion model
173+
rclcpp::executors::MultiThreadedExecutor executor_; //!< A single/multi-threaded spinner assigned to the local callback queue
174+
rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr waitables_interface_;
178175

179176
/**
180177
* @brief Constructor

fuse_core/include/fuse_core/motion_model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace fuse_core
5353
class MotionModel
5454
{
5555
public:
56-
FUSE_SMART_PTR_ALIASES_ONLY(MotionModel);
56+
FUSE_SMART_PTR_ALIASES_ONLY(MotionModel)
5757

5858
/**
5959
* @brief Destructor

fuse_core/src/async_motion_model.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@
3636
#include <fuse_core/callback_wrapper.h>
3737
#include <fuse_core/graph.h>
3838
#include <fuse_core/transaction.h>
39-
#include <ros/node_handle.h>
40-
41-
#include <boost/make_shared.hpp>
39+
#include <rclcpp/rclcpp.hpp>
4240

4341
#include <functional>
42+
#include <memory>
4443
#include <utility>
4544
#include <string>
4645

@@ -50,7 +49,7 @@ namespace fuse_core
5049

5150
AsyncMotionModel::AsyncMotionModel(size_t thread_count) :
5251
name_("uninitialized"),
53-
spinner_(thread_count, &callback_queue_)
52+
executor_(rclcpp::ExecutorOptions(), thread_count)
5453
{
5554
}
5655

@@ -62,56 +61,67 @@ bool AsyncMotionModel::apply(Transaction& transaction)
6261
// Thus, it is functionally similar to a service callback, and should be a familiar pattern for ROS developers.
6362
// This function blocks until the queryCallback() call completes, thus enforcing that motion models are generated
6463
// in order.
65-
auto callback = boost::make_shared<CallbackWrapper<bool>>(
64+
auto callback = std::make_shared<CallbackWrapper<bool>>(
6665
std::bind(&AsyncMotionModel::applyCallback, this, std::ref(transaction)));
67-
auto result = callback->getFuture();
68-
callback_queue_.addCallback(callback, reinterpret_cast<uint64_t>(this));
66+
auto result = callback->get_future();
67+
waitables_interface_->add_waitable(callback, nullptr);
6968
result.wait();
69+
waitables_interface_->remove_waitable(callback, nullptr);
70+
7071
return result.get();
7172
}
7273

7374
void AsyncMotionModel::graphCallback(Graph::ConstSharedPtr graph)
7475
{
75-
callback_queue_.addCallback(
76-
boost::make_shared<CallbackWrapper<void>>(std::bind(&AsyncMotionModel::onGraphUpdate, this, std::move(graph))),
77-
reinterpret_cast<uint64_t>(this));
76+
auto callback = std::make_shared<CallbackWrapper<void>>(std::bind(&AsyncMotionModel::onGraphUpdate, this, std::move(graph)));
77+
auto result = callback->get_future();
78+
waitables_interface_->add_waitable(callback, nullptr);
79+
result.wait();
80+
waitables_interface_->remove_waitable(callback, nullptr);
81+
82+
// @todo TAM: figure out whether we needed the second argument to addCallback here
83+
//callback_queue_.addCallback(
84+
// boost::make_shared<CallbackWrapper<void>>(std::bind(&AsyncMotionModel::onGraphUpdate, this, std::move(graph))),
85+
// reinterpret_cast<uint64_t>(this));
7886
}
7987

8088
void AsyncMotionModel::initialize(const std::string& name)
8189
{
8290
// Initialize internal state
8391
name_ = name;
84-
node_handle_.setCallbackQueue(&callback_queue_);
85-
private_node_handle_ = ros::NodeHandle("~/" + name_);
86-
private_node_handle_.setCallbackQueue(&callback_queue_);
92+
node_ = rclcpp::Node::make_shared(name);
93+
waitables_interface_ = node_->get_node_waitables_interface();
8794

8895
// Call the derived onInit() function to perform implementation-specific initialization
8996
onInit();
9097

91-
// Start the async spinner to service the local callback queue
92-
spinner_.start();
98+
executor_.add_node(node_);
9399
}
94100

95101
void AsyncMotionModel::start()
96102
{
97-
auto callback = boost::make_shared<CallbackWrapper<void>>(std::bind(&AsyncMotionModel::onStart, this));
98-
auto result = callback->getFuture();
99-
callback_queue_.addCallback(callback, reinterpret_cast<uint64_t>(this));
103+
auto callback = std::make_shared<CallbackWrapper<void>>(std::bind(&AsyncMotionModel::onStart, this));
104+
auto result = callback->get_future();
105+
waitables_interface_->add_waitable(callback, nullptr);
100106
result.wait();
107+
waitables_interface_->remove_waitable(callback, nullptr);
101108
}
102109

103110
void AsyncMotionModel::stop()
104111
{
105-
if (ros::ok())
112+
if (rclcpp::ok())
106113
{
107-
auto callback = boost::make_shared<CallbackWrapper<void>>(std::bind(&AsyncMotionModel::onStop, this));
108-
auto result = callback->getFuture();
109-
callback_queue_.addCallback(callback, reinterpret_cast<uint64_t>(this));
114+
auto callback = std::make_shared<CallbackWrapper<void>>(std::bind(&AsyncMotionModel::onStop, this));
115+
auto result = callback->get_future();
116+
waitables_interface_->add_waitable(callback, nullptr);
110117
result.wait();
118+
waitables_interface_->remove_waitable(callback, nullptr);
111119
}
112120
else
113121
{
114-
spinner_.stop();
122+
executor_.cancel();
123+
executor_.remove_node(node_);
124+
115125
onStop();
116126
}
117127
}

0 commit comments

Comments
 (0)