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/Wheel.h@6:477382219bcf, 2019-03-26 (annotated)
- Committer:
- mazdo25
- Date:
- Tue Mar 26 16:19:47 2019 +0000
- Revision:
- 6:477382219bcf
- Parent:
- 5:f1613df66ceb
- Child:
- 8:5ed6685f6edd
LATEST WORKING
Who changed what in which revision?
| User | Revision | Line number | New 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 | |
| mazdo25 | 5:f1613df66ceb | 6 | |
| mazdo25 | 5:f1613df66ceb | 7 | float distance; //distance traversed by wheel |
| mazdo25 | 5:f1613df66ceb | 8 | float angularVelocity; |
| mazdo25 | 5:f1613df66ceb | 9 | |
| mazdo25 | 5:f1613df66ceb | 10 | float const static gain = 0.9f; //closed loop gain, (amount to amplify the difference) you have to tune this value |
| mazdo25 | 5:f1613df66ceb | 11 | //but make sure its less than 1.5 otherwise you'll have a really sensitive motor |
| mazdo25 | 5:f1613df66ceb | 12 | |
| mazdo25 | 5:f1613df66ceb | 13 | PwmOut Mtr; //connect this pin to the motor driveboard pwm |
| mazdo25 | 5:f1613df66ceb | 14 | DigitalOut direction; //connected to the direction pin of motor drive board |
| mazdo25 | 5:f1613df66ceb | 15 | DigitalOut polarity; //connected to the bipolar of motor drive board. 0 = unipolar 1 = bipolar |
| mazdo25 | 5:f1613df66ceb | 16 | |
| mazdo25 | 5:f1613df66ceb | 17 | Ticker updater; |
| mazdo25 | 5:f1613df66ceb | 18 | |
| mazdo25 | 5:f1613df66ceb | 19 | Encoder* enc; |
| mazdo25 | 5:f1613df66ceb | 20 | |
| mazdo25 | 5:f1613df66ceb | 21 | PID controller; |
| mazdo25 | 5:f1613df66ceb | 22 | |
| mazdo25 | 5:f1613df66ceb | 23 | public: |
| mazdo25 | 5:f1613df66ceb | 24 | |
| mazdo25 | 5:f1613df66ceb | 25 | float maxAngularVel; |
| mazdo25 | 5:f1613df66ceb | 26 | float minAngularVel; |
| mazdo25 | 5:f1613df66ceb | 27 | |
| mazdo25 | 5:f1613df66ceb | 28 | float static const wheelDiameter = 0.18; //used in calculation of Linear velocity i.e never |
| mazdo25 | 5:f1613df66ceb | 29 | |
| mazdo25 | 5:f1613df66ceb | 30 | Wheel (Encoder* E, PinName M, PinName D, PinName Mode) : Mtr(M), direction(D), polarity(Mode), controller(gain,2.0f,0.0f,0.0005f) |
| mazdo25 | 5:f1613df66ceb | 31 | { |
| mazdo25 | 5:f1613df66ceb | 32 | maxAngularVel = 0.0f; |
| mazdo25 | 5:f1613df66ceb | 33 | enc = E; |
| mazdo25 | 5:f1613df66ceb | 34 | polarity = 0; |
| mazdo25 | 5:f1613df66ceb | 35 | direction = 0; |
| mazdo25 | 5:f1613df66ceb | 36 | distance = 0; |
| mazdo25 | 5:f1613df66ceb | 37 | Mtr.period_us(100); //frequency of 5KHz determine this constant value based on switching losses+frequency losses |
| mazdo25 | 5:f1613df66ceb | 38 | //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance |
| mazdo25 | 5:f1613df66ceb | 39 | Mtr.write(1); //start off on the turned off state |
| mazdo25 | 5:f1613df66ceb | 40 | |
| mazdo25 | 5:f1613df66ceb | 41 | updater.detach(); |
| mazdo25 | 5:f1613df66ceb | 42 | |
| mazdo25 | 5:f1613df66ceb | 43 | float *maxAVptr = &maxAngularVel; |
| mazdo25 | 5:f1613df66ceb | 44 | float *minAVPtr = &minAngularVel; |
| mazdo25 | 5:f1613df66ceb | 45 | controller.assignLimitAddress(maxAVptr,minAVPtr); |
| mazdo25 | 5:f1613df66ceb | 46 | } |
| mazdo25 | 5:f1613df66ceb | 47 | |
| mazdo25 | 5:f1613df66ceb | 48 | void calculateAngularVelocity() //returns a float value which is the angular velocity of the WHEEL |
| mazdo25 | 5:f1613df66ceb | 49 | { |
| mazdo25 | 5:f1613df66ceb | 50 | float eTR = enc->encoderTickRate(); |
| mazdo25 | 5:f1613df66ceb | 51 | angularVelocity = (eTR/256.0f)*2.0f*(float)PI; |
| mazdo25 | 5:f1613df66ceb | 52 | } |
| mazdo25 | 5:f1613df66ceb | 53 | |
| mazdo25 | 5:f1613df66ceb | 54 | void setFrequency(int freq) //if you want to adjust the frequency |
| mazdo25 | 5:f1613df66ceb | 55 | { |
| mazdo25 | 5:f1613df66ceb | 56 | Mtr.period(1/freq); |
| mazdo25 | 5:f1613df66ceb | 57 | } |
| mazdo25 | 5:f1613df66ceb | 58 | |
| mazdo25 | 5:f1613df66ceb | 59 | float returnAngularVelocity() //as it says |
| mazdo25 | 5:f1613df66ceb | 60 | { |
| mazdo25 | 5:f1613df66ceb | 61 | return angularVelocity; |
| mazdo25 | 5:f1613df66ceb | 62 | } |
| mazdo25 | 5:f1613df66ceb | 63 | |
| mazdo25 | 5:f1613df66ceb | 64 | //only called once during initialization to calculate max angular velocity |
| mazdo25 | 5:f1613df66ceb | 65 | //dir = direction, do opposite for each wheel just so your buggy doesn't move FORWARD but rather rotates |
| mazdo25 | 5:f1613df66ceb | 66 | void init(int dir) |
| mazdo25 | 5:f1613df66ceb | 67 | { |
| mazdo25 | 5:f1613df66ceb | 68 | enc->startTimer(); |
| mazdo25 | 5:f1613df66ceb | 69 | Mtr.write(0); //max speed |
| mazdo25 | 5:f1613df66ceb | 70 | angularVelocity = 10.0f; |
| mazdo25 | 5:f1613df66ceb | 71 | if (dir == 0) {direction = 0;} else {direction = 1;} |
| mazdo25 | 5:f1613df66ceb | 72 | updater.attach(callback(this, &Wheel::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 | 5:f1613df66ceb | 73 | } |
| mazdo25 | 5:f1613df66ceb | 74 | |
| mazdo25 | 5:f1613df66ceb | 75 | void init2(void) //used as a temporarily wait command for the wheel to spin to max |
| mazdo25 | 5:f1613df66ceb | 76 | { |
| mazdo25 | 6:477382219bcf | 77 | enc->startTimer(); |
| mazdo25 | 5:f1613df66ceb | 78 | updater.detach(); |
| mazdo25 | 5:f1613df66ceb | 79 | calculateAngularVelocity(); |
| mazdo25 | 5:f1613df66ceb | 80 | maxAngularVel = 60.0f; |
| mazdo25 | 5:f1613df66ceb | 81 | minAngularVel = -1.0f*maxAngularVel; |
| mazdo25 | 5:f1613df66ceb | 82 | controller.setSetPoint(0.0f); |
| mazdo25 | 5:f1613df66ceb | 83 | updater.attach(callback(this, &Wheel::wheelUpdates),0.0005f); |
| mazdo25 | 5:f1613df66ceb | 84 | } |
| mazdo25 | 5:f1613df66ceb | 85 | |
| mazdo25 | 5:f1613df66ceb | 86 | void startController() |
| mazdo25 | 5:f1613df66ceb | 87 | { |
| mazdo25 | 5:f1613df66ceb | 88 | updater.attach(callback(this, &Wheel::wheelUpdates),0.01f); |
| mazdo25 | 5:f1613df66ceb | 89 | } |
| mazdo25 | 5:f1613df66ceb | 90 | |
| mazdo25 | 5:f1613df66ceb | 91 | void stopController() |
| mazdo25 | 5:f1613df66ceb | 92 | { |
| mazdo25 | 5:f1613df66ceb | 93 | updater.detach(); |
| mazdo25 | 5:f1613df66ceb | 94 | } |
| mazdo25 | 5:f1613df66ceb | 95 | |
| mazdo25 | 5:f1613df66ceb | 96 | void wheelUpdates(void) //sampling rate the ticker is attached I.E the wheel speed is updated everytime this function is called |
| mazdo25 | 5:f1613df66ceb | 97 | { |
| mazdo25 | 5:f1613df66ceb | 98 | calculateAngularVelocity(); |
| 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 | 5:f1613df66ceb | 102 | } |
| mazdo25 | 5:f1613df66ceb | 103 | |
| mazdo25 | 5:f1613df66ceb | 104 | 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 | 105 | { |
| mazdo25 | 5:f1613df66ceb | 106 | controller.setSetPoint(W); |
| mazdo25 | 5:f1613df66ceb | 107 | 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 | 5:f1613df66ceb | 108 | }; |
| mazdo25 | 5:f1613df66ceb | 109 | |
| mazdo25 | 5:f1613df66ceb | 110 | float getDistance(void) |
| mazdo25 | 5:f1613df66ceb | 111 | { |
| mazdo25 | 5:f1613df66ceb | 112 | return distance; //distance traversed by wheel |
| mazdo25 | 5:f1613df66ceb | 113 | } |
| mazdo25 | 5:f1613df66ceb | 114 | |
| mazdo25 | 5:f1613df66ceb | 115 | float returnMaxAngularVel(void) |
| mazdo25 | 5:f1613df66ceb | 116 | { |
| mazdo25 | 5:f1613df66ceb | 117 | return maxAngularVel; |
| mazdo25 | 5:f1613df66ceb | 118 | } |
| mazdo25 | 5:f1613df66ceb | 119 | |
| mazdo25 | 5:f1613df66ceb | 120 | float* returnMaxAVptr() |
| mazdo25 | 5:f1613df66ceb | 121 | { |
| mazdo25 | 5:f1613df66ceb | 122 | float *tempPTR = &maxAngularVel; |
| mazdo25 | 5:f1613df66ceb | 123 | return tempPTR; |
| mazdo25 | 5:f1613df66ceb | 124 | } |
| mazdo25 | 5:f1613df66ceb | 125 | |
| mazdo25 | 5:f1613df66ceb | 126 | float* returnMinAVptr() |
| mazdo25 | 5:f1613df66ceb | 127 | { |
| mazdo25 | 5:f1613df66ceb | 128 | float *tempPTR = &minAngularVel; |
| mazdo25 | 5:f1613df66ceb | 129 | return tempPTR; |
| mazdo25 | 5:f1613df66ceb | 130 | } |
| mazdo25 | 5:f1613df66ceb | 131 | |
| mazdo25 | 5:f1613df66ceb | 132 | }; |