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