Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
WheelControl/RWheel.h@4:208f5279143a, 2019-03-23 (annotated)
- 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?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mazdo25 | 4:208f5279143a | 1 | class RWheel |
| 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 | RWheel (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 = 0;} |
| mazdo25 | 4:208f5279143a | 67 | updater.attach(callback(this, &RWheel::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*maxAngularVel,maxAngularVel); |
| mazdo25 | 4:208f5279143a | 76 | controller.setSetPoint(0.0f); |
| mazdo25 | 4:208f5279143a | 77 | updater.attach(callback(this, &RWheel::wheelUpdates),0.05f); |
| mazdo25 | 4:208f5279143a | 78 | } |
| mazdo25 | 4:208f5279143a | 79 | |
| mazdo25 | 4:208f5279143a | 80 | void wheelUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called |
| mazdo25 | 4:208f5279143a | 81 | { |
| mazdo25 | 4:208f5279143a | 82 | calculateAngularVelocity(); |
| mazdo25 | 4:208f5279143a | 83 | if (controller.returnPrevCA() >= controller.returnInMax()) |
| mazdo25 | 4:208f5279143a | 84 | { |
| mazdo25 | 4:208f5279143a | 85 | maxAngularVel = abs(angularVelocity); |
| mazdo25 | 4:208f5279143a | 86 | controller.setInputLimits(-1.0f*maxAngularVel,maxAngularVel); |
| mazdo25 | 4:208f5279143a | 87 | } |
| mazdo25 | 4:208f5279143a | 88 | float temp2 = controller.compute(angularVelocity); //another temporary value to store the computed angular velocity |
| mazdo25 | 4:208f5279143a | 89 | if (temp2 < 0) {direction = 1;} else {direction = 0;} //change direction according to the computed value |
| mazdo25 | 4:208f5279143a | 90 | Mtr.write((1.0f - abs(temp2))); //write the value as a pwm |
| mazdo25 | 4:208f5279143a | 91 | } |
| mazdo25 | 4:208f5279143a | 92 | |
| mazdo25 | 4:208f5279143a | 93 | 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 | 94 | { |
| mazdo25 | 4:208f5279143a | 95 | controller.setSetPoint(W); |
| mazdo25 | 4:208f5279143a | 96 | 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 | 4:208f5279143a | 97 | }; |
| mazdo25 | 4:208f5279143a | 98 | |
| mazdo25 | 4:208f5279143a | 99 | float getDistance(void) |
| mazdo25 | 4:208f5279143a | 100 | { |
| mazdo25 | 4:208f5279143a | 101 | return distance; //distance traversed by wheel |
| mazdo25 | 4:208f5279143a | 102 | } |
| mazdo25 | 4:208f5279143a | 103 | |
| mazdo25 | 4:208f5279143a | 104 | float returnMaxAngularVel(void) |
| mazdo25 | 4:208f5279143a | 105 | { |
| mazdo25 | 4:208f5279143a | 106 | return maxAngularVel; |
| mazdo25 | 4:208f5279143a | 107 | } |
| mazdo25 | 4:208f5279143a | 108 | |
| mazdo25 | 4:208f5279143a | 109 | }; |