sensor_updater.cpp
Go to the documentation of this file.
1 
28 #include <boost/foreach.hpp>
29 #include <boost/thread/mutex.hpp>
30 #include <boost/smart_ptr.hpp>
31 #include <iostream>
32 #include <vector>
33 
34 namespace generic_updater
35 {
36  template<class CommandType>
37  SensorUpdater<CommandType>::SensorUpdater(std::vector<UpdateConfig> update_configs_vector,
39  : GenericUpdater<CommandType>(update_configs_vector, update_state)
40  {
41  }
42 
43  template<class CommandType>
45  CommandType *command)
46  {
47  if (!this->mutex->try_lock())
48  {
49  return this->update_state;
50  }
51 
53  {
54  if (this->initialization_configs_vector.size() > 0)
55  {
57  // First we ask for the next data we want to receive
58 
59  this->which_data_to_request++;
60 
61  if (this->which_data_to_request >= this->initialization_configs_vector.size())
62  {
63  this->which_data_to_request = 0;
64  }
65 
66  // initialization data
67  command->tactile_data_type = this->initialization_configs_vector[this->which_data_to_request].what_to_update;
68  ROS_DEBUG_STREAM("Updating sensor initialization data type: " << command->tactile_data_type << " | [" <<
69  this->which_data_to_request << "/" << this->initialization_configs_vector.size() << "] ");
70  }
71  }
72  else
73  {
74  // For the last message sent when a change of update_state happens
75  // (after that we use build_command instead of build_init_command)
76  // we use the TACTILE_SENSOR_TYPE_WHICH_SENSORS message, which is supposed to be always implemented
77  // This is to avoid sending a random command (initialization_configs_vector is empty at this time)
78  ROS_DEBUG_STREAM("Important data size: " << this->important_update_configs_vector.size());
79 
80 
81  command->tactile_data_type = TACTILE_SENSOR_TYPE_WHICH_SENSORS;
82  ROS_DEBUG_STREAM("Updating sensor initialization data type: " << command->tactile_data_type << " | [" <<
83  this->which_data_to_request << "/" << this->important_update_configs_vector.size() << "] ");
84  }
85  this->mutex->unlock();
86 
87  return this->update_state;
88  }
89 
90  template<class CommandType>
92  {
93  if (!this->mutex->try_lock())
94  {
95  return this->update_state;
96  }
97 
99  // First we ask for the next data we want to receive
100 
101  this->which_data_to_request++;
102 
103  if (this->which_data_to_request >= this->important_update_configs_vector.size())
104  {
105  this->which_data_to_request = 0;
106  }
107 
108  if (!this->unimportant_data_queue.empty())
109  {
110  // an unimportant data is available
111  command->tactile_data_type = this->unimportant_data_queue.front();
112  this->unimportant_data_queue.pop();
113 
114  ROS_DEBUG_STREAM("Updating sensor unimportant data type: " << command->tactile_data_type << " | queue size: " <<
115  this->unimportant_data_queue.size());
116  }
117  else
118  {
119  // important data to update as often as possible
120  command->tactile_data_type = this->important_update_configs_vector[this->which_data_to_request].what_to_update;
121  ROS_DEBUG_STREAM("Updating sensor important data type: " << command->tactile_data_type << " | [" <<
122  this->which_data_to_request << "/" << this->important_update_configs_vector.size() << "] ");
123  }
124 
125  this->mutex->unlock();
126 
127  return this->update_state;
128  }
129 
130  template<class CommandType>
132  {
133  // We need to send the reset command twice in a row to make sure
134  // the tactiles are reset.
135  boost::mutex::scoped_lock l(*(this->mutex));
136  for (unsigned int i = 0; i < 2; ++i)
137  {
139  }
140  return true;
141  }
142 
143  // Only to ensure that the template class is compiled for the types we are interested in
144  template
146 
147  template
149 
150  template
152 
153  template
155 } // namespace generic_updater
156 
157 
158 
159 
160 /* For the emacs weenies in the crowd.
161 Local Variables:
162  c-basic-offset: 2
163 End:
164 */
std::queue< int32u, std::list< int32u > > unimportant_data_queue
int which_data_to_request
iterate through the important or initialization data types.
operation_mode::device_update_state::DeviceUpdateState update_state
boost::shared_ptr< boost::mutex > mutex
TACTILE_SENSOR_TYPE_RESET_COMMAND
operation_mode::device_update_state::DeviceUpdateState build_init_command(CommandType *command)
std::vector< UpdateConfig > initialization_configs_vector
operation_mode::device_update_state::DeviceUpdateState build_command(CommandType *command)
#define ROS_DEBUG_STREAM(args)
std::vector< UpdateConfig > important_update_configs_vector
Contains all the important data types.
TACTILE_SENSOR_TYPE_WHICH_SENSORS
SensorUpdater(std::vector< UpdateConfig > update_configs_vector, operation_mode::device_update_state::DeviceUpdateState update_state)


sr_robot_lib
Author(s): Ugo Cupcic, Toni Oliver
autogenerated on Tue Oct 13 2020 04:01:57