Buggy bois / Mbed 2 deprecated headache

Dependencies:   mbed

Committer:
UditSrivastav
Date:
Mon Apr 29 09:40:30 2019 +0000
Revision:
13:521d6c6e7042
Parent:
11:78c645fb76cd
Anythign um

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