Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions turtlebot3_bringup/launch/joystick_teleop.launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import PathJoinSubstitution
from launch_ros.substitutions import FindPackageShare


def generate_launch_description():

# Include the robot bringup launch file
robot_bringup = IncludeLaunchDescription(
PythonLaunchDescriptionSource([
PathJoinSubstitution([
FindPackageShare('turtlebot3_bringup'),
'launch',
'robot.launch.py'
])
])
)

# Joy node to read joystick
joy_node = Node(
package='joy',
executable='joy_node',
name='joy_node'
)

# Teleop node to convert joy to cmd_vel
teleop_node = Node(
package='teleop_twist_joy',
executable='teleop_node',
name='teleop_twist_joy_node',
parameters=[{
'axis_linear.x': 1,
'axis_angular.yaw': 3,
'publish_stamped_twist': True,
'scale_linear.x': 0.5,
'scale_angular.yaw': 0.5,
}]
)

return LaunchDescription([
robot_bringup,
joy_node,
teleop_node
])
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import time

from geometry_msgs.msg import Point
from geometry_msgs.msg import Twist
from geometry_msgs.msg import Twist, TwistStamped
from nav_msgs.msg import Odometry
import rclpy
from rclpy.action import ActionServer
Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(self):
goal_callback=self.goal_callback)

self.goal_msg = Patrol.Goal()
self.twist = Twist()
self.twist = TwistStamped()
self.odom = Odometry()
self.position = Point()
self.rotation = 0.0
Expand All @@ -60,15 +60,15 @@ def __init__(self):

qos = QoSProfile(depth=10)

self.cmd_vel_pub = self.create_publisher(Twist, 'cmd_vel', qos)
self.cmd_vel_pub = self.create_publisher(TwistStamped, 'cmd_vel', qos)

self.odom_sub = self.create_subscription(
Odometry, 'odom', self.odom_callback, qos
)

def init_twist(self):
self.twist.linear.x = 0.0
self.twist.angular.z = 0.0
self.twist.twist.linear.x = 0.0
self.twist.twist.angular.z = 0.0
Comment on lines +66 to +67

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When using TwistStamped messages, it's crucial to populate the header field, especially the timestamp, before publishing. This allows other nodes to know when the command was issued. You should update the timestamp every time you are about to publish the message.

Additionally, it would be good practice to set the frame_id in the __init__ method, for example: self.twist.header.frame_id = 'base_link'.

Suggested change
self.twist.twist.linear.x = 0.0
self.twist.twist.angular.z = 0.0
self.twist.header.stamp = self.get_clock().now().to_msg()
self.twist.twist.linear.x = 0.0
self.twist.twist.angular.z = 0.0

self.cmd_vel_pub.publish(self.twist)

def odom_callback(self, msg):
Expand All @@ -82,11 +82,11 @@ def get_yaw(self):

def go_front(self, position, length):
while True:
position += self.twist.linear.x
position += self.twist.twist.linear.x

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There appears to be a logic error here. You are incrementing position using self.twist.twist.linear.x from the previous loop iteration (or from before this function was called). This will likely not work as intended, especially on the first iteration where the value might be zero. You probably meant to use self.linear_x, which is the speed you are setting in this loop.

A more robust implementation would use odometry to track the actual distance traveled instead of relying on time.sleep().

Suggested change
position += self.twist.twist.linear.x
position += self.linear_x

if position >= length:
break
self.twist.linear.x = self.linear_x
self.twist.angular.z = 0.0
self.twist.twist.linear.x = self.linear_x
self.twist.twist.angular.z = 0.0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The TwistStamped message's header needs to be updated with a new timestamp before each publication. This is crucial for systems that rely on the age of the velocity command.

Suggested change
self.twist.twist.linear.x = self.linear_x
self.twist.twist.angular.z = 0.0
self.twist.header.stamp = self.get_clock().now().to_msg()
self.twist.twist.linear.x = self.linear_x
self.twist.twist.angular.z = 0.0

self.cmd_vel_pub.publish(self.twist)

time.sleep(1)
Expand All @@ -110,8 +110,8 @@ def turn(self, target_angle):
if yaw_diff < 0.01:
break

self.twist.linear.x = 0.0
self.twist.angular.z = self.angular_z
self.twist.twist.linear.x = 0.0
self.twist.twist.angular.z = self.angular_z

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

As in other parts of the file, the timestamp in the TwistStamped message header should be updated with the current time before publishing.

Suggested change
self.twist.twist.linear.x = 0.0
self.twist.twist.angular.z = self.angular_z
self.twist.header.stamp = self.get_clock().now().to_msg()
self.twist.twist.linear.x = 0.0
self.twist.twist.angular.z = self.angular_z

self.cmd_vel_pub.publish(self.twist)

self.init_twist()
Expand Down