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: QEI Motordriver ros_lib_melodic
motor/Motor.cpp@11:35809512ec11, 2019-11-13 (annotated)
- Committer:
- florine_van
- Date:
- Wed Nov 13 15:59:06 2019 +0000
- Revision:
- 11:35809512ec11
- Parent:
- 6:858a5116688e
- Child:
- 12:817da876ae2f
Implement QEI and distance moving
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| florine_van | 6:858a5116688e | 1 | #include "mbed.h" |
| florine_van | 6:858a5116688e | 2 | |
| florine_van | 5:8ef79eebbc97 | 3 | #include "Motor.h" |
| florine_van | 11:35809512ec11 | 4 | #include "QEI.h" |
| florine_van | 5:8ef79eebbc97 | 5 | |
| florine_van | 5:8ef79eebbc97 | 6 | /////////////////////////////////////////////////////////////////// |
| florine_van | 5:8ef79eebbc97 | 7 | // Constructor |
| florine_van | 5:8ef79eebbc97 | 8 | /////////////////////////////////////////////////////////////////// |
| florine_van | 11:35809512ec11 | 9 | Motor::Motor(PinName in1, PinName in2, PinName pwm, QEI wheel) |
| florine_van | 11:35809512ec11 | 10 | : in1(in1), in2(in2), pwm(pwm), wheel(wheel) |
| florine_van | 6:858a5116688e | 11 | { |
| florine_van | 6:858a5116688e | 12 | this->pwm.period_ms(10); |
| florine_van | 6:858a5116688e | 13 | } |
| florine_van | 5:8ef79eebbc97 | 14 | |
| florine_van | 5:8ef79eebbc97 | 15 | /////////////////////////////////////////////////////////////////// |
| florine_van | 5:8ef79eebbc97 | 16 | // Public methods |
| florine_van | 5:8ef79eebbc97 | 17 | /////////////////////////////////////////////////////////////////// |
| florine_van | 5:8ef79eebbc97 | 18 | |
| florine_van | 11:35809512ec11 | 19 | void Motor::moveForward(int pulse) |
| florine_van | 5:8ef79eebbc97 | 20 | { |
| florine_van | 11:35809512ec11 | 21 | moveDistance(-0.5, pulse, 1); |
| florine_van | 5:8ef79eebbc97 | 22 | } |
| florine_van | 5:8ef79eebbc97 | 23 | |
| florine_van | 11:35809512ec11 | 24 | void Motor::moveBackward(int pulse) |
| florine_van | 11:35809512ec11 | 25 | { |
| florine_van | 11:35809512ec11 | 26 | moveDistance(0.5, pulse, -1); |
| florine_van | 11:35809512ec11 | 27 | } |
| florine_van | 11:35809512ec11 | 28 | |
| florine_van | 11:35809512ec11 | 29 | void Motor::moveLeft(float speed) |
| florine_van | 11:35809512ec11 | 30 | { |
| florine_van | 11:35809512ec11 | 31 | moveDistance(speed, 3000, -1); |
| florine_van | 11:35809512ec11 | 32 | } |
| florine_van | 11:35809512ec11 | 33 | |
| florine_van | 11:35809512ec11 | 34 | void Motor::moveRight(float speed) |
| florine_van | 11:35809512ec11 | 35 | { |
| florine_van | 11:35809512ec11 | 36 | moveDistance(speed, 3000, 1); |
| florine_van | 5:8ef79eebbc97 | 37 | } |
| florine_van | 5:8ef79eebbc97 | 38 | |
| florine_van | 6:858a5116688e | 39 | void Motor::setSpeed(float speed) |
| florine_van | 5:8ef79eebbc97 | 40 | { |
| florine_van | 6:858a5116688e | 41 | pwm.write(speed); |
| florine_van | 5:8ef79eebbc97 | 42 | } |
| florine_van | 11:35809512ec11 | 43 | |
| florine_van | 11:35809512ec11 | 44 | void Motor::moveDistance(float speed, int pulse, int pulse_direction) |
| florine_van | 11:35809512ec11 | 45 | { |
| florine_van | 11:35809512ec11 | 46 | // Variables to allow for any pulse reading |
| florine_van | 11:35809512ec11 | 47 | float initial_pulse_reading = wheel.getPulses(); |
| florine_van | 11:35809512ec11 | 48 | float current_pulse_reading = wheel.getPulses(); |
| florine_van | 11:35809512ec11 | 49 | |
| florine_van | 11:35809512ec11 | 50 | // - is forward on our robot |
| florine_van | 11:35809512ec11 | 51 | setSpeed(speed); |
| florine_van | 11:35809512ec11 | 52 | |
| florine_van | 11:35809512ec11 | 53 | // loop for moving forward |
| florine_van | 11:35809512ec11 | 54 | if (pulse_direction > 0) |
| florine_van | 11:35809512ec11 | 55 | { |
| florine_van | 11:35809512ec11 | 56 | while(current_pulse_reading <= (initial_pulse_reading + pulse * pulse_direction)) |
| florine_van | 11:35809512ec11 | 57 | { |
| florine_van | 11:35809512ec11 | 58 | current_pulse_reading = wheel.getPulses(); |
| florine_van | 11:35809512ec11 | 59 | } |
| florine_van | 11:35809512ec11 | 60 | } |
| florine_van | 11:35809512ec11 | 61 | else |
| florine_van | 11:35809512ec11 | 62 | { |
| florine_van | 11:35809512ec11 | 63 | while(current_pulse_reading >= (initial_pulse_reading + pulse * pulse_direction)) |
| florine_van | 11:35809512ec11 | 64 | { |
| florine_van | 11:35809512ec11 | 65 | current_pulse_reading = wheel.getPulses(); |
| florine_van | 11:35809512ec11 | 66 | } |
| florine_van | 11:35809512ec11 | 67 | } |
| florine_van | 11:35809512ec11 | 68 | |
| florine_van | 11:35809512ec11 | 69 | setSpeed(0); |
| florine_van | 11:35809512ec11 | 70 | } |
| florine_van | 11:35809512ec11 | 71 | |
| florine_van | 11:35809512ec11 | 72 | |
| florine_van | 11:35809512ec11 | 73 | void Motor::move(float speed) |
| florine_van | 11:35809512ec11 | 74 | { |
| florine_van | 11:35809512ec11 | 75 | // - is forward on our robot |
| florine_van | 11:35809512ec11 | 76 | setSpeed(speed); |
| florine_van | 11:35809512ec11 | 77 | } |