Tutorials/drcsim/ros animated robot


 * 1) DRC Tutorial: Moving DRC robot joints by animation

In this tutorial, we'll move a robot in simulation without dynamics through the use of a model plugin that listens to the trajectory_msgs::JointTrajectory message over a ROS topic.


 * 1) Setup

We assume that you've already done the installation step.

If you haven't done so, add the environment setup.sh files to your .bashrc. Important note: Fill in drcsim-x.y to refer to the correct location of the setup files. For example, if you have drcsim-1.0.x installed, type:

echo 'source /usr/share/drcsim-1.0/setup.sh' >> ~/.bashrc source ~/.bashrc

If you haven't already, create a ros directory in your home directory and add it to your $ROS_PACKAGE_PATH. From the command line

mkdir ~/ros echo "export ROS_PACKAGE_PATH=\$HOME/ros:\$ROS_PACKAGE_PATH" >> ~/.bashrc source ~/.bashrc

Use roscreate-pkg to create a ROS package for this tutorial, depending on `roscpp` and `trajectory_msgs`:

cd ~/ros roscreate-pkg joint_animation_tutorial roscpp trajectory_msgs cd joint_animation_tutorial mkdir scripts cd scripts


 * 1) Model Plugin Controller

A joint trajectory model plugin has been embedded in the DRC robot. The plugin subscribes to a ROS topic containing JointTrajectory messages, and plays back the joint trajectory on the robot. This is useful when you want to move the robot without worrying about the dynamics of the robot model. This plugin also disables physics updates while playing back the joint trajectory.


 * 1) Create a ROS Publisher

Create a python ROS node that publishes joint trajectory messages `~/ros/joint_animation_tutorial/nodes/joint_animation.py`:

gedit joint_animation.py

Paste the following content into it:


 * 1) !/usr/bin/env python

import roslib; roslib.load_manifest('joint_animation_tutorial') import rospy, math

from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint

def jointTrajectoryCommand: # Initialize the node rospy.init_node('joint_control')

pub = rospy.Publisher('/joint_trajectory', JointTrajectory) jt = JointTrajectory

jt.header.stamp = rospy.Time.now jt.header.frame_id = "drc_robot::pelvis"

jt.joint_names.append("drc_robot::back.lbz" ) jt.joint_names.append("drc_robot::back.mby" ) jt.joint_names.append("drc_robot::back.ubx" ) jt.joint_names.append("drc_robot::neck.ay" ) jt.joint_names.append("drc_robot::l.leg.uhz") jt.joint_names.append("drc_robot::l.leg.mhx") jt.joint_names.append("drc_robot::l.leg.lhy") jt.joint_names.append("drc_robot::l.leg.kny") jt.joint_names.append("drc_robot::l.leg.uay") jt.joint_names.append("drc_robot::l.leg.lax") jt.joint_names.append("drc_robot::r.leg.lax") jt.joint_names.append("drc_robot::r.leg.uay") jt.joint_names.append("drc_robot::r.leg.kny") jt.joint_names.append("drc_robot::r.leg.lhy") jt.joint_names.append("drc_robot::r.leg.mhx") jt.joint_names.append("drc_robot::r.leg.uhz") jt.joint_names.append("drc_robot::l.arm.elx") jt.joint_names.append("drc_robot::l.arm.ely") jt.joint_names.append("drc_robot::l.arm.mwx") jt.joint_names.append("drc_robot::l.arm.shx") jt.joint_names.append("drc_robot::l.arm.usy") jt.joint_names.append("drc_robot::l.arm.uwy") jt.joint_names.append("drc_robot::r.arm.elx") jt.joint_names.append("drc_robot::r.arm.ely") jt.joint_names.append("drc_robot::r.arm.mwx") jt.joint_names.append("drc_robot::r.arm.shx") jt.joint_names.append("drc_robot::r.arm.usy") jt.joint_names.append("drc_robot::r.arm.uwy")

n = 1500 dt = 0.01 rps = 0.05 for i in range (n): p = JointTrajectoryPoint theta = rps*2.0*math.pi*i*dt x1 = -0.5*math.sin(2*theta) x2 = 0.5*math.sin(1*theta) p.positions.append(x1) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) jt.points.append(p)

# set duration jt.points[i].time_from_start = rospy.Duration.from_sec(dt) rospy.loginfo("test: angles[%d][%f, %f]",n,x1,x2)

