CreateDriver.h
Go to the documentation of this file.00001
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
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
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
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);
00210
00211 void resetStatus(CreateStatus &status);
00212 void mergeStatus(CreateStatus &oldStatus, CreateStatus &newStatus);
00213
00214
00215 void provideOutput(unsigned int idx) { if(idx<NumOutputs) providingOutput(idx); }
00216
00217 void ignoreOutput(unsigned int idx) { if(idx<NumOutputs) ignoringOutput(idx); }
00218
00219
00220 virtual float getAnalog(unsigned int inputIdx, unsigned char s);
00221
00222 virtual float getDigital(unsigned int inputIdx, unsigned char cur, unsigned char latch);
00223
00224
00225 inline bool calcLEDValue(unsigned int i,float x) {
00226 if(x<=0.0) {
00227 ledActivation[i]*=.9f;
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;
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];
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
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;
00265 };
00266
00267
00268
00269
00270
00271
00272
00273
00274 #endif