Buggy bois / Mbed 2 deprecated HEATS_2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Wheel.h Source File

Wheel.h

00001 class Wheel 
00002 {
00003     private:
00004     
00005     float distance; //distance traversed by wheel
00006     float angularVelocity;
00007     int br;
00008     
00009         float const static gain = 1.1f; //closed loop gain, (amount to amplify the difference) you have to tune this value
00010     //but make sure its less than 1.5 otherwise you'll have a really sensitive motor
00011     
00012     PwmOut Mtr; //connect this pin to the motor driveboard pwm
00013     DigitalOut direction; //connected to the direction pin of motor drive board
00014     DigitalOut polarity; //connected to the bipolar of motor drive board. 0 = unipolar 1 = bipolar
00015     
00016     Ticker updater;
00017 
00018     Encoder* enc;
00019     
00020     PID controller;
00021     
00022     public:
00023     
00024     float  maxAngularVel;
00025     float  minAngularVel; 
00026     
00027     float static const wheelDiameter = 0.18; //used in calculation of Linear velocity i.e never
00028     
00029     Wheel (Encoder* E, PinName M, PinName D, PinName Mode) : Mtr(M), direction(D), polarity(Mode), controller(gain,4.0f,0.0f,0.0003f)
00030         {
00031         maxAngularVel = 0.0f;
00032         enc = E;
00033         polarity = 0;   
00034         direction = 0;
00035         distance = 0;
00036         Mtr.period_us(100); //frequency of 5KHz determine this constant value based on switching losses+frequency losses
00037         //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance
00038         Mtr.write(1); //start off on the turned off state
00039         
00040         updater.detach();
00041         
00042         float *maxAVptr = &maxAngularVel;
00043         float *minAVPtr = &minAngularVel;
00044         controller.assignLimitAddress(maxAVptr,minAVPtr);
00045         }
00046     
00047     void calculateAngularVelocity() //returns a float value which is the angular velocity of the WHEEL
00048     {
00049         float eTR = enc->encoderTickRate();
00050         angularVelocity = (eTR/256.0f)*2.0f*(float)PI;
00051     }
00052     
00053     void setFrequency(int freq) //if you want to adjust the frequency
00054     {
00055         Mtr.period(1/freq);
00056     }
00057     
00058     float returnAngularVelocity() //as it says
00059     {
00060         return angularVelocity;
00061     }
00062     
00063     //only called once during initialization to calculate max angular velocity 
00064     //dir = direction, do opposite for each wheel just so your buggy doesn't move FORWARD but rather rotates
00065     void init(int dir) 
00066     {
00067       enc->startTimer();
00068       Mtr.write(0); //max speed
00069       angularVelocity = 10.0f;
00070       if (dir == 0) {direction = 0;} else {direction = 1;}
00071       updater.attach(callback(this, &Wheel::init2),0.6f);
00072     }
00073     
00074     void init2(void) //used as a temporarily wait command for the wheel to spin to max
00075     {
00076         enc->startTimer();
00077         calculateAngularVelocity();
00078         maxAngularVel = 75.0f;
00079         minAngularVel = -1.0f*maxAngularVel;
00080         controller.setSetPoint(0.0f);
00081         updater.attach(callback(this, &Wheel::wheelUpdates),0.0003f);
00082     }
00083     
00084     void startController()
00085     {
00086        updater.attach(callback(this, &Wheel::wheelUpdates),0.01f); 
00087     }
00088     
00089     void stopController()
00090     {
00091         updater.detach();
00092     }
00093     
00094     void wheelUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called
00095         {
00096             calculateAngularVelocity();
00097             //distance += angularVelocity*(wheelDiameter/2)*0.0005f;
00098             float temp2 = controller.compute(angularVelocity); //another temporary value to store the computed angular velocity
00099             if (temp2 < 0) {direction = 0;} else {direction = 1;} //change direction according to the computed value
00100             Mtr.write((1.0f - abs(temp2))); //write the value as a pwm
00101         }
00102         
00103     void adjustAngularVelocity(float W) // W = angular velocity you want, obviously putting a |w| value that is > max angular velocity will set dutcy cycle to max
00104         {
00105         controller.setSetPoint(W);
00106         };
00107         
00108     float getDistance(void)
00109     {
00110     return distance; //distance traversed by wheel
00111     }
00112     
00113     void stop()
00114     {
00115         controller.setSetPoint(0);
00116     }
00117     
00118     float returnMaxAngularVel(void)
00119     {
00120         return  maxAngularVel;
00121     }
00122     
00123     float* returnMaxAVptr()
00124     {
00125         float *tempPTR = &maxAngularVel;
00126         return tempPTR;
00127     }
00128     
00129     float* returnMinAVptr()
00130     {
00131         float *tempPTR = &minAngularVel;
00132         return tempPTR;
00133     }
00134     
00135     void setBR (int p)
00136     {
00137         br = p;
00138     }
00139     
00140     void brake()
00141     {
00142         
00143     }
00144     
00145 };