The first example that we are going to see is random motion planning using MoveIt! C++ APIs. You will get the code named test_random.cpp from the src folder. The code and the description of each line follows. When we execute this node, it will plan a random path and execute it:
//MoveIt! header file
#include <moveit/move_group_interface/move_group.h>
int main(int argc, char **argv)
{
ros::init(argc, argv, "test_random_node",
ros::init_options::AnonymousName);
// start a ROS spinning thread
ros::AsyncSpinner spinner(1);
spinner.start();
// this connects to a running instance of the move_group node
// Here the Planning group is "arm"
move_group_interface::MoveGroup group("arm");
// specify that our target will be a random one
group.setRandomTarget();
// plan the motion and then move the group to the sampled target
group.move();
ros::waitForShutdown();
}
To build the source code, we should add the following lines of code to CMakeLists.txt. You will get the complete CMakeLists.txt file from the existing package itself:
add_executable(test_random_node src/test_random.cpp)
add_dependencies(test_random_node
seven_dof_arm_test_generate_messages_cpp)
target_link_libraries(test_random_node
${catkin_LIBRARIES} )
We can build the package using the catkin_make command. Check whether
test_random.cpp is built properly or not. If the code is built properly, we can
start testing the code.
The following command will start the RViz with 7-DOF arm with motion planning plugin:
$ roslaunch seven_dof_arm_config demo.launch
Move the end-effector to check whether everything is working properly in RViz.
Run the C++ node for planning to a random position using the following command:
$ rosrun seven_dof_arm_test test_random_node
The output of RViz is shown next. The arm will select a random position that has a valid IK and motion plan from the current position:
![](http://imgdetail.ebookreading.net/hardware/2/9781788627436/9781788627436__ros-programming-building__9781788627436__assets__2acadcad-5095-479c-a6f5-7cc428bf7564.png)