Buggy bois / Mbed 2 deprecated UDITTEST

Dependencies:   mbed

Committer:
mazdo25
Date:
Mon Mar 25 22:42:31 2019 +0000
Revision:
5:f1613df66ceb
Child:
6:477382219bcf
LATEST WORKING VERSION;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazdo25 5:f1613df66ceb 1 class Wheel
mazdo25 5:f1613df66ceb 2 {
mazdo25 5:f1613df66ceb 3 private:
mazdo25 5:f1613df66ceb 4
mazdo25 5:f1613df66ceb 5
mazdo25 5:f1613df66ceb 6
mazdo25 5:f1613df66ceb 7 float distance; //distance traversed by wheel
mazdo25 5:f1613df66ceb 8 float angularVelocity;
mazdo25 5:f1613df66ceb 9
mazdo25 5:f1613df66ceb 10 float const static gain = 0.9f; //closed loop gain, (amount to amplify the difference) you have to tune this value
mazdo25 5:f1613df66ceb 11 //but make sure its less than 1.5 otherwise you'll have a really sensitive motor
mazdo25 5:f1613df66ceb 12
mazdo25 5:f1613df66ceb 13 PwmOut Mtr; //connect this pin to the motor driveboard pwm
mazdo25 5:f1613df66ceb 14 DigitalOut direction; //connected to the direction pin of motor drive board
mazdo25 5:f1613df66ceb 15 DigitalOut polarity; //connected to the bipolar of motor drive board. 0 = unipolar 1 = bipolar
mazdo25 5:f1613df66ceb 16
mazdo25 5:f1613df66ceb 17 Ticker updater;
mazdo25 5:f1613df66ceb 18
mazdo25 5:f1613df66ceb 19 Encoder* enc;
mazdo25 5:f1613df66ceb 20
mazdo25 5:f1613df66ceb 21 PID controller;
mazdo25 5:f1613df66ceb 22
mazdo25 5:f1613df66ceb 23 public:
mazdo25 5:f1613df66ceb 24
mazdo25 5:f1613df66ceb 25 float maxAngularVel;
mazdo25 5:f1613df66ceb 26 float minAngularVel;
mazdo25 5:f1613df66ceb 27
mazdo25 5:f1613df66ceb 28 float static const wheelDiameter = 0.18; //used in calculation of Linear velocity i.e never
mazdo25 5:f1613df66ceb 29
mazdo25 5:f1613df66ceb 30 Wheel (Encoder* E, PinName M, PinName D, PinName Mode) : Mtr(M), direction(D), polarity(Mode), controller(gain,2.0f,0.0f,0.0005f)
mazdo25 5:f1613df66ceb 31 {
mazdo25 5:f1613df66ceb 32 maxAngularVel = 0.0f;
mazdo25 5:f1613df66ceb 33 enc = E;
mazdo25 5:f1613df66ceb 34 polarity = 0;
mazdo25 5:f1613df66ceb 35 direction = 0;
mazdo25 5:f1613df66ceb 36 distance = 0;
mazdo25 5:f1613df66ceb 37 Mtr.period_us(100); //frequency of 5KHz determine this constant value based on switching losses+frequency losses
mazdo25 5:f1613df66ceb 38 //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance
mazdo25 5:f1613df66ceb 39 Mtr.write(1); //start off on the turned off state
mazdo25 5:f1613df66ceb 40
mazdo25 5:f1613df66ceb 41 updater.detach();
mazdo25 5:f1613df66ceb 42
mazdo25 5:f1613df66ceb 43 float *maxAVptr = &maxAngularVel;
mazdo25 5:f1613df66ceb 44 float *minAVPtr = &minAngularVel;
mazdo25 5:f1613df66ceb 45 controller.assignLimitAddress(maxAVptr,minAVPtr);
mazdo25 5:f1613df66ceb 46 }
mazdo25 5:f1613df66ceb 47
mazdo25 5:f1613df66ceb 48 void calculateAngularVelocity() //returns a float value which is the angular velocity of the WHEEL
mazdo25 5:f1613df66ceb 49 {
mazdo25 5:f1613df66ceb 50 float eTR = enc->encoderTickRate();
mazdo25 5:f1613df66ceb 51 angularVelocity = (eTR/256.0f)*2.0f*(float)PI;
mazdo25 5:f1613df66ceb 52 }
mazdo25 5:f1613df66ceb 53
mazdo25 5:f1613df66ceb 54 void setFrequency(int freq) //if you want to adjust the frequency
mazdo25 5:f1613df66ceb 55 {
mazdo25 5:f1613df66ceb 56 Mtr.period(1/freq);
mazdo25 5:f1613df66ceb 57 }
mazdo25 5:f1613df66ceb 58
mazdo25 5:f1613df66ceb 59 float returnAngularVelocity() //as it says
mazdo25 5:f1613df66ceb 60 {
mazdo25 5:f1613df66ceb 61 return angularVelocity;
mazdo25 5:f1613df66ceb 62 }
mazdo25 5:f1613df66ceb 63
mazdo25 5:f1613df66ceb 64 //only called once during initialization to calculate max angular velocity
mazdo25 5:f1613df66ceb 65 //dir = direction, do opposite for each wheel just so your buggy doesn't move FORWARD but rather rotates
mazdo25 5:f1613df66ceb 66 void init(int dir)
mazdo25 5:f1613df66ceb 67 {
mazdo25 5:f1613df66ceb 68 enc->startTimer();
mazdo25 5:f1613df66ceb 69 Mtr.write(0); //max speed
mazdo25 5:f1613df66ceb 70 angularVelocity = 10.0f;
mazdo25 5:f1613df66ceb 71 if (dir == 0) {direction = 0;} else {direction = 1;}
mazdo25 5:f1613df66ceb 72 updater.attach(callback(this, &Wheel::init2),0.6f); //used as a wait preferably put this wait just long enough that the buggy will do a full 360 degree turn so that it hasn't moved
mazdo25 5:f1613df66ceb 73 }
mazdo25 5:f1613df66ceb 74
mazdo25 5:f1613df66ceb 75 void init2(void) //used as a temporarily wait command for the wheel to spin to max
mazdo25 5:f1613df66ceb 76 {
mazdo25 5:f1613df66ceb 77 updater.detach();
mazdo25 5:f1613df66ceb 78 calculateAngularVelocity();
mazdo25 5:f1613df66ceb 79 maxAngularVel = 60.0f;
mazdo25 5:f1613df66ceb 80 minAngularVel = -1.0f*maxAngularVel;
mazdo25 5:f1613df66ceb 81 controller.setSetPoint(0.0f);
mazdo25 5:f1613df66ceb 82 updater.attach(callback(this, &Wheel::wheelUpdates),0.0005f);
mazdo25 5:f1613df66ceb 83 }
mazdo25 5:f1613df66ceb 84
mazdo25 5:f1613df66ceb 85 void startController()
mazdo25 5:f1613df66ceb 86 {
mazdo25 5:f1613df66ceb 87 updater.attach(callback(this, &Wheel::wheelUpdates),0.01f);
mazdo25 5:f1613df66ceb 88 }
mazdo25 5:f1613df66ceb 89
mazdo25 5:f1613df66ceb 90 void stopController()
mazdo25 5:f1613df66ceb 91 {
mazdo25 5:f1613df66ceb 92 updater.detach();
mazdo25 5:f1613df66ceb 93 }
mazdo25 5:f1613df66ceb 94
mazdo25 5:f1613df66ceb 95 void wheelUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called
mazdo25 5:f1613df66ceb 96 {
mazdo25 5:f1613df66ceb 97 calculateAngularVelocity();
mazdo25 5:f1613df66ceb 98 float temp2 = controller.compute(angularVelocity); //another temporary value to store the computed angular velocity
mazdo25 5:f1613df66ceb 99 if (temp2 < 0) {direction = 0;} else {direction = 1;} //change direction according to the computed value
mazdo25 5:f1613df66ceb 100 Mtr.write((1.0f - abs(temp2))); //write the value as a pwm
mazdo25 5:f1613df66ceb 101 }
mazdo25 5:f1613df66ceb 102
mazdo25 5:f1613df66ceb 103 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
mazdo25 5:f1613df66ceb 104 {
mazdo25 5:f1613df66ceb 105 controller.setSetPoint(W);
mazdo25 5:f1613df66ceb 106 if (W < 0.0f) {direction = 0;} else {direction = 1;} //obvs if you put a negative value -> will get a negative direction i.e 0;
mazdo25 5:f1613df66ceb 107 };
mazdo25 5:f1613df66ceb 108
mazdo25 5:f1613df66ceb 109 float getDistance(void)
mazdo25 5:f1613df66ceb 110 {
mazdo25 5:f1613df66ceb 111 return distance; //distance traversed by wheel
mazdo25 5:f1613df66ceb 112 }
mazdo25 5:f1613df66ceb 113
mazdo25 5:f1613df66ceb 114 float returnMaxAngularVel(void)
mazdo25 5:f1613df66ceb 115 {
mazdo25 5:f1613df66ceb 116 return maxAngularVel;
mazdo25 5:f1613df66ceb 117 }
mazdo25 5:f1613df66ceb 118
mazdo25 5:f1613df66ceb 119 float* returnMaxAVptr()
mazdo25 5:f1613df66ceb 120 {
mazdo25 5:f1613df66ceb 121 float *tempPTR = &maxAngularVel;
mazdo25 5:f1613df66ceb 122 return tempPTR;
mazdo25 5:f1613df66ceb 123 }
mazdo25 5:f1613df66ceb 124
mazdo25 5:f1613df66ceb 125 float* returnMinAVptr()
mazdo25 5:f1613df66ceb 126 {
mazdo25 5:f1613df66ceb 127 float *tempPTR = &minAngularVel;
mazdo25 5:f1613df66ceb 128 return tempPTR;
mazdo25 5:f1613df66ceb 129 }
mazdo25 5:f1613df66ceb 130
mazdo25 5:f1613df66ceb 131 };