arm_trajectory_demo.cpp
Go to the documentation of this file.
00001 //
00002 // Created by tom on 22/11/16.
00003 //
00004 
00005 #include <ros/ros.h>
00006 #include <control_msgs/GripperCommandAction.h>
00007 #include <actionlib/client/simple_action_client.h>
00008 #include <control_msgs/FollowJointTrajectoryAction.h>
00009 
00010 typedef actionlib::SimpleActionClient<control_msgs::FollowJointTrajectoryAction> ArmClient;
00011 typedef actionlib::SimpleActionClient<control_msgs::GripperCommandAction> GripperClient;
00012 
00013 void activeCb();
00014 void feedbackCb(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback);
00015 void doneCb(const actionlib::SimpleClientGoalState& state, const control_msgs::FollowJointTrajectoryResultConstPtr& result);
00016 
00017 void buildGoal(control_msgs::FollowJointTrajectoryGoal &goal);
00018 
00019 class ArmThrowNode {
00020 private:
00021     ros::NodeHandle _nodeHandle;
00022     ros::AsyncSpinner _spinner;
00023     ros::Subscriber _feedbackListener;
00024     ArmClient _armClient;
00025     GripperClient _gripperClient;
00026     bool _needToOpen;
00027     bool _alreadyOpen;
00028     void doneCb(const actionlib::SimpleClientGoalState& state, const control_msgs::FollowJointTrajectoryResultConstPtr& result) {
00029         ROS_INFO("Finish in state [%s]", state.toString().c_str());
00030         _needToOpen = !_needToOpen;
00031     }
00032     void feedbackCb(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback) {
00033         if((-M_PI / 4)  < feedback->actual.positions[1] && _needToOpen && !_alreadyOpen) {
00034             _alreadyOpen = true;
00035             ROS_INFO("OPEN %f", feedback->actual.positions[1]);
00036             openGripper();
00037         }
00038     }
00039     void activeCb() {
00040         ROS_INFO("Arm is moving");
00041     }
00042 
00043     bool setGripperCmd(double pos, double effort = 0) {
00044         control_msgs::GripperCommandGoal goal;
00045         goal.command.position = pos;
00046         goal.command.max_effort = effort;
00047 
00048         _gripperClient.sendGoal(goal);
00049         _gripperClient.waitForResult();
00050 
00051         return _gripperClient.getState() == actionlib::SimpleClientGoalState::SUCCEEDED;
00052 
00053     }
00054 
00055     control_msgs::FollowJointTrajectoryGoal setGoalTja(double pos) {
00056         control_msgs::FollowJointTrajectoryGoal goal;
00057         trajectory_msgs::JointTrajectoryPoint point;
00058 
00059         goal.trajectory.header.frame_id = "/map";
00060         goal.trajectory.header.stamp = ros::Time::now();
00061         goal.trajectory.joint_names.push_back("rotation1_joint");
00062         goal.trajectory.joint_names.push_back("shoulder1_joint");
00063         goal.trajectory.joint_names.push_back("shoulder2_joint");
00064         goal.trajectory.joint_names.push_back("rotation2_joint");
00065         goal.trajectory.joint_names.push_back("shoulder3_joint");
00066         goal.trajectory.joint_names.push_back("wrist_joint");
00067 
00068         point.time_from_start = ros::Duration(2.0);
00069         point.positions.resize(goal.trajectory.joint_names.size());
00070         point.positions[1] = pos;
00071         goal.trajectory.points.push_back(point);
00072         return goal;
00073     }
00074 
00075     bool setArmCmd(double pos) {
00076         _armClient.sendGoal(setGoalTja(pos),
00077                             boost::bind(&ArmThrowNode::doneCb, this, _1, _2),
00078                             boost::bind(&ArmThrowNode::activeCb, this),
00079                             ArmClient::SimpleFeedbackCallback());
00080         _armClient.waitForResult();
00081         return _armClient.getState() == actionlib::SimpleClientGoalState::SUCCEEDED;
00082     }
00083 
00084 public:
00085     ArmThrowNode() : _nodeHandle(), _spinner(1),_armClient( "arm_trajectory_controller/follow_joint_trajectory"), _gripperClient("gripper_controller/gripper_cmd") {
00086         _feedbackListener = _nodeHandle.subscribe("arm_trajectory_controller/state", 10, &ArmThrowNode::feedbackCb, this);
00087 
00088         _spinner.start();
00089         _armClient.waitForServer();
00090         _gripperClient.waitForServer();
00091 
00092         _needToOpen = false;
00093         _alreadyOpen = false;
00094 
00095 
00096     }
00097 
00098     void run() {
00099         char choice;
00100         do {
00101             std::cout << "Please type 'g' to execute a throw command or 'q' to quit: ";
00102             std::cin >> choice;
00103             if(choice == 'g') throwProcess();
00104             else if(choice == 'q') ROS_INFO("bye bye");
00105             else ROS_WARN("Unknown syntax");
00106 
00107 
00108         } while (ros::ok() && choice != 'q');
00109         ros::shutdown();
00110     }
00111 
00112     void throwProcess() {
00113         _alreadyOpen = false;
00114         openGripper();
00115         preThrowCmd();
00116         ros::Duration(10.0).sleep();
00117         closeGripper();
00118         ros::Duration(3.0).sleep();
00119         throwCmd();
00120 
00121     }
00122 
00123     void openGripper() {
00124         setGripperCmd(0.14);
00125     }
00126 
00127     void closeGripper() {
00128         setGripperCmd(0.01, 0.3);
00129     }
00130 
00131 
00132     void preThrowCmd() {
00133         setArmCmd(-1.30);
00134     }
00135 
00136     void throwCmd() {
00137         setArmCmd(1.0);
00138     }
00139 
00140 };
00141 
00142 
00143 int main(int argc, char** argv) {
00144     ros::init(argc, argv, "arm_trajectory_demo_node");
00145     ArmThrowNode armThrowNode;
00146     armThrowNode.run();
00147 
00148     //armClient.sendGoal(goal, &doneCb, &activeCb, ArmClient::SimpleFeedbackCallback());
00149 
00150     return 0;
00151 }
00152 
00153 void buildGoal(control_msgs::FollowJointTrajectoryGoal &goal) {
00154     trajectory_msgs::JointTrajectoryPoint final;
00155 
00156     goal.trajectory.header.frame_id = "/map";
00157     goal.trajectory.header.stamp = ros::Time::now();
00158     goal.trajectory.joint_names.push_back("rotation1_joint");
00159     goal.trajectory.joint_names.push_back("shoulder1_joint");
00160     goal.trajectory.joint_names.push_back("shoulder2_joint");
00161     goal.trajectory.joint_names.push_back("rotation2_joint");
00162     goal.trajectory.joint_names.push_back("shoulder3_joint");
00163     goal.trajectory.joint_names.push_back("wrist_joint");
00164 
00165     final.time_from_start = ros::Duration(10.0);
00166     final.positions.resize(goal.trajectory.joint_names.size());
00167     final.positions[1] = 1.57;
00168     goal.trajectory.points.push_back(final);
00169 
00170 }
00171 
00172 void doneCb(const actionlib::SimpleClientGoalState& state, const control_msgs::FollowJointTrajectoryResultConstPtr& result) {
00173     ROS_INFO("Finish in state [%s]", state.toString().c_str());
00174 //    ROS_INFO_STREAM((*result));
00175     ros::shutdown();
00176 }
00177 
00178 void feedbackCb(const control_msgs::FollowJointTrajectoryFeedbackConstPtr& feedback) {
00179     //ROS_INFO("%f", feedback->actual.positions[1]);
00180     static bool isOpen = false;
00181    if((-M_PI / 4)  < feedback->actual.positions[1] && !isOpen) {
00182        isOpen = true;
00183        ROS_INFO("OPEN %f", feedback->actual.positions[1]);
00184        GripperClient gripperClient("gripper_controller/gripper_cmd", true);
00185        gripperClient.waitForServer();
00186        control_msgs::GripperCommandGoal openGoal;
00187        openGoal.command.position = 0.14;
00188        gripperClient.sendGoal(openGoal);
00189        ROS_INFO("SEND TO GRIPPER");
00190        gripperClient.waitForResult();
00191        ROS_INFO("GRIPPER IS OPEN");
00192 
00193    }
00194 }
00195 
00196 void activeCb() {
00197     ROS_INFO("Arm is moving");
00198 }


robotican_demos
Author(s):
autogenerated on Fri Oct 27 2017 03:02:45