event_manager.cpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Includes
12 *****************************************************************************/
13 
14 #include "../../include/kobuki_driver/event_manager.hpp"
15 #include "../../include/kobuki_driver/modules/battery.hpp"
16 #include "../../include/kobuki_driver/packets/core_sensors.hpp"
17 
18 /*****************************************************************************
19 ** Namespaces
20 *****************************************************************************/
21 
22 namespace kobuki {
23 
24 /*****************************************************************************
25 ** Implementation
26 *****************************************************************************/
27 
28 void EventManager::init ( const std::string &sigslots_namespace ) {
29  sig_button_event.connect(sigslots_namespace + std::string("/button_event"));
30  sig_bumper_event.connect(sigslots_namespace + std::string("/bumper_event"));
31  sig_cliff_event.connect(sigslots_namespace + std::string("/cliff_event"));
32  sig_wheel_event.connect(sigslots_namespace + std::string("/wheel_event"));
33  sig_power_event.connect(sigslots_namespace + std::string("/power_event"));
34  sig_input_event.connect(sigslots_namespace + std::string("/input_event"));
35  sig_robot_event.connect(sigslots_namespace + std::string("/robot_event"));
36 }
37 
43 void EventManager::update(const CoreSensors::Data &new_state, const std::vector<uint16_t> &cliff_data) {
44  if (last_state.buttons != new_state.buttons)
45  {
46  // ------------
47  // Button Event
48  // ------------
49 
50  // Note that the touch pad means at most one button can be pressed
51  // at a time.
52  ButtonEvent event;
53 
54  // Check changes in each button state's; even if this block of code
55  // supports it, two buttons cannot be pressed simultaneously
58  if (new_state.buttons & CoreSensors::Flags::Button0) {
59  event.state = ButtonEvent::Pressed;
60  } else {
61  event.state = ButtonEvent::Released;
62  }
63  sig_button_event.emit(event);
64  }
65 
67  event.button = ButtonEvent::Button1;
68  if (new_state.buttons & CoreSensors::Flags::Button1) {
69  event.state = ButtonEvent::Pressed;
70  } else {
71  event.state = ButtonEvent::Released;
72  }
73  sig_button_event.emit(event);
74  }
75 
77  event.button = ButtonEvent::Button2;
78  if (new_state.buttons & CoreSensors::Flags::Button2) {
79  event.state = ButtonEvent::Pressed;
80  } else {
81  event.state = ButtonEvent::Released;
82  }
83  sig_button_event.emit(event);
84  }
85  }
86 
87  // ------------
88  // Bumper Event
89  // ------------
90 
91  if (last_state.bumper != new_state.bumper)
92  {
93  BumperEvent event;
94 
95  // Check changes in each bumper state's and raise an event if so
97  event.bumper = BumperEvent::Left;
98  if (new_state.bumper & CoreSensors::Flags::LeftBumper) {
99  event.state = BumperEvent::Pressed;
100  } else {
101  event.state = BumperEvent::Released;
102  }
103  sig_bumper_event.emit(event);
104  }
105 
107  event.bumper = BumperEvent::Center;
108  if (new_state.bumper & CoreSensors::Flags::CenterBumper) {
109  event.state = BumperEvent::Pressed;
110  } else {
111  event.state = BumperEvent::Released;
112  }
113  sig_bumper_event.emit(event);
114  }
115 
117  event.bumper = BumperEvent::Right;
118  if (new_state.bumper & CoreSensors::Flags::RightBumper) {
119  event.state = BumperEvent::Pressed;
120  } else {
121  event.state = BumperEvent::Released;
122  }
123  sig_bumper_event.emit(event);
124  }
125  }
126 
127  // ------------
128  // Cliff Event
129  // ------------
130 
131  if (last_state.cliff != new_state.cliff)
132  {
133  CliffEvent event;
134 
135  // Check changes in each cliff sensor state's and raise an event if so
136  if ((new_state.cliff ^ last_state.cliff) & CoreSensors::Flags::LeftCliff) {
137  event.sensor = CliffEvent::Left;
138  if (new_state.cliff & CoreSensors::Flags::LeftCliff) {
139  event.state = CliffEvent::Cliff;
140  } else {
141  event.state = CliffEvent::Floor;
142  }
143  event.bottom = cliff_data[event.sensor];
144  sig_cliff_event.emit(event);
145  }
146 
148  event.sensor = CliffEvent::Center;
149  if (new_state.cliff & CoreSensors::Flags::CenterCliff) {
150  event.state = CliffEvent::Cliff;
151  } else {
152  event.state = CliffEvent::Floor;
153  }
154  event.bottom = cliff_data[event.sensor];
155  sig_cliff_event.emit(event);
156  }
157 
159  event.sensor = CliffEvent::Right;
160  if (new_state.cliff & CoreSensors::Flags::RightCliff) {
161  event.state = CliffEvent::Cliff;
162  } else {
163  event.state = CliffEvent::Floor;
164  }
165  event.bottom = cliff_data[event.sensor];
166  sig_cliff_event.emit(event);
167  }
168  }
169 
170  // ------------
171  // Wheel Drop Event
172  // ------------
173 
174  if (last_state.wheel_drop != new_state.wheel_drop)
175  {
176  WheelEvent event;
177 
178  // Check changes in each wheel_drop sensor state's and raise an event if so
180  event.wheel = WheelEvent::Left;
181  if (new_state.wheel_drop & CoreSensors::Flags::LeftWheel) {
182  event.state = WheelEvent::Dropped;
183  } else {
184  event.state = WheelEvent::Raised;
185  }
186  sig_wheel_event.emit(event);
187  }
188 
190  event.wheel = WheelEvent::Right;
191  if (new_state.wheel_drop & CoreSensors::Flags::RightWheel) {
192  event.state = WheelEvent::Dropped;
193  } else {
194  event.state = WheelEvent::Raised;
195  }
196  sig_wheel_event.emit(event);
197  }
198  }
199 
200  // ------------
201  // Power System Event
202  // ------------
203 
204  if (last_state.charger != new_state.charger)
205  {
206  Battery battery_new(new_state.battery, new_state.charger);
208 
209  if (battery_last.charging_state != battery_new.charging_state)
210  {
211  PowerEvent event;
212  switch (battery_new.charging_state)
213  {
215  event.event = PowerEvent::Unplugged;
216  break;
217  case Battery::Charged:
218  event.event = PowerEvent::ChargeCompleted;
219  break;
220  case Battery::Charging:
221  if (battery_new.charging_source == Battery::Adapter)
222  event.event = PowerEvent::PluggedToAdapter;
223  else
224  event.event = PowerEvent::PluggedToDockbase;
225  break;
226  }
227  sig_power_event.emit(event);
228  }
229  }
230 
231  if (last_state.battery > new_state.battery)
232  {
233  Battery battery_new(new_state.battery, new_state.charger);
235 
236  if (battery_last.level() != battery_new.level())
237  {
238  PowerEvent event;
239  switch (battery_new.level())
240  {
241  case Battery::Low:
242  event.event = PowerEvent::BatteryLow;
243  break;
244  case Battery::Dangerous:
245  event.event = PowerEvent::BatteryCritical;
246  break;
247  default:
248  break;
249  }
250  sig_power_event.emit(event);
251  }
252  }
253 
254  last_state = new_state;
255 }
256 
261 void EventManager::update(const uint16_t &new_digital_input)
262 {
263  if (last_digital_input != new_digital_input)
264  {
265  InputEvent event;
266 
267  event.values[0] = new_digital_input&0x0001;
268  event.values[1] = new_digital_input&0x0002;
269  event.values[2] = new_digital_input&0x0004;
270  event.values[3] = new_digital_input&0x0008;
271 
272  sig_input_event.emit(event);
273 
274  last_digital_input = new_digital_input;
275  }
276 }
277 
283 void EventManager::update(bool is_plugged, bool is_alive)
284 {
285  RobotEvent::State robot_state =
286  (is_plugged && is_alive)?RobotEvent::Online:RobotEvent::Offline;
287  if (last_robot_state != robot_state)
288  {
289  RobotEvent event;
290  event.state = robot_state;
291 
292  sig_robot_event.emit(event);
293 
294  last_robot_state = robot_state;
295  }
296 }
297 
298 } // namespace kobuki
ecl::Signal< const InputEvent & > sig_input_event
static const uint8_t Button0
ecl::Signal< const ButtonEvent & > sig_button_event
static const uint8_t RightWheel
static const uint8_t Button2
Source charging_source
Definition: battery.hpp:69
ecl::Signal< const CliffEvent & > sig_cliff_event
static const uint8_t RightCliff
Level level() const
Definition: battery.cpp:61
ecl::Signal< const RobotEvent & > sig_robot_event
enum kobuki::BumperEvent::Bumper bumper
void update(const CoreSensors::Data &new_state, const std::vector< uint16_t > &cliff_data)
CoreSensors::Data last_state
static const uint8_t CenterCliff
static const uint8_t LeftCliff
static const uint8_t LeftBumper
static const uint8_t CenterBumper
enum kobuki::ButtonEvent::Button button
static const uint8_t LeftWheel
State charging_state
Definition: battery.hpp:68
RobotEvent::State last_robot_state
static const uint8_t Button1
enum kobuki::WheelEvent::Wheel wheel
enum kobuki::RobotEvent::State state
enum kobuki::CliffEvent::Sensor sensor
Battery level module.
Definition: battery.hpp:40
ecl::Signal< const WheelEvent & > sig_wheel_event
ecl::Signal< const BumperEvent & > sig_bumper_event
static const uint8_t RightBumper
void init(const std::string &sigslots_namespace)
ecl::Signal< const PowerEvent & > sig_power_event


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Fri Sep 18 2020 03:22:01