KobukiDriver.h
Go to the documentation of this file.00001 #ifndef INCLUDED_KobukiDriver_h_
00002 #define INCLUDED_KobukiDriver_h_
00003
00004 #include "local/DeviceDriver.h"
00005 #include "local/MotionHook.h"
00006 #include "local/DataSource.h"
00007 #include "local/CommPort.h"
00008 #include "Shared/plist.h"
00009 #include "IPC/CallbackThread.h"
00010 #include <iostream>
00011
00012 namespace Kobuki {
00013
00014 struct CoreSensor {
00015 unsigned char header;
00016 unsigned char length;
00017 unsigned char timestamp[2];
00018 unsigned char bumper;
00019 unsigned char wheelDrop;
00020 unsigned char cliff;
00021 unsigned char leftEncoder[2];
00022 unsigned char rightEncoder[2];
00023 unsigned char leftPwm;
00024 unsigned char rightPwm;
00025 unsigned char buttons;
00026 unsigned char charger;
00027 unsigned char battery;
00028 unsigned char overCurrent;
00029 };
00030
00031 struct DockInfraRed {
00032 unsigned char header;
00033 unsigned char length;
00034 unsigned char docking[3];
00035 };
00036
00037 struct Inertia {
00038 unsigned char header;
00039 unsigned char length;
00040 unsigned char angle[2];
00041 unsigned char angleRate[2];
00042 unsigned char acc[3];
00043 };
00044
00045 struct Cliff {
00046 unsigned char header;
00047 unsigned char length;
00048 unsigned char bottom0[2];
00049 unsigned char bottom1[2];
00050 unsigned char bottom2[2];
00051 };
00052
00053 struct Current {
00054 unsigned char header;
00055 unsigned char length;
00056 unsigned char current[2];
00057 };
00058
00059 struct ThreeAxisGyro {
00060 unsigned char header;
00061 unsigned char length;
00062 unsigned char frameId;
00063 unsigned char followedDataLength;
00064 unsigned char parameters[0];
00065 };
00066
00067 struct GpInput {
00068 unsigned char header;
00069 unsigned char length;
00070 unsigned char digitalInput[2];
00071 unsigned char analogInput[14];
00072 };
00073
00074 struct KobukiSensors {
00075
00076 unsigned short int timeStamp;
00077 unsigned char bumper;
00078 unsigned char wheelDrop;
00079 unsigned char cliff;
00080 short int leftEncoder;
00081 short int rightEncoder;
00082 unsigned char leftPwm;
00083 unsigned char rightPwm;
00084 unsigned char buttons;
00085 unsigned char charger;
00086 unsigned char battery;
00087 unsigned char overCurrent;
00088
00089
00090 unsigned char docking[3];
00091
00092
00093 short int angle;
00094 short int angleRate;
00095 unsigned char acc[3];
00096
00097
00098 short int bottom[3];
00099
00100
00101 unsigned char current[2];
00102
00103
00104
00105 unsigned char frameId;
00106 unsigned char followedDataLength;
00107 short parameters[0];
00108
00109
00110 short int digitalInput;
00111 short int analogInput[7];
00112 };
00113
00114
00115 enum State {
00116 lookingForHeader0,
00117 lookingForHeader1,
00118 waitingForPacket,
00119 gotPacket
00120 };
00121
00122 enum Sensor {
00123 coreSensor = 1,
00124 dockInfraRed = 3,
00125 inertia = 4,
00126 cliff = 5,
00127 current = 6,
00128 threeAxisGyro = 13,
00129 gpInput = 16,
00130 };
00131
00132 struct BaseControl {
00133 unsigned char speed[2];
00134 unsigned char radius[2];
00135 };
00136
00137
00138 struct SetDigitalOut {
00139 unsigned char gpOut;
00140 };
00141
00142 struct KobukiCommand {
00143 unsigned int Length;
00144 unsigned int commandData;
00145 unsigned int commandDataSize;
00146
00147
00148
00149 unsigned char speedHigh;
00150 unsigned char speedLow;
00151 unsigned char radiusHigh;
00152 unsigned char radiusLow;
00153 };
00154
00155 }
00156
00157 class KobukiDriver : public virtual DeviceDriver, public MotionHook, public DataSource, public virtual plist::PrimitiveListener {
00158 public:
00159 explicit KobukiDriver(const std::string& name)
00160 : DeviceDriver(autoRegisterKobukiDriver,name), MotionHook(), DataSource(),
00161 commName(), poller(&KobukiDriver::advance,*this,TimeET(0L),TimeET(1L),true,CallbackPollThread::IGNORE_RETURN),
00162 motionActive(false), sensorsActive(false),
00163 frameNumber(0), lastDesiredMode(0), lastPollTime(0)
00164 {
00165 addEntry("CommPort",commName,"The name of the comm port where output will be sent");
00166 }
00167 virtual ~KobukiDriver() {}
00168
00169 virtual std::string getClassName() const { return autoRegisterKobukiDriver; }
00170
00171 virtual MotionHook* getMotionSink() { return dynamic_cast<MotionHook*>(this); }
00172
00173 virtual void getSensorSources(std::map<std::string,DataSource*>& sources) {
00174 sources.clear();
00175 sources["Sensors"]=dynamic_cast<DataSource*>(this);
00176 }
00177
00178 virtual void motionStarting();
00179 virtual bool isConnected();
00180 virtual void motionStopping();
00181 virtual void motionCheck(const float outputs[][NumOutputs]);
00182
00183 virtual unsigned int nextTimestamp();
00184 virtual const std::string& nextName() { return instanceName; }
00185 virtual bool advance();
00186 virtual void registerSource();
00187 virtual void deregisterSource();
00188
00189 virtual bool readPacket(std::istream &is);
00190 virtual void packetParser(unsigned char packet[],const unsigned int packetLenght);
00191
00192 virtual void enteringRealtime(const plist::Primitive<double>& simTimeScale) { DataSource::enteringRealtime(simTimeScale); }
00193 virtual void leavingRealtime(bool isFullSpeed) { DataSource::leavingRealtime(isFullSpeed); }
00194
00195 virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00196
00197 plist::Primitive<std::string> commName;
00198
00199 protected:
00200 void doFreeze();
00201 void doUnfreeze();
00202
00203 virtual bool sendCommand(std::vector<unsigned char> bytes, unsigned int timeout);
00204 virtual void connect();
00205
00206
00207
00208 float ledActivation[NumLEDs];
00209
00210 CallbackPollThread poller;
00211
00212 bool motionActive;
00213 bool sensorsActive;
00214 unsigned int frameNumber;
00215
00216 unsigned char lastDesiredMode;
00217 private:
00218
00219 static const std::string autoRegisterKobukiDriver;
00220
00221 static const int PACKET_LENGTH = 52+4;
00222
00223 unsigned int lastPollTime;
00224 static const unsigned int pollInterval = 250;
00225 };
00226
00227 #endif