muscle_updater.cpp
Go to the documentation of this file.
00001 
00029 #include "sr_robot_lib/muscle_updater.hpp"
00030 #include <boost/foreach.hpp>
00031 #include <iostream>
00032 #include <vector>
00033 
00034 namespace generic_updater
00035 {
00036   template<class CommandType>
00037   MuscleUpdater<CommandType>::MuscleUpdater(std::vector<UpdateConfig> update_configs_vector,
00038                                             operation_mode::device_update_state::DeviceUpdateState update_state)
00039           : GenericUpdater<CommandType>(update_configs_vector, update_state)
00040   {
00041   }
00042 
00043   template<class CommandType>
00044   operation_mode::device_update_state::DeviceUpdateState MuscleUpdater<CommandType>::build_init_command(
00045           CommandType *command)
00046   {
00047     if (!this->mutex->try_lock())
00048     {
00049       return this->update_state;
00050     }
00051 
00052     if ((this->update_state == operation_mode::device_update_state::INITIALIZATION)
00053         && (this->initialization_configs_vector.size() > 0))
00054     {
00056       // First we ask for the next data we want to receive
00057       this->which_data_to_request++;
00058 
00059       if (this->which_data_to_request >= this->initialization_configs_vector.size())
00060       {
00061         this->which_data_to_request = 0;
00062       }
00063 
00064       // initialization data
00065       command->from_muscle_data_type =
00066               static_cast<FROM_MUSCLE_DATA_TYPE>(
00067                       this->initialization_configs_vector[this->which_data_to_request].what_to_update);
00068 
00069       ROS_DEBUG_STREAM("Updating initialization data type: " << command->from_muscle_data_type << " | [" <<
00070                        this->which_data_to_request << "/" << this->initialization_configs_vector.size() << "] ");
00071     }
00072     else
00073     {
00074       this->update_state == operation_mode::device_update_state::OPERATION;
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 first important message
00078       // This is to avoid sending a random command
00079       command->from_muscle_data_type =
00080               static_cast<FROM_MUSCLE_DATA_TYPE>(this->important_update_configs_vector[0].what_to_update);
00081       ROS_DEBUG_STREAM("Updating important data type: " << command->from_muscle_data_type << " | [" <<
00082                        this->which_data_to_request << "/" << this->important_update_configs_vector.size() << "] ");
00083     }
00084 
00085     this->mutex->unlock();
00086 
00087     return this->update_state;
00088   }
00089 
00090   template<class CommandType>
00091   operation_mode::device_update_state::DeviceUpdateState MuscleUpdater<CommandType>::build_command(CommandType *command)
00092   {
00093     if (!this->mutex->try_lock())
00094     {
00095       return this->update_state;
00096     }
00097 
00099     // First we ask for the next data we want to receive
00100     this->which_data_to_request++;
00101 
00102     if (this->which_data_to_request >= this->important_update_configs_vector.size())
00103     {
00104       this->which_data_to_request = 0;
00105     }
00106 
00107     if (!this->unimportant_data_queue.empty())
00108     {
00109       // an unimportant data is available
00110       command->from_muscle_data_type = static_cast<FROM_MUSCLE_DATA_TYPE>(this->unimportant_data_queue.front());
00111       this->unimportant_data_queue.pop();
00112 
00113       ROS_DEBUG_STREAM("Updating unimportant data type: " << command->from_muscle_data_type << " | queue size: " <<
00114                        this->unimportant_data_queue.size());
00115     }
00116     else
00117     {
00118       // important data to update as often as possible
00119       command->from_muscle_data_type =
00120               static_cast<FROM_MUSCLE_DATA_TYPE>(
00121                       this->important_update_configs_vector[this->which_data_to_request].what_to_update);
00122       ROS_DEBUG_STREAM("Updating important data type: " << command->from_muscle_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   // Only to ensure that the template class is compiled for the types we are interested in
00132   template
00133   class MuscleUpdater<ETHERCAT_DATA_STRUCTURE_0300_PALM_EDC_COMMAND>;
00134 }  // namespace generic_updater
00135 
00136 
00137 
00138 /* For the emacs weenies in the crowd.
00139    Local Variables:
00140    c-basic-offset: 2
00141    End:
00142 */


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