Skip to content

Commit 2d984e9

Browse files
committed
rebase stamped-support
1 parent a3abcb7 commit 2d984e9

File tree

7 files changed

+128
-179
lines changed

7 files changed

+128
-179
lines changed

include/twist_mux/topic_handle.hpp

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -154,69 +154,41 @@ class TopicHandle_
154154
T msg_;
155155
};
156156

157-
class VelocityTopicHandle : public TopicHandle_<geometry_msgs::msg::Twist>
157+
template<typename T>
158+
class VelocityTopicHandle : public TopicHandle_<T>
158159
{
159160
private:
160-
typedef TopicHandle_<geometry_msgs::msg::Twist> base_type;
161+
typedef TopicHandle_<T> base_type;
162+
163+
// https://index.ros.org/doc/ros2/About-Quality-of-Service-Settings
164+
// rmw_qos_profile_t twist_qos_profile = rmw_qos_profile_sensor_data;
161165

162166
public:
167+
using base_type::subscriber_;
168+
using base_type::mux_;
169+
using base_type::topic_;
170+
using base_type::stamp_;
171+
using base_type::msg_;
172+
163173
typedef typename base_type::priority_type priority_type;
164174

165175
VelocityTopicHandle(
166176
const std::string & name, const std::string & topic, const rclcpp::Duration & timeout,
167177
priority_type priority, TwistMux * mux)
168178
: base_type(name, topic, timeout, priority, mux)
169179
{
170-
subscriber_ = mux_->create_subscription<geometry_msgs::msg::Twist>(
180+
subscriber_ = mux_->template create_subscription<T>(
171181
topic_, rclcpp::SystemDefaultsQoS(),
172182
std::bind(&VelocityTopicHandle::callback, this, std::placeholders::_1));
173183
}
174184

175185
bool isMasked(priority_type lock_priority) const
176186
{
177187
// std::cout << hasExpired() << " / " << (getPriority() < lock_priority) << std::endl;
178-
return hasExpired() || (getPriority() < lock_priority);
179-
}
180-
181-
void callback(const geometry_msgs::msg::Twist::ConstSharedPtr msg)
182-
{
183-
stamp_ = mux_->now();
184-
msg_ = *msg;
185-
186-
// Check if this twist has priority.
187-
// Note that we have to check all the locks because they might time out
188-
// and since we have several topics we must look for the highest one in
189-
// all the topic list; so far there's no O(1) solution.
190-
if (mux_->hasPriority(*this)) {
191-
mux_->publishTwist(msg);
192-
}
193-
}
194-
};
195-
196-
class VelocityStampedTopicHandle : public TopicHandle_<geometry_msgs::msg::TwistStamped>
197-
{
198-
private:
199-
typedef TopicHandle_<geometry_msgs::msg::TwistStamped> base_type;
200-
201-
public:
202-
typedef typename base_type::priority_type priority_type;
203-
204-
VelocityStampedTopicHandle(
205-
const std::string & name, const std::string & topic, const rclcpp::Duration & timeout,
206-
priority_type priority, TwistMux * mux)
207-
: base_type(name, topic, timeout, priority, mux)
208-
{
209-
subscriber_ = mux_->create_subscription<geometry_msgs::msg::TwistStamped>(
210-
topic_, rclcpp::SystemDefaultsQoS(),
211-
std::bind(&VelocityStampedTopicHandle::callback, this, std::placeholders::_1));
212-
}
213-
214-
bool isMasked(priority_type lock_priority) const
215-
{
216-
return hasExpired() || (getPriority() < lock_priority);
188+
return base_type::hasExpired() || (base_type::getPriority() < lock_priority);
217189
}
218190

219-
void callback(const geometry_msgs::msg::TwistStamped::ConstSharedPtr msg)
191+
void callback(const typename T::ConstSharedPtr msg)
220192
{
221193
stamp_ = mux_->now();
222194
msg_ = *msg;
@@ -225,8 +197,8 @@ class VelocityStampedTopicHandle : public TopicHandle_<geometry_msgs::msg::Twist
225197
// Note that we have to check all the locks because they might time out
226198
// and since we have several topics we must look for the highest one in
227199
// all the topic list; so far there's no O(1) solution.
228-
if (mux_->hasPriorityStamped(*this)) {
229-
mux_->publishTwistStamped(msg);
200+
if (mux_->template hasPriority(*this)) {
201+
mux_->template publishTwist(msg);
230202
}
231203
}
232204
};
@@ -244,7 +216,7 @@ class LockTopicHandle : public TopicHandle_<std_msgs::msg::Bool>
244216
priority_type priority, TwistMux * mux)
245217
: base_type(name, topic, timeout, priority, mux)
246218
{
247-
subscriber_ = mux_->create_subscription<std_msgs::msg::Bool>(
219+
subscriber_ = mux_->template create_subscription<std_msgs::msg::Bool>(
248220
topic_, rclcpp::SystemDefaultsQoS(),
249221
std::bind(&LockTopicHandle::callback, this, std::placeholders::_1));
250222
}

include/twist_mux/twist_mux.hpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ namespace twist_mux
5151
// Forwarding declarations:
5252
class TwistMuxDiagnostics;
5353
struct TwistMuxDiagnosticsStatus;
54+
template <typename T>
5455
class VelocityTopicHandle;
55-
class VelocityStampedTopicHandle;
5656
class LockTopicHandle;
5757

5858
/**
@@ -64,23 +64,24 @@ class TwistMux : public rclcpp::Node
6464
public:
6565
template<typename T>
6666
using handle_container = std::list<T>;
67+
using velocity_handle_variant = std::variant<VelocityTopicHandle<geometry_msgs::msg::Twist>, VelocityTopicHandle<geometry_msgs::msg::TwistStamped>>;
68+
using publisher_variant = std::variant<rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr, rclcpp::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr>;
69+
using message_variant = std::variant<geometry_msgs::msg::Twist, geometry_msgs::msg::TwistStamped>;
6770

68-
using velocity_topic_container = handle_container<VelocityTopicHandle>;
69-
using velocity_stamped_topic_container = handle_container<VelocityStampedTopicHandle>;
71+
using velocity_topic_container = handle_container<velocity_handle_variant>;
7072
using lock_topic_container = handle_container<LockTopicHandle>;
7173

7274
TwistMux();
7375
~TwistMux() = default;
7476

7577
void init();
7678

77-
bool hasPriority(const VelocityTopicHandle & twist);
79+
template <typename VelocityTopicHandleT>
80+
bool hasPriority(const VelocityTopicHandleT & twist);
7881

79-
bool hasPriorityStamped(const VelocityStampedTopicHandle & twist);
80-
81-
void publishTwist(const geometry_msgs::msg::Twist::ConstSharedPtr & msg);
82-
83-
void publishTwistStamped(const geometry_msgs::msg::TwistStamped::ConstSharedPtr & msg);
82+
83+
template <typename MessageConstSharedPtrT>
84+
void publishTwist(const MessageConstSharedPtrT & msg);
8485

8586
void updateDiagnostics();
8687

@@ -99,14 +100,12 @@ class TwistMux : public rclcpp::Node
99100
* must reserve the number of handles initially.
100101
*/
101102
std::shared_ptr<velocity_topic_container> velocity_hs_;
102-
std::shared_ptr<velocity_stamped_topic_container> velocity_stamped_hs_;
103103
std::shared_ptr<lock_topic_container> lock_hs_;
104104

105-
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr cmd_pub_;
106-
rclcpp::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr cmd_pub_stamped_;
105+
publisher_variant cmd_pub_;
106+
message_variant last_cmd_;
107107

108-
geometry_msgs::msg::Twist last_cmd_;
109-
geometry_msgs::msg::TwistStamped last_cmd_stamped_;
108+
bool output_stamped;
110109

111110
template<typename T>
112111
void getTopicHandles(const std::string & param_name, handle_container<T> & topic_hs);

include/twist_mux/twist_mux_diagnostics_status.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,16 @@ struct TwistMuxDiagnosticsStatus
5555

5656
LockTopicHandle::priority_type priority;
5757

58-
bool use_stamped;
59-
6058
std::shared_ptr<TwistMux::velocity_topic_container> velocity_hs;
61-
std::shared_ptr<TwistMux::velocity_stamped_topic_container> velocity_stamped_hs;
6259
std::shared_ptr<TwistMux::lock_topic_container> lock_hs;
6360

6461
TwistMuxDiagnosticsStatus()
6562
: reading_age(0),
6663
last_loop_update(rclcpp::Clock().now()),
6764
main_loop_time(0),
68-
priority(0),
69-
use_stamped(true)
65+
priority(0)
7066
{
7167
velocity_hs = std::make_shared<TwistMux::velocity_topic_container>();
72-
velocity_stamped_hs = std::make_shared<TwistMux::velocity_stamped_topic_container>();
7368
lock_hs = std::make_shared<TwistMux::lock_topic_container>();
7469
}
7570
};

launch/twist_mux_launch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ def generate_launch_description():
5252
'use_sim_time',
5353
default_value='False',
5454
description='Use simulation time'),
55+
DeclareLaunchArgument(
56+
'output_stamped',
57+
default_value=False,
58+
description='Output as geometry_msgs/TwistStamped instead of geometry_msgs/Twist'),
5559
Node(
5660
package='twist_mux',
5761
executable='twist_mux',
5862
output='screen',
5963
remappings={('/cmd_vel_out', LaunchConfiguration('cmd_vel_out'))},
6064
parameters=[
61-
{'use_sim_time': LaunchConfiguration('use_sim_time')},
65+
{'use_sim_time': LaunchConfiguration('use_sim_time'),
66+
'output_stamped': LaunchConfiguration('output_stamped')},
6267
LaunchConfiguration('config_locks'),
6368
LaunchConfiguration('config_topics')]
6469
),

