sr_tcat.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 
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   //remove parameters from server
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   //get the alias from the parameter server if it exists
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     //no alias found, using the serial number directly.
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   // ETHERCAT_COMMAND_DATA
00079   //
00080   // This is for data going TO the board
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_,            // Logical Start Address    (in ROS address space?)
00095                                       command_size_,
00096                                       0x00,                   // Logical Start Bit
00097                                       0x07,                   // Logical End Bit
00098                                       COMMAND_ADDRESS,        // Physical Start Address   (in ET1200 address space?)
00099                                       0x00,                   // Physical Start Bit
00100                                       false,                   // Read Enable
00101                                       true,                    // Write Enable
00102                                       true                     // Channel Enable
00103     );
00104 
00105 
00106   // WARNING!!!
00107   // We are leaving (command_size_ * 4) bytes in the physical memory of the device, but strictly we only need to
00108   // leave (command_size_ * 3). This change should be done in the firmware as well, otherwise it won't work.
00109   // This triple buffer is needed in the ethercat devices to work in EC_BUFFERED mode (in opposition to the other mode EC_QUEUED, the so called mailbox mode)
00110 
00111   // ETHERCAT_STATUS_DATA
00112   //
00113   // This is for data coming FROM the board
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 // SyncMan takes the physical address
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();// - hand_->getBridgeRingPosition();
00165 
00166   build_topics_();
00167 
00168   ROS_INFO_STREAM("Adding a "<< product_alias_ <<" RoNeX module to the hardware interface: " << device_name_);
00169   //Using the name of the ronex to prefix the state topic
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   // Checking that the received command type matches RONEX_COMMAND_02000003_COMMAND_TYPE_NORMAL
00187   // (the one that was used in the command). The ronex firmware will answer with whatever command type we send.
00188   // The purpose of this is to filter those status_data structures that come filled with zeros due to the jitter
00189   // in the realtime loop. The jitter causes that the host tries to read the status when the microcontroller in the ronex
00190   // module has not finished writing it to memory yet.
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     //ignore if receiver_number = -1 (data is not filled in)
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       //fill in the state message with the new data.
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       //printf("0x%04x%08x = %f\n", status_data->receiver_data.timestamp_H, status_data->receiver_data.timestamp_L, state_msg_.received_data[status_data->receiver_number].timestamp_ns);
00236       //state_msg_.received_data[status_data->receiver_number].timestamp_ns = static_cast<double>(status_data->receiver_data.timestamp_L + (static_cast<u_int64_t>(status_data->receiver_data.timestamp_H) << 32)*(15.65/1000.0));
00237     }
00238   }
00239   
00240   //publishing  if the sequence number is increased
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       //publish the message
00248       if( state_publisher_->trylock() )
00249       {
00250         state_publisher_->msg_ = state_msg_;
00251         state_publisher_->unlockAndPublish();
00252       }
00253       
00254       //reset the data received flags to false
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   //loading everything into the parameter server
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   //the device is stored using path as the key in the CustomHW map
00285   ros::param::set(param_path.str() + "path", device_name_);
00286   ros::param::set(param_path.str() + "serial", serial_number_);
00287 
00288   //Advertising the realtime state publisher
00289   state_publisher_.reset(new realtime_tools::RealtimePublisher<sr_ronex_msgs::TCATState>(node_, device_name_ + "/state", 1));
00290 }
00291 
00292 /* For the emacs weenies in the crowd.
00293    Local Variables:
00294    c-basic-offset: 2
00295    End:
00296 */


sr_ronex_drivers
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless
autogenerated on Fri Aug 28 2015 13:12:23