How to run turtlebot in Gazebo using a python code

asked 2021-04-20 15:49:54 -0500

Confused Girl gravatar image

Dear friends, I am very new in ROS and I have a problem that I cannot solve. I would like to connect my joystick to turtlebot in Gazebo environment. Then get data from joystick and do some calculations based on them and then send command velocities to the turtlebot. I am not good at C++. I want to do this with python. I use indigo and ubuntu 14.04. To simplify my problem, I want to first get data from joystick and run the turtlebot. Later I can add other calculations. I have install joy package. I can get the data from joystick using this command: "rostopic echo joy" I have found the following python code somewhere and I want to move the turtlebot with it.

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import Joy
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=3)
def callback(msg):
  cmd_msg = Twist()
  cmd_msg.linear.x = speed_factor * msg.axes[1]
  cmd_msg.angular.z = speed_factor * msg.axes[2]
  pub.publish(cmd_msg)
def joy_teleop():
  rospy.init_node('joy_teleop')
  # Get parameters from the server
  global speed_factor
  speed_factor = rospy.get_param('~speed_factor', 10.0)
  rospy.loginfo('Using speed_factor: [%.1f]' % speed_factor)
  # Susbscribe to the topic that contains the ps3 keys
  rospy.Subscriber('/joy', Joy, callback)   
  # Keeps python from exiting until this node is stopped
  rospy.spin()
if __name__ == '__main__':
  joy_teleop()

I run this code using this command:

python Name_of_Code.py

I use this ros launch command to bring turtlebot to Gazebo environment:

 roslaunch turtlebot_gazebo turtlebot_world.launch

However, when I move the joystick, the turtlebot in Gazebo does not move. I do not know which step I am missing. I appreciate any help or a complete tutorial that can help me on that. Thank you very much

edit retag flag offensive close merge delete

Comments

To check if the script(python code) is working Run this python script. And in another terminal run rostopic echo /cmd_vel and try to press on the joystick and if you see messages on the terminal this means the code is workind and message is being published.

To which topic is responsible for moving the turtlebot run the launch file above and check if the turtlebot is subscribing to the /cmd_vel topic using this command

rostopic info /cmd_vel

mohamed ahmed gravatar image mohamed ahmed  ( 2021-04-21 08:03:12 -0500 )edit