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
Wheel.h@0:f45212966fb1, 2019-02-18 (annotated)
- Committer:
- mazdo25
- Date:
- Mon Feb 18 13:13:34 2019 +0000
- Revision:
- 0:f45212966fb1
initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mazdo25 | 0:f45212966fb1 | 1 | class Wheel { |
| mazdo25 | 0:f45212966fb1 | 2 | private: |
| mazdo25 | 0:f45212966fb1 | 3 | float angularVelocity; //angular speed of the wheel |
| mazdo25 | 0:f45212966fb1 | 4 | float linearVelocity; |
| mazdo25 | 0:f45212966fb1 | 5 | PwmOut Mtr; //connect this pin to the motor driveboard pwm |
| mazdo25 | 0:f45212966fb1 | 6 | DigitalOut direction; //connected to the direction pin of motor drive board |
| mazdo25 | 0:f45212966fb1 | 7 | float static const wheelDiameter = 0.18; |
| mazdo25 | 0:f45212966fb1 | 8 | Encoder* enc; |
| mazdo25 | 0:f45212966fb1 | 9 | float const static MAV = 510; |
| mazdo25 | 0:f45212966fb1 | 10 | |
| mazdo25 | 0:f45212966fb1 | 11 | public: |
| mazdo25 | 0:f45212966fb1 | 12 | Wheel (Encoder* E, PinName M, PinName C) : Mtr(M), direction(C) { |
| mazdo25 | 0:f45212966fb1 | 13 | enc = E; |
| mazdo25 | 0:f45212966fb1 | 14 | Mtr.write(1.0f); //duty cycle of 100% |
| mazdo25 | 0:f45212966fb1 | 15 | Mtr.period_us(100000); //frequency of 1KHz determine this constant value based on switching losses+frequency losses |
| mazdo25 | 0:f45212966fb1 | 16 | //higher freq -> more switching losses lower freq -> more "capacitive losses" need to find a balance |
| mazdo25 | 0:f45212966fb1 | 17 | direction = 0; |
| mazdo25 | 0:f45212966fb1 | 18 | Mtr.write(0.1f); //duty cycle of 0.1 -> turn it off |
| mazdo25 | 0:f45212966fb1 | 19 | } |
| mazdo25 | 0:f45212966fb1 | 20 | |
| mazdo25 | 0:f45212966fb1 | 21 | |
| mazdo25 | 0:f45212966fb1 | 22 | float calculateAngularVelocity(){ //returns a float value which is the angular velocity of the WHEEL |
| mazdo25 | 0:f45212966fb1 | 23 | angularVelocity = ((enc->encoderTickRate())/512)*2*3.141593f; |
| mazdo25 | 0:f45212966fb1 | 24 | return ((enc->encoderTickRate())/512)*2*3.141593f; |
| mazdo25 | 0:f45212966fb1 | 25 | }; |
| mazdo25 | 0:f45212966fb1 | 26 | |
| mazdo25 | 0:f45212966fb1 | 27 | void setDutyCycle(float f) |
| mazdo25 | 0:f45212966fb1 | 28 | { |
| mazdo25 | 0:f45212966fb1 | 29 | if (f > 1.0f) |
| mazdo25 | 0:f45212966fb1 | 30 | { |
| mazdo25 | 0:f45212966fb1 | 31 | Mtr.write(1); |
| mazdo25 | 0:f45212966fb1 | 32 | } |
| mazdo25 | 0:f45212966fb1 | 33 | else if (f < 0.0f) |
| mazdo25 | 0:f45212966fb1 | 34 | { |
| mazdo25 | 0:f45212966fb1 | 35 | Mtr.write (0); |
| mazdo25 | 0:f45212966fb1 | 36 | }else |
| mazdo25 | 0:f45212966fb1 | 37 | { |
| mazdo25 | 0:f45212966fb1 | 38 | Mtr.write(f); |
| mazdo25 | 0:f45212966fb1 | 39 | } |
| mazdo25 | 0:f45212966fb1 | 40 | }; |
| mazdo25 | 0:f45212966fb1 | 41 | |
| mazdo25 | 0:f45212966fb1 | 42 | void setFrequency(int freq){ |
| mazdo25 | 0:f45212966fb1 | 43 | Mtr.period(1/freq); |
| mazdo25 | 0:f45212966fb1 | 44 | }; |
| mazdo25 | 0:f45212966fb1 | 45 | |
| mazdo25 | 0:f45212966fb1 | 46 | 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 | 0:f45212966fb1 | 47 | { |
| mazdo25 | 0:f45212966fb1 | 48 | if (abs(W) >= MAV) |
| mazdo25 | 0:f45212966fb1 | 49 | { |
| mazdo25 | 0:f45212966fb1 | 50 | Mtr.write(1); |
| mazdo25 | 0:f45212966fb1 | 51 | }else |
| mazdo25 | 0:f45212966fb1 | 52 | { |
| mazdo25 | 0:f45212966fb1 | 53 | Mtr.write(abs(W)/MAV); //assuming linear relationship between dutcy cycle and speed. |
| mazdo25 | 0:f45212966fb1 | 54 | } |
| mazdo25 | 0:f45212966fb1 | 55 | if (W<0) |
| mazdo25 | 0:f45212966fb1 | 56 | { |
| mazdo25 | 0:f45212966fb1 | 57 | direction = 0; |
| mazdo25 | 0:f45212966fb1 | 58 | } else { |
| mazdo25 | 0:f45212966fb1 | 59 | direction = 1; |
| mazdo25 | 0:f45212966fb1 | 60 | } |
| mazdo25 | 0:f45212966fb1 | 61 | }; |
| mazdo25 | 0:f45212966fb1 | 62 | |
| mazdo25 | 0:f45212966fb1 | 63 | }; |