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 #ifdef TGT_IS_CREATE2
00065 typedef struct CreateStatus_t{
00066 
00067     unsigned char bumpsWheelDrops;
00068     unsigned char wall;
00069     unsigned char cliffLeft;
00070     unsigned char cliffFrontLeft;
00071     unsigned char cliffFrontRight;
00072     unsigned char cliffRight;
00073     unsigned char virtualWall;
00074     unsigned char overcurrents;
00075     unsigned char ir;
00076     unsigned char buttons;
00077     float distance;
00078     float angle;
00079     unsigned char chargingState;
00080     unsigned short voltage;
00081     short current;
00082     char batteryTemperature;
00083     unsigned short batteryCharge;
00084     unsigned short batteryCapacity;
00085     unsigned short wallSignal;
00086     unsigned short cliffLeftSignal;
00087     unsigned short cliffFrontLeftSignal;
00088     unsigned short cliffFrontRightSignal;
00089     unsigned short cliffRightSignal;
00090     unsigned char chargingSourcesAvailable;
00091     unsigned char oiMode;
00092     unsigned char songNumber;
00093     unsigned char songPlay;
00094     unsigned char streamSize;
00095     short velocity;
00096     short radius;
00097     short rightVelocity;
00098     short leftVelocity;
00099 
00100 
00101     char dirt;
00102     unsigned char irLeft;
00103     unsigned char irRight;
00104     short encoderLeft;
00105     short encoderRight;
00106     unsigned char lightBumper;
00107     unsigned short lightBumperLeft;
00108     unsigned short lightBumperRight;
00109     unsigned short lightBumperFrontLeft;
00110     unsigned short lightBumperFrontRight;
00111     unsigned short lightBumperCenterLeft;
00112     unsigned short lightBumperCenterRight;
00113     short currentLeftMotor;
00114     short currentRightMotor;
00115     short currentMainBrush;
00116     short currentSideBrush;
00117     unsigned char stasis;
00118 } CreateStatus;
00119 #else
00120 typedef struct CreateStatus_t{
00121     unsigned short userAnalogInput;
00122     unsigned char userDigitalInputs;
00123     unsigned char bumpsWheelDrops;
00124     unsigned char wall;
00125     unsigned char cliffLeft;
00126     unsigned char cliffFrontLeft;
00127     unsigned char cliffFrontRight;
00128     unsigned char cliffRight;
00129     unsigned char virtualWall;
00130     unsigned char overcurrents;
00131     unsigned char ir;
00132     unsigned char buttons;
00133     short distance;
00134     short angle;
00135     unsigned char chargingState;
00136     unsigned short voltage;
00137     short current;
00138     char batteryTemperature;
00139     unsigned short batteryCharge;
00140     unsigned short batteryCapacity;
00141     unsigned short wallSignal;
00142     unsigned short cliffLeftSignal;
00143     unsigned short cliffFrontLeftSignal;
00144     unsigned short cliffFrontRightSignal;
00145     unsigned short cliffRightSignal;
00146     unsigned char chargingSourcesAvailable;
00147     unsigned char oiMode;
00148     unsigned char songNumber;
00149     unsigned char songPlay;
00150     unsigned char streamSize;
00151     short velocity;
00152     short radius;
00153     short rightVelocity;
00154     short leftVelocity;
00155 } CreateStatus;
00156 #endif
00157 //! description of CreateDriver
00158 class CreateDriver : public virtual DeviceDriver, public MotionHook, public DataSource, public virtual plist::PrimitiveListener {
00159 public:
00160   explicit CreateDriver(const std::string& name)
00161     : DeviceDriver(autoRegisterCreateDriver,name), MotionHook(), DataSource(),
00162       commName(), poller(&CreateDriver::advance,*this,TimeET(0L),TimeET(1L),true,CallbackPollThread::IGNORE_RETURN),
00163       motionActive(false), sensorsActive(false), firstMessage(true),
00164       frameNumber(0), globalStatus(), lastDesiredMode(0), packetFailures(0), lastPollTime(0),
00165                   prevEncoderLeft(0), prevEncoderRight(0), distOffset(0), angleOffset(0)
00166   {
00167     resetStatus(globalStatus);
00168     addEntry("CommPort",commName,"The name of the comm port where output will be sent");
00169   }
00170   virtual ~CreateDriver() {}
00171   
00172   virtual std::string getClassName() const { return autoRegisterCreateDriver; }
00173   
00174   virtual MotionHook* getMotionSink() { return dynamic_cast<MotionHook*>(this); }
00175   virtual void getSensorSources(std::map<std::string,DataSource*>& sources) {
00176     sources.clear();
00177     sources["Sensors"]=dynamic_cast<DataSource*>(this);
00178   }
00179   
00180   virtual void motionStarting();
00181   virtual bool isConnected();
00182   virtual void motionStopping();
00183   virtual void motionCheck(const float outputs[][NumOutputs]);
00184 
00185   virtual unsigned int nextTimestamp();
00186   virtual const std::string& nextName() { return instanceName; }
00187   virtual bool advance();
00188   virtual void registerSource();
00189   virtual void deregisterSource();
00190   virtual void enteringRealtime(const plist::Primitive<double>& simTimeScale) { DataSource::enteringRealtime(simTimeScale); }
00191   virtual void leavingRealtime(bool isFullSpeed) { DataSource::leavingRealtime(isFullSpeed); }
00192   
00193   virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00194   
00195   plist::Primitive<std::string> commName;
00196   
00197 protected:
00198   void doFreeze();
00199   void doUnfreeze();
00200   
00201   bool sendCommand(std::vector<unsigned char> bytes, unsigned int timeout);
00202   virtual void connect();
00203 
00204   virtual unsigned int readUnsignedChar(std::istream &is, unsigned char &chk);
00205   virtual unsigned int readUnsignedShort(std::istream &is, unsigned char &chk);
00206   virtual int readPacket(std::istream &is, const char &type, CreateStatus &createStatus, unsigned char &chk);
00207   bool attemptPacketRead(std::istream &is, CreateStatus &createStatus);
00208 
00209   bool attemptStreamRead(std::istream &is, CreateStatus &createStatus); //!< Not used at present
00210 
00211   void resetStatus(CreateStatus &status);
00212   void mergeStatus(CreateStatus &oldStatus, CreateStatus &newStatus);
00213 
00214   //! forwards call to DataSource::providingOutput() if the index is valid
00215   void provideOutput(unsigned int idx) { if(idx<NumOutputs) providingOutput(idx); }
00216   //! forwards call to DataSource::ignoringOutput() if the index is valid
00217   void ignoreOutput(unsigned int idx) { if(idx<NumOutputs) ignoringOutput(idx); }
00218   
00219   //! converts the value @a s from specified input's signal to voltage
00220   virtual float getAnalog(unsigned int inputIdx, unsigned char s);
00221   //! 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)
00222   virtual float getDigital(unsigned int inputIdx, unsigned char cur, unsigned char latch);
00223   
00224   //! allows LEDs to flicker at various frequencies to emulate having linear brightness control instead of boolean control
00225   inline bool calcLEDValue(unsigned int i,float x) {
00226     if(x<=0.0) {
00227       ledActivation[i]*=.9f; //decay activation... resets to keeps LEDs in sync, looks a little better
00228       return false;
00229     } else if(x>=1.0) {
00230       return true;
00231     } else {
00232       float x3 = x*x*x;
00233       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
00234       ledActivation[i]+=x;
00235       if(ledActivation[i]>=1) {
00236         ledActivation[i]-=1;
00237         return true;
00238       } else {
00239         return false;
00240       }
00241     }
00242   }
00243   float ledActivation[NumLEDs]; //!< used to track partial LED activation (see calcLEDValue())
00244   
00245   CallbackPollThread poller;
00246 
00247   bool motionActive;
00248   bool sensorsActive;
00249         bool firstMessage;
00250   unsigned int frameNumber;
00251   CreateStatus globalStatus;
00252   
00253   unsigned char lastDesiredMode;
00254   unsigned int packetFailures;
00255 private:
00256   //! holds the class name, set via registration with the DeviceDriver registry
00257   static const std::string autoRegisterCreateDriver;
00258 
00259         static const int PACKET_LENGTH = 52+4;
00260 
00261   unsigned int lastPollTime;
00262   short prevEncoderLeft, prevEncoderRight;
00263   float distOffset, angleOffset;
00264   static const unsigned int pollInterval = 100; // msec; was 250
00265 };
00266 
00267 /*! @file
00268  * @brief 
00269  * @author Benson Tsai (btsai)
00270  * @author Ethan Tira-Thompson (ejt)
00271  * @author Alex Grubb (agrub)
00272  */
00273 
00274 #endif

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:38 2016 by Doxygen 1.6.3