/joy
#
Caution
This documentation page has been auto-generated.
It may be missing some details.
/joy
Quick Facts
- Category
- Message type
- Direction
⬅️ you normally publish to this topic.
Raw joystick readings coming from the joystick driver
Quick snippets#
$ ros2 topic pub /joy sensor_msgs/msg/Joy # press Tab twice to complete
How to use in your code#
1#!/usr/bin/env python
2
3import rclpy
4from rclpy.node import Node
5
6from sensor_msgs.msg import Joy
7
8
9class JoyPublisher(Node):
10
11 def __init__(self):
12 super().__init__('joy_publisher')
13 self.publisher = self.create_publisher(
14 Joy,
15 '/joy',
16 10)
17 timer_period = 0.5 # seconds
18 self.timer = self.create_timer(timer_period, self.timer_callback)
19
20 def timer_callback(self):
21
22 msg = Joy()
23
24 # check https://docs.ros2.org/latest/api/sensor_msgs/msg/Joy.html
25 # for the msg structure
26 # msg.data = ...
27
28 self.publisher.publish(msg)
29 self.get_logger().info('Publishing: "%s"' % msg.data)
30
31
32if __name__ == '__main__':
33 rclpy.init(args=args)
34
35 publisher = JoyPublisher()
36
37 rclpy.spin(publisher)
38
39 rclpy.shutdown()
1#include <chrono>
2#include <functional>
3#include <memory>
4#include <string>
5
6#include "rclcpp/rclcpp.hpp"
7#include "sensor_msgs/msg/joy.hpp"
8
9using namespace std::chrono_literals;
10using namespace rclcpp;
11
12class JoyPublisher : public rclcpp::Node
13{
14public:
15 JoyPublisher()
16 : Node("joy_publisher")
17 {
18 publisher_ = this->create_publisher<sensor_msgs::msg::Joy>("/joy", 10);
19 timer_ = this->create_wall_timer(
20 500ms,
21 std::bind(&JoyPublisher::timer_callback, this));
22 }
23
24private:
25 void timer_callback()
26 {
27 auto msg = sensor_msgs::msg::Joy();
28
29 // check https://docs.ros2.org/latest/api/sensor_msgs/msg/Joy.html
30 // for the msg structure
31 //msg.data = ...;
32 publisher_->publish(msg);
33 }
34
35 TimerBase::SharedPtr timer_;
36 Publisher<sensor_msgs::msg::Joy>::SharedPtr publisher_;
37};
38
39int main(int argc, char * argv[])
40{
41init(argc, argv);
42spin(std::make_shared<JoyPublisher>());
43shutdown();
44return 0;
45}