3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ROS2 Joy Stick Controller を使って Turtlesim を動かす

Posted at

ROS2でJoyスティックコントローラを使いたい

01 2023-06-11 00-19-39.jpg

やりたいことはこの動画の感じ。

Joy Stickを使う場合は以下実行するだけでOK。

$ ros2 run joy joy_node

しかしjoyトピックなので亀を動かすためにはTwistに変換する必要がある。
自分でパッケージを作ってみる。

Screenshot from 2023-06-10 23-27-18.png

rosgraph.png

JoyからTwistへの変換のためのPythonコード

import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy

class JoyTranslate(Node): 
    def __init__(self):
        super().__init__('joy_translate_node') 
        self.publisher = self.create_publisher(Twist,'cmd_vel', 10)
        self.subscription = self.create_subscription(Joy,'joy', self.listener_callback, 10)
        self.vel = Twist()

    def listener_callback(self, Joy): 
        self.vel.linear.x  = 2*Joy.axes[1]
        self.vel.angular.z  = 2*Joy.axes[3]
        self.publisher.publish(self.vel)
        self.get_logger().info("Velocity: Linear=%f" % (Joy.axes[1])) 

def main(args=None):
    rclpy.init(args=args)
    joy_translate = JoyTranslate()
    rclpy.spin(joy_translate)
    rclpy.shutdown()

if __name__ == '__main__':
    main()

パッケージ作成

$ cd ~/ros2_ws/src
$ ros2 pkg create --build-type ament_python --node-name joy_translate_node joy_translate

$ cd ~/ros2ws/src/joy_translate/joy_translate

ここでjoy_translate_node.pyを編集する。「JoyからTwistへの変換のためのPythonコード」で示したコードをコピペでOKのはず。

実行

$ ros2 run turtlesim turtlesim_node
$ ros2 run joy_translate joy_translate_node --remap cmd_vel:=turtle1/cmd_vel
$ ros2 run turtlesim turtlesim_node

参考

自分用のメモ

自分用メモ

トピックを見る場合
ros2 topic echo /joy

rosmsg show sensor_msgs/Joy

std_msgs/Header header
uint32 seq
time stamp
string frame_id
float32[] axes
int32[] buttons

geometry_msgs/Twist Message

geometry_msgs/Vector3 linear
geometry_msgs/Vector3 angular

bashrcに書くやつ
source /opt/ros/humble/setup.bash
ビルド後に必要なやつ
. install/setup.bash

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?