DC_motor_small_passthrough_controller.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2016, Shadow Robot Company, All rights reserved.
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 3.0 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library.
00016  */
00017 
00024 #include <sr_ronex_controllers/DC_motor_small_passthrough_controller.h>
00025 #include "sr_ronex_msgs/MotorPacketCommand.h"
00026 #include "pluginlib/class_list_macros.h"
00027 #include <string>
00028 
00029 PLUGINLIB_EXPORT_CLASS(ronex::DCMotorSmallPassthroughController, controller_interface::ControllerBase)
00030 
00031 namespace ronex
00032 {
00033 DCMotorSmallPassthroughController::DCMotorSmallPassthroughController()
00034 :loop_count_(0)
00035 {
00036 }
00037 
00038 bool DCMotorSmallPassthroughController::init(ros_ethercat_model::RobotStateInterface* robot, ros::NodeHandle &n)
00039 {
00040   assert(robot);
00041   node_ = n;
00042 
00043   std::string ronex_id;
00044   if (!node_.getParam("ronex_id", ronex_id))
00045   {
00046     ROS_ERROR("No RoNeX ID given (namespace: %s)", node_.getNamespace().c_str());
00047     return false;
00048   }
00049 
00050   // get the path from the parameters
00051   std::string path;
00052   int parameter_id = get_ronex_param_id(ronex_id);
00053   {
00054     if ( parameter_id == -1 )
00055     {
00056       ROS_ERROR_STREAM("Could not find the RoNeX id in the parameter server: " << ronex_id <<
00057                                " not loading the controller.");
00058       return false;
00059     }
00060     else
00061     {
00062       std::stringstream ss;
00063       ss << "/ronex/devices/" << parameter_id << "/path";
00064       if ( !ros::param::get(ss.str(), path) )
00065       {
00066         ROS_ERROR_STREAM("Couldn't read the parameter " << ss.str() <<
00067                                  " from the parameter server. Not loading the controller.");
00068         return false;
00069       }
00070     }
00071   }
00072 
00073   std::string robot_state_name;
00074   node_.param<std::string>("robot_state_name", robot_state_name, "unique_robot_hw");
00075 
00076   try
00077   {
00078     dc_motor_small_ = static_cast<ronex::DCMotor*>(robot->getHandle(robot_state_name).getState()->getCustomHW(path));
00079   }
00080   catch(const hardware_interface::HardwareInterfaceException& e)
00081   {
00082     ROS_ERROR_STREAM("Could not find robot state: " << robot_state_name << " Not loading the controller. " << e.what());
00083     return false;
00084   }
00085 
00086   if ( dc_motor_small_ == NULL)
00087   {
00088     ROS_ERROR_STREAM("Could not find RoNeX module: " << ronex_id << " not loading the controller");
00089     return false;
00090   }
00091 
00092   // init the subscribers
00093   std::stringstream sub_topic;
00094   for ( size_t i = 0; i < dc_motor_small_->command_.digital_.size(); ++i)
00095   {
00096     sub_topic.str("");
00097     sub_topic << path << "/command/digital/" << i;
00098     digital_subscribers_.push_back(
00099             node_.subscribe<std_msgs::Bool>(sub_topic.str(), 1,
00100                                             boost::bind(&DCMotorSmallPassthroughController::digital_commands_cb,
00101                                                         this, _1,  i)));
00102   }
00103 
00104   for ( size_t i = 0; i < dc_motor_small_->command_.motor_packet_command_.size(); ++i)
00105   {
00106     sub_topic.str("");
00107     sub_topic << path << "/command/motor_packet_command/" << i;
00108     motor_command_subscribers_.push_back(
00109                 node_.subscribe<sr_ronex_msgs::MotorPacketCommand>(sub_topic.str(), 1,
00110                                                 boost::bind(&DCMotorSmallPassthroughController::motor_packet_cb,
00111                                                             this, _1,  i)));
00112   }
00113 
00114   return true;
00115 }
00116 
00117 void DCMotorSmallPassthroughController::digital_commands_cb(const std_msgs::BoolConstPtr& msg, int index)
00118 {
00119   dc_motor_small_->command_.digital_[index] = msg->data;
00120 }
00121 
00122 void DCMotorSmallPassthroughController::motor_packet_cb(const sr_ronex_msgs::MotorPacketCommandConstPtr &msg, int index)
00123 {
00124   dc_motor_small_->command_.motor_packet_command_[index].flags = msg->flags;
00125   dc_motor_small_->command_.motor_packet_command_[index].on_time = msg->onTime;
00126   dc_motor_small_->command_.motor_packet_command_[index].period = msg->period;
00127 }
00128 void DCMotorSmallPassthroughController::stopping(const ros::Time& time)
00129 {
00130   for (size_t index = 0; index < dc_motor_small_->command_.motor_packet_command_.size(); ++index)
00131   {
00132     ROS_INFO_STREAM("Stopping DC motor");
00133     dc_motor_small_->command_.motor_packet_command_[index].flags = 0;
00134     dc_motor_small_->command_.motor_packet_command_[index].on_time = 0;
00135     dc_motor_small_->command_.motor_packet_command_[index].period = 0;
00136   }
00137 }
00138 }  // namespace ronex
00139 
00140 /* For the emacs weenies in the crowd.
00141 Local Variables:
00142    c-basic-offset: 2
00143 End:
00144 */


sr_ronex_controllers
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless
autogenerated on Thu Jun 6 2019 21:22:06