Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "packml_ros/packml_ros.h"
00020 #include "packml_msgs/utils.h"
00021
00022 namespace packml_ros
00023 {
00024
00025
00026 PackmlRos::PackmlRos(ros::NodeHandle nh, ros::NodeHandle pn,
00027 std::shared_ptr<packml_sm::StateMachine> sm) :
00028 nh_(nh), pn_(pn), sm_(sm)
00029 {
00030 ros::NodeHandle packml_node("~/packml");
00031
00032 status_pub_ = packml_node.advertise<packml_msgs::Status>("status", 10, true);
00033 trans_server_ = packml_node.advertiseService("transition", &PackmlRos::transRequest, this);
00034 status_msg_ = packml_msgs::initStatus(pn.getNamespace());
00035
00036 connect(sm.get(), SIGNAL(stateChanged(int, QString)), this, SLOT(pubState(int, QString)));
00037 }
00038
00039 void PackmlRos::spin()
00040 {
00041 while(ros::ok())
00042 {
00043 spinOnce();
00044 ros::Duration(0.001).sleep();
00045 }
00046 return;
00047 }
00048
00049
00050 void PackmlRos::spinOnce()
00051 {
00052 ros::spinOnce();
00053 QCoreApplication::instance()->processEvents();
00054 }
00055
00056
00057 void PackmlRos::pubState(int value, QString name)
00058 {
00059 ROS_DEBUG_STREAM("Publishing state change: " << name.toStdString() << "(" << value << ")");
00060
00061 status_msg_.header.stamp = ros::Time().now();
00062 if( packml_msgs::isStandardState(value) )
00063 {
00064 status_msg_.state.val = value;
00065 status_msg_.sub_state = packml_msgs::State::UNDEFINED;
00066 }
00067 else
00068 {
00069 status_msg_.sub_state = value;
00070 }
00071 status_pub_.publish(status_msg_);
00072
00073 }
00074
00075 bool PackmlRos::transRequest(packml_msgs::Transition::Request &req,
00076 packml_msgs::Transition::Response &res)
00077 {
00078 bool command_rtn = false;
00079 bool command_valid = true;
00080 int command_int = static_cast<int>(req.command);
00081 std::stringstream ss;
00082
00083 ROS_DEBUG_STREAM("Evaluating transition request command: " << command_int);
00084
00085 switch(command_int) {
00086 case req.ABORT:
00087 case req.ESTOP:
00088 command_rtn = sm_->abort();
00089 break;
00090 case req.CLEAR:
00091 command_rtn = sm_->clear();
00092 break;
00093 case req.HOLD:
00094 command_rtn = sm_->hold();
00095 break;
00096 case req.RESET:
00097 command_rtn = sm_->reset();
00098 break;
00099 case req.START:
00100 command_rtn = sm_->start();
00101 break;
00102 case req.STOP:
00103 command_rtn = sm_->stop();
00104 break;
00105 case req.SUSPEND:
00106 command_rtn = sm_->suspend();
00107 break;
00108 case req.UNHOLD:
00109 command_rtn = sm_->unhold();
00110 break;
00111 case req.UNSUSPEND:
00112 command_rtn = sm_->unsuspend();
00113 break;
00114
00115 default:
00116 command_valid = false;
00117 break;
00118
00119 }
00120 if(command_valid)
00121 {
00122 if(command_rtn)
00123 {
00124 ss << "Successful transition request command: " << command_int;
00125 ROS_INFO_STREAM(ss.str());
00126 res.success = true;
00127 res.error_code = res.SUCCESS;
00128 res.message = ss.str();
00129 }
00130 else
00131 {
00132 ss << "Invalid transition request command: " << command_int;
00133 ROS_ERROR_STREAM(ss.str());
00134 res.success = false;
00135 res.error_code = res.INVALID_TRANSITION_REQUEST;
00136 res.message = ss.str();
00137 }
00138 }
00139 else
00140 {
00141 ss << "Unrecognized transition request command: " << command_int;
00142 ROS_ERROR_STREAM(ss.str());
00143 res.success = false;
00144 res.error_code = res.UNRECGONIZED_REQUEST;
00145 res.message = ss.str();
00146 }
00147
00148 }
00149
00150 }