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