packml_ros.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (Apache License)
00003  *
00004  * Copyright (c) 2017 Shaun Edwards
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  * http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
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 } // namespace kitsune_robot


packml_ros
Author(s): Shaun Edwards
autogenerated on Sat Jun 8 2019 20:13:36