Homepage Demos Overview Downloads Tutorials Reference
Credits

BatteryCheckControl.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_BatteryCheckControl_h_
00003 #define INCLUDED_BatteryCheckControl_h_
00004 
00005 #include "ControlBase.h"
00006 #include "Shared/WorldState.h"
00007 #include "Motion/MMAccessor.h"
00008 #include "Motion/LedMC.h"
00009 #include "NullControl.h"
00010 #include "Events/EventRouter.h"
00011 
00012 //! when activated, this will print a battery report to stdout and light up LEDs to specify power level
00013 /*! The LEDs use the LedEngine::displayPercent() function, with minor/major style.  This means
00014  *  the left column (viewing the dog head on) will show the overall power level, and the
00015  *  right column will show the level within the tick lit up in the left column.  The more geeky
00016  *  among you may prefer to think of this as a two digit base 5 display.
00017  *
00018  *  This gives you pretty precise visual feedback as to remaining power (perhaps more than
00019  *  you really need, but it's as much a demo as a useful tool)
00020  *
00021  *  This is implemented as a Control instead of a Behavior on the assumption you
00022  *  wouldn't want to leave this running while you were doing other things (ie not
00023  *  in e-stop). But it definitely blurs the line between the two.
00024  */
00025 class BatteryCheckControl : public ControlBase, public EventListener {
00026 public:
00027 
00028   //!Constructor
00029   BatteryCheckControl() : ControlBase("Battery Check","Reports % power remaining, and gives details on console") {}
00030 
00031   //!Destructor
00032   virtual ~BatteryCheckControl() {}
00033 
00034   //!Prints a report to stdio and lights up the face to show battery level
00035   /*! keeps running until deactivated - will listen for power events and continue to update display */
00036   virtual ControlBase * activate(MotionManager::MC_ID display, Socket * gui) {
00037     sout->printf("Press the \"back\" button to leave battery display\n");
00038     erouter->addListener(this,EventBase::powerEGID);
00039     return ControlBase::activate(display,gui);
00040   }
00041   //! stops listening for power events and sets display to invalid
00042   virtual void pause() {
00043     erouter->forgetListener(this);
00044     display_id=MotionManager::invalid_MC_ID;
00045   }
00046   //! calls report()
00047   virtual void refresh() {
00048     report();
00049     if(gui_comm!=NULL && wireless->isConnected(gui_comm->sock)) {
00050       char tmp[20];
00051       sprintf(tmp,"%d",(unsigned int)(state->sensors[PowerRemainOffset]*100));
00052       //    pushSlot(new NullControl(std::string("Power remain: ")+tmp+std::string("%"),"See console output for details"));
00053       std::string s("refresh\n");
00054       s+=getName()+"\n1\n0\n0\nPower remain: ";
00055       s+=tmp;
00056       s+="%\nSee console output for details\n";
00057       s+="status\nPower remaining: ";
00058       s+=tmp;
00059       s+="%\n";
00060       gui_comm->write((const byte*)s.c_str(),s.size());
00061     }
00062   }
00063   //! stops listening for power events and sets display to invalid
00064   virtual void deactivate() {
00065     erouter->forgetListener(this);
00066     display_id=MotionManager::invalid_MC_ID;
00067   }
00068   //! calls refresh() to redisplay with new information if it's not a vibration event
00069   virtual void processEvent(const EventBase& event) {
00070     if(event.getSourceID()!=PowerSourceID::VibrationSID)
00071       refresh();
00072   }
00073   virtual ControlBase * doSelect() {
00074     return this;
00075   }
00076   //! redisplay text to sout and refresh LED values
00077   void report() {
00078     sout->printf("BATTERY REPORT:\n");
00079     sout->printf("\tPower Remain:\t%d%%\n",(int)(state->sensors[PowerRemainOffset]*100));
00080     sout->printf("\tCapacity:\t%d\n",state->sensors[PowerCapacityOffset]);
00081     sout->printf("\tVoltage:\t%g\n",state->sensors[PowerVoltageOffset]);
00082     sout->printf("\tCurrent:\t%d\n",state->sensors[PowerCurrentOffset]);
00083     sout->printf("\tTemperature:\t%g\n",state->sensors[PowerThermoOffset]);
00084     sout->printf("\tFlags:\t");
00085     if(state->powerFlags[PowerSourceID::BatteryConnectSID])
00086       sout->printf("BatteryConnect ");
00087     if(state->powerFlags[PowerSourceID::DischargingSID])
00088       sout->printf("Discharging ");
00089     if(state->powerFlags[PowerSourceID::ChargingSID])
00090       sout->printf("Charging ");
00091     if(state->powerFlags[PowerSourceID::ExternalPowerSID])
00092       sout->printf("ExternalPower ");
00093     if(state->powerFlags[PowerSourceID::PowerGoodSID])
00094       sout->printf("PowerGood ");
00095     if(state->powerFlags[PowerSourceID::LowPowerWarnSID])
00096       sout->printf("LowPowerWarn ");
00097     if(state->powerFlags[PowerSourceID::BatteryEmptySID])
00098       sout->printf("BatteryEmpty ");
00099     sout->printf("\n");
00100     if(display_id!=MotionManager::invalid_MC_ID) {
00101       MMAccessor<LedMC> disp(display_id);
00102       disp->displayPercent(state->sensors[PowerRemainOffset],LedEngine::major,LedEngine::major);
00103     }
00104   }
00105 
00106 };
00107 
00108 /*! @file
00109  * @brief Defines BatteryCheckControl, which will spew a power report to stdout upon activation
00110  * @author ejt (Creator)
00111  *
00112  * $Author: ejt $
00113  * $Name: tekkotsu-1_5 $
00114  * $Revision: 1.7 $
00115  * $State: Rel $
00116  * $Date: 2003/07/26 01:48:03 $
00117  */
00118 
00119 #endif

Tekkotsu v1.5
Generated Fri Oct 10 15:51:58 2003 by Doxygen 1.3.4