general_io_passthrough_controller.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, 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 
00025 #include "sr_ronex_controllers/general_io_passthrough_controller.hpp"
00026 #include "pluginlib/class_list_macros.h"
00027 #include <string>
00028 
00029 PLUGINLIB_EXPORT_CLASS(ronex::GeneralIOPassthroughController, controller_interface::ControllerBase)
00030 
00031 namespace ronex
00032 {
00033 GeneralIOPassthroughController::GeneralIOPassthroughController()
00034   : loop_count_(0)
00035 {}
00036 
00037 bool GeneralIOPassthroughController::init(ros_ethercat_model::RobotStateInterface* robot, ros::NodeHandle &n)
00038 {
00039   assert(robot);
00040   node_ = n;
00041 
00042   std::string ronex_id;
00043   if (!node_.getParam("ronex_id", ronex_id))
00044   {
00045     ROS_ERROR("No RoNeX ID given (namespace: %s)", node_.getNamespace().c_str());
00046     return false;
00047   }
00048 
00049   // get the path from the parameters
00050   std::string path;
00051   int parameter_id = get_ronex_param_id(ronex_id);
00052   {
00053     if ( parameter_id == -1 )
00054     {
00055       ROS_ERROR_STREAM("Could not find the RoNeX id in the parameter server: " << ronex_id <<
00056                                " not loading the controller.");
00057       return false;
00058     }
00059     else
00060     {
00061       std::stringstream ss;
00062       ss << "/ronex/devices/" << parameter_id << "/path";
00063       if ( !ros::param::get(ss.str(), path) )
00064       {
00065         ROS_ERROR_STREAM("Couldn't read the parameter " << ss.str() <<
00066                                  " from the parameter server. Not loading the controller.");
00067         return false;
00068       }
00069     }
00070   }
00071 
00072   std::string robot_state_name;
00073   node_.param<std::string>("robot_state_name", robot_state_name, "unique_robot_hw");
00074 
00075   try
00076   {
00077     general_io_ = static_cast<ronex::GeneralIO*>(robot->getHandle(robot_state_name).getState()->getCustomHW(path));
00078   }
00079   catch(const hardware_interface::HardwareInterfaceException& e)
00080   {
00081     ROS_ERROR_STREAM("Could not find robot state: " << robot_state_name << " Not loading the controller. " << e.what());
00082     return false;
00083   }
00084 
00085   if ( general_io_ == NULL)
00086   {
00087     ROS_ERROR_STREAM("Could not find RoNeX module: " << ronex_id << " not loading the controller");
00088     return false;
00089   }
00090 
00091   // init the subscribers
00092   std::stringstream sub_topic;
00093   for ( size_t i = 0; i < general_io_->command_.digital_.size(); ++i)
00094   {
00095     sub_topic.str("");
00096     sub_topic << path << "/command/digital/" << i;
00097     digital_subscribers_.push_back(
00098             node_.subscribe<std_msgs::Bool>(sub_topic.str(), 1,
00099                                             boost::bind(&GeneralIOPassthroughController::digital_commands_cb,
00100                                                         this, _1,  i)));
00101   }
00102 
00103   for ( size_t i = 0; i < general_io_->command_.pwm_.size(); ++i)
00104   {
00105     sub_topic.str("");
00106     sub_topic << path << "/command/pwm/" << i;
00107     pwm_subscribers_.push_back(
00108             node_.subscribe<sr_ronex_msgs::PWM>(sub_topic.str(), 1,
00109                                                 boost::bind(&GeneralIOPassthroughController::pwm_commands_cb,
00110                                                             this, _1, i)));
00111   }
00112 
00113   return true;
00114 }
00115 
00116 void GeneralIOPassthroughController::digital_commands_cb(const std_msgs::BoolConstPtr& msg, int index)
00117 {
00118   general_io_->command_.digital_[index] = msg->data;
00119 }
00120 
00121 void GeneralIOPassthroughController::pwm_commands_cb(const sr_ronex_msgs::PWMConstPtr& msg, int index)
00122 {
00123   general_io_->command_.pwm_[index].period = msg->pwm_period;
00124   general_io_->command_.pwm_[index].on_time_0 = msg->pwm_on_time_0;
00125   general_io_->command_.pwm_[index].on_time_1 = msg->pwm_on_time_1;
00126 }
00127 
00128 }  // namespace ronex
00129 
00130 /* For the emacs weenies in the crowd.
00131 Local Variables:
00132    c-basic-offset: 2
00133 End:
00134 */


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