event_manager.cpp
Go to the documentation of this file.
00001 
00010 /*****************************************************************************
00011 ** Includes
00012 *****************************************************************************/
00013 
00014 #include "../../include/kobuki_driver/event_manager.hpp"
00015 #include "../../include/kobuki_driver/modules/battery.hpp"
00016 #include "../../include/kobuki_driver/packets/core_sensors.hpp"
00017 
00018 /*****************************************************************************
00019 ** Namespaces
00020 *****************************************************************************/
00021 
00022 namespace kobuki {
00023 
00024 /*****************************************************************************
00025 ** Implementation
00026 *****************************************************************************/
00027 
00028 void EventManager::init ( const std::string &sigslots_namespace ) {
00029   sig_button_event.connect(sigslots_namespace + std::string("/button_event"));
00030   sig_bumper_event.connect(sigslots_namespace + std::string("/bumper_event"));
00031   sig_cliff_event.connect(sigslots_namespace  + std::string("/cliff_event"));
00032   sig_wheel_event.connect(sigslots_namespace  + std::string("/wheel_event"));
00033   sig_power_event.connect(sigslots_namespace  + std::string("/power_event"));
00034   sig_input_event.connect(sigslots_namespace  + std::string("/input_event"));
00035   sig_robot_event.connect(sigslots_namespace  + std::string("/robot_event"));
00036 }
00037 
00043 void EventManager::update(const CoreSensors::Data &new_state, const std::vector<uint16_t> &cliff_data) {
00044   if (last_state.buttons != new_state.buttons)
00045   {
00046     // ------------
00047     // Button Event
00048     // ------------
00049 
00050     // Note that the touch pad means at most one button can be pressed
00051     // at a time.
00052     ButtonEvent event;
00053 
00054     // Check changes in each button state's; even if this block of code
00055     // supports it, two buttons cannot be pressed simultaneously
00056     if ((new_state.buttons ^ last_state.buttons) & CoreSensors::Flags::Button0) {
00057       event.button = ButtonEvent::Button0;
00058       if (new_state.buttons & CoreSensors::Flags::Button0) {
00059         event.state = ButtonEvent::Pressed;
00060       } else {
00061         event.state = ButtonEvent::Released;
00062       }
00063       sig_button_event.emit(event);
00064     }
00065 
00066     if ((new_state.buttons ^ last_state.buttons) & CoreSensors::Flags::Button1) {
00067       event.button = ButtonEvent::Button1;
00068       if (new_state.buttons & CoreSensors::Flags::Button1) {
00069         event.state = ButtonEvent::Pressed;
00070       } else {
00071         event.state = ButtonEvent::Released;
00072       }
00073       sig_button_event.emit(event);
00074     }
00075 
00076     if ((new_state.buttons ^ last_state.buttons) & CoreSensors::Flags::Button2) {
00077       event.button = ButtonEvent::Button2;
00078       if (new_state.buttons & CoreSensors::Flags::Button2) {
00079         event.state = ButtonEvent::Pressed;
00080       } else {
00081         event.state = ButtonEvent::Released;
00082       }
00083       sig_button_event.emit(event);
00084     }
00085   }
00086 
00087   // ------------
00088   // Bumper Event
00089   // ------------
00090 
00091   if (last_state.bumper != new_state.bumper)
00092   {
00093     BumperEvent event;
00094 
00095     // Check changes in each bumper state's and raise an event if so
00096     if ((new_state.bumper ^ last_state.bumper) & CoreSensors::Flags::LeftBumper) {
00097       event.bumper = BumperEvent::Left;
00098       if (new_state.bumper & CoreSensors::Flags::LeftBumper) {
00099         event.state = BumperEvent::Pressed;
00100       } else {
00101         event.state = BumperEvent::Released;
00102       }
00103       sig_bumper_event.emit(event);
00104     }
00105 
00106     if ((new_state.bumper ^ last_state.bumper) & CoreSensors::Flags::CenterBumper) {
00107       event.bumper = BumperEvent::Center;
00108       if (new_state.bumper & CoreSensors::Flags::CenterBumper) {
00109         event.state = BumperEvent::Pressed;
00110       } else {
00111         event.state = BumperEvent::Released;
00112       }
00113       sig_bumper_event.emit(event);
00114     }
00115 
00116     if ((new_state.bumper ^ last_state.bumper) & CoreSensors::Flags::RightBumper) {
00117       event.bumper = BumperEvent::Right;
00118       if (new_state.bumper & CoreSensors::Flags::RightBumper) {
00119         event.state = BumperEvent::Pressed;
00120       } else {
00121         event.state = BumperEvent::Released;
00122       }
00123       sig_bumper_event.emit(event);
00124     }
00125   }
00126 
00127   // ------------
00128   // Cliff Event
00129   // ------------
00130 
00131   if (last_state.cliff != new_state.cliff)
00132   {
00133     CliffEvent event;
00134 
00135     // Check changes in each cliff sensor state's and raise an event if so
00136     if ((new_state.cliff ^ last_state.cliff) & CoreSensors::Flags::LeftCliff) {
00137       event.sensor = CliffEvent::Left;
00138       if (new_state.cliff & CoreSensors::Flags::LeftCliff) {
00139         event.state = CliffEvent::Cliff;
00140       } else {
00141         event.state = CliffEvent::Floor;
00142       }
00143       event.bottom = cliff_data[event.sensor];
00144       sig_cliff_event.emit(event);
00145     }
00146 
00147     if ((new_state.cliff ^ last_state.cliff) & CoreSensors::Flags::CenterCliff) {
00148       event.sensor = CliffEvent::Center;
00149       if (new_state.cliff & CoreSensors::Flags::CenterCliff) {
00150         event.state = CliffEvent::Cliff;
00151       } else {
00152         event.state = CliffEvent::Floor;
00153       }
00154       event.bottom = cliff_data[event.sensor];
00155       sig_cliff_event.emit(event);
00156     }
00157 
00158     if ((new_state.cliff ^ last_state.cliff) & CoreSensors::Flags::RightCliff) {
00159       event.sensor = CliffEvent::Right;
00160       if (new_state.cliff & CoreSensors::Flags::RightCliff) {
00161         event.state = CliffEvent::Cliff;
00162       } else {
00163         event.state = CliffEvent::Floor;
00164       }
00165       event.bottom = cliff_data[event.sensor];
00166       sig_cliff_event.emit(event);
00167     }
00168   }
00169 
00170   // ------------
00171   // Wheel Drop Event
00172   // ------------
00173 
00174   if (last_state.wheel_drop != new_state.wheel_drop)
00175   {
00176     WheelEvent event;
00177 
00178     // Check changes in each wheel_drop sensor state's and raise an event if so
00179     if ((new_state.wheel_drop ^ last_state.wheel_drop) & CoreSensors::Flags::LeftWheel) {
00180       event.wheel = WheelEvent::Left;
00181       if (new_state.wheel_drop & CoreSensors::Flags::LeftWheel) {
00182         event.state = WheelEvent::Dropped;
00183       } else {
00184         event.state = WheelEvent::Raised;
00185       }
00186       sig_wheel_event.emit(event);
00187     }
00188 
00189     if ((new_state.wheel_drop ^ last_state.wheel_drop) & CoreSensors::Flags::RightWheel) {
00190       event.wheel = WheelEvent::Right;
00191       if (new_state.wheel_drop & CoreSensors::Flags::RightWheel) {
00192         event.state = WheelEvent::Dropped;
00193       } else {
00194         event.state = WheelEvent::Raised;
00195       }
00196       sig_wheel_event.emit(event);
00197     }
00198   }
00199 
00200   // ------------
00201   // Power System Event
00202   // ------------
00203 
00204   if (last_state.charger != new_state.charger)
00205   {
00206     Battery battery_new(new_state.battery, new_state.charger);
00207     Battery battery_last(last_state.battery, last_state.charger);
00208 
00209     if (battery_last.charging_state != battery_new.charging_state)
00210     {
00211       PowerEvent event;
00212       switch (battery_new.charging_state)
00213       {
00214         case Battery::Discharging:
00215           event.event = PowerEvent::Unplugged;
00216           break;
00217         case Battery::Charged:
00218           event.event = PowerEvent::ChargeCompleted;
00219           break;
00220         case Battery::Charging:
00221           if (battery_new.charging_source == Battery::Adapter)
00222             event.event = PowerEvent::PluggedToAdapter;
00223           else
00224             event.event = PowerEvent::PluggedToDockbase;
00225           break;
00226       }
00227       sig_power_event.emit(event);
00228     }
00229   }
00230 
00231   if (last_state.battery > new_state.battery)
00232   {
00233     Battery battery_new(new_state.battery, new_state.charger);
00234     Battery battery_last(last_state.battery, last_state.charger);
00235 
00236     if (battery_last.level() != battery_new.level())
00237     {
00238       PowerEvent event;
00239       switch (battery_new.level())
00240       {
00241         case Battery::Low:
00242           event.event = PowerEvent::BatteryLow;
00243           break;
00244         case Battery::Dangerous:
00245           event.event = PowerEvent::BatteryCritical;
00246           break;
00247         default:
00248           break;
00249       }
00250       sig_power_event.emit(event);
00251     }
00252   }
00253 
00254   last_state = new_state;
00255 }
00256 
00261 void EventManager::update(const uint16_t &new_digital_input)
00262 {
00263   if (last_digital_input != new_digital_input)
00264   {
00265     InputEvent event;
00266 
00267     event.values[0] = new_digital_input&0x0001;
00268     event.values[1] = new_digital_input&0x0002;
00269     event.values[2] = new_digital_input&0x0004;
00270     event.values[3] = new_digital_input&0x0008;
00271 
00272     sig_input_event.emit(event);
00273 
00274     last_digital_input = new_digital_input;
00275   }
00276 }
00277 
00283 void EventManager::update(bool is_plugged, bool is_alive)
00284 {
00285   RobotEvent::State robot_state =
00286       (is_plugged && is_alive)?RobotEvent::Online:RobotEvent::Offline;
00287   if (last_robot_state != robot_state)
00288   {
00289     RobotEvent event;
00290     event.state = robot_state;
00291 
00292     sig_robot_event.emit(event);
00293 
00294     last_robot_state = robot_state;
00295   }
00296 }
00297 
00298 } // namespace kobuki


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Mon Oct 6 2014 01:31:10