sensor_updater.cpp
Go to the documentation of this file.
00001 
00028 #include "sr_robot_lib/sensor_updater.hpp"
00029 #include <boost/foreach.hpp>
00030 #include <boost/thread/mutex.hpp>
00031 #include <boost/smart_ptr.hpp>
00032 #include <iostream>
00033 #include <vector>
00034 
00035 namespace generic_updater
00036 {
00037   template<class CommandType>
00038   SensorUpdater<CommandType>::SensorUpdater(std::vector<UpdateConfig> update_configs_vector,
00039                                             operation_mode::device_update_state::DeviceUpdateState update_state)
00040           : GenericUpdater<CommandType>(update_configs_vector, update_state)
00041   {
00042   }
00043 
00044   template<class CommandType>
00045   operation_mode::device_update_state::DeviceUpdateState SensorUpdater<CommandType>::build_init_command(
00046           CommandType *command)
00047   {
00048     if (!this->mutex->try_lock())
00049     {
00050       return this->update_state;
00051     }
00052 
00053     if (this->update_state == operation_mode::device_update_state::INITIALIZATION)
00054     {
00055       if (this->initialization_configs_vector.size() > 0)
00056       {
00058         // First we ask for the next data we want to receive
00059 
00060         this->which_data_to_request++;
00061 
00062         if (this->which_data_to_request >= this->initialization_configs_vector.size())
00063         {
00064           this->which_data_to_request = 0;
00065         }
00066 
00067         // initialization data
00068         command->tactile_data_type = this->initialization_configs_vector[this->which_data_to_request].what_to_update;
00069         ROS_DEBUG_STREAM("Updating sensor initialization data type: " << command->tactile_data_type << " | [" <<
00070                          this->which_data_to_request << "/" << this->initialization_configs_vector.size() << "] ");
00071       }
00072     }
00073     else
00074     {
00075       // For the last message sent when a change of update_state happens
00076       // (after that we use build_command instead of build_init_command)
00077       // we use the TACTILE_SENSOR_TYPE_WHICH_SENSORS message, which is supposed to be always implemented
00078       // This is to avoid sending a random command (initialization_configs_vector is empty at this time)
00079       ROS_DEBUG_STREAM("Important data size: " << this->important_update_configs_vector.size());
00080 
00081 
00082       command->tactile_data_type = TACTILE_SENSOR_TYPE_WHICH_SENSORS;
00083       ROS_DEBUG_STREAM("Updating sensor initialization data type: " << command->tactile_data_type << " | [" <<
00084                        this->which_data_to_request << "/" << this->important_update_configs_vector.size() << "] ");
00085     }
00086     this->mutex->unlock();
00087 
00088     return this->update_state;
00089   }
00090 
00091   template<class CommandType>
00092   operation_mode::device_update_state::DeviceUpdateState SensorUpdater<CommandType>::build_command(CommandType *command)
00093   {
00094     if (!this->mutex->try_lock())
00095     {
00096       return this->update_state;
00097     }
00098 
00100     // First we ask for the next data we want to receive
00101 
00102     this->which_data_to_request++;
00103 
00104     if (this->which_data_to_request >= this->important_update_configs_vector.size())
00105     {
00106       this->which_data_to_request = 0;
00107     }
00108 
00109     if (!this->unimportant_data_queue.empty())
00110     {
00111       // an unimportant data is available
00112       command->tactile_data_type = this->unimportant_data_queue.front();
00113       this->unimportant_data_queue.pop();
00114 
00115       ROS_DEBUG_STREAM("Updating sensor unimportant data type: " << command->tactile_data_type << " | queue size: " <<
00116                        this->unimportant_data_queue.size());
00117     }
00118     else
00119     {
00120       // important data to update as often as possible
00121       command->tactile_data_type = this->important_update_configs_vector[this->which_data_to_request].what_to_update;
00122       ROS_DEBUG_STREAM("Updating sensor important data type: " << command->tactile_data_type << " | [" <<
00123                        this->which_data_to_request << "/" << this->important_update_configs_vector.size() << "] ");
00124     }
00125 
00126     this->mutex->unlock();
00127 
00128     return this->update_state;
00129   }
00130 
00131   template<class CommandType>
00132   bool SensorUpdater<CommandType>::reset()
00133   {
00134     // We need to send the reset command twice in a row to make sure
00135     // the tactiles are reset.
00136     boost::mutex::scoped_lock l(*(this->mutex));
00137     for (unsigned int i = 0; i < 2; ++i)
00138     {
00139       this->unimportant_data_queue.push(TACTILE_SENSOR_TYPE_RESET_COMMAND);
00140     }
00141     return true;
00142   }
00143 
00144   // Only to ensure that the template class is compiled for the types we are interested in
00145   template
00146   class SensorUpdater<ETHERCAT_DATA_STRUCTURE_0200_PALM_EDC_COMMAND>;
00147 
00148   template
00149   class SensorUpdater<ETHERCAT_DATA_STRUCTURE_0230_PALM_EDC_COMMAND>;
00150 
00151   template
00152   class SensorUpdater<ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_COMMAND>;
00153 }  // namespace generic_updater
00154 
00155 
00156 
00157 
00158 /* For the emacs weenies in the crowd.
00159 Local Variables:
00160    c-basic-offset: 2
00161 End:
00162 */


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