src/twist_marker.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
#include <rclcpp/rclcpp.hpp>
3636
#include <geometry_msgs/msg/twist.hpp>
37-
#include <geometry_msgs/msg/twist_stamped.hpp>
3837
#include <visualization_msgs/msg/marker.hpp>
3938
#include <visualization_msgs/msg/marker_array.hpp>
4039

@@ -108,33 +107,21 @@ class TwistMarkerPublisher : public rclcpp::Node
108107
{
109108
std::string frame_id;
110109
double scale;
111-
bool use_stamped;
112110
double z;
113111

114112
this->declare_parameter("frame_id", "base_footprint");
115113
this->declare_parameter("scale", 1.0);
116-
this->declare_parameter("use_stamped", false);
117114
this->declare_parameter("vertical_position", 2.0);
118115

119116
this->get_parameter<std::string>("frame_id", frame_id);
120117
this->get_parameter<double>("scale", scale);
121-
this->get_parameter<bool>("use_stamped", use_stamped);
122118
this->get_parameter<double>("vertical_position", z);
123119

124120
marker_ = std::make_shared<TwistMarker>(frame_id, scale, z);
125121

126-
if (use_stamped)
127-
{
128-
sub_stamped_ = this->create_subscription<geometry_msgs::msg::TwistStamped>(
129-
"twist", rclcpp::SystemDefaultsQoS(),
130-
std::bind(&TwistMarkerPublisher::callback_stamped, this, std::placeholders::_1));
131-
}
132-
else
133-
{
134-
sub_ = this->create_subscription<geometry_msgs::msg::Twist>(
135-
"twist", rclcpp::SystemDefaultsQoS(),
136-
std::bind(&TwistMarkerPublisher::callback, this, std::placeholders::_1));
137-
}
122+
sub_ = this->create_subscription<geometry_msgs::msg::Twist>(
123+
"twist", rclcpp::SystemDefaultsQoS(),
124+
std::bind(&TwistMarkerPublisher::callback, this, std::placeholders::_1));
138125

139126
pub_ =
140127
this->create_publisher<visualization_msgs::msg::Marker>(
@@ -149,16 +136,8 @@ class TwistMarkerPublisher : public rclcpp::Node
149136
pub_->publish(marker_->getMarker());
150137
}
151138

152-
void callback_stamped(const geometry_msgs::msg::TwistStamped::ConstSharedPtr twist)
153-
{
154-
marker_->update(twist->twist);
155-
156-
pub_->publish(marker_->getMarker());
157-
}
158-
159139
private:
160140
rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr sub_;
161-
rclcpp::Subscription<geometry_msgs::msg::TwistStamped>::SharedPtr sub_stamped_;
162141
rclcpp::Publisher<visualization_msgs::msg::Marker>::SharedPtr pub_;
163142

164143
std::shared_ptr<TwistMarker> marker_ = nullptr;

0 commit comments

Comments
 (0)