Struggling with Exercise 3.2

I’ve done ROS basics, so I shouldn’t be struggling with topics, but I think I am struggling with the way this exercise is worded. Can someone please tell me what I am doing wrong? This is the source code for my publisher:

#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist

move = Twist()
rospy.init_node(‘publish_twist’)

pub = rospy.Publisher(’/twist_vels’, Twist, queue_size=10)

rate = rospy.Rate(2)

while not rospy.is_shutdown():
move.linear.x = 0.3
move.angular.z = 0.3
pub.publish(move)
rate.sleep()

#####################################################################
and the source for my subscriber:

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist

def callback(msg):
Vr = (2.0 * (msg.x) + (msg.z) * 0.3) / 2 * (0.1)
Vl = (2.0 * (msg.x) - (msg.z) * 0.3) / 2 * (0.1)
pub_left.publish(Vl)
pub_right.publish(Vr)

rospy.init_node(‘convert_twist’, anonymous=True)
sub = rospy.Subscriber(’/twist_vels’, Twist, callback)
pub_left = rospy.Publisher(’/left_wheel_controller/command’, Twist, queue_size=10)
pub_right = rospy.Publisher(’/right_wheel_controller/command’, Twist, queue_size=10)

rospy.spin()

Hi @adamSB ,

Welcome to the Community!

I have done this course and I have not had any issues. The code does not actually have any issues as far as I see.

Let me help you understand it.

  1. You have a publisher that publishes twist command into /twist_vels topic. Here the /twist_vels topic is a dummy topic that you have created - it will NOT make the robot move.
  2. You have a subscriber that receives the twist command that is posted on /twist_vels topic.
  3. In the subscriber, you convert the received twist command and decompose the linear velocity X and angular velocity Z into velocities for the wheels Vl and Vr.
  4. Finally, you publish left wheel velocity Vl into /left_wheel_controller/command topic and similarly Vr for right wheel - this would eventually make the robot move.

Basically, this example shows you how publishing to /cmd_vel works. The background of what happens to the robot after sending a twist command.

I hope my explanation helped you understand this better. Let me know if you still need clarification.

Regards,
Girish

Thank you! Your response helped me realize that I was atleast on the right track. I had to do msg.linear.x and switch my message type in the publishers to Float64.

@adamSB ,

Glad I could be of help to you!

Please mark as solution if you find that my reply helped you solve your problem.
That way this issue can be closed!

Regards,
Girish