motion_ctrl.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2013, Southwest Research Institute
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *  * Redistributions of source code must retain the above copyright
00011  *  notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above copyright
00013  *  notice, this list of conditions and the following disclaimer in the
00014  *  documentation and/or other materials provided with the distribution.
00015  *  * Neither the name of the Southwest Research Institute, nor the names
00016  *  of its contributors may be used to endorse or promote products derived
00017  *  from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include "motoman_driver/motion_ctrl.h"
00033 #include "motoman_driver/simple_message/motoman_motion_ctrl_message.h"
00034 #include "motoman_driver/simple_message/motoman_motion_reply_message.h"
00035 #include "ros/ros.h"
00036 #include "simple_message/simple_message.h"
00037 #include <string>
00038 
00039 namespace MotionControlCmds = motoman::simple_message::motion_ctrl::MotionControlCmds;
00040 namespace MotionReplyResults = motoman::simple_message::motion_reply::MotionReplyResults;
00041 using motoman::simple_message::motion_ctrl::MotionCtrl;
00042 using motoman::simple_message::motion_ctrl_message::MotionCtrlMessage;
00043 using motoman::simple_message::motion_reply_message::MotionReplyMessage;
00044 using industrial::simple_message::SimpleMessage;
00045 
00046 namespace motoman
00047 {
00048 namespace motion_ctrl
00049 {
00050 
00051 bool MotomanMotionCtrl::init(SmplMsgConnection* connection, int robot_id)
00052 {
00053   connection_ = connection;
00054   robot_id_ = robot_id;
00055   return true;
00056 }
00057 
00058 bool MotomanMotionCtrl::controllerReady()
00059 {
00060   std::string err_str;
00061   MotionReply reply;
00062 
00063   if (!sendAndReceive(MotionControlCmds::CHECK_MOTION_READY, reply))
00064   {
00065     ROS_ERROR("Failed to send CHECK_MOTION_READY command");
00066     return false;
00067   }
00068 
00069   return (reply.getResult() == MotionReplyResults::TRUE);
00070 }
00071 
00072 
00073 bool MotomanMotionCtrl::setTrajMode(bool enable)
00074 {
00075   MotionReply reply;
00076   MotionControlCmd cmd = enable ? MotionControlCmds::START_TRAJ_MODE : MotionControlCmds::STOP_TRAJ_MODE;
00077 
00078   if (!sendAndReceive(cmd, reply))
00079   {
00080     ROS_ERROR("Failed to send TRAJ_MODE command");
00081     return false;
00082   }
00083 
00084   if (reply.getResult() != MotionReplyResults::SUCCESS)
00085   {
00086     ROS_ERROR_STREAM("Failed to set TrajectoryMode: " << getErrorString(reply));
00087     return false;
00088   }
00089 
00090   return true;
00091 }
00092 
00093 bool MotomanMotionCtrl::stopTrajectory()
00094 {
00095   MotionReply reply;
00096 
00097   if (!sendAndReceive(MotionControlCmds::STOP_MOTION, reply))
00098   {
00099     ROS_ERROR("Failed to send STOP_MOTION command");
00100     return false;
00101   }
00102 
00103   if (reply.getResult() != MotionReplyResults::SUCCESS)
00104   {
00105     ROS_ERROR_STREAM("Failed to Stop Motion: " << getErrorString(reply));
00106     return false;
00107   }
00108 
00109   return true;
00110 }
00111 
00112 bool MotomanMotionCtrl::sendAndReceive(MotionControlCmd command, MotionReply &reply)
00113 {
00114   SimpleMessage req, res;
00115   MotionCtrl data;
00116   MotionCtrlMessage ctrl_msg;
00117   MotionReplyMessage ctrl_reply;
00118 
00119   data.init(robot_id_, 0, command, 0);
00120   ctrl_msg.init(data);
00121   ctrl_msg.toRequest(req);
00122 
00123   if (!this->connection_->sendAndReceiveMsg(req, res))
00124   {
00125     ROS_ERROR("Failed to send MotionCtrl message");
00126     return false;
00127   }
00128 
00129   ctrl_reply.init(res);
00130   reply.copyFrom(ctrl_reply.reply_);
00131 
00132   return true;
00133 }
00134 
00135 std::string MotomanMotionCtrl::getErrorString(const MotionReply &reply)
00136 {
00137   std::ostringstream ss;
00138   ss << reply.getResultString() << " (" << reply.getResult() << ")";
00139   ss << " : ";
00140   ss << reply.getSubcodeString() << " (" << reply.getSubcode() << ")";
00141   return ss.str();
00142 }
00143 
00144 
00145 }  // namespace motion_ctrl
00146 }  // namespace motoman
00147 


motoman_driver
Author(s): Jeremy Zoss (Southwest Research Institute)
autogenerated on Sat Jun 8 2019 19:06:57