00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00028 #include "sr_ronex_drivers/cod_decod/cod_decod_manager.hpp"
00029 #include "sr_ronex_drivers/cod_decod/cod_decod_std_io.hpp"
00030 #include <boost/foreach.hpp>
00031 #include <boost/regex.hpp>
00032 #include <string>
00033 #include <vector>
00034
00035 namespace sr_cod_decod
00036 {
00037
00038 CodDecodManager::CodDecodManager(hardware_interface::HardwareInterface *hw, EtherCAT_SlaveHandler *sh,
00039 int n_digital_outputs, int n_analog_outputs, int n_digital_inputs, int n_analog_inputs, int n_PWM_outputs)
00040 :cod_decod_loader_("sr_ronex_drivers", "sr_cod_decod::CodDecod")
00041 {
00042 uint32_t product_code = sh->get_product_code();
00043 uint32_t serial = sh->get_serial();
00044 uint32_t revision = sh->get_revision();
00045 uint32_t slave = sh->get_station_address()-1;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 stringstream class_name_regex_str;
00063 class_name_regex_str << "(.*/)?" << product_code << "_" << serial;
00064 boost::regex class_name_regex(class_name_regex_str.str(), boost::regex::extended);
00065
00066 std::vector<std::string> classes = cod_decod_loader_.getDeclaredClasses();
00067 std::string matching_class_name;
00068
00069 BOOST_FOREACH(const std::string &class_name, classes)
00070 {
00071 if (regex_match(class_name, class_name_regex))
00072 {
00073 if (matching_class_name.size() != 0)
00074 {
00075 ROS_ERROR("Found more than 1 CodDecod class for device with product code : %u (0x%X) and serial number : "
00076 "%u (0x%X)", product_code, product_code, serial, serial);
00077 ROS_ERROR("First class name = '%s'. Second class name = '%s'",
00078 class_name.c_str(), matching_class_name.c_str());
00079 }
00080 matching_class_name = class_name;
00081 }
00082 }
00083
00084 if (matching_class_name.size() != 0)
00085 {
00086
00087
00088 try
00089 {
00090 cod_decod_ = cod_decod_loader_.createInstance(matching_class_name);
00091 }
00092 catch (pluginlib::LibraryLoadException &e)
00093 {
00094 cod_decod_.reset();
00095 ROS_FATAL("Unable to load CodDecod plugin for slave #%d, product code: %u (0x%X), serial: %u (0x%X), "
00096 "revision: %d (0x%X)",
00097 slave, product_code, product_code, serial, serial, revision, revision);
00098 ROS_FATAL("%s", e.what());
00099 }
00100 }
00101 else
00102 {
00103 ROS_INFO("Unable to find a dedicated CodDecod plugin for slave #%d, product code: %u (0x%X), serial: %u (0x%X), "
00104 "revision: %d (0x%X)",
00105 slave, product_code, product_code, serial, serial, revision, revision);
00106 ROS_INFO("Possible classes:");
00107 BOOST_FOREACH(const std::string &class_name, classes)
00108 {
00109 ROS_INFO(" %s", class_name.c_str());
00110 }
00111 ROS_INFO("Loading the default CodDecod plugin: CodDecodStdIo");
00112 try
00113 {
00114 cod_decod_ = cod_decod_loader_.createInstance("sr_ronex_drivers/87032868_0");
00115 }
00116 catch (pluginlib::LibraryLoadException &e)
00117 {
00118 cod_decod_.reset();
00119 ROS_FATAL("Unable to load CodDecod plugin for slave #%d, product code: %u (0x%X), serial: %u (0x%X), "
00120 "revision: %d (0x%X)",
00121 slave, product_code, product_code, serial, serial, revision, revision);
00122 ROS_FATAL("%s", e.what());
00123 }
00124 }
00125
00126 if (cod_decod_ != NULL)
00127 {
00128 cod_decod_->construct(hw, sh, n_digital_outputs, n_analog_outputs, n_digital_inputs, n_analog_inputs,
00129 n_PWM_outputs);
00130 }
00131 }
00132
00133 void CodDecodManager::update(unsigned char *status_buffer)
00134 {
00135 if (cod_decod_)
00136 {
00137 cod_decod_->update(status_buffer);
00138 }
00139 }
00140
00141 void CodDecodManager::build_command(unsigned char *command_buffer)
00142 {
00143 if (cod_decod_)
00144 {
00145 cod_decod_->build_command(command_buffer);
00146 }
00147 }
00148
00149 }