Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

CreateDriver.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_CreateDriver_h_
00003 #define INCLUDED_CreateDriver_h_
00004 
00005 #include "local/DeviceDriver.h"
00006 #include "local/MotionHook.h"
00007 #include "local/DataSource.h"
00008 #include "local/CommPort.h"
00009 #include "Shared/plist.h"
00010 #include "IPC/CallbackThread.h"
00011 #include <iostream>
00012 
00013 /*
00014 Specs
00015 Big Endian <---!!!!!
00016 
00017 
00018 Motors: 5
00019 0 - LD1          - 0.5A
00020 1 - LD0          - 0.5A
00021 2 - LD2          - 1.6A
00022 3 - Right Wheel  - 1.0A
00023 4 - Left Wheel   - 1.0A
00024 
00025 Digital Inputs: 4
00026 
00027 Digital Outputs: 3
00028 
00029 LEDs: 2 (Play + Advanced)
00030 
00031 Buttons: 2 (Play + Advanced)
00032 
00033 Sensors: 5
00034 0 - bump right
00035 1 - bump left
00036 2 - wheeldrop right
00037 3 - wheeldrop left
00038 4 - wheeldrop caster
00039 
00040 Wall Sensor
00041 
00042 Cliff Left
00043 
00044 Cliff Right
00045 
00046 Cliff Front Left
00047 
00048 Cliff Front Right
00049 
00050 Distance (per wheel) -- capped if not polled frequent enough
00051 
00052 Angle (per wheel) -- capped if not polled frequent enough
00053 
00054 Voltage
00055 
00056 Current
00057 
00058 Battery Temperature
00059 
00060 Battery Charge
00061 
00062 Battery Capacity (not accurate for alkaline)
00063 */
00064 
00065 typedef struct CreateStatus_t{
00066     unsigned char bumpsWheelDrops;
00067     unsigned char wall;
00068     unsigned char cliffLeft;
00069     unsigned char cliffFrontLeft;
00070     unsigned char cliffFrontRight;
00071     unsigned char cliffRight;
00072     unsigned char virtualWall;
00073     unsigned char overcurrents;
00074     unsigned char ir;
00075     unsigned char buttons;
00076     short distance;
00077     short angle;
00078     unsigned char chargingState;
00079     unsigned short voltage;
00080     short current;
00081     char batteryTemperature;
00082     unsigned short batteryCharge;
00083     unsigned short batteryCapacity;
00084     unsigned short wallSignal;
00085     unsigned short cliffLeftSignal;
00086     unsigned short cliffFrontLeftSignal;
00087     unsigned short cliffFrontRightSignal;
00088     unsigned short cliffRightSignal;
00089     unsigned char userDigitalInputs;
00090     unsigned short userAnalogInput;
00091     unsigned char chargingSourcesAvailable;
00092     unsigned char oiMode;
00093     unsigned char songNumber;
00094     unsigned char songPlay;
00095     unsigned char streamSize;
00096     short velocity;
00097     short radius;
00098     short rightVelocity;
00099     short leftVelocity;
00100 } CreateStatus;
00101 
00102 //! description of CreateDriver
00103 class CreateDriver : public virtual DeviceDriver, public MotionHook, public DataSource, public virtual plist::PrimitiveListener {
00104 public:
00105   explicit CreateDriver(const std::string& name)
00106     : DeviceDriver(autoRegisterCreateDriver,name), MotionHook(), DataSource(),
00107       commName(), poller(&CreateDriver::advance,*this,TimeET(0L),TimeET(1L),true,CallbackPollThread::IGNORE_RETURN),
00108       motionActive(false), sensorsActive(false),
00109       frameNumber(0), globalStatus(), lastDesiredMode(0), packetFailures(0), lastPollTime(0)
00110   {
00111     resetStatus(globalStatus);
00112     addEntry("CommPort",commName,"The name of the comm port where output will be sent");
00113   }
00114   virtual ~CreateDriver() {}
00115   
00116   virtual std::string getClassName() const { return autoRegisterCreateDriver; }
00117   
00118   virtual MotionHook* getMotionSink() { return dynamic_cast<MotionHook*>(this); }
00119   virtual void getSensorSources(std::map<std::string,DataSource*>& sources) {
00120     sources.clear();
00121     sources["Sensors"]=dynamic_cast<DataSource*>(this);
00122   }
00123   
00124   virtual void motionStarting();
00125   virtual bool isConnected();
00126   virtual void motionStopping();
00127   virtual void motionCheck(const float outputs[][NumOutputs]);
00128 
00129   virtual unsigned int nextTimestamp();
00130   virtual const std::string& nextName() { return instanceName; }
00131   virtual bool advance();
00132   virtual void registerSource();
00133   virtual void deregisterSource();
00134   virtual void enteringRealtime(const plist::Primitive<double>& simTimeScale) { DataSource::enteringRealtime(simTimeScale); }
00135   virtual void leavingRealtime(bool isFullSpeed) { DataSource::leavingRealtime(isFullSpeed); }
00136   
00137   virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00138   
00139   plist::Primitive<std::string> commName;
00140   
00141 protected:
00142   void doFreeze();
00143   void doUnfreeze();
00144   
00145   bool sendCommand(std::vector<unsigned char> bytes, unsigned int timeout);
00146   virtual void connect();
00147 
00148   virtual char readChar(std::istream &is, unsigned char &chk);
00149   virtual unsigned char readUnsignedChar(std::istream &is, unsigned char &chk);
00150   virtual short readShort(std::istream &is, unsigned char &chk);
00151   virtual unsigned short readUnsignedShort(std::istream &is, unsigned char &chk);
00152 
00153   virtual int readPacket(std::istream &is, const char &type, CreateStatus &createStatus, unsigned char &chk);
00154 
00155   bool attemptPacketRead(std::istream &is, CreateStatus &createStatus);
00156 
00157   bool attemptStreamRead(std::istream &is, CreateStatus &createStatus); //!< Not used at present
00158 
00159   void resetStatus(CreateStatus &status);
00160   void mergeStatus(CreateStatus &oldStatus, CreateStatus &newStatus);
00161 
00162   //! forwards call to DataSource::providingOutput() if the index is valid
00163   void provideOutput(unsigned int idx) { if(idx<NumOutputs) providingOutput(idx); }
00164   //! forwards call to DataSource::ignoringOutput() if the index is valid
00165   void ignoreOutput(unsigned int idx) { if(idx<NumOutputs) ignoringOutput(idx); }
00166   
00167   //! converts the value @a s from specified input's signal to voltage
00168   virtual float getAnalog(unsigned int inputIdx, unsigned char s);
00169   //! converts the value @a cur and @a latch to the output format (0 if low, 0.5 if high but has been low, 1 if consistent high)
00170   virtual float getDigital(unsigned int inputIdx, unsigned char cur, unsigned char latch);
00171   
00172   //! allows LEDs to flicker at various frequencies to emulate having linear brightness control instead of boolean control
00173   inline bool calcLEDValue(unsigned int i,float x) {
00174     if(x<=0.0) {
00175       ledActivation[i]*=.9f; //decay activation... resets to keeps LEDs in sync, looks a little better
00176       return false;
00177     } else if(x>=1.0) {
00178       return true;
00179     } else {
00180       float x3 = x*x*x;
00181       x = .25f*x3*x3 + .75f*x; // fancy but unscientific equation to "gamma correct" - we can see a single pulse better than a single flicker - after image and all that
00182       ledActivation[i]+=x;
00183       if(ledActivation[i]>=1) {
00184         ledActivation[i]-=1;
00185         return true;
00186       } else {
00187         return false;
00188       }
00189     }
00190   }
00191   float ledActivation[NumLEDs]; //!< used to track partial LED activation (see calcLEDValue())
00192   
00193   CallbackPollThread poller;
00194 
00195   bool motionActive;
00196   bool sensorsActive;
00197   unsigned int frameNumber;
00198   
00199         CreateStatus globalStatus;
00200   unsigned char lastDesiredMode;
00201   unsigned int packetFailures;
00202   
00203 private:
00204   //! holds the class name, set via registration with the DeviceDriver registry
00205   static const std::string autoRegisterCreateDriver;
00206 
00207         static const int PACKET_LENGTH = 52+4;
00208 
00209   unsigned int lastPollTime;
00210   static const unsigned int pollInterval = 250; // msec
00211 };
00212 
00213 /*! @file
00214  * @brief 
00215  * @author Benson Tsai (btsai)
00216  * @author Ethan Tira-Thompson (ejt)
00217  * @author Alex Grubb (agrub)
00218  */
00219 
00220 #endif

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Tue Jan 31 04:35:30 2012 by Doxygen 1.6.3