Kinematics/C++ API

In this section, we will walk you through the C++ API for using kinematics.

The RobotModel and RobotState classes
The [/classmoveit_1_1core_1_1RobotModel.html RobotModel] and [/classmoveit_1_1core_1_1RobotState.html RobotState] classes are the core classes that give you access to the kinematics. In this example, we will walk through the process of using the classes for the right arm of the PR2.

Start
Setting up to start using the RobotModel class is very easy. In general, you will find that most higher-level components will return a shared pointer to the RobotModel. You should always use that when possible. In this example, we will start with such a shared pointer and discuss only the basic API. You can have a look at the actual code API for these classes to get more information about how to use more features provided by these classes.

We will start by instantiating a [/classrobot__model__loader_1_1RobotModelLoader.html RobotModelLoader class], which will look up the robot description on the ROS parameter server and construct a RobotModel for us to use.

Using the RobotModel, we can construct a RobotState that maintains the configuration of the robot. We will set all joints in the state to their default values. We can then get a JointStateGroup, which represents the joint_state for a particular group, e.g. the "right_arm" of the PR2 robot.

Forward Kinematics
Now, we can compute forward kinematics for a set of random joint values. Note that we would like to find the pose of the "r_wrist_roll_link" which is the most distal link in the "right_arm" of the robot.

Get Joint Values
We can retreive the current set of joint values stored in the state for the right arm.

Inverse Kinematics
We can now solve inverse kinematics (IK) for the right arm of the PR2 robot. To solve IK, we will need the following:
 * The desired pose of the end-effector (by default, this is the last link in the "right_arm" chain): end_effector_state that we computed in the step above.
 * The number of attempts to be made at solving IK: 5
 * The timeout for each attempt: 0.1 s

Now, we can print out the IK solution (if found):



Get the Jacobian
We can also get the Jacobian from the JointStateGroup.

The entire code
The entire code can be seen here in the moveit_pr2 github project.

Compiling the code
Follow the instructions for compiling code from source.

The launch file
To run the code, you will need a launch file that does two things:
 * Upload the PR2 URDF and SRDF onto the param server:
 * Puts the kinematics_solver configuration generated by the MoveIt! Setup Assistant onto the ROS parameter server in the namespace of the node that instantiates the classes in this tutorial:

The entire launch file is here on github. All the code in this tutorial can be compiled and run from the pr2_moveit_tutorials package that you have as part of your MoveIt! setup.

Running the code

 * Roslaunch the launch file to run the code directly from pr2_moveit_tutorials:

Expected Output
The expected output will be in the following form. The numbers will not match since we are using random joint values:

Additional Reading

 * RobotState Display - Visualization of the RobotState using Rviz

Links

 * [/classrobot__model_1_1RobotModel.html RobotModel Code API]
 * [/classrobot__state_1_1RobotState.html RobotState Code API]
 * Back to Kinematics