[ROS Q&A] 149 – How to command joint position of a robot in ROS using Python?

[ROS Q&A] 149 - How to command joint position of a robot in ROS using Python?

Written by Arif Rahman


This video extends the excellent tutorial on Gazebo-ROS control by demonstrating a Python script that will send the command to move a robot. The robot is used as an example, and a simple Python code sends joint position command to it through the appropriate topic.

This video is a response to a question posted on Gazebo Answers

Step 0. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s create a new project and call it rrbot_control. You can easily run the RRBOT simulation from Simulations->RRBOT in ROSDS.

Step 1. Load controller

If you launch the simulation successfully, you should see the robot is swinging its arm. As an example, the controller for this robot is already developed. You can launch the controller with the following command.

roslaunch rrbot_control rrbot_control.launch

Now if you type rostopic list , you should see several topic like /rrbot/joint1_position_controller/command in the list. We can publish to these topics to control the robot. For example,

rostopic pub /rrbot/joint1_position_controller/command std_msgs/Float64 "data: 0.5"

You should see the rrbot move its joint to a new position. The value here is in radian. Let’s try to control the robot with code instead of sending command.

Step 2. Create a package

We’ll create a ros package for our code first.

cd ~/catkin_ws/src
catkin_create_pkg rrbot_control rospy
cd rrbot_control
mkdir scripts

Then we create a script called main.py under the scripts folder with the following content.

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import Float64
import math

def talker():
    pub = rospy.Publisher('/rrbot/joint1_position_controller/command', Float64, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        position = math.pi/2

if __name__ == '__main__':
    except rospy.ROSInterruptException:

We can use rosrun rrbot_control main.pyto tun our script(remember to give it permission). You should see the robot move to the desired position. You can also try the code with different position function like sin, cos or even publish to more joints if you want.

Want to learn more?

If you are interested in learning ROS, please check our Robot Ignite Academy.


Edit by: Tony Huang


Gazebo-ROS control tutorial
Official ROS Publisher / Subscriber tutorial
ROS Development Studio (ROSDS)
Robot Ignite Academy

We love feedback!

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

1 Comment

  1. mamaligadoc

    With respect !!!


Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This