Buggy bois / Mbed 2 deprecated HEATS_1

Dependencies:   mbed

Committer:
mazdo25
Date:
Sun Mar 03 00:54:07 2019 +0000
Revision:
1:813f4b17ae65
Child:
3:01b5e80d842d
Buggy project

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 1:813f4b17ae65 8 float const static gain = 0.6f; //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 1:813f4b17ae65 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 1:813f4b17ae65 27 Wheel (Encoder* E, PinName M, PinName D, PinName Mode) : Mtr(M), direction(D), polarity(Mode), controller(0.6f)
mazdo25 1:813f4b17ae65 28 {
mazdo25 1:813f4b17ae65 29 enc = E;
mazdo25 1:813f4b17ae65 30 polarity = 0;
mazdo25 1:813f4b17ae65 31 direction = 0;
mazdo25 1:813f4b17ae65 32 distance = 0;
mazdo25 1:813f4b17ae65 33
mazdo25 1:813f4b17ae65 34 Mtr.period_us(200); //frequency of 1KHz determine this constant value based on switching losses+frequency losses
mazdo25 1:813f4b17ae65 35 //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance
mazdo25 1:813f4b17ae65 36 updater.detach();
mazdo25 1:813f4b17ae65 37
mazdo25 1:813f4b17ae65 38 controller.setControl(10.0f);
mazdo25 1:813f4b17ae65 39 controller.setOutputLimits(-1.0f, 1.0f);
mazdo25 1:813f4b17ae65 40 }
mazdo25 1:813f4b17ae65 41
mazdo25 1:813f4b17ae65 42 float calculateAngularVelocity() //returns a float value which is the angular velocity of the WHEEL
mazdo25 1:813f4b17ae65 43 {
mazdo25 1:813f4b17ae65 44 float eTR;
mazdo25 1:813f4b17ae65 45 eTR = enc->encoderTickRate();
mazdo25 1:813f4b17ae65 46 angularVelocity = (eTR/256.0f)*2.0f*(float)PI;
mazdo25 1:813f4b17ae65 47 return eTR;
mazdo25 1:813f4b17ae65 48 }
mazdo25 1:813f4b17ae65 49
mazdo25 1:813f4b17ae65 50 void setFrequency(int freq) //if you want to adjust the frequency
mazdo25 1:813f4b17ae65 51 {
mazdo25 1:813f4b17ae65 52 Mtr.period(1/freq);
mazdo25 1:813f4b17ae65 53 }
mazdo25 1:813f4b17ae65 54
mazdo25 1:813f4b17ae65 55 float returnAngularVelocity() //as it says
mazdo25 1:813f4b17ae65 56 {
mazdo25 1:813f4b17ae65 57 return angularVelocity;
mazdo25 1:813f4b17ae65 58 }
mazdo25 1:813f4b17ae65 59
mazdo25 1:813f4b17ae65 60 //only called once during initialization to calculate max angular velocity
mazdo25 1:813f4b17ae65 61 //dir = direction, do opposite for each wheel just so your buggy doesn't move FORWARD but rather rotates
mazdo25 1:813f4b17ae65 62 void init(int dir)
mazdo25 1:813f4b17ae65 63 {
mazdo25 1:813f4b17ae65 64 Mtr.write(0); //max speed
mazdo25 1:813f4b17ae65 65 direction = dir;
mazdo25 1:813f4b17ae65 66 updater.attach(callback(this, &Wheel::init2),1); //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 67 }
mazdo25 1:813f4b17ae65 68
mazdo25 1:813f4b17ae65 69 void init2(void) //used as a temporarily wait command for the wheel to spin to max
mazdo25 1:813f4b17ae65 70 {
mazdo25 1:813f4b17ae65 71 float temp = enc->encoderTickRate();
mazdo25 1:813f4b17ae65 72 angularVelocity = (temp/256.0f)*2.0f*(float)PI;
mazdo25 1:813f4b17ae65 73 maxAngularVel = angularVelocity;
mazdo25 1:813f4b17ae65 74 controller.setInputLimits(-1.0f*angularVelocity,angularVelocity);
mazdo25 1:813f4b17ae65 75 updater.attach(callback(this, &Wheel::wheelUpdates),0.1); //attached the actual update function from now ON
mazdo25 1:813f4b17ae65 76 }
mazdo25 1:813f4b17ae65 77
mazdo25 1:813f4b17ae65 78 void wheelUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytiem this function is called
mazdo25 1:813f4b17ae65 79 {
mazdo25 1:813f4b17ae65 80 if (angularVelocity >= (controller.returnControl()+1.0f) || angularVelocity <= (controller.returnControl()-1.0f)) //only compute if their is a difference between wanted and current val
mazdo25 1:813f4b17ae65 81 {
mazdo25 1:813f4b17ae65 82 float temp = enc->encoderTickRate(); //get the encoder tick rate and store in a value called temp
mazdo25 1:813f4b17ae65 83 angularVelocity = (temp/256.0f)*2.0f*(float)PI; //use it to calculate the angular velocity of the wheel
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 1:813f4b17ae65 86 Mtr.write(temp2); //write the value as a pwm
mazdo25 1:813f4b17ae65 87 distance += angularVelocity * wheelDiameter;
mazdo25 1:813f4b17ae65 88 }
mazdo25 1:813f4b17ae65 89 }
mazdo25 1:813f4b17ae65 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 1
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 1:813f4b17ae65 101 };