Homepage Demos Overview Downloads Tutorials Reference
Credits

LedEngine.h

Go to the documentation of this file.
00001 #ifndef INCLUDED_LedEngine_h
00002 #define INCLUDED_LedEngine_h
00003 
00004 #include "Shared/RobotInfo.h"
00005 #include "Shared/get_time.h"
00006 #include "OutputCmd.h"
00007 #include <math.h>
00008 
00009 class MotionCommand;
00010 
00011 // LEDBitMask_t and associated constants are defined in RobotInfo.h
00012 
00013 //! Provides basic LED effects to anything that inherits from (recommended method for MotionCommands) or instantiates it (just in case you have reason to)
00014 /*! Provides a collection of special effects so that the code can be
00015  *  reused various places as feedback to to the user.
00016  *
00017  * Cycling ("pulsing") and single-value setting are mutually exclusive;
00018  * one will cut off the other
00019  *
00020  * A flash will invert and override the current setting, so that it
00021  * will "reset" after the flash.  Flashes change mid-range values to
00022  * extremes to make the flash visible (ie not just (1-current)) Normal
00023  * invert will do simple inverses (just (1-current))
00024  *
00025  * getSetting() returns value of last set();
00026  * getValue() returns what's actually being returned to Motion at the moment
00027  *
00028  * There's some nice functions for using the LEDs to display numbers.
00029  * This is handy for when you want to be free of the terminal.
00030  * <img src="NumberLEDs.jpg">
00031  *
00032  * The ERS-220 and ERS-7 have enough LEDs that they just use a "count
00033  * the lights" style of display instead of this pseudo-arabic display.
00034  * (look close to see that green bar LED at the top of the 210, which 
00035  * doesn't show up well in the camera image for some reason. )
00036  */
00037 
00038 class LedEngine {
00039  public:
00040   //!constructor - don't forget to call if you inherit
00041   LedEngine();
00042   //!destructor
00043   virtual ~LedEngine() {}
00044   
00045   //! call this from a MotionCommand's updateOutputs() - makes calls to MotionManager to update LED values
00046   /*! @param caller pass the "parent" motioncommand's address here (usually will pass 'this')
00047    *  @param mask a bitmask of which leds to update (uses weight of 1) */
00048   int updateLEDs(const MotionCommand* caller,LEDBitMask_t mask=AllLEDMask);
00049   
00050   //! call this from a MotionCommand's updateOutputs() - performs the calculations to update LEDs' values
00051   /*! @param cmds on input, used for weight values - on return, holds the resulting OutputCmd's*/
00052   int updateLEDs(OutputCmd cmds[NumLEDs]);
00053 
00054   //! call this from a MotionCommand's updateOutputs() - performs the calculations to update LEDs' values
00055   /*! @param cmds on input, used for weight values - on return, holds the resulting OutputCmd's*/
00056   int updateLEDFrames(OutputCmd cmds[NumLEDs][NumFrames]);
00057   
00058   //! recalculates #nextFlashEnd so we can tell when a flash has completed
00059   void recalcFlashEnd();
00060 
00061   //! returns true if there are changes since the last updateLEDs()
00062   int isDirty();
00063 
00064   //!sets the leds specified by @a leds to the inverse of their current value
00065   void invert(LEDBitMask_t leds);
00066   //!sets the leds specified by @a leds to @a value, clears all the rest
00067   inline void cset(LEDBitMask_t leds, float value) { clear(); set(leds,value); }
00068   //!sets the leds specified by @a leds to @a value
00069   void set(LEDBitMask_t leds, float value);
00070   //!sets the leds specified by @a leds to @a value for @a ms milliseconds, then sets back.  Clears ~leds
00071   void cflash(LEDBitMask_t leds, float value, unsigned int ms);
00072   //!sets the leds specified by @a leds to @a value for @a ms milliseconds, then sets back.
00073   void flash(LEDBitMask_t leds, float value, unsigned int ms);
00074   //!sets the leds specified by @a leds to either a much higher or much lower value for @a ms milliseconds, then sets back.
00075   void flash(LEDBitMask_t leds, unsigned int ms);
00076   //!causes the leds specified by @a leds to cycle between low and high, clears others.  See cycle() for parameter documentation.
00077   inline void ccycle(LEDBitMask_t leds, unsigned int period, float amp, float offset=0, int phase=0) { clear(); cycle(leds,period,amp,offset,phase); }
00078   //!causes the leds specified by @a leds to cycle between low and high
00079   void cycle(LEDBitMask_t leds, unsigned int period, float amp, float offset=0, int phase=0);
00080   //!sets all leds to 0.
00081   void clear();
00082 
00083   //!returns the current setting of the LED specified by @a led_id (the value you passed in set())
00084   float getSetting(LEDOffset_t led_id) { return infos[led_id-LEDOffset].value; }
00085   //!returns the current value of the LED specified by @a led_id (the value being expressed - may change if cycling for instance)
00086   float getValue(LEDOffset_t led_id,unsigned int planahead=0) { return calcValue(led_id-LEDOffset,get_time()+planahead); }
00087   
00088   //!holds a series of bit masks for the onedigit style of numerical display (0-10 and '.')
00089   /*!the hope is that these actually resemble the shapes of the numbers so people can
00090    * recognize them more easily - without converting base 2 in their heads. */
00091   static const LEDBitMask_t ERS210numMasks[11];
00092   static const LEDBitMask_t ERS220numMasks[11]; //!< bit masks for the ondigit style of numberical display - just count the LEDs on the head
00093   static const LEDBitMask_t ERS7numMasks[11]; //!< writes a number on the display
00094   //!Use these to specify a style for displaying numbers using displayNumber()
00095   enum numStyle_t {
00096     onedigit, //!< can display a number -9 thru 9, using #numMask.  For negative numbers, blinks the top bar - fast if it's supposed to be on, slow if it would otherwise be off
00097     twodigit  //!< can display a number -99 thru 99, using setOneOfTwo().  For negative numbers, sets the top bar to 1 (off otherwise).
00098   };
00099   //!Use these to specify a style for displaying a percentage value [0-1] using displayPercent()
00100   enum percentStyle_t {
00101     major, //!< shows overall value
00102     minor, //!< shows value within major tick
00103     none   //!< if you want to leave blank
00104   };
00105     
00106   //!Allows convenient display of numerical information to the LEDs on the face.
00107   /*!If overflow occurs, the face LEDs are set to flash on and off 3 every 333 milliseconds*/
00108   void displayNumber(int x, numStyle_t style);
00109   //!Allows convenient display of percentage information to the LEDs on the face.
00110   /*!Besides allowing a two-digit display, the 'edge' bar for each type is blinked to
00111    * denote how full it is.  So you can get up to a two-digit, base 5 display, with an
00112    * extra digit of estimated value.
00113    *
00114    * If overflow (>1) occurs, sets everything to .75. <br>
00115    * If underflow (<0) occurs, sets everything to .25.
00116    * 
00117    * The left and right columns are combined with an OR operation.  (they overlap on the top bar)
00118    * Left and right designations are <em>dog centric!</em> */
00119   void displayPercent(float x, percentStyle_t left_style, percentStyle_t right_style);
00120   
00121  protected:
00122   //!Performs the 'invert' calculation based on current value (returns 1-value)
00123   static float calcInvert(float value) {
00124     return 1-value;
00125   }
00126   //!Performs the 'flash' calculation based on current value (uses calcInvert() if value upper or lower third, 0 or 1 otherwise)
00127   static float calcFlash(float value) {
00128     if(value>.33333 && value<.66666)
00129       return (value<.5 ? 1 : 0);
00130     else
00131       return calcInvert(value);
00132   }
00133   //!Performs the 'cycle' calculation based on desired period, amplituted, amplitude offset, and time since start.  See cycle()
00134   static float calcCycle(unsigned int period, float amp, float offset, unsigned int t) {
00135     //    cout << period << ',' << amp << ',' << offset << ',' << time << " -> " << x;
00136     float x=cos(t*6.2831853/period)*(-amp/2)+.5+offset;
00137     return x;
00138   }
00139   //!Calculates the current value of led @a i for current time t
00140   float calcValue(unsigned int i, unsigned int t) {
00141     if(t<infos[i].flashtime)
00142       return infos[i].flashvalue;
00143     else if(infos[i].isCycling)
00144       return calcCycle(infos[i].period,infos[i].amp,infos[i].offset,t-infos[i].starttime);
00145     else
00146       return infos[i].value;
00147   }
00148   //!used by displayNumber() to determine settings of LEDs when using #numStyle_t::twodigit
00149   void setOneOfTwo(unsigned int x, unsigned int low, unsigned int mid, unsigned int high);
00150   //!used by displayPercent() to determine settings of LEDs
00151   void setColumn(float x, unsigned int low, unsigned int mid, unsigned int high, unsigned int top);
00152 
00153   //!Holds all the information needed by each of the LEDs
00154   struct LEDInfo {
00155     float value;           //!< the current value being expressed
00156     float amp;             //!< the amplitude of the cycle (if cycling)
00157     unsigned int period;    //!< the period of the cycle (if cycling)
00158     unsigned int starttime; //!< the start time of the cycle (if cycling)
00159     float offset;          //!< the phase shift from normal of the cycle (if cycling)
00160     float flashvalue;      //!< the value being 'flashed' (only valid if current time is less than flashtime
00161     unsigned int flashtime; //!< the time the 'flash' should retire
00162     bool isCycling;         //!< true if in cycle mode
00163   };
00164   
00165   LEDInfo infos[NumLEDs]; //!< the information regarding each of the LEDs
00166   bool dirty;             //!< true if changes since last updateLEDs: either at least one LED is cycling or a flash has begun/ended
00167   unsigned int numCycling;//!< the number of LEDs currently cycling (if non-zero, always dirty)
00168   unsigned int nextFlashEnd; //!< the soonest time a flash will end (and we'll become dirty)
00169 };
00170   
00171 /*! @file
00172  * @brief Describes LedEngine, which provides basic LED effects to anything that inherits or instantiates it
00173  * @author ejt (Creator)
00174  *
00175  * $Author: dst $
00176  * $Name: tekkotsu-2_2 $
00177  * $Revision: 1.17 $
00178  * $State: Exp $
00179  * $Date: 2004/09/12 04:22:37 $
00180  */
00181 
00182 #endif

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