motor_updater.cpp
Go to the documentation of this file.
00001 
00029 #include "sr_robot_lib/motor_updater.hpp"
00030 #include <boost/foreach.hpp>
00031 #include <iostream>
00032 #include <vector>
00033 
00034 namespace generic_updater
00035 {
00036   template<class CommandType>
00037   MotorUpdater<CommandType>::MotorUpdater(std::vector<UpdateConfig> update_configs_vector,
00038                                           operation_mode::device_update_state::DeviceUpdateState update_state)
00039           : GenericUpdater<CommandType>(update_configs_vector, update_state), even_motors(1)
00040   {
00041   }
00042 
00043   template<class CommandType>
00044   operation_mode::device_update_state::DeviceUpdateState MotorUpdater<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     {
00055       // First we ask for the next data we want to receive
00056       if (even_motors)
00057       {
00058         even_motors = 0;
00059       }
00060       else
00061       {
00062         even_motors = 1;
00063         this->which_data_to_request++;
00064 
00065         if (this->which_data_to_request >= this->initialization_configs_vector.size())
00066         {
00067           this->which_data_to_request = 0;
00068         }
00069       }
00070 
00071       command->which_motors = even_motors;
00072 
00073       // initialization data
00074       command->from_motor_data_type =
00075               static_cast<FROM_MOTOR_DATA_TYPE>(
00076                       this->initialization_configs_vector[this->which_data_to_request].what_to_update);
00077       ROS_DEBUG_STREAM("Updating initialization data type: " << command->from_motor_data_type << " | [" <<
00078                        this->which_data_to_request << "/" << this->initialization_configs_vector.size() << "] ");
00079     }
00080     else
00081     {
00082       // For the last message sent when a change of update_state happens
00083       // (after that we use build_command instead of build_init_command)
00084       // we use the first important message and ask it to the even motors (0)
00085       // This is to avoid sending a random command
00086       command->which_motors = 0;
00087       command->from_motor_data_type =
00088               static_cast<FROM_MOTOR_DATA_TYPE>(this->important_update_configs_vector[0].what_to_update);
00089       ROS_DEBUG_STREAM("Updating important data type: " << command->from_motor_data_type << " | [" <<
00090                        this->which_data_to_request << "/" << this->important_update_configs_vector.size() << "] ");
00091     }
00092 
00093     this->mutex->unlock();
00094 
00095     return this->update_state;
00096   }
00097 
00098   template<class CommandType>
00099   operation_mode::device_update_state::DeviceUpdateState MotorUpdater<CommandType>::build_command(CommandType *command)
00100   {
00101     if (!this->mutex->try_lock())
00102     {
00103       return this->update_state;
00104     }
00105 
00107     // First we ask for the next data we want to receive
00108     if (even_motors)
00109     {
00110       even_motors = 0;
00111     }
00112     else
00113     {
00114       even_motors = 1;
00115       this->which_data_to_request++;
00116 
00117       if (this->which_data_to_request >= this->important_update_configs_vector.size())
00118       {
00119         this->which_data_to_request = 0;
00120       }
00121     }
00122 
00123     command->which_motors = even_motors;
00124 
00125     if (!this->unimportant_data_queue.empty())
00126     {
00127       // an unimportant data is available
00128       command->from_motor_data_type = static_cast<FROM_MOTOR_DATA_TYPE>(this->unimportant_data_queue.front());
00129       this->unimportant_data_queue.pop();
00130 
00131       ROS_DEBUG_STREAM("Updating unimportant data type: " << command->from_motor_data_type << " | queue size: " <<
00132                        this->unimportant_data_queue.size());
00133     }
00134     else
00135     {
00136       // important data to update as often as possible
00137       command->from_motor_data_type =
00138               static_cast<FROM_MOTOR_DATA_TYPE>(
00139                       this->important_update_configs_vector[this->which_data_to_request].what_to_update);
00140       ROS_DEBUG_STREAM("Updating important data type: " << command->from_motor_data_type << " | [" <<
00141                        this->which_data_to_request << "/" << this->important_update_configs_vector.size() << "] ");
00142     }
00143 
00144     this->mutex->unlock();
00145 
00146     return this->update_state;
00147   }
00148 
00149   // Only to ensure that the template class is compiled for the types we are interested in
00150   template
00151   class MotorUpdater<ETHERCAT_DATA_STRUCTURE_0200_PALM_EDC_COMMAND>;
00152 
00153   template
00154   class MotorUpdater<ETHERCAT_DATA_STRUCTURE_0230_PALM_EDC_COMMAND>;
00155 }  // namespace generic_updater
00156 
00157 
00158 
00159 /* For the emacs weenies in the crowd.
00160    Local Variables:
00161    c-basic-offset: 2
00162    End:
00163 */


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