00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef STATE_MACHINE_H_
00018 #define STATE_MACHINE_H_
00019
00020 #include <string>
00021 #include <map>
00022
00023 #include <boost/assign/list_of.hpp>
00024 #include <boost/assign/list_inserter.hpp>
00025 #include <boost/shared_ptr.hpp>
00026
00027 #include <ros/ros.h>
00028 #include <actionlib/client/simple_action_client.h>
00029 #include <actionlib/server/simple_action_server.h>
00030
00031 #include <object_manipulation_msgs/GraspHandPostureExecutionAction.h>
00032 #include <control_msgs/FollowJointTrajectoryAction.h>
00033 #include <arm_navigation_msgs/FilterJointTrajectoryWithConstraints.h>
00034
00035 #include <mtconnect_msgs/CloseChuckAction.h>
00036 #include <mtconnect_msgs/OpenChuckAction.h>
00037 #include <mtconnect_msgs/CloseDoorAction.h>
00038 #include <mtconnect_msgs/OpenDoorAction.h>
00039
00040 #include <mtconnect_msgs/MaterialLoadAction.h>
00041 #include <mtconnect_msgs/MaterialUnloadAction.h>
00042
00043 #include <mtconnect_msgs/RobotSpindle.h>
00044 #include <mtconnect_msgs/RobotStates.h>
00045
00046 #include <mtconnect_example_msgs/StateMachineCmd.h>
00047 #include <mtconnect_example_msgs/StateMachineStatus.h>
00048
00049 #include <mtconnect_msgs/SetMTConnectState.h>
00050
00051 #include <mtconnect_task_parser/task.h>
00052
00053 #include <industrial_msgs/RobotStatus.h>
00054
00055 namespace mtconnect_state_machine
00056 {
00057
00061 namespace StateTypes
00062 {
00063 enum StateType
00064 {
00065 INVALID = 0, IDLE = 1,
00066
00067 INITING = 100,
00068 CHECK_HOME, WAIT_FOR_ACTIONS, WAIT_FOR_SERVICES, SET_MAT_ACTIONS_READY,
00069
00070 CYCLE_BEGIN = 2000,
00071 WAITING = 2001,
00072 MATERIAL_LOADING = 2100,
00073 ML_MOVE_PICK_APPROACH, ML_WAIT_MOVE_PICK_APPROACH, ML_MOVE_PICK,
00074 ML_WAIT_MOVE_PICK, ML_PICK, ML_WAIT_PICK, ML_MOVE_CHUCK, ML_WAIT_MOVE_CHUCK,
00075 ML_CLOSE_CHUCK, ML_WAIT_CLOSE_CHUCK, ML_RELEASE_PART, ML_WAIT_RELEASE_PART,
00076 ML_MOVE_DOOR, ML_WAIT_MOVE_DOOR, ML_MOVE_HOME, ML_WAIT_MOVE_HOME,
00077 MATERIAL_LOADED = 2199,
00078
00079
00080 MATERIAL_UNLOADING = 2200,
00081 MU_MOVE_DOOR, MU_WAIT_MOVE_DOOR, MU_MOVE_CHUCK, MU_WAIT_MOVE_CHUCK, MU_PICK_PART, MU_WAIT_PICK_PART,
00082 MU_OPEN_CHUCK, MU_WAIT_OPEN_CHUCK, MU_MOVE_DROP, MU_WAIT_MOVE_DROP, MU_DROP, MU_WAIT_DROP,
00083 MU_MOVE_HOME, MU_WAIT_MOVE_HOME,
00084 MATERIAL_UNLOADED = 2299,
00085 CYCLE_END = 2999,
00086
00087 STOPPING = 300,
00088 S_SET_MAT_ACTIONS_NOT_READY,
00089 STOPPED = 299,
00090
00091 ABORTING = 500,
00092 ABORT_GOALS, CANCEL_REQUESTS,
00093 ABORTED = 599,
00094
00095 RESETTING = 700,
00096 R_WAIT_FOR_HOME, R_SET_MAT_ACTIONS_NOT_READY,
00097 RESET = 799,
00098
00099 };
00103 static std::map<int, std::string> STATE_MAP =
00104 boost::assign::map_list_of(INVALID, "INVALID")(IDLE, "IDLE")
00105 (INITING, "INITING")
00106 (CHECK_HOME, "CHECK_HOME")
00107 (WAIT_FOR_ACTIONS, "WAIT_FOR_ACTIONS")
00108 (WAIT_FOR_SERVICES, "WAIT_FOR_SERVICES")
00109 (SET_MAT_ACTIONS_READY,"SET_MAT_ACTIONS_READY")
00110 (WAITING, "WAITING")
00111
00112 (MATERIAL_LOADING, "MATERIAL_LOADING")
00113 (ML_MOVE_PICK_APPROACH,"ML_MOVE_PICK_APPROACH")
00114 (ML_WAIT_MOVE_PICK_APPROACH,"ML_WAIT_MOVE_PICK_APPROACH")
00115 (ML_MOVE_PICK,"ML_MOVE_PICK")
00116 (ML_WAIT_MOVE_PICK,"ML_WAIT_MOVE_PICK")
00117 (ML_PICK,"ML_PICK")
00118 (ML_WAIT_PICK,"ML_WAIT_PICK")
00119 (ML_MOVE_CHUCK,"ML_MOVE_CHUCK")
00120 (ML_WAIT_MOVE_CHUCK,"ML_WAIT_MOVE_CHUCK")
00121 (ML_CLOSE_CHUCK,"ML_CLOSE_CHUCK")
00122 (ML_WAIT_CLOSE_CHUCK, "ML_WAIT_CLOSE_CHUCK")
00123 (ML_RELEASE_PART, "ML_RELEASE_PART")
00124 (ML_WAIT_RELEASE_PART, "ML_WAIT_RELEASE_PART")
00125 (ML_MOVE_DOOR, "ML_MOVE_DOOR")
00126 (ML_WAIT_MOVE_DOOR, "ML_WAIT_MOVE_DOOR")
00127 (ML_MOVE_HOME, "ML_MOVE_HOME")
00128 (ML_WAIT_MOVE_HOME, "ML_WAIT_MOVE_HOME")
00129 (MATERIAL_LOADED, "MATERIAL_LOADED")
00130
00131 (MATERIAL_UNLOADING, "MATERIAL_UNLOADING")
00132 (MU_MOVE_DOOR, "MU_MOVE_DOOR")
00133 (MU_WAIT_MOVE_DOOR, "MU_WAIT_MOVE_DOOR")
00134 (MU_MOVE_CHUCK, "MU_MOVE_CHUCK")
00135 (MU_WAIT_MOVE_CHUCK, "MU_WAIT_MOVE_CHUCK")
00136 (MU_PICK_PART, "MU_PICK_PART")
00137 (MU_WAIT_PICK_PART, "MU_WAIT_PICK_PART")
00138 (MU_OPEN_CHUCK, "MU_OPEN_CHUCK")
00139 (MU_WAIT_OPEN_CHUCK, "MU_WAIT_OPEN_CHUCK")
00140 (MU_MOVE_DROP, "MU_MOVE_DROP")
00141 (MU_WAIT_MOVE_DROP, "MU_WAIT_MOVE_DROP")
00142 (MU_DROP, "MU_DROP")
00143 (MU_WAIT_DROP, "MU_WAIT_DROP")
00144 (MU_MOVE_HOME, "MU_MOVE_HOME")
00145 (MU_WAIT_MOVE_HOME, "MU_WAIT_MOVE_HOME")
00146 (MATERIAL_UNLOADED, "MATERIAL_UNLOADED")
00147
00148 (STOPPING, "STOPPING")
00149 (S_SET_MAT_ACTIONS_NOT_READY, "S_SET_MAT_ACTIONS_NOT_READY")
00150 (STOPPED, "STOPPED")
00151
00152 (ABORTING, "ABORTING")
00153 (ABORT_GOALS, "ABORT_GOALS")(CANCEL_REQUESTS, "CANCEL_REQUESTS")
00154 (ABORTED, "ABORTED")
00155
00156 (RESETTING, "RESETTING")
00157 (R_WAIT_FOR_HOME, "R_WAIT_FOR_HOME")(R_SET_MAT_ACTIONS_NOT_READY, "SET_MAT_ACTIONS_NOT_READY")
00158 (RESET, "RESETED");
00159
00160 }
00161 typedef StateTypes::StateType StateType;
00162
00163
00164 typedef actionlib::SimpleActionClient<mtconnect_msgs::OpenDoorAction> CncOpenDoorClient;
00165 typedef actionlib::SimpleActionClient<mtconnect_msgs::CloseDoorAction> CncCloseDoorClient;
00166 typedef actionlib::SimpleActionClient<mtconnect_msgs::OpenChuckAction> CncOpenChuckClient;
00167 typedef actionlib::SimpleActionClient<mtconnect_msgs::CloseChuckAction> CncCloseChuckClient;
00168 typedef actionlib::SimpleActionServer<mtconnect_msgs::MaterialLoadAction> MaterialLoadServer;
00169 typedef actionlib::SimpleActionServer<mtconnect_msgs::MaterialUnloadAction> MaterialUnloadServer;
00170 typedef actionlib::SimpleActionClient<object_manipulation_msgs::GraspHandPostureExecutionAction> GraspActionClient;
00171 typedef actionlib::SimpleActionClient<control_msgs::FollowJointTrajectoryAction> JointTractoryClient;
00172
00173 typedef boost::shared_ptr<CncOpenDoorClient> CncOpenDoorClientPtr;
00174 typedef boost::shared_ptr<CncCloseDoorClient> CncCloseDoorClientPtr;
00175 typedef boost::shared_ptr<CncOpenChuckClient> CncOpenChuckClientPtr;
00176 typedef boost::shared_ptr<CncCloseChuckClient> CncCloseChuckClientPtr;
00177 typedef boost::shared_ptr<MaterialLoadServer> MaterialLoadServerPtr;
00178 typedef boost::shared_ptr<MaterialUnloadServer> MaterialUnloadServerPtr;
00179 typedef boost::shared_ptr<GraspActionClient> GraspActionClientPtr;
00180 typedef boost::shared_ptr<JointTractoryClient> JointTractoryClientPtr;
00181
00182 class StateMachine
00183 {
00184 public:
00185
00187
00191 StateMachine();
00192
00197 ~StateMachine();
00198
00204 bool init();
00205
00212 void run();
00213
00219 void runOnce();
00220
00221 protected:
00222
00224
00225 void setState(StateType state)
00226 {
00227 ROS_INFO_STREAM("Changing state from: " << StateTypes::STATE_MAP[state_] << "(" << state_ << ")"
00228 " to " << StateTypes::STATE_MAP[state] << "(" << state << ")");
00229 state_ = state;
00230 }
00231 ;
00232
00233 StateType getState()
00234 {
00235 return state_;
00236 }
00237 ;
00238
00240
00244 ros::NodeHandle nh_;
00245
00246
00247 void materialLoadGoalCB();
00248 void materialUnloadGoalCB();
00249 void robotStatusCB(const industrial_msgs::RobotStatusConstPtr &msg);
00250 void jointStatesCB(const sensor_msgs::JointStateConstPtr &msg);
00251 bool externalCommandCB(mtconnect_example_msgs::StateMachineCmd::Request &req,
00252 mtconnect_example_msgs::StateMachineCmd::Response &res);
00259 bool areActionsReady();
00260
00267 bool areServicesReady();
00268
00273 void abortActionServers();
00274
00279 void cancelActionClients();
00280
00287 bool setMatActionsReady();
00288
00295 bool setMatActionsNotReady();
00296
00303 bool setMatLoad(int state);
00304 bool setMatUnload(int state);
00305
00306
00307
00308 void callPublishers();
00309 void robotStatusPublisher();
00310 void robotSpindlePublisher();
00311 void stateMachineStatusPublisher();
00312
00313
00314
00315 bool isActionComplete(int action_state);
00316 bool moveArm(const std::string & move_name);
00317 bool isMoveDone();
00318
00319 void openDoor();
00320 bool isDoorOpened();
00321 void closeDoor();
00322 bool isDoorClosed();
00323
00324 void openChuck();
00325 bool isChuckOpened();
00326 void closeChuck();
00327 bool isChuckClosed();
00328
00329 void openGripper();
00330 bool isGripperOpened();
00331 void closeGripper();
00332 bool isGripperClosed();
00333
00334
00335 void errorChecks();
00336
00337
00338 void overrideChecks();
00339
00340
00341 bool isHome();
00342
00343 private:
00344
00346
00350 StateType state_;
00351
00356 int loop_rate_;
00357
00362 bool home_check_;
00363
00368 double home_tol_;
00373 bool cycle_stop_req_;
00374
00379 bool material_state_;
00380
00385 int material_load_state_;
00387
00388 std::map<std::string, trajectory_msgs::JointTrajectoryPtr> joint_paths_;
00389 boost::shared_ptr<mtconnect::JointPoint> home_;
00390
00391
00392 MaterialLoadServerPtr material_load_server_ptr_;
00393 MaterialUnloadServerPtr material_unload_server_ptr_;
00394
00395
00396 CncOpenDoorClientPtr open_door_client_ptr_;
00397 CncCloseDoorClientPtr close_door_client_ptr_;
00398 CncOpenChuckClientPtr open_chuck_client_ptr_;
00399 CncCloseChuckClientPtr close_chuck_client_ptr_;
00400 GraspActionClientPtr grasp_action_client_ptr_;
00401 GraspActionClientPtr vise_action_client_ptr_;
00402 JointTractoryClientPtr joint_traj_client_ptr_;
00403
00404
00405 ros::Publisher robot_states_pub_;
00406 ros::Publisher robot_spindle_pub_;
00407 ros::Publisher state_machine_pub_;
00408
00409
00410 ros::Subscriber robot_status_sub_;
00411 ros::Subscriber joint_states_sub_;
00412
00413
00414 ros::ServiceServer external_command_srv_;
00415
00416
00417 ros::ServiceClient material_load_set_state_client_;
00418 ros::ServiceClient material_unload_set_state_client_;
00419 ros::ServiceClient trajectory_filter_client_;
00420
00421
00422 mtconnect_msgs::RobotStates robot_state_msg_;
00423 mtconnect_msgs::RobotSpindle robot_spindle_msg_;
00424 mtconnect_example_msgs::StateMachineStatus state_machine_stat_msg_;
00425
00426
00427 sensor_msgs::JointState joint_state_msg_;
00428 industrial_msgs::RobotStatus robot_status_msg_;
00429
00430
00431
00432 mtconnect_msgs::SetMTConnectState mat_load_set_state_;
00433 mtconnect_msgs::SetMTConnectState mat_unload_set_state_;
00434 arm_navigation_msgs::FilterJointTrajectoryWithConstraints trajectory_filter_;
00435
00436
00437 control_msgs::FollowJointTrajectoryGoal joint_traj_goal_;
00438
00439 };
00440
00441 }
00442
00443 #endif