Skip to content

Commit 25c894c

Browse files
Fix publishing of spacenav button values (#243)
* Fix publishing of spacenav button values We now always publish the two default button states, in which 0 = not pressed 1 = pressed. Additional buttons emerge and stay in that list upon being pressed for the first time. This supports more advanced spacenav devices with a yet unknown number of buttons. Fixes #223 * Fix format * Switch back to shared memory transport We now only keep the buttons' memory as class member.
1 parent f4bdd56 commit 25c894c

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

spacenav/include/spacenav/spacenav.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#ifndef SPACENAV__SPACENAV_HPP_
3131
#define SPACENAV__SPACENAV_HPP_
3232

33+
#include <vector>
3334
#include <rclcpp/rclcpp.hpp>
3435

3536
#include <geometry_msgs/msg/twist.hpp>
@@ -84,6 +85,9 @@ class Spacenav final : public rclcpp::Node
8485
double normed_wx = 0;
8586
double normed_wy = 0;
8687
double normed_wz = 0;
88+
89+
// We'll resize dynamically to support spacenav devices with more buttons.
90+
std::vector<int> joystick_buttons = {0, 0};
8791
};
8892

8993
} // namespace spacenav

spacenav/src/spacenav.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,20 @@ void Spacenav::poll_spacenav()
246246
break;
247247

248248
case SPNAV_EVENT_BUTTON:
249-
if (sev.button.bnum >= static_cast<int>(msg_joystick->buttons.size())) {
250-
msg_joystick->buttons.resize(sev.button.bnum + 1);
249+
250+
if (sev.button.bnum < 0) {
251+
RCLCPP_WARN(
252+
get_logger(), "Negative spacenav buttons not supported. Got %i", sev.button.bnum);
253+
break;
254+
}
255+
if (sev.button.bnum < static_cast<int>(joystick_buttons.size())) {
256+
// Update known buttons
257+
joystick_buttons[sev.button.bnum] = sev.button.press;
258+
} else {
259+
// Enlarge, fill up with zeros, and support the new button
260+
joystick_buttons.resize(sev.button.bnum + 1, 0);
261+
joystick_buttons[sev.button.bnum] = sev.button.press;
251262
}
252-
msg_joystick->buttons[sev.button.bnum] = sev.button.press;
253263
joy_stale = true;
254264
break;
255265

@@ -293,6 +303,7 @@ void Spacenav::poll_spacenav()
293303
msg_joystick->axes[3] = normed_wx;
294304
msg_joystick->axes[4] = normed_wy;
295305
msg_joystick->axes[5] = normed_wz;
306+
msg_joystick->buttons = joystick_buttons;
296307
publisher_joy->publish(std::move(msg_joystick));
297308
}
298309
}

0 commit comments

Comments
 (0)