utils.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (Apache License)
3  *
4  * Copyright (c) 2019 Joshua Hatzenbuehler
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
20 
21 namespace packml_stacklight
22 {
24 {
25  return;
26 }
27 
29 {
30  return;
31 }
32 
33 std::vector<Action> Utils::initDefaultStatusActions()
34 {
35  std::vector<Action> temp_vec;
36  for (int8_t state = packml_msgs::State::UNDEFINED; state <= packml_msgs::State::COMPLETE; state++)
37  {
38  Action action;
39  action.state_ = state;
40 
41  for (int8_t light_value = Light::Value::UNDEFINED; light_value <= Light::Value::BLUE; light_value++)
42  {
43  Light light;
44  light.current_ = (Light::Value)light_value;
45  action.light_vec_.push_back(light);
46  }
47 
48  for (int8_t button_value = Button::Value::UNDEFINED; button_value <= Button::Value::RESET; button_value++)
49  {
50  Button button;
51  button.current_ = (Button::Value)button_value;
52  action.button_vec_.push_back(button);
53  }
54 
55  switch (action.state_)
56  {
57  case packml_msgs::State::ABORTING:
58  case packml_msgs::State::ABORTED:
59  action.light_vec_[Light::Value::RED].active_ = true;
60  action.light_vec_[Light::Value::RED].flashing_ = true;
61 
62  action.light_vec_[Light::Value::BLUE].active_ = true;
63  action.light_vec_[Light::Value::BLUE].flashing_ = true;
64 
65  action.button_vec_[Button::Value::RESET].light_.active_ = true;
66  action.button_vec_[Button::Value::RESET].light_.current_ = Light::Value::BLUE;
67  action.button_vec_[Button::Value::RESET].light_.flashing_ = true;
68  break;
69 
70  case packml_msgs::State::CLEARING:
71  action.light_vec_[Light::Value::RED].active_ = true;
72  action.light_vec_[Light::Value::RED].flashing_ = true;
73 
74  action.light_vec_[Light::Value::BLUE].active_ = true;
75  action.light_vec_[Light::Value::BLUE].flashing_ = true;
76  break;
77 
78  case packml_msgs::State::STOPPING:
79  case packml_msgs::State::STOPPED:
80  action.light_vec_[Light::Value::RED].active_ = true;
81  action.light_vec_[Light::Value::RED].flashing_ = false;
82 
83  action.light_vec_[Light::Value::BLUE].active_ = true;
84  action.light_vec_[Light::Value::BLUE].flashing_ = true;
85  break;
86 
87  case packml_msgs::State::RESETTING:
88  action.light_vec_[Light::Value::BLUE].active_ = true;
89  action.light_vec_[Light::Value::BLUE].flashing_ = true;
90  break;
91 
92  case packml_msgs::State::IDLE:
93  action.light_vec_[Light::Value::GREEN].active_ = true;
94  action.light_vec_[Light::Value::GREEN].flashing_ = true;
95 
96  action.light_vec_[Light::Value::BLUE].active_ = true;
97  action.light_vec_[Light::Value::BLUE].flashing_ = true;
98 
99  action.button_vec_[Button::Value::START].light_.active_ = true;
100  action.button_vec_[Button::Value::START].light_.current_ = Light::Value::GREEN;
101  action.button_vec_[Button::Value::START].light_.flashing_ = true;
102  break;
103 
104  case packml_msgs::State::STARTING:
105  case packml_msgs::State::UNHOLDING:
106  action.light_vec_[Light::Value::GREEN].active_ = true;
107  action.light_vec_[Light::Value::GREEN].flashing_ = false;
108 
109  action.light_vec_[Light::Value::BLUE].active_ = true;
110  action.light_vec_[Light::Value::BLUE].flashing_ = true;
111 
112  action.button_vec_[Button::Value::START].light_.active_ = true;
113  action.button_vec_[Button::Value::START].light_.current_ = Light::Value::GREEN;
114  action.button_vec_[Button::Value::START].light_.flashing_ = false;
115 
116  action.buzzer_.active_ = true;
117  action.buzzer_.flashing_ = true;
118  break;
119 
120  case packml_msgs::State::EXECUTE:
121  action.light_vec_[Light::Value::GREEN].active_ = true;
122  action.light_vec_[Light::Value::GREEN].flashing_ = false;
123 
124  action.light_vec_[Light::Value::BLUE].active_ = true;
125  action.light_vec_[Light::Value::BLUE].flashing_ = true;
126 
127  action.button_vec_[Button::Value::START].light_.active_ = true;
128  action.button_vec_[Button::Value::START].light_.current_ = Light::Value::GREEN;
129  action.button_vec_[Button::Value::START].light_.flashing_ = false;
130  break;
131 
132  case packml_msgs::State::HOLDING:
133  case packml_msgs::State::HELD:
134  action.light_vec_[Light::Value::BLUE].active_ = true;
135  action.light_vec_[Light::Value::BLUE].flashing_ = false;
136  break;
137 
138  case packml_msgs::State::SUSPENDING:
139  case packml_msgs::State::SUSPENDED:
140  // todo starving vs blocked => flashing vs steady
141  action.light_vec_[Light::Value::AMBER].active_ = true;
142  action.light_vec_[Light::Value::AMBER].flashing_ = true;
143 
144  action.light_vec_[Light::Value::BLUE].active_ = true;
145  action.light_vec_[Light::Value::BLUE].flashing_ = true;
146 
147  action.button_vec_[Button::Value::START].light_.active_ = true;
148  action.button_vec_[Button::Value::START].light_.current_ = Light::Value::GREEN;
149  action.button_vec_[Button::Value::START].light_.flashing_ = false;
150  break;
151 
152  case packml_msgs::State::UNSUSPENDING:
153  action.light_vec_[Light::Value::GREEN].active_ = true;
154  action.light_vec_[Light::Value::GREEN].flashing_ = false;
155 
156  action.light_vec_[Light::Value::BLUE].active_ = true;
157  action.light_vec_[Light::Value::BLUE].flashing_ = true;
158 
159  action.buzzer_.active_ = true;
160  action.buzzer_.flashing_ = true;
161  break;
162 
163  default:
164  break;
165  }
166 
167  temp_vec.push_back(action);
168  }
169 
170  return temp_vec;
171 }
172 
174 {
175  return action_vec_[packml_msgs::State::SUSPENDING].light_vec_[Light::Value::AMBER].flashing_;
176 }
177 
178 bool Utils::setSuspendStarving(bool starving)
179 {
180  action_vec_[packml_msgs::State::SUSPENDING].light_vec_[Light::Value::AMBER].flashing_ = starving;
181  action_vec_[packml_msgs::State::SUSPENDED].light_vec_[Light::Value::AMBER].flashing_ = starving;
182  return getSuspendStarving();
183 }
184 
185 Flash::Value Utils::getLightFlash(packml_msgs::State current_state)
186 {
187  static ros::Time last_time(0);
188  static int8_t last_state = -1;
189  static Flash::Value last_flash = Flash::Value::ON;
190 
191  getFlash(current_state, last_state, last_flash, last_time, flash_sec_light_on_, flash_sec_light_off_);
192 
193  return last_flash;
194 }
195 
196 Flash::Value Utils::getBuzzerFlash(packml_msgs::State current_state)
197 {
198  static ros::Time last_time(0);
199  static int8_t last_state = -1;
200  static Flash::Value last_flash = Flash::Value::ON;
201 
202  getFlash(current_state, last_state, last_flash, last_time, flash_sec_buzzer_on_, flash_sec_buzzer_off_);
203 
204  return last_flash;
205 }
206 
207 void Utils::getFlash(packml_msgs::State current_state, int8_t& last_state, Flash::Value& last_flash,
208  ros::Time& last_time, double on_secs, double off_secs)
209 {
210  ros::Time new_time = ros::Time::now();
211  ros::Duration dur = new_time - last_time;
212 
213  if (last_state != current_state.val)
214  {
215  last_time = new_time;
216  last_state = current_state.val;
217  last_flash = Flash::Value::ON;
218  return;
219  }
220 
221  if (last_flash == Flash::Value::ON && dur.toSec() >= on_secs)
222  {
223  last_time = new_time;
224  last_flash = Flash::Value::OFF;
225  return;
226  }
227 
228  if (last_flash == Flash::Value::OFF && dur.toSec() >= off_secs)
229  {
230  last_time = new_time;
231  last_flash = Flash::Value::ON;
232  return;
233  }
234 }
235 
236 bool Utils::getShouldPublish(packml_msgs::State current_state)
237 {
238  static ros::Time last_time(0);
239  static int8_t last_state = -1;
240  ros::Time new_time = ros::Time::now();
241  ros::Duration dur = new_time - last_time;
242 
243  if (last_state != current_state.val)
244  {
245  last_time = new_time;
246  last_state = current_state.val;
247  return true;
248  }
249 
250  if (dur.toSec() >= publish_frequency_)
251  {
252  last_time = new_time;
253  return true;
254  }
255 
256  return false;
257 }
258 
259 Action Utils::getActionFromState(packml_msgs::State current_state)
260 {
261  return action_vec_[current_state.val];
262 }
263 
264 std::map<std::string, uint8_t> Utils::getPubMap(packml_msgs::State current_state)
265 {
266  return getPubMap(getActionFromState(current_state));
267 }
268 
269 std::map<std::string, uint8_t> Utils::getPubMap(Action action)
270 {
271  std::map<std::string, uint8_t> out_map;
272  packml_msgs::State current;
273  current.val = action.state_;
274 
275  Flash::Value light_flash = getLightFlash(current);
276  Flash::Value buzzer_flash = getBuzzerFlash(current);
277 
278  std::vector<Light>::iterator light_it;
279  for (light_it = action.light_vec_.begin(); light_it != action.light_vec_.end(); ++light_it)
280  {
281  if (light_it->current_ == Light::Value::UNDEFINED)
282  {
283  continue;
284  }
285 
286  uint8_t light_val = 0;
287  if (light_it->active_)
288  {
289  light_val = 1;
290  }
291 
292  if (light_it->flashing_ && light_flash == Flash::Value::OFF)
293  {
294  light_val = 0;
295  }
296 
297  out_map.insert(std::pair<std::string, uint8_t>(light_it->map_.find(light_it->current_)->second, light_val));
298  }
299 
300  std::vector<Button>::iterator button_it;
301  for (button_it = action.button_vec_.begin(); button_it != action.button_vec_.end(); ++button_it)
302  {
303  if (button_it->current_ == Button::Value::UNDEFINED)
304  {
305  continue;
306  }
307 
308  uint8_t button_val = 0;
309  if (button_it->light_.active_)
310  {
311  button_val = 1;
312  }
313 
314  if (button_it->light_.flashing_ && light_flash == Flash::Value::OFF)
315  {
316  button_val = 0;
317  }
318 
319  out_map.insert(std::pair<std::string, uint8_t>(button_it->map_.find(button_it->current_)->second, button_val));
320  // todo output color mapp as well
321  }
322 
323  uint8_t buz_val = 0;
324  if (action.buzzer_.active_)
325  {
326  buz_val = 1;
327  }
328 
329  if (action.buzzer_.flashing_ && buzzer_flash == Flash::Value::OFF)
330  {
331  buz_val = 0;
332  }
333  out_map.insert(std::pair<std::string, uint8_t>(action.buzzer_.map_, buz_val));
334 
335  return out_map;
336 }
337 
338 void Utils::maybeResetState(packml_msgs::State& current_state, ros::Time& last_time)
339 {
340  ros::Time new_time = ros::Time::now();
341  ros::Duration dur = new_time - last_time;
342 
343  if (status_timeout_ <= 0)
344  {
345  last_time = new_time;
346  return;
347  }
348 
349  if (last_time == ros::Time(0))
350  {
351  last_time = new_time;
352  return;
353  }
354 
355  if (current_state.val == packml_msgs::State::UNDEFINED)
356  {
357  last_time = new_time;
358  return;
359  }
360 
361  if (dur.toSec() >= status_timeout_)
362  {
363  ROS_WARN("%s status_timeout_ reached, setting current_state to %d", __FUNCTION__, packml_msgs::State::UNDEFINED);
364  last_time = new_time;
365  current_state.val = packml_msgs::State::UNDEFINED;
366  return;
367  }
368 }
369 
370 } // namespace packml_stacklight
std::map< std::string, uint8_t > getPubMap(Action action)
Definition: utils.cpp:269
std::vector< Button > button_vec_
Definition: action.h:34
double flash_sec_light_off_
Definition: utils.h:39
bool setSuspendStarving(bool starving=true)
Definition: utils.cpp:178
double status_timeout_
Definition: utils.h:43
void maybeResetState(packml_msgs::State &current_state, ros::Time &last_time)
Definition: utils.cpp:338
Action getActionFromState(packml_msgs::State current_state)
Definition: utils.cpp:259
std::vector< Action > action_vec_
Definition: utils.h:35
bool getShouldPublish(packml_msgs::State current_state)
Definition: utils.cpp:236
#define ROS_WARN(...)
double publish_frequency_
Definition: utils.h:42
double flash_sec_buzzer_off_
Definition: utils.h:41
std::vector< Light > light_vec_
Definition: action.h:33
double flash_sec_light_on_
Definition: utils.h:38
Flash::Value getBuzzerFlash(packml_msgs::State current_state)
Definition: utils.cpp:196
Flash::Value getLightFlash(packml_msgs::State current_state)
Definition: utils.cpp:185
static Time now()
const std::string map_
Definition: buzzer.h:31
void getFlash(packml_msgs::State current_state, int8_t &last_state, packml_stacklight::Flash::Value &last_flash, ros::Time &last_time, double on_secs, double off_secs)
Definition: utils.cpp:207
double flash_sec_buzzer_on_
Definition: utils.h:40
std::vector< Action > initDefaultStatusActions()
Definition: utils.cpp:33


packml_stacklight
Author(s): Joshua Hatzenbuehler
autogenerated on Fri Jul 12 2019 03:31:01