UBI0.cpp
Go to the documentation of this file.
00001 
00028 #include "sr_robot_lib/UBI0.hpp"
00029 #include <sr_utilities/sr_math_utils.hpp>
00030 #include <string>
00031 #include <vector>
00032 
00033 #define TACTILE_DATA_LENGTH_BYTES TACTILE_DATA_LENGTH_BYTES_v2
00034 
00035 namespace tactiles
00036 {
00037   template<class StatusType, class CommandType>
00038   UBI0<StatusType, CommandType>::UBI0(ros::NodeHandle nh, std::string device_id,
00039                                       std::vector<generic_updater::UpdateConfig> update_configs_vector,
00040                                       operation_mode::device_update_state::DeviceUpdateState update_state)
00041           : GenericTactiles<StatusType, CommandType>(nh, device_id, update_configs_vector, update_state)
00042   {
00043     init(update_configs_vector, update_state);
00044   }
00045 
00046   template<class StatusType, class CommandType>
00047   UBI0<StatusType, CommandType>::UBI0(ros::NodeHandle nh, std::string device_id,
00048                                       std::vector<generic_updater::UpdateConfig> update_configs_vector,
00049                                       operation_mode::device_update_state::DeviceUpdateState update_state,
00050                                       boost::shared_ptr<std::vector<GenericTactileData> > init_tactiles_vector)
00051           : GenericTactiles<StatusType, CommandType>(nh, device_id, update_configs_vector, update_state)
00052   {
00053     init(update_configs_vector, update_state);
00054     tactiles_vector->clear();
00055     for (unsigned int i = 0; i < this->nb_tactiles; i++)
00056     {
00057       UBI0Data tmp_pst(init_tactiles_vector->at(i));
00058       tactiles_vector->push_back(tmp_pst);
00059     }
00060   }
00061 
00062   template<class StatusType, class CommandType>
00063   void UBI0<StatusType, CommandType>::init(std::vector<generic_updater::UpdateConfig> update_configs_vector,
00064                                            operation_mode::device_update_state::DeviceUpdateState update_state)
00065   {
00066     // Auxiliar Spi data (sometimes it is a palm tactile sensor) real time publisher
00067     aux_spi_publisher = boost::shared_ptr<realtime_tools::RealtimePublisher<sr_robot_msgs::AuxSpiData> >(
00068             new realtime_tools::RealtimePublisher<sr_robot_msgs::AuxSpiData>(this->nodehandle_, "tactile_aux_spi", 4));
00069 
00070     // initialize the vector of tactiles
00071     tactiles_vector = boost::shared_ptr<std::vector<UBI0Data> >(new std::vector<UBI0Data>(this->nb_tactiles));
00072     this->all_tactile_data = boost::shared_ptr<std::vector<AllTactileData> >(
00073             new std::vector<AllTactileData>(this->nb_tactiles));
00074 
00075     for (size_t i = 0; i < this->all_tactile_data->size(); ++i)
00076     {
00077       this->all_tactile_data->at(i).type = "ubi";
00078     }
00079 
00080     // initialize the palm sensors
00081     palm_tactiles = boost::shared_ptr<UBI0PalmData>(new UBI0PalmData());
00082   }
00083 
00084   template<class StatusType, class CommandType>
00085   void UBI0<StatusType, CommandType>::update(StatusType *status_data)
00086   {
00087     int tactile_mask = static_cast<int16u>(status_data->tactile_data_valid);
00088     // @todo use memcopy instead?
00089     for (unsigned int id_sensor = 0; id_sensor < this->nb_tactiles; ++id_sensor)
00090     {
00091       // the rest of the data is sampled at different rates
00092       switch (static_cast<int32u>(status_data->tactile_data_type))
00093       {
00094         // TACTILE DATA
00095         case TACTILE_SENSOR_TYPE_UBI0_TACTILE:
00096           for (unsigned int i = 0; i < tactiles_vector->at(id_sensor).distal.size(); ++i)
00097           {
00098             tactiles_vector->at(id_sensor).distal[i] =
00099                     static_cast<int>(static_cast<int16u>(status_data->tactile[id_sensor].word[i]));
00100           }
00101           for (unsigned int i = 0; i < tactiles_vector->at(id_sensor).middle.size(); ++i)
00102           {
00103             tactiles_vector->at(id_sensor).middle[i] =
00104                     static_cast<int>(static_cast<int16u>(status_data->tactile_mid_prox[id_sensor].named.middle[i]));
00105           }
00106           for (unsigned int i = 0; i < tactiles_vector->at(id_sensor).proximal.size(); ++i)
00107           {
00108             tactiles_vector->at(id_sensor).proximal[i] =
00109                     static_cast<int>(static_cast<int16u>(status_data->tactile_mid_prox[id_sensor].named.proximal[i]));
00110           }
00111           break;
00112 
00113           // COMMON DATA
00114         case TACTILE_SENSOR_TYPE_SAMPLE_FREQUENCY_HZ:
00115           if (sr_math_utils::is_bit_mask_index_true(tactile_mask, id_sensor))
00116           {
00117             tactiles_vector->at(id_sensor).sample_frequency =
00118                     static_cast<unsigned int>(static_cast<int16u>(status_data->tactile[id_sensor].word[0]));
00119           }
00120           break;
00121 
00122         case TACTILE_SENSOR_TYPE_MANUFACTURER:
00123         {
00124           if (sr_math_utils::is_bit_mask_index_true(tactile_mask, id_sensor))
00125           {
00126             tactiles_vector->at(id_sensor).manufacturer = this->sanitise_string(status_data->tactile[id_sensor].string,
00127                                                                                 TACTILE_DATA_LENGTH_BYTES);
00128           }
00129         }
00130           break;
00131 
00132         case TACTILE_SENSOR_TYPE_SERIAL_NUMBER:
00133         {
00134           if (sr_math_utils::is_bit_mask_index_true(tactile_mask, id_sensor))
00135           {
00136             tactiles_vector->at(id_sensor).serial_number = this->sanitise_string(status_data->tactile[id_sensor].string,
00137                                                                                  TACTILE_DATA_LENGTH_BYTES);
00138           }
00139         }
00140           break;
00141 
00142         case TACTILE_SENSOR_TYPE_SOFTWARE_VERSION:
00143           if (sr_math_utils::is_bit_mask_index_true(tactile_mask, id_sensor))
00144           {
00145             tactiles_vector->at(id_sensor).set_software_version(status_data->tactile[id_sensor].string);
00146           }
00147           break;
00148 
00149         case TACTILE_SENSOR_TYPE_PCB_VERSION:
00150           if (sr_math_utils::is_bit_mask_index_true(tactile_mask, id_sensor))
00151           {
00152             tactiles_vector->at(id_sensor).pcb_version = this->sanitise_string(status_data->tactile[id_sensor].string,
00153                                                                                TACTILE_DATA_LENGTH_BYTES);
00154           }
00155           break;
00156 
00157         default:
00158           break;
00159       }  // end switch
00160     }  // end for tactile
00161 
00162     for (unsigned int i = 0; i < palm_tactiles->palm.size(); ++i)
00163     {
00164       palm_tactiles->palm[i] = static_cast<int>(static_cast<int16u>(status_data->aux_spi_sensor.sensor[i]));
00165     }
00166 
00167     if (this->sensor_updater->update_state == operation_mode::device_update_state::INITIALIZATION)
00168     {
00169       this->process_received_data_type(static_cast<int32u>(status_data->tactile_data_type));
00170       if (this->sensor_updater->initialization_configs_vector.size() == 0)
00171       {
00172         this->sensor_updater->update_state = operation_mode::device_update_state::OPERATION;
00173       }
00174     }
00175   }
00176 
00177   template<class StatusType, class CommandType>
00178   void UBI0<StatusType, CommandType>::publish()
00179   {
00180     // @TODO move this to the controller publisher (issue #38)
00181     // this is not easily accessible from the tactile controller publisher yet
00182     if (aux_spi_publisher->trylock())
00183     {
00184       sr_robot_msgs::AuxSpiData tactiles;
00185       tactiles.header.stamp = ros::Time::now();
00186 
00187       tactiles.sensors = palm_tactiles->palm;
00188 
00189       aux_spi_publisher->msg_ = tactiles;
00190       aux_spi_publisher->unlockAndPublish();
00191     }
00192   }  // end publish
00193 
00194   template<class StatusType, class CommandType>
00195   void UBI0<StatusType, CommandType>::add_diagnostics(std::vector<diagnostic_msgs::DiagnosticStatus> &vec,
00196                                                       diagnostic_updater::DiagnosticStatusWrapper &d)
00197   {
00198     for (unsigned int id_tact = 0; id_tact < this->nb_tactiles; ++id_tact)
00199     {
00200       std::stringstream ss;
00201       std::string prefix = this->device_id_.empty() ? this->device_id_ : (this->device_id_ + " ");
00202 
00203       ss << prefix << "Tactile " << id_tact + 1;
00204 
00205       d.name = ss.str().c_str();
00206       d.summary(d.OK, "OK");
00207       d.clear();
00208 
00209       d.addf("Sample Frequency", "%d", tactiles_vector->at(id_tact).sample_frequency);
00210       d.addf("Manufacturer", "%s", tactiles_vector->at(id_tact).manufacturer.c_str());
00211       d.addf("Serial Number", "%s", tactiles_vector->at(id_tact).serial_number.c_str());
00212 
00213       d.addf("Software Version", "%s", tactiles_vector->at(id_tact).get_software_version().c_str());
00214       d.addf("PCB Version", "%s", tactiles_vector->at(id_tact).pcb_version.c_str());
00215 
00216       vec.push_back(d);
00217     }
00218   }
00219 
00220   template<class StatusType, class CommandType>
00221   std::vector<AllTactileData> *UBI0<StatusType, CommandType>::get_tactile_data()
00222   {
00223     for (unsigned int i = 0; i < tactiles_vector->size(); ++i)
00224     {
00225       this->all_tactile_data->at(i).ubi0 = tactiles_vector->at(i);
00226     }
00227 
00228     return this->all_tactile_data.get();
00229   }
00230 
00231   // Only to ensure that the template class is compiled for the types we are interested in
00232   template
00233   class UBI0<ETHERCAT_DATA_STRUCTURE_0200_PALM_EDC_STATUS, ETHERCAT_DATA_STRUCTURE_0200_PALM_EDC_COMMAND>;
00234 
00235   template
00236   class UBI0<ETHERCAT_DATA_STRUCTURE_0230_PALM_EDC_STATUS, ETHERCAT_DATA_STRUCTURE_0230_PALM_EDC_COMMAND>;
00237 
00238   template
00239   class UBI0<ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_STATUS, ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_COMMAND>;
00240 }  // namespace tactiles
00241 
00242 /* For the emacs weenies in the crowd.
00243    Local Variables:
00244    c-basic-offset: 2
00245    End:
00246 */


sr_robot_lib
Author(s): Ugo Cupcic, Toni Oliver
autogenerated on Mon Jul 1 2019 20:06:26