pub.publish(jt) rospy.spin

if __name__ == '__main__': try: jointTrajectoryCommand except rospy.ROSInterruptException: pass

Make the file executable

chmod +x joint_animation.py


 * 1) The Code explained ###


 * 1) !/usr/bin/env python

import roslib; roslib.load_manifest('joint_animation_tutorial') Standard for every rospy node. This imports roslib and then loads the manifest.xml included in the package so those packages are importable as well.

import rospy, math from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint Import more modules, and import the message file for JointTrajectory and JointTrajectoryPoint.

def jointTrajectoryCommand: # Initialize the node rospy.init_node('joint_control')

pub = rospy.Publisher('/joint_trajectory', JointTrajectory) This initializes the node and creates a publisher for the /joint_trajectory topic.

jt = JointTrajectory

jt.header.stamp = rospy.Time.now jt.header.frame_id = "drc_robot::pelvis" Create an instantiation of a JointTrajectory message and add the time stamp and frame_id to the header.

jt.joint_names.append("drc_robot::back.lbz" ) jt.joint_names.append("drc_robot::back.mby" ) jt.joint_names.append("drc_robot::back.ubx" ) jt.joint_names.append("drc_robot::neck.ay" ) jt.joint_names.append("drc_robot::l.leg.uhz") jt.joint_names.append("drc_robot::l.leg.mhx") jt.joint_names.append("drc_robot::l.leg.lhy") jt.joint_names.append("drc_robot::l.leg.kny") jt.joint_names.append("drc_robot::l.leg.uay") jt.joint_names.append("drc_robot::l.leg.lax") jt.joint_names.append("drc_robot::r.leg.lax") jt.joint_names.append("drc_robot::r.leg.uay") jt.joint_names.append("drc_robot::r.leg.kny") jt.joint_names.append("drc_robot::r.leg.lhy") jt.joint_names.append("drc_robot::r.leg.mhx") jt.joint_names.append("drc_robot::r.leg.uhz") jt.joint_names.append("drc_robot::l.arm.elx") jt.joint_names.append("drc_robot::l.arm.ely") jt.joint_names.append("drc_robot::l.arm.mwx") jt.joint_names.append("drc_robot::l.arm.shx") jt.joint_names.append("drc_robot::l.arm.usy") jt.joint_names.append("drc_robot::l.arm.uwy") jt.joint_names.append("drc_robot::r.arm.elx") jt.joint_names.append("drc_robot::r.arm.ely") jt.joint_names.append("drc_robot::r.arm.mwx") jt.joint_names.append("drc_robot::r.arm.shx") jt.joint_names.append("drc_robot::r.arm.usy") jt.joint_names.append("drc_robot::r.arm.uwy") Create the list of names of joints that will be controlled.

n = 1500 dt = 0.01 rps = 0.05 for i in range(n): p = JointTrajectoryPoint theta = rps*2.0*math.pi*i*dt x1 = -0.5*math.sin(2*theta) x2 = 0.5*math.sin(1*theta) Setup a for loop that runs for n=1500 times. It calculates joint angles at two different positions x1 and x2. There should be a position for each joint added above.

p.positions.append(x1) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x2) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) p.positions.append(x2) p.positions.append(x1) p.positions.append(x1) Create a list of positions that the JointTrajectoryPoint will follow.

jt.points.append(p) Add the JointTrajectoryPoint to the JointTrajectory and proceed to the next point.

# set duration jt.points[i].time_from_start = rospy.Duration.from_sec(dt) rospy.loginfo("test: angles[%d][%f, %f]",n,x1,x2) Log the point that was created.

pub.publish(jt) rospy.spin This will publish the single JointTrajectory message, which the robot will execute. The node will then spin, which allows the node to continue running without blocking the CPU.

if __name__ == '__main__': try: jointTrajectoryCommand except rospy.ROSInterruptException: pass The main method of the rospy node. It prevents the node from executing code if the thread has been shutdown.


 * 1) Running the Simulation

In terminal, source the DRC simulator setup script and start the DRC robot simulation (version without active mechanism controllers so they do not conflict with the joint animation): roslaunch drc_robot_utils drc_robot_no_controllers.launch

In a separate terminal, rosrun joint_animation_tutorial joint_animation.py

The DRC robot should move according to the published ROS JointTrajectory message.