00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00024 #include <sr_ronex_drivers/sr_tcat.hpp>
00025
00026 #include <sstream>
00027 #include <iomanip>
00028 #include <boost/foreach.hpp>
00029 #include <math.h>
00030
00031 #include "sr_ronex_drivers/ronex_utils.hpp"
00032
00033 PLUGINLIB_EXPORT_CLASS(SrTCAT, EthercatDevice);
00034
00035 const std::string SrTCAT::product_alias_ = "tcat";
00036
00037 SrTCAT::SrTCAT() :
00038 node_("~"), previous_sequence_number_(0)
00039 {}
00040
00041 SrTCAT::~SrTCAT()
00042 {
00043
00044 std::stringstream param_path;
00045 param_path << "/ronex/devices/" << parameter_id_ ;
00046 ros::param::del(param_path.str());
00047 }
00048
00049 void SrTCAT::construct(EtherCAT_SlaveHandler *sh, int &start_address)
00050 {
00051 sh_ = sh;
00052 serial_number_ = ronex::get_serial_number( sh );
00053
00054
00055 std::string path_to_alias, alias;
00056 path_to_alias = "/ronex/mapping/" + serial_number_;
00057 if( ros::param::get(path_to_alias, alias))
00058 {
00059 ronex_id_ = alias;
00060 }
00061 else
00062 {
00063
00064 ronex_id_ = serial_number_ ;
00065 }
00066
00067 device_name_ = ronex::build_name( product_alias_, ronex_id_ );
00068
00069 command_base_ = start_address;
00070 command_size_ = COMMAND_ARRAY_SIZE_BYTES;
00071
00072 start_address += command_size_;
00073 status_base_ = start_address;
00074 status_size_ = STATUS_ARRAY_SIZE_BYTES;
00075
00076 start_address += status_size_;
00077
00078
00079
00080
00081
00082
00083 if ( (PROTOCOL_TYPE) == (EC_BUFFERED) )
00084 {
00085 ROS_INFO("Using EC_BUFFERED");
00086 }
00087 else if ( (PROTOCOL_TYPE) == (EC_QUEUED) )
00088 {
00089 ROS_INFO("Using EC_QUEUED");
00090 }
00091
00092 ROS_INFO("First FMMU (command) : Logical address: 0x%08X ; size: %3d bytes ; ET1200 address: 0x%08X", command_base_, command_size_,
00093 static_cast<int>(COMMAND_ADDRESS) );
00094 EC_FMMU *commandFMMU = new EC_FMMU( command_base_,
00095 command_size_,
00096 0x00,
00097 0x07,
00098 COMMAND_ADDRESS,
00099 0x00,
00100 false,
00101 true,
00102 true
00103 );
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 ROS_INFO("Second FMMU (status) : Logical address: 0x%08X ; size: %3d bytes ; ET1200 address: 0x%08X", status_base_, status_size_,
00116 static_cast<int>(STATUS_ADDRESS) );
00117 EC_FMMU *statusFMMU = new EC_FMMU( status_base_,
00118 status_size_,
00119 0x00,
00120 0x07,
00121 STATUS_ADDRESS,
00122 0x00,
00123 true,
00124 false,
00125 true);
00126
00127
00128 EtherCAT_FMMU_Config *fmmu = new EtherCAT_FMMU_Config(2);
00129
00130 (*fmmu)[0] = *commandFMMU;
00131 (*fmmu)[1] = *statusFMMU;
00132
00133 sh->set_fmmu_config(fmmu);
00134
00135 EtherCAT_PD_Config *pd = new EtherCAT_PD_Config(2);
00136
00137
00138 (*pd)[0] = EC_SyncMan(COMMAND_ADDRESS, command_size_, PROTOCOL_TYPE, EC_WRITTEN_FROM_MASTER);
00139 (*pd)[1] = EC_SyncMan(STATUS_ADDRESS, status_size_, PROTOCOL_TYPE);
00140
00141
00142 (*pd)[0].ChannelEnable = true;
00143 (*pd)[0].ALEventEnable = true;
00144 (*pd)[0].WriteEvent = true;
00145
00146 (*pd)[1].ChannelEnable = true;
00147
00148 sh->set_pd_config(pd);
00149
00150 ROS_INFO("status_size_ : %d ; command_size_ : %d", status_size_, command_size_);
00151
00152 ROS_INFO("Finished constructing the SrTCAT driver");
00153 }
00154
00155 int SrTCAT::initialize(hardware_interface::HardwareInterface *hw, bool allow_unprogrammed)
00156 {
00157 ROS_INFO("Device #%02d: Product code: %u (%#010X) , Serial #: %u (%#010X)",
00158 sh_->get_ring_position(),
00159 sh_->get_product_code(),
00160 sh_->get_product_code(),
00161 sh_->get_serial(),
00162 sh_->get_serial());
00163
00164 device_offset_ = sh_->get_ring_position();
00165
00166 build_topics_();
00167
00168 ROS_INFO_STREAM("Adding a "<< product_alias_ <<" RoNeX module to the hardware interface: " << device_name_);
00169
00170
00171 return 0;
00172 }
00173
00174 void SrTCAT::packCommand(unsigned char *buffer, bool halt, bool reset)
00175 {
00176 RONEX_COMMAND_02000003* command = (RONEX_COMMAND_02000003*)(buffer);
00177
00178 command->command_type = RONEX_COMMAND_02000003_COMMAND_TYPE_NORMAL;
00179 }
00180
00181 bool SrTCAT::unpackState(unsigned char *this_buffer, unsigned char *prev_buffer)
00182 {
00183 RONEX_STATUS_02000003* status_data = (RONEX_STATUS_02000003 *)(this_buffer+ command_size_);
00184 u_int64_t timestamp_64;
00185
00186
00187
00188
00189
00190
00191 ROS_DEBUG_STREAM("-----\nNEW unpack");
00192 ROS_DEBUG_STREAM(" command type: " << status_data->command_type << " ("<< RONEX_COMMAND_02000003_COMMAND_TYPE_NORMAL <<")");
00193 ROS_DEBUG_STREAM(" receiver number: " << status_data->receiver_number);
00194 ROS_DEBUG_STREAM(" seq number: " << status_data->sequence_number << " ("<<previous_sequence_number_<<")");
00195
00196 if( status_data->command_type == RONEX_COMMAND_02000003_COMMAND_TYPE_NORMAL)
00197 {
00198
00199 if( status_data->receiver_number != -1 )
00200 {
00201 state_msg_.received_data[status_data->receiver_number].data_received = true;
00202
00203 state_msg_.sequence_number = status_data->sequence_number;
00204
00205 state_msg_.received_data[status_data->receiver_number].reserved.resize(NUM_RESERVED_WORDS);
00206 for(size_t i=0 ; i<NUM_RESERVED_WORDS; ++i)
00207 state_msg_.received_data[status_data->receiver_number].reserved[i] = status_data->receiver_data.reserved[i];
00208
00209 state_msg_.received_data[status_data->receiver_number].impulse_response.resize(IMPULSE_RESPONSE_SIZE);
00210 for(size_t i=0 ; i<IMPULSE_RESPONSE_SIZE; ++i)
00211 {
00212 state_msg_.received_data[status_data->receiver_number].impulse_response[i].real = status_data->receiver_data.impulse_response[i].real;
00213 state_msg_.received_data[status_data->receiver_number].impulse_response[i].imaginary = status_data->receiver_data.impulse_response[i].imaginary;
00214 }
00215
00216 state_msg_.received_data[status_data->receiver_number].first_sample_number = status_data->receiver_data.first_sample_number;
00217
00218 state_msg_.received_data[status_data->receiver_number].payload.resize(PAYLOAD_MAX_SIZE);
00219 for(size_t i=0 ; i<PAYLOAD_MAX_SIZE; ++i)
00220 state_msg_.received_data[status_data->receiver_number].payload[i] = status_data->receiver_data.payload[i];
00221
00222 state_msg_.received_data[status_data->receiver_number].rx_frame_information = status_data->receiver_data.rx_frame_information;
00223 state_msg_.received_data[status_data->receiver_number].std_noise = status_data->receiver_data.std_noise;
00224 state_msg_.received_data[status_data->receiver_number].flags = status_data->receiver_data.flags;
00225 state_msg_.received_data[status_data->receiver_number].FPI = FPI_FIXED_POINT_TO_FLOAT(status_data->receiver_data.FPI);
00226
00227 timestamp_64 = status_data->receiver_data.timestamp_H;
00228 timestamp_64 <<= 32;
00229 timestamp_64 += status_data->receiver_data.timestamp_L;
00230
00231 state_msg_.received_data[status_data->receiver_number].timestamp_ns = static_cast<double>(timestamp_64) * (15.65/1000.0);
00232
00233 ROS_DEBUG_STREAM(" timestamp: " << state_msg_.received_data[status_data->receiver_number].timestamp_ns);
00234
00235
00236
00237 }
00238 }
00239
00240
00241 if ( status_data->sequence_number &&
00242 (status_data->sequence_number != previous_sequence_number_ )
00243 )
00244 {
00245 state_msg_.header.stamp = ros::Time::now();
00246
00247
00248 if( state_publisher_->trylock() )
00249 {
00250 state_publisher_->msg_ = state_msg_;
00251 state_publisher_->unlockAndPublish();
00252 }
00253
00254
00255 for(size_t i=0; i<NUM_RECEIVERS; ++i)
00256 state_msg_.received_data[i].data_received = false;
00257
00258 previous_sequence_number_ = status_data->sequence_number;
00259 }
00260
00261 return true;
00262 }
00263
00264 void SrTCAT::diagnostics(diagnostic_updater::DiagnosticStatusWrapper &d, unsigned char *buffer)
00265 {
00266 d.name = device_name_;
00267 d.summary(d.OK, "OK");
00268 d.hardware_id = serial_number_;
00269
00270 d.clear();
00271 }
00272
00273 void SrTCAT::build_topics_()
00274 {
00275
00276 parameter_id_ = ronex::get_ronex_param_id("");
00277 std::stringstream param_path, tmp_param;
00278 param_path << "/ronex/devices/" << parameter_id_ << "/";
00279 tmp_param << ronex::get_product_code(sh_);
00280 ros::param::set(param_path.str() + "product_id", tmp_param.str());
00281 ros::param::set(param_path.str() + "product_name", product_alias_);
00282 ros::param::set(param_path.str() + "ronex_id", ronex_id_);
00283
00284
00285 ros::param::set(param_path.str() + "path", device_name_);
00286 ros::param::set(param_path.str() + "serial", serial_number_);
00287
00288
00289 state_publisher_.reset(new realtime_tools::RealtimePublisher<sr_ronex_msgs::TCATState>(node_, device_name_ + "/state", 1));
00290 }
00291
00292
00293
00294
00295
00296