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

Tekkotsu v2.2
Generated Tue Oct 19 14:19:13 2004 by Doxygen 1.3.9.1