Buggy bois / Mbed 2 deprecated HEATS_2

Dependencies:   mbed

Committer:
mazdo25
Date:
Sun Apr 28 18:27:46 2019 +0000
Revision:
10:acf5cb8d58d5
Parent:
9:cefa177c1353
Child:
11:78c645fb76cd
Brakes arent working;

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 10:acf5cb8d58d5 29 Wheel (Encoder* E, PinName M, PinName D, PinName Mode) : Mtr(M), direction(D), polarity(Mode), controller(gain,4.0f,0.00002f,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
mazdo25 5:f1613df66ceb 63 //only called once during initialization to calculate max angular velocity
mazdo25 5:f1613df66ceb 64 //dir = direction, do opposite for each wheel just so your buggy doesn't move FORWARD but rather rotates
mazdo25 5:f1613df66ceb 65 void init(int dir)
mazdo25 5:f1613df66ceb 66 {
mazdo25 5:f1613df66ceb 67 enc->startTimer();
mazdo25 5:f1613df66ceb 68 Mtr.write(0); //max speed
mazdo25 5:f1613df66ceb 69 angularVelocity = 10.0f;
mazdo25 5:f1613df66ceb 70 if (dir == 0) {direction = 0;} else {direction = 1;}
mazdo25 10:acf5cb8d58d5 71 updater.attach(callback(this, &Wheel::init2),0.6f);
mazdo25 5:f1613df66ceb 72 }
mazdo25 5:f1613df66ceb 73
mazdo25 5:f1613df66ceb 74 void init2(void) //used as a temporarily wait command for the wheel to spin to max
mazdo25 5:f1613df66ceb 75 {
mazdo25 6:477382219bcf 76 enc->startTimer();
mazdo25 5:f1613df66ceb 77 calculateAngularVelocity();
mazdo25 9:cefa177c1353 78 maxAngularVel = 75.0f;
mazdo25 5:f1613df66ceb 79 minAngularVel = -1.0f*maxAngularVel;
mazdo25 5:f1613df66ceb 80 controller.setSetPoint(0.0f);
mazdo25 5:f1613df66ceb 81 updater.attach(callback(this, &Wheel::wheelUpdates),0.0005f);
mazdo25 5:f1613df66ceb 82 }
mazdo25 5:f1613df66ceb 83
mazdo25 5:f1613df66ceb 84 void startController()
mazdo25 5:f1613df66ceb 85 {
mazdo25 5:f1613df66ceb 86 updater.attach(callback(this, &Wheel::wheelUpdates),0.01f);
mazdo25 5:f1613df66ceb 87 }
mazdo25 5:f1613df66ceb 88
mazdo25 5:f1613df66ceb 89 void stopController()
mazdo25 5:f1613df66ceb 90 {
mazdo25 5:f1613df66ceb 91 updater.detach();
mazdo25 5:f1613df66ceb 92 }
mazdo25 5:f1613df66ceb 93
mazdo25 5:f1613df66ceb 94 void wheelUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called
mazdo25 5:f1613df66ceb 95 {
mazdo25 10:acf5cb8d58d5 96 if (br == 0) {
mazdo25 5:f1613df66ceb 97 calculateAngularVelocity();
mazdo25 8:5ed6685f6edd 98 //distance += angularVelocity*(wheelDiameter/2)*0.0005f;
mazdo25 5:f1613df66ceb 99 float temp2 = controller.compute(angularVelocity); //another temporary value to store the computed angular velocity
mazdo25 5:f1613df66ceb 100 if (temp2 < 0) {direction = 0;} else {direction = 1;} //change direction according to the computed value
mazdo25 5:f1613df66ceb 101 Mtr.write((1.0f - abs(temp2))); //write the value as a pwm
mazdo25 10:acf5cb8d58d5 102 }
mazdo25 10:acf5cb8d58d5 103 else
mazdo25 10:acf5cb8d58d5 104 {
mazdo25 10:acf5cb8d58d5 105 return;
mazdo25 10:acf5cb8d58d5 106 }
mazdo25 5:f1613df66ceb 107 }
mazdo25 5:f1613df66ceb 108
mazdo25 5:f1613df66ceb 109 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 110 {
mazdo25 5:f1613df66ceb 111 controller.setSetPoint(W);
mazdo25 5:f1613df66ceb 112 };
mazdo25 5:f1613df66ceb 113
mazdo25 5:f1613df66ceb 114 float getDistance(void)
mazdo25 5:f1613df66ceb 115 {
mazdo25 5:f1613df66ceb 116 return distance; //distance traversed by wheel
mazdo25 5:f1613df66ceb 117 }
mazdo25 5:f1613df66ceb 118
mazdo25 8:5ed6685f6edd 119 void stop()
mazdo25 8:5ed6685f6edd 120 {
mazdo25 8:5ed6685f6edd 121 controller.setSetPoint(0);
mazdo25 8:5ed6685f6edd 122 }
mazdo25 8:5ed6685f6edd 123
mazdo25 5:f1613df66ceb 124 float returnMaxAngularVel(void)
mazdo25 5:f1613df66ceb 125 {
mazdo25 5:f1613df66ceb 126 return maxAngularVel;
mazdo25 5:f1613df66ceb 127 }
mazdo25 5:f1613df66ceb 128
mazdo25 5:f1613df66ceb 129 float* returnMaxAVptr()
mazdo25 5:f1613df66ceb 130 {
mazdo25 5:f1613df66ceb 131 float *tempPTR = &maxAngularVel;
mazdo25 5:f1613df66ceb 132 return tempPTR;
mazdo25 5:f1613df66ceb 133 }
mazdo25 5:f1613df66ceb 134
mazdo25 5:f1613df66ceb 135 float* returnMinAVptr()
mazdo25 5:f1613df66ceb 136 {
mazdo25 5:f1613df66ceb 137 float *tempPTR = &minAngularVel;
mazdo25 5:f1613df66ceb 138 return tempPTR;
mazdo25 5:f1613df66ceb 139 }
mazdo25 5:f1613df66ceb 140
mazdo25 10:acf5cb8d58d5 141 void setBR (int p)
mazdo25 10:acf5cb8d58d5 142 {
mazdo25 10:acf5cb8d58d5 143 br = p;
mazdo25 10:acf5cb8d58d5 144 }
mazdo25 10:acf5cb8d58d5 145
mazdo25 10:acf5cb8d58d5 146 void brake()
mazdo25 10:acf5cb8d58d5 147 {
mazdo25 10:acf5cb8d58d5 148 direction = 0;
mazdo25 10:acf5cb8d58d5 149 Mtr.write(0);
mazdo25 10:acf5cb8d58d5 150 }
mazdo25 10:acf5cb8d58d5 151
mazdo25 5:f1613df66ceb 152 };