Buggy bois / Mbed 2 deprecated WORKING_TRIAL

Dependencies:   mbed

Committer:
mazdo25
Date:
Sat Mar 23 19:46:09 2019 +0000
Revision:
4:208f5279143a
latest working, but line following not;

Who changed what in which revision?

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