Buggy bois / Mbed 2 deprecated headache

Dependencies:   mbed

Committer:
mazdo25
Date:
Sat Mar 09 14:27:48 2019 +0000
Revision:
3:01b5e80d842d
Parent:
1:813f4b17ae65
Initialization working, sensors not

Who changed what in which revision?

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