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);
207  Battery battery_last(last_state.battery, last_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);
234  Battery battery_last(last_state.battery, last_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
kobuki::WheelEvent::Left
@ Left
Definition: event_manager.hpp:86
kobuki::CliffEvent::Left
@ Left
Definition: event_manager.hpp:73
kobuki::InputEvent
Definition: event_manager.hpp:102
kobuki::ButtonEvent::Button1
@ Button1
Definition: event_manager.hpp:58
kobuki::CoreSensors::Flags::Button1
static const uint8_t Button1
Definition: core_sensors.hpp:72
kobuki::ButtonEvent::Button0
@ Button0
Definition: event_manager.hpp:57
kobuki::EventManager::sig_wheel_event
ecl::Signal< const WheelEvent & > sig_wheel_event
Definition: event_manager.hpp:146
kobuki::EventManager::update
void update(const CoreSensors::Data &new_state, const std::vector< uint16_t > &cliff_data)
Definition: event_manager.cpp:47
kobuki::CoreSensors::Data::charger
uint8_t charger
Definition: core_sensors.hpp:64
kobuki::BumperEvent::Left
@ Left
Definition: event_manager.hpp:61
kobuki::CoreSensors::Flags::LeftCliff
static const uint8_t LeftCliff
Definition: core_sensors.hpp:81
kobuki::CoreSensors::Data
Definition: core_sensors.hpp:54
kobuki::BumperEvent::Right
@ Right
Definition: event_manager.hpp:63
kobuki::EventManager::sig_bumper_event
ecl::Signal< const BumperEvent & > sig_bumper_event
Definition: event_manager.hpp:144
kobuki::RobotEvent::Offline
@ Offline
Definition: event_manager.hpp:108
kobuki
Definition: command.hpp:31
kobuki::CoreSensors::Data::bumper
uint8_t bumper
Definition: core_sensors.hpp:56
kobuki::ButtonEvent::Pressed
@ Pressed
Definition: event_manager.hpp:62
kobuki::EventManager::last_robot_state
RobotEvent::State last_robot_state
Definition: event_manager.hpp:141
kobuki::CoreSensors::Flags::RightCliff
static const uint8_t RightCliff
Definition: core_sensors.hpp:83
kobuki::ButtonEvent::Button2
@ Button2
Definition: event_manager.hpp:59
kobuki::PowerEvent::ChargeCompleted
@ ChargeCompleted
Definition: event_manager.hpp:96
kobuki::Battery::Adapter
@ Adapter
Definition: battery.hpp:66
kobuki::Battery::Charged
@ Charged
Definition: battery.hpp:69
kobuki::RobotEvent::state
enum kobuki::RobotEvent::State state
kobuki::CoreSensors::Flags::CenterBumper
static const uint8_t CenterBumper
Definition: core_sensors.hpp:77
kobuki::WheelEvent::Dropped
@ Dropped
Definition: event_manager.hpp:83
kobuki::Battery::Dangerous
@ Dangerous
Definition: battery.hpp:62
kobuki::CoreSensors::Flags::LeftBumper
static const uint8_t LeftBumper
Definition: core_sensors.hpp:76
kobuki::EventManager::sig_power_event
ecl::Signal< const PowerEvent & > sig_power_event
Definition: event_manager.hpp:147
kobuki::PowerEvent::PluggedToDockbase
@ PluggedToDockbase
Definition: event_manager.hpp:95
kobuki::PowerEvent::BatteryCritical
@ BatteryCritical
Definition: event_manager.hpp:98
kobuki::EventManager::last_digital_input
uint16_t last_digital_input
Definition: event_manager.hpp:140
kobuki::CliffEvent::Floor
@ Floor
Definition: event_manager.hpp:69
kobuki::CoreSensors::Data::buttons
uint8_t buttons
Definition: core_sensors.hpp:63
kobuki::CoreSensors::Flags::Button2
static const uint8_t Button2
Definition: core_sensors.hpp:73
kobuki::WheelEvent::Raised
@ Raised
Definition: event_manager.hpp:82
kobuki::EventManager::sig_button_event
ecl::Signal< const ButtonEvent & > sig_button_event
Definition: event_manager.hpp:143
kobuki::CoreSensors::Data::battery
uint8_t battery
Definition: core_sensors.hpp:65
kobuki::EventManager::init
void init(const std::string &sigslots_namespace)
Definition: event_manager.cpp:32
kobuki::RobotEvent
Definition: event_manager.hpp:106
kobuki::EventManager::sig_input_event
ecl::Signal< const InputEvent & > sig_input_event
Definition: event_manager.hpp:148
kobuki::WheelEvent::Right
@ Right
Definition: event_manager.hpp:87
kobuki::CoreSensors::Flags::LeftWheel
static const uint8_t LeftWheel
Definition: core_sensors.hpp:86
kobuki::ButtonEvent::Released
@ Released
Definition: event_manager.hpp:61
kobuki::ButtonEvent::button
enum kobuki::ButtonEvent::Button button
kobuki::Battery::Charging
@ Charging
Definition: battery.hpp:70
kobuki::CoreSensors::Flags::CenterCliff
static const uint8_t CenterCliff
Definition: core_sensors.hpp:82
kobuki::BumperEvent::Released
@ Released
Definition: event_manager.hpp:57
kobuki::CoreSensors::Flags::RightWheel
static const uint8_t RightWheel
Definition: core_sensors.hpp:87
kobuki::PowerEvent::BatteryLow
@ BatteryLow
Definition: event_manager.hpp:97
kobuki::PowerEvent::PluggedToAdapter
@ PluggedToAdapter
Definition: event_manager.hpp:94
kobuki::Battery::Low
@ Low
Definition: battery.hpp:63
kobuki::RobotEvent::Online
@ Online
Definition: event_manager.hpp:109
kobuki::CoreSensors::Flags::Button0
static const uint8_t Button0
Definition: core_sensors.hpp:71
kobuki::Battery::Discharging
@ Discharging
Definition: battery.hpp:68
kobuki::CliffEvent::Cliff
@ Cliff
Definition: event_manager.hpp:70
kobuki::EventManager::sig_cliff_event
ecl::Signal< const CliffEvent & > sig_cliff_event
Definition: event_manager.hpp:145
kobuki::ButtonEvent
Definition: event_manager.hpp:43
kobuki::BumperEvent::Pressed
@ Pressed
Definition: event_manager.hpp:58
kobuki::InputEvent::values
bool values[4]
Definition: event_manager.hpp:103
kobuki::CliffEvent::Center
@ Center
Definition: event_manager.hpp:74
kobuki::CoreSensors::Data::wheel_drop
uint8_t wheel_drop
Definition: core_sensors.hpp:57
kobuki::CliffEvent::Right
@ Right
Definition: event_manager.hpp:75
kobuki::CoreSensors::Data::cliff
uint8_t cliff
Definition: core_sensors.hpp:58
kobuki::CoreSensors::Flags::RightBumper
static const uint8_t RightBumper
Definition: core_sensors.hpp:78
kobuki::EventManager::sig_robot_event
ecl::Signal< const RobotEvent & > sig_robot_event
Definition: event_manager.hpp:149
kobuki::RobotEvent::State
State
Definition: event_manager.hpp:107
kobuki::EventManager::last_state
CoreSensors::Data last_state
Definition: event_manager.hpp:139
kobuki::BumperEvent::Center
@ Center
Definition: event_manager.hpp:62
kobuki::PowerEvent::Unplugged
@ Unplugged
Definition: event_manager.hpp:93


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Wed Mar 2 2022 00:26:14