Motor Shield Example code for 2.74 Class @ MIT
Dependents: experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky
MotorShield.cpp@4:2b45973bdc67, 2020-08-26 (annotated)
- Committer:
- elijahsj
- Date:
- Wed Aug 26 02:49:34 2020 +0000
- Revision:
- 4:2b45973bdc67
- Parent:
- 3:2f46953e7c8b
- Child:
- 5:d2dffc88e94d
documentation
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 | 3:2f46953e7c8b | 25 | void MotorShield::motorAWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 26 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 27 | TIM12->CCR2 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 28 | TIM12->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 29 | } |
elijahsj | 3:2f46953e7c8b | 30 | else { |
elijahsj | 3:2f46953e7c8b | 31 | TIM12->CCR2 = 0; |
elijahsj | 3:2f46953e7c8b | 32 | TIM12->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 33 | } |
elijahsj | 3:2f46953e7c8b | 34 | |
elijahsj | 0:f2ede00aed8a | 35 | } |
elijahsj | 0:f2ede00aed8a | 36 | |
elijahsj | 3:2f46953e7c8b | 37 | void MotorShield::motorBWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 38 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 39 | TIM15->CCR2 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 40 | TIM15->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 41 | } |
elijahsj | 3:2f46953e7c8b | 42 | else { |
elijahsj | 3:2f46953e7c8b | 43 | TIM15->CCR2 = 0; |
elijahsj | 3:2f46953e7c8b | 44 | TIM15->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 45 | } |
elijahsj | 0:f2ede00aed8a | 46 | } |
elijahsj | 0:f2ede00aed8a | 47 | |
elijahsj | 3:2f46953e7c8b | 48 | void MotorShield::motorCWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 49 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 50 | TIM13->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 51 | TIM14->CCR1 = 0; |
elijahsj | 0:f2ede00aed8a | 52 | } |
elijahsj | 3:2f46953e7c8b | 53 | else { |
elijahsj | 3:2f46953e7c8b | 54 | TIM13->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 55 | TIM14->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 0:f2ede00aed8a | 56 | } |
elijahsj | 0:f2ede00aed8a | 57 | } |
elijahsj | 3:2f46953e7c8b | 58 | void MotorShield::motorDWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 59 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 60 | TIM16->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 61 | TIM17->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 62 | } |
elijahsj | 3:2f46953e7c8b | 63 | else { |
elijahsj | 3:2f46953e7c8b | 64 | TIM16->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 65 | TIM17->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 66 | } |
elijahsj | 3:2f46953e7c8b | 67 | } |
elijahsj | 3:2f46953e7c8b | 68 | |
elijahsj | 3:2f46953e7c8b | 69 | void MotorShield::changePeriod(int periodTicks){ |
elijahsj | 3:2f46953e7c8b | 70 | periodTickVal = periodTicks; |
elijahsj | 3:2f46953e7c8b | 71 | init(); |
elijahsj | 3:2f46953e7c8b | 72 | } |
elijahsj | 3:2f46953e7c8b | 73 |