Motor Shield Example code for 2.74 Class @ MIT
Dependents: experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky
MotorShield.cpp@5:d2dffc88e94d, 2020-08-26 (annotated)
- Committer:
- elijahsj
- Date:
- Wed Aug 26 14:37:16 2020 +0000
- Revision:
- 5:d2dffc88e94d
- Parent:
- 4:2b45973bdc67
- Child:
- 7:e3a2ade56b79
PWM + Encoders work;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 0:f2ede00aed8a | 1 | /* Library to interface with 2.74 Motor Shield |
elijahsj | 3:2f46953e7c8b | 2 | ** Uses low level HAL libraries to enable high speed PWM |
elijahsj | 4:2b45973bdc67 | 3 | ** Use as follows: |
elijahsj | 4:2b45973bdc67 | 4 | ** - Create shield object and specify PWM period for the motors |
elijahsj | 4:2b45973bdc67 | 5 | ** - Set the duty cycle and direction for each motor |
elijahsj | 0:f2ede00aed8a | 6 | */ |
elijahsj | 0:f2ede00aed8a | 7 | |
elijahsj | 0:f2ede00aed8a | 8 | #include "mbed.h" |
elijahsj | 0:f2ede00aed8a | 9 | #include "MotorShield.h" |
elijahsj | 1:4c3c2b7337a6 | 10 | #include "HardwareSetup.h" |
elijahsj | 1:4c3c2b7337a6 | 11 | |
elijahsj | 3:2f46953e7c8b | 12 | MotorShield::MotorShield(int periodTicks) { |
elijahsj | 3:2f46953e7c8b | 13 | periodTickVal = periodTicks; |
elijahsj | 0:f2ede00aed8a | 14 | init(); |
elijahsj | 0:f2ede00aed8a | 15 | } |
elijahsj | 0:f2ede00aed8a | 16 | |
elijahsj | 0:f2ede00aed8a | 17 | void MotorShield::init() { |
elijahsj | 0:f2ede00aed8a | 18 | /** Initial config for the STM32H743 **/ |
elijahsj | 1:4c3c2b7337a6 | 19 | |
elijahsj | 3:2f46953e7c8b | 20 | initHardware(periodTickVal); // Setup PWM |
elijahsj | 3:2f46953e7c8b | 21 | wait_us(100); |
elijahsj | 1:4c3c2b7337a6 | 22 | |
elijahsj | 0:f2ede00aed8a | 23 | } |
elijahsj | 0:f2ede00aed8a | 24 | |
elijahsj | 5:d2dffc88e94d | 25 | void MotorShield::motorAWrite(float duty_cycle, int direction) { |
elijahsj | 5:d2dffc88e94d | 26 | int tick = (int)(periodTickVal * duty_cycle); |
elijahsj | 5:d2dffc88e94d | 27 | |
elijahsj | 3:2f46953e7c8b | 28 | if (direction){ |
elijahsj | 5:d2dffc88e94d | 29 | |
elijahsj | 5:d2dffc88e94d | 30 | TIM15->CCR2 = tick; |
elijahsj | 5:d2dffc88e94d | 31 | TIM15->CCR1 = 0; |
elijahsj | 5:d2dffc88e94d | 32 | |
elijahsj | 3:2f46953e7c8b | 33 | } |
elijahsj | 3:2f46953e7c8b | 34 | else { |
elijahsj | 5:d2dffc88e94d | 35 | TIM15->CCR2 = 0; |
elijahsj | 5:d2dffc88e94d | 36 | TIM15->CCR1 = tick; |
elijahsj | 3:2f46953e7c8b | 37 | } |
elijahsj | 3:2f46953e7c8b | 38 | |
elijahsj | 0:f2ede00aed8a | 39 | } |
elijahsj | 0:f2ede00aed8a | 40 | |
elijahsj | 5:d2dffc88e94d | 41 | void MotorShield::motorBWrite(float duty_cycle, int direction) { |
elijahsj | 5:d2dffc88e94d | 42 | int tick = (int)(periodTickVal * duty_cycle); |
elijahsj | 5:d2dffc88e94d | 43 | |
elijahsj | 3:2f46953e7c8b | 44 | if (direction){ |
elijahsj | 5:d2dffc88e94d | 45 | TIM12->CCR2 = tick; |
elijahsj | 5:d2dffc88e94d | 46 | TIM12->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 47 | } |
elijahsj | 3:2f46953e7c8b | 48 | else { |
elijahsj | 5:d2dffc88e94d | 49 | TIM12->CCR2 = 0; |
elijahsj | 5:d2dffc88e94d | 50 | TIM12->CCR1 = tick; |
elijahsj | 3:2f46953e7c8b | 51 | } |
elijahsj | 5:d2dffc88e94d | 52 | |
elijahsj | 0:f2ede00aed8a | 53 | } |
elijahsj | 0:f2ede00aed8a | 54 | |
elijahsj | 5:d2dffc88e94d | 55 | void MotorShield::motorCWrite(float duty_cycle, int direction) { |
elijahsj | 5:d2dffc88e94d | 56 | int tick = (int)(periodTickVal * duty_cycle); |
elijahsj | 5:d2dffc88e94d | 57 | |
elijahsj | 3:2f46953e7c8b | 58 | if (direction){ |
elijahsj | 5:d2dffc88e94d | 59 | TIM13->CCR1 = 0; |
elijahsj | 5:d2dffc88e94d | 60 | TIM14->CCR1 = tick; |
elijahsj | 0:f2ede00aed8a | 61 | } |
elijahsj | 3:2f46953e7c8b | 62 | else { |
elijahsj | 5:d2dffc88e94d | 63 | TIM13->CCR1 = tick; |
elijahsj | 5:d2dffc88e94d | 64 | TIM14->CCR1 = 0; |
elijahsj | 0:f2ede00aed8a | 65 | } |
elijahsj | 0:f2ede00aed8a | 66 | } |
elijahsj | 5:d2dffc88e94d | 67 | void MotorShield::motorDWrite(float duty_cycle, int direction) { |
elijahsj | 5:d2dffc88e94d | 68 | int tick = (int)(periodTickVal * duty_cycle); |
elijahsj | 5:d2dffc88e94d | 69 | |
elijahsj | 3:2f46953e7c8b | 70 | if (direction){ |
elijahsj | 5:d2dffc88e94d | 71 | TIM16->CCR1 = tick; |
elijahsj | 3:2f46953e7c8b | 72 | TIM17->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 73 | } |
elijahsj | 3:2f46953e7c8b | 74 | else { |
elijahsj | 3:2f46953e7c8b | 75 | TIM16->CCR1 = 0; |
elijahsj | 5:d2dffc88e94d | 76 | TIM17->CCR1 = tick; |
elijahsj | 3:2f46953e7c8b | 77 | } |
elijahsj | 3:2f46953e7c8b | 78 | } |
elijahsj | 3:2f46953e7c8b | 79 | |
elijahsj | 3:2f46953e7c8b | 80 | void MotorShield::changePeriod(int periodTicks){ |
elijahsj | 3:2f46953e7c8b | 81 | periodTickVal = periodTicks; |
elijahsj | 3:2f46953e7c8b | 82 | init(); |
elijahsj | 3:2f46953e7c8b | 83 | } |
elijahsj | 3:2f46953e7c8b | 84 |