This ROS 2 package provides a node that publishes SpaceMouse input as a Joy
message. It continuously reads the state of the SpaceMouse and publishes the data, and can handle the SpaceMouse being disconnected.
The main node in this package is spacemouse_joy.py
, which handles the connection to the SpaceMouse, reads its state, and publishes the data as a Joy
message. If the SpaceMouse is disconnected, the node will attempt to reconnect and continue publishing zero values for all axes until the connection is restored.
The outputs represent the axes as follows:
float
- Translation X (left/right)float
- Translation Y (forward/backward)float
- Translation Z (up/down)float
- Rotation Pitch (lateral axis - forward/backward)float
- Rotation Roll (longitudinal axis - left right)float
- Rotation Yaw (perpendicular axis - twist)int
- Left buttonint
- Right button
-
spacemouse_joy.py: This file contains the
SpaceMouseJoy
class, which is responsible for connecting to the SpaceMouse, reading its state, and publishing the data as aJoy
message. It also handles reconnection attempts if the SpaceMouse is disconnected. -
joy_listener.py: This file contains the
JoyListener
class, which subscribes to theJoy
messages published by thespacemouse_joy.py
node. It prints the axes and button states to the console, providing a clean output to verify that the publisher is working correctly.
This setup is simplified for Ubuntu 22.04. See the original documentation for a complete setup.
Source: PySpaceMouse GitHub
-
Install the package:
pip install pyspacemouse
-
Install dependencies:
-
The library uses
hidapi
as a low-level interface to the device andeasyhid
as a Python abstraction for easier use.sudo apt-get install libhidapi-dev
-
Add rules for permissions:
sudo echo 'KERNEL=="hidraw*", SUBSYSTEM=="hidraw", MODE="0664", GROUP="plugdev"' > /etc/udev/rules.d/99-hidraw-permissions.rules sudo usermod -aG plugdev $USER newgrp plugdev
-
easyhid
is ahidapi
interface for Python - required on all platforms:pip install git+https://github.com/bglopez/python-easyhid.git
-
Source (Chinese): CSDN Blog
By default, ordinary users may not have permission to access HID devices.
-
Run the following command to add the
idVendor
andidProduct
of the 3D SpaceMouse in udev rules:sudo tee /etc/udev/rules.d/99-spacemouse.rules > /dev/null <<EOF SUBSYSTEM=="input", GROUP="input", MODE="0660" KERNEL=="hidraw*", ATTRS{idVendor}=="256f", ATTRS{idProduct}=="c635", MODE="0666" EOF
These IDs are for CAPRA's SpaceMouse. See the original setup for other devices.
-
Reload udev rules:
sudo udevadm control --reload-rules sudo udevadm trigger
-
Disconnect and reconnect the SpaceMouse.
-
Make sure the current user belongs to the input group (expect no output):
sudo usermod -a -G input $USER
-
Log out and log back in to Ubuntu.