Skip to content

Commit 1bc08a5

Browse files
authored
add option to use TwistStamped (#251)
Signed-off-by: Borong Yuan <[email protected]>
1 parent 1b65aca commit 1bc08a5

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

spacenav/CMakeLists.txt

+7-12
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ find_package(rclcpp_components REQUIRED)
1919
find_package(sensor_msgs REQUIRED)
2020
find_package(SPNAV REQUIRED)
2121

22-
# Convenience variable for dependencies
23-
set(THIS_PACKAGE_INCLUDE_DEPENDS
24-
geometry_msgs
25-
rclcpp
26-
rclcpp_components
27-
sensor_msgs
28-
)
29-
3022
add_library(spacenav
3123
SHARED
3224
src/spacenav.cpp)
@@ -36,12 +28,15 @@ target_include_directories(spacenav PUBLIC
3628
$<INSTALL_INTERFACE:include/${PROJECT_NAME}>
3729
${spnav_INCLUDE_DIR})
3830

39-
ament_target_dependencies(spacenav
40-
${THIS_PACKAGE_INCLUDE_DEPENDS}
31+
target_link_libraries(spacenav PUBLIC
32+
${geometry_msgs_TARGETS}
33+
rclcpp::rclcpp
34+
${sensor_msgs_TARGETS}
35+
spnav
4136
)
4237

43-
target_link_libraries(spacenav
44-
spnav
38+
target_link_libraries(spacenav PRIVATE
39+
rclcpp_components::component
4540
)
4641

4742
install(TARGETS spacenav EXPORT export_spacenav

spacenav/include/spacenav/spacenav.hpp

+4
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

+17-3
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,7 @@ 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+
use_twist_stamped = this->declare_parameter<bool>(SPACENAV_USE_TWIST_STAMPED_PARAM_S, false);
9496

9597
auto param_change_callback = [this](
9698
std::vector<rclcpp::Parameter> parameters) {
@@ -117,8 +119,13 @@ Spacenav::Spacenav(const rclcpp::NodeOptions & options)
117119
"spacenav/offset", 10);
118120
publisher_rot_offset = this->create_publisher<geometry_msgs::msg::Vector3>(
119121
"spacenav/rot_offset", 10);
120-
publisher_twist =
121-
this->create_publisher<geometry_msgs::msg::Twist>("spacenav/twist", 10);
122+
if (use_twist_stamped) {
123+
publisher_twist_stamped =
124+
this->create_publisher<geometry_msgs::msg::TwistStamped>("spacenav/twist_stamped", 10);
125+
} else {
126+
publisher_twist =
127+
this->create_publisher<geometry_msgs::msg::Twist>("spacenav/twist", 10);
128+
}
122129
publisher_joy =
123130
this->create_publisher<sensor_msgs::msg::Joy>("spacenav/joy", 10);
124131

@@ -288,7 +295,14 @@ void Spacenav::poll_spacenav()
288295

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

293307
no_motion_count = 0;
294308
motion_stale = false;

0 commit comments

Comments
 (0)