Homepage | Demos | Overview | Downloads | Tutorials | Reference | Credits |
LedEngine.hGo 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, this 00035 * isn't actually too hard to read once you "see" it) 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 //!returns true if there are changes since the last updateLEDs() 00059 int isDirty(); 00060 00061 //!sets the leds specified by @a leds to the inverse of their current value 00062 void invert(LEDBitMask_t leds); 00063 //!sets the leds specified by @a leds to @a value, clears all the rest 00064 inline void cset(LEDBitMask_t leds, float value) { clear(); set(leds,value); } 00065 //!sets the leds specified by @a leds to @a value 00066 void set(LEDBitMask_t leds, float value); 00067 //!sets the leds specified by @a leds to @a value for @a ms milliseconds, then sets back. Clears ~leds 00068 void cflash(LEDBitMask_t leds, float value, unsigned int ms); 00069 //!sets the leds specified by @a leds to @a value for @a ms milliseconds, then sets back. 00070 void flash(LEDBitMask_t leds, float value, unsigned int ms); 00071 //!sets the leds specified by @a leds to either a much higher or much lower value for @a ms milliseconds, then sets back. 00072 void flash(LEDBitMask_t leds, unsigned int ms); 00073 //!causes the leds specified by @a leds to cycle between low and high, clears others. See cycle() for parameter documentation. 00074 inline void ccycle(LEDBitMask_t leds, unsigned int period, float amp, float offset=0, int phase=0) { clear(); cycle(leds,period,amp,offset,phase); } 00075 //!causes the leds specified by @a leds to cycle between low and high 00076 void cycle(LEDBitMask_t leds, unsigned int period, float amp, float offset=0, int phase=0); 00077 //!sets all leds to 0. 00078 void clear(); 00079 00080 //!returns the current setting of the LED specified by @a led_id (the value you passed in set()) 00081 float getSetting(LEDOffset_t led_id) { return infos[led_id-LEDOffset].value; } 00082 //!returns the current value of the LED specified by @a led_id (the value being expressed - may change if cycling for instance) 00083 float getValue(LEDOffset_t led_id,unsigned int planahead=0) { return calcValue(led_id-LEDOffset,get_time()+planahead); } 00084 00085 //!holds a series of bit masks for the onedigit style of numerical display (0-10 and '.') 00086 /*!the hope is that these actually resemble the shapes of the numbers so people can 00087 * recognize them more easily - without converting base 2 in their heads. */ 00088 static const LEDBitMask_t ERS210numMasks[11]; 00089 static const LEDBitMask_t ERS220numMasks[11]; //!< bit masks for the ondigit style of numberical display - just count the LEDs on the head 00090 static const LEDBitMask_t ERS7numMasks[11]; //!< writes a number on the display 00091 //!Use these to specify a style for displaying numbers using displayNumber() 00092 enum numStyle_t { 00093 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 00094 twodigit //!< can display a number -99 thru 99, using setOneOfTwo(). For negative numbers, sets the top bar to 1 (off otherwise). 00095 }; 00096 //!Use these to specify a style for displaying a percentage value [0-1] using displayPercent() 00097 enum percentStyle_t { 00098 major, //!< shows overall value 00099 minor, //!< shows value within major tick 00100 none //!< if you want to leave blank 00101 }; 00102 00103 //!Allows convenient display of numerical information to the LEDs on the face. 00104 /*!If overflow occurs, the face LEDs are set to flash on and off 3 every 333 milliseconds*/ 00105 void displayNumber(int x, numStyle_t style); 00106 //!Allows convenient display of percentage information to the LEDs on the face. 00107 /*!Besides allowing a two-digit display, the 'edge' bar for each type is blinked to 00108 * denote how full it is. So you can get up to a two-digit, base 5 display, with an 00109 * extra digit of estimated value. 00110 * 00111 * If overflow (>1) occurs, sets everything to .75. <br> 00112 * If underflow (<0) occurs, sets everything to .25. 00113 * 00114 * The left and right columns are combined with an OR operation. (they overlap on the top bar) 00115 * Left and right designations are <em>dog centric!</em> */ 00116 void displayPercent(float x, percentStyle_t left_style, percentStyle_t right_style); 00117 00118 protected: 00119 //!Performs the 'invert' calculation based on current value (returns 1-value) 00120 static float calcInvert(float value) { 00121 return 1-value; 00122 } 00123 //!Performs the 'flash' calculation based on current value (uses calcInvert() if value upper or lower third, 0 or 1 otherwise) 00124 static float calcFlash(float value) { 00125 if(value>.33333 && value<.66666) 00126 return (value<.5 ? 1 : 0); 00127 else 00128 return calcInvert(value); 00129 } 00130 //!Performs the 'cycle' calculation based on desired period, amplituted, amplitude offset, and time since start. See cycle() 00131 static float calcCycle(unsigned int period, float amp, float offset, unsigned int t) { 00132 // cout << period << ',' << amp << ',' << offset << ',' << time << " -> " << x; 00133 float x=cos(t*6.2831853/period)*(-amp/2)+.5+offset; 00134 return x; 00135 } 00136 //!Calculates the current value of led @a i for current time t 00137 float calcValue(unsigned int i, unsigned int t) { 00138 if(t<infos[i].flashtime) 00139 return infos[i].flashvalue; 00140 else if(infos[i].isCycling) 00141 return calcCycle(infos[i].period,infos[i].amp,infos[i].offset,t-infos[i].starttime); 00142 else 00143 return infos[i].value; 00144 } 00145 //!used by displayNumber() to determine settings of LEDs when using #numStyle_t::twodigit 00146 void setOneOfTwo(unsigned int x, unsigned int low, unsigned int mid, unsigned int high); 00147 //!used by displayPercent() to determine settings of LEDs 00148 void setColumn(float x, unsigned int low, unsigned int mid, unsigned int high, unsigned int top); 00149 00150 //!Holds all the information needed by each of the LEDs 00151 struct LEDInfo { 00152 float value; //!< the current value being expressed 00153 float amp; //!< the amplitude of the cycle (if cycling) 00154 unsigned int period; //!< the period of the cycle (if cycling) 00155 unsigned int starttime; //!< the start time of the cycle (if cycling) 00156 float offset; //!< the phase shift from normal of the cycle (if cycling) 00157 float flashvalue; //!< the value being 'flashed' (only valid if current time is less than flashtime 00158 unsigned int flashtime; //!< the time the 'flash' should retire 00159 bool isCycling; //!< true if in cycle mode 00160 }; 00161 00162 LEDInfo infos[NumLEDs]; //!< the information regarding each of the LEDs 00163 unsigned int numCycling;//!< the number of LEDs currently cycling (if non-zero, always dirty) 00164 bool dirty; //!< true if changes since last updateLEDs 00165 unsigned int dirtyTime; //!< the time at which it becomes dirty again (if flashing) 00166 }; 00167 00168 /*! @file 00169 * @brief Describes LedEngine, which provides basic LED effects to anything that inherits or instantiates it 00170 * @author ejt (Creator) 00171 * 00172 * $Author: ejt $ 00173 * $Name: tekkotsu-2_1 $ 00174 * $Revision: 1.13 $ 00175 * $State: Exp $ 00176 * $Date: 2004/02/09 22:45:28 $ 00177 */ 00178 00179 #endif |
Tekkotsu v2.1 |
Generated Tue Mar 16 23:19:13 2004 by Doxygen 1.3.5 |