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.
src/motors.cpp@3:35deb5c21b33, 2018-11-26 (annotated)
- Committer:
- rwgriffithv
- Date:
- Mon Nov 26 23:50:58 2018 +0000
- Revision:
- 3:35deb5c21b33
- Parent:
- 0:88c60458332e
pid controller updated
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rwgriffithv | 0:88c60458332e | 1 | #include "motors.h" |
| rwgriffithv | 0:88c60458332e | 2 | #include "mbed.h" |
| rwgriffithv | 0:88c60458332e | 3 | #include "pins.h" |
| rwgriffithv | 0:88c60458332e | 4 | |
| rwgriffithv | 0:88c60458332e | 5 | PwmOut m_RF(MOTOR_RF); |
| rwgriffithv | 0:88c60458332e | 6 | PwmOut m_RB(MOTOR_RB); |
| rwgriffithv | 0:88c60458332e | 7 | PwmOut m_LF(MOTOR_LF); |
| rwgriffithv | 0:88c60458332e | 8 | PwmOut m_LB(MOTOR_LB); |
| rwgriffithv | 0:88c60458332e | 9 | |
| rwgriffithv | 0:88c60458332e | 10 | /*** |
| rwgriffithv | 0:88c60458332e | 11 | * Assignment 2 |
| rwgriffithv | 0:88c60458332e | 12 | * |
| rwgriffithv | 0:88c60458332e | 13 | * Add logic to set the PWM based on postiive/negative number. |
| rwgriffithv | 0:88c60458332e | 14 | ***/ |
| rwgriffithv | 0:88c60458332e | 15 | |
| rwgriffithv | 0:88c60458332e | 16 | void Motors::setMotorPwm(int motor, float pwm) { |
| rwgriffithv | 0:88c60458332e | 17 | if (abs(pwm) > MAX_SPEED) { |
| rwgriffithv | 0:88c60458332e | 18 | if (pwm > 0) pwm = MAX_SPEED; |
| rwgriffithv | 0:88c60458332e | 19 | else if (pwm < 0) pwm = -MAX_SPEED; |
| rwgriffithv | 0:88c60458332e | 20 | } |
| rwgriffithv | 0:88c60458332e | 21 | /* |
| rwgriffithv | 0:88c60458332e | 22 | else if (ABS_PWM < MIN_SPEED) { |
| rwgriffithv | 0:88c60458332e | 23 | if (pwm > 0) pwm = MIN_SPEED; |
| rwgriffithv | 0:88c60458332e | 24 | else if (pwm < 0) pwm = -MIN_SPEED; |
| rwgriffithv | 0:88c60458332e | 25 | } |
| rwgriffithv | 0:88c60458332e | 26 | */ |
| rwgriffithv | 0:88c60458332e | 27 | |
| rwgriffithv | 0:88c60458332e | 28 | // TODO |
| rwgriffithv | 0:88c60458332e | 29 | // Use the "PwmOut" objects defined above |
| rwgriffithv | 0:88c60458332e | 30 | // Hint: Stop your backwards/forward motor before going forward/backwards. |
| rwgriffithv | 0:88c60458332e | 31 | if (pwm >= 0) { |
| rwgriffithv | 0:88c60458332e | 32 | if (motor == RIGHT_MOTOR) { |
| rwgriffithv | 0:88c60458332e | 33 | m_RF.period_us(PERIOD_US); |
| rwgriffithv | 0:88c60458332e | 34 | m_RB.pulsewidth_us(0); |
| rwgriffithv | 0:88c60458332e | 35 | m_RF.pulsewidth_us((int)PERIOD_US*pwm); |
| rwgriffithv | 0:88c60458332e | 36 | } else { |
| rwgriffithv | 0:88c60458332e | 37 | m_LF.period_us(PERIOD_US); |
| rwgriffithv | 0:88c60458332e | 38 | m_LB.pulsewidth_us(0); |
| rwgriffithv | 0:88c60458332e | 39 | m_LF.pulsewidth_us((int)PERIOD_US*pwm); |
| rwgriffithv | 0:88c60458332e | 40 | } |
| rwgriffithv | 0:88c60458332e | 41 | } else { |
| rwgriffithv | 0:88c60458332e | 42 | if (motor == RIGHT_MOTOR) { |
| rwgriffithv | 0:88c60458332e | 43 | m_RB.period_us(PERIOD_US); |
| rwgriffithv | 0:88c60458332e | 44 | m_RF.pulsewidth_us(0); |
| rwgriffithv | 0:88c60458332e | 45 | m_RB.pulsewidth_us((int)PERIOD_US*abs(pwm)); |
| rwgriffithv | 0:88c60458332e | 46 | } else { |
| rwgriffithv | 0:88c60458332e | 47 | m_LB.period_us(PERIOD_US); |
| rwgriffithv | 0:88c60458332e | 48 | m_LF.pulsewidth_us(0); |
| rwgriffithv | 0:88c60458332e | 49 | m_LB.pulsewidth_us((int)PERIOD_US*abs(pwm)); |
| rwgriffithv | 0:88c60458332e | 50 | } |
| rwgriffithv | 0:88c60458332e | 51 | } |
| rwgriffithv | 0:88c60458332e | 52 | } |
| rwgriffithv | 0:88c60458332e | 53 | |
| rwgriffithv | 0:88c60458332e | 54 | Motors::Motors() { |
| rwgriffithv | 0:88c60458332e | 55 | // DO NOT initialize PWM!!! It breaks the mouse. |
| rwgriffithv | 0:88c60458332e | 56 | } |
| rwgriffithv | 0:88c60458332e | 57 | |
| rwgriffithv | 0:88c60458332e | 58 | void Motors::stop() { |
| rwgriffithv | 0:88c60458332e | 59 | setRightPwm(0); |
| rwgriffithv | 0:88c60458332e | 60 | setLeftPwm(0); |
| rwgriffithv | 0:88c60458332e | 61 | } |
| rwgriffithv | 0:88c60458332e | 62 | |
| rwgriffithv | 0:88c60458332e | 63 | void Motors::setRightPwm(float pwm) { |
| rwgriffithv | 0:88c60458332e | 64 | m_rpwm = pwm; |
| rwgriffithv | 0:88c60458332e | 65 | setMotorPwm(RIGHT_MOTOR, pwm); |
| rwgriffithv | 0:88c60458332e | 66 | } |
| rwgriffithv | 0:88c60458332e | 67 | |
| rwgriffithv | 0:88c60458332e | 68 | void Motors::setLeftPwm(float pwm) { |
| rwgriffithv | 0:88c60458332e | 69 | m_lpwm = pwm; |
| rwgriffithv | 0:88c60458332e | 70 | setMotorPwm(LEFT_MOTOR, pwm); |
| rwgriffithv | 0:88c60458332e | 71 | } |
| rwgriffithv | 0:88c60458332e | 72 | |
| rwgriffithv | 0:88c60458332e | 73 |
