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);
00044 newt = oldt;
00045 newt.c_lflag &= ~(ICANON | ECHO);
00046 tcsetattr(STDIN_FILENO, TCSANOW, &newt);
00047 ch = getchar();
00048 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
00049 return ch;
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
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
00081 move_group_interface::MoveGroup group("arm");
00082
00083 ROS_DEBUG("\nSetting starting position to current position...");
00084 group.setStartStateToCurrentState();
00085
00086 if(!group.setNamedTarget(argv[1]))
00087 {
00088 ROS_ERROR("\nInvalid pose name provided.");
00089 return;
00090 }
00091
00092
00093 move_group_interface::MoveGroup::Plan p;
00094
00095
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
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
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();
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 }