Skip to content

Commit 71ca2b7

Browse files
committed
add option to use TwistStamped
Signed-off-by: Borong Yuan <[email protected]>
1 parent 92bf37d commit 71ca2b7

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

spacenav/include/spacenav/spacenav.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <rclcpp/rclcpp.hpp>
3535

3636
#include <geometry_msgs/msg/twist.hpp>
37+
#include <geometry_msgs/msg/twist_stamped.hpp>
3738
#include <geometry_msgs/msg/vector3.hpp>
3839
#include <sensor_msgs/msg/joy.hpp>
3940

@@ -60,6 +61,8 @@ class Spacenav final : public rclcpp::Node
6061

6162
rclcpp::Publisher<geometry_msgs::msg::Vector3>::SharedPtr publisher_rot_offset;
6263

64+
rclcpp::Publisher<geometry_msgs::msg::TwistStamped>::SharedPtr publisher_twist_stamped;
65+
6366
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_twist;
6467

6568
rclcpp::Publisher<sensor_msgs::msg::Joy>::SharedPtr publisher_joy;
@@ -74,6 +77,7 @@ class Spacenav final : public rclcpp::Node
7477
bool zero_when_static;
7578
double static_trans_deadband;
7679
double static_rot_deadband;
80+
bool use_twist_stamped;
7781

7882
spnav_event sev;
7983
bool joy_stale = false;

spacenav/src/spacenav.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define SPACENAV_ZERO_WHEN_STATIC_PARAM_S "zero_when_static"
5757
#define SPACENAV_STATIC_TRANS_DEADBAND_PARAM_S "static_trans_deadband"
5858
#define SPACENAV_STATIC_ROT_DEADBAND_PARAM_S "static_rot_deadband"
59+
#define SPACENAV_USE_TWIST_STAMPED_PARAM_S "use_twist_stamped"
5960

6061
using namespace std::chrono_literals;
6162

@@ -91,6 +92,8 @@ Spacenav::Spacenav(const rclcpp::NodeOptions & options)
9192
// translation, or both.
9293
this->declare_parameter<double>(SPACENAV_STATIC_TRANS_DEADBAND_PARAM_S, 0.1);
9394
this->declare_parameter<double>(SPACENAV_STATIC_ROT_DEADBAND_PARAM_S, 0.1);
95+
this->declare_parameter<bool>(SPACENAV_USE_TWIST_STAMPED_PARAM_S, false);
96+
this->get_parameter<bool>(SPACENAV_USE_TWIST_STAMPED_PARAM_S, use_twist_stamped);
9497

9598
auto param_change_callback = [this](
9699
std::vector<rclcpp::Parameter> parameters) {
@@ -117,8 +120,13 @@ Spacenav::Spacenav(const rclcpp::NodeOptions & options)
117120
"spacenav/offset", 10);
118121
publisher_rot_offset = this->create_publisher<geometry_msgs::msg::Vector3>(
119122
"spacenav/rot_offset", 10);
120-
publisher_twist =
121-
this->create_publisher<geometry_msgs::msg::Twist>("spacenav/twist", 10);
123+
if (use_twist_stamped) {
124+
publisher_twist_stamped =
125+
this->create_publisher<geometry_msgs::msg::TwistStamped>("spacenav/twist", 10);
126+
} else {
127+
publisher_twist =
128+
this->create_publisher<geometry_msgs::msg::Twist>("spacenav/twist", 10);
129+
}
122130
publisher_joy =
123131
this->create_publisher<sensor_msgs::msg::Joy>("spacenav/joy", 10);
124132

@@ -288,7 +296,14 @@ void Spacenav::poll_spacenav()
288296

289297
publisher_offset->publish(std::move(msg_offset));
290298
publisher_rot_offset->publish(std::move(msg_rot_offset));
291-
publisher_twist->publish(std::move(msg_twist));
299+
if (use_twist_stamped) {
300+
auto msg_twist_stamped = std::make_unique<geometry_msgs::msg::TwistStamped>();
301+
msg_twist_stamped->header.stamp = msg_joystick->header.stamp;
302+
msg_twist_stamped->twist = *msg_twist;
303+
publisher_twist_stamped->publish(std::move(msg_twist_stamped));
304+
} else {
305+
publisher_twist->publish(std::move(msg_twist));
306+
}
292307

293308
no_motion_count = 0;
294309
motion_stale = false;

0 commit comments

Comments
 (0)