[ROS Q&A] 149 – How to command joint position of a robot in ROS using Python?
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.
catkin_create_pkg rrbot_control rospy
Then we create a script called main.py under the scripts folder with the following content.
# license removed for brevity
from std_msgs.msg import Float64
pub = rospy.Publisher('/rrbot/joint1_position_controller/command', Float64, queue_size=10)
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__':
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
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.
- Click to share on Twitter (Opens in new window)
- Click to share on Facebook (Opens in new window)
- Click to share on Reddit (Opens in new window)
- Click to email this to a friend (Opens in new window)
- Click to print (Opens in new window)
- Click to share on Google+ (Opens in new window)
- Click to share on Tumblr (Opens in new window)
- Share on Skype (Opens in new window)
- Click to share on Pinterest (Opens in new window)
- Click to share on WhatsApp (Opens in new window)
- Click to share on Telegram (Opens in new window)