youbot_moveit_to_pose.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <assert.h>
00003 #include "ros/ros.h"
00004 #include <stdio.h>
00005 #include <unistd.h>
00006 #include <termios.h>
00007 #include "std_msgs/String.h"
00008 #include "visualization_msgs/Marker.h"
00009 #include "tf/transform_datatypes.h"
00010 #include "tf/transform_listener.h"
00011 #include "moveit_msgs/CollisionObject.h"
00012 #include "moveit_msgs/PlanningScene.h"
00013 #include <moveit/planning_scene/planning_scene.h>
00014 #include <moveit/collision_detection/world.h>
00015 #include "geometry_msgs/Pose.h"
00016 #include "shape_msgs/SolidPrimitive.h"
00017 #include "control_msgs/FollowJointTrajectoryActionGoal.h"
00018 #include "boost/thread.hpp"
00019 
00020 #include <moveit/move_group_interface/move_group.h>
00021 
00022 volatile bool trajectory_complete = false;
00023 volatile char cancel = '0';
00024 std::string goal_string = "0";
00025 
00026 using namespace std;
00027 
00028 void trajectory_execution(move_group_interface::MoveGroup *g, move_group_interface::MoveGroup::Plan *plan)
00029 {
00030   trajectory_complete = g->execute(*plan);
00031 }
00032 
00033 void goal_callback(const control_msgs::FollowJointTrajectoryActionGoal::ConstPtr& msg)
00034 {
00035   goal_string = msg->goal_id.id;
00036 }
00037 
00038 char getch(void)
00039 {
00040         char ch;
00041         struct termios oldt;
00042         struct termios newt;
00043         tcgetattr(STDIN_FILENO, &oldt); /*store old settings */
00044         newt = oldt; /* copy old settings to new settings */
00045         newt.c_lflag &= ~(ICANON | ECHO); /* make one change to old settings in new settings */
00046         tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediately */
00047         ch = getchar(); /* standard getchar call */
00048         tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /*reapply the old settings */
00049         return ch; /*return received char */
00050 }
00051 
00052 void getch_check()
00053 {
00054   cancel = '0';
00055   while(cancel != 'c')
00056   {
00057     boost::this_thread::interruption_point();
00058     cancel = getch();
00059   }
00060 }
00061 
00062 int main(int argc, char **argv)
00063 {
00064   ros::init(argc, argv, "youbot_moveit_to_pose", ros::init_options::AnonymousName);
00065   // start a ROS spinning thread
00066   ros::AsyncSpinner spinner(1);
00067   ros::Rate r(10);
00068 
00069   ros::NodeHandle n;
00070 
00071   ros::Publisher cancel_publisher = n.advertise<actionlib_msgs::GoalID>("/arm_1/arm_controller/follow_joint_trajectory/cancel", 1);
00072   ros::Subscriber goal_listener = n.subscribe("/arm_1/arm_controller/follow_joint_trajectory/goal",1,goal_callback);
00073 
00074   tf::TransformListener tf_listener;
00075 
00076   ROS_DEBUG("Starting youbot_moveit_teleop...");
00077   spinner.start();
00078 
00079   ROS_INFO("Connecting to move_group node for youbot arm...");
00080   // this connecs to a running instance of the move_group node
00081   move_group_interface::MoveGroup group("arm");
00082 
00083   ROS_DEBUG("\nSetting starting position to current position...");
00084   group.setStartStateToCurrentState();  //Sets the starting state for the move_group to be the current state of the robot
00085 
00086   if(!group.setNamedTarget(argv[1]))
00087   {
00088     ROS_ERROR("\nInvalid pose name provided.");
00089     return;
00090   }
00091 
00092   //Plan a motion path to the user-provided position
00093   move_group_interface::MoveGroup::Plan p;
00094 
00095   //If position is unreachable or a path is not possible, the user will be prompted to put in new information
00096   if(!group.plan(p))
00097   {
00098     ROS_ERROR("Destination is unreachable!");
00099     return;
00100   }
00101 
00102   trajectory_complete = false;
00103 
00104   goal_string = "0";
00105   //Execute will perform the trajectory.  It is non-blocking.
00106   boost::thread execute_thread(trajectory_execution, &group, &p);
00107 
00108   boost::thread read_cancel(getch_check);
00109 
00110   while(ros::ok())
00111   {
00112     ros::spinOnce();
00113     //Hitting 'c' mid trajectory will cancel the current trajectory
00114     if(cancel == 'c')
00115     {
00116       actionlib_msgs::GoalID g_id;
00117       g_id.id = goal_string;
00118 
00119       cancel_publisher.publish(g_id);
00120       ROS_WARN("Canceled the current trajectory!");
00121       ROS_DEBUG("Setting starting position to current position...");
00122 group.setStartStateToCurrentState();  //Sets the starting state for the move_group to be the current state of the robot
00123       read_cancel.join();
00124       break;
00125     }
00126 
00127     if(trajectory_complete)
00128     {
00129       ROS_INFO("Trajectory completed.");
00130       execute_thread.join();
00131       read_cancel.interrupt();
00132       break;
00133     }
00134   }
00135 }


youbot_teleop
Author(s): Russell Toris , Graylin Trevor Jay
autogenerated on Mon Oct 6 2014 09:08:50