Planning a random target

Planning a random target can be effectively performed in two steps: first of all, we need to create the random target itself and then check its validity. If the validity is confirmed, then we can proceed by requesting the goal as usual; otherwise, we will cancel (although we could retry until we find a valid random target). In order to verify the validity of the target, we need to perform a service call to a service provided by MoveIt! for this specific purpose. As usual, to perform a service call, we need a service client:

ros::ServiceClientvalidity_srv = 
nh.serviceClient<moveit_msgs::GetStateValidity>("/check_state_vali
dity");

Once the service client is set up, we need to create the random target. To do so, we need to create a robot state object containing the random positions, but to simplify the process, we can start by acquiring the current robot state object:

robot_state::RobotStatecurrent_state = 
*plan_group.getCurrentState();

We will then set the current robot state object to random positions, but to do so, we need to provide the joint model group for this robot state. The joint model group can be obtained using the already created robot state object as follows:

current_state.setToRandomPositions(current_state.getJointModelGrou
p("arm"));

Up to this point, we have a service client waiting to be used as well as a random robot state object, which we want to validate. We will create a pair of messages: one for the request and another for the response. Fill in the request message with the random robot state using one of the API conversion functions, and request the service call:

moveit_msgs::GetStateValidity::Request validity_request; 
moveit_msgs::GetStateValidity::Response validity_response; 
 
robot_state::robotStateToRobotStateMsg(current_state, 
validity_request.robot_state); validity_request.group_name = "arm"; validity_srv.call(validity_request, validity_response);

Once the service call is complete, we can check the response message. If the state appears to be invalid, we would simply stop running the module; otherwise, we will continue. As explained earlier, at this point, we could retry until we get a valid random state; this can be an easy exercise for the reader:

if (!validity_response.valid) 
{ 
  ROS_INFO("Random state is not valid"); 
  ros::shutdown(); 
  return 1; 
} 

Finally, we will set the robot state we just created as the goal using the planning group interface, which will then be planned and executed as usual by MoveIt!:

plan_group.setJointValueTarget(current_state); 

We can run this snippet of code with the following command, which should lead to the arm repositioning itself on a random configuration:

    $ rosrun rosbook_arm_snippets move_group_plan_random_target  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.148.124