Updated for the next revision of the motor board
MotorShield.cpp@3:2f46953e7c8b, 2020-08-26 (annotated)
- Committer:
- elijahsj
- Date:
- Wed Aug 26 02:43:57 2020 +0000
- Revision:
- 3:2f46953e7c8b
- Parent:
- 1:4c3c2b7337a6
- Child:
- 4:2b45973bdc67
finished shield code;
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 | 0:f2ede00aed8a | 3 | */ |
elijahsj | 0:f2ede00aed8a | 4 | |
elijahsj | 0:f2ede00aed8a | 5 | #include "mbed.h" |
elijahsj | 0:f2ede00aed8a | 6 | #include "MotorShield.h" |
elijahsj | 1:4c3c2b7337a6 | 7 | #include "HardwareSetup.h" |
elijahsj | 1:4c3c2b7337a6 | 8 | |
elijahsj | 3:2f46953e7c8b | 9 | MotorShield::MotorShield(int periodTicks) { |
elijahsj | 3:2f46953e7c8b | 10 | periodTickVal = periodTicks; |
elijahsj | 0:f2ede00aed8a | 11 | init(); |
elijahsj | 0:f2ede00aed8a | 12 | } |
elijahsj | 0:f2ede00aed8a | 13 | |
elijahsj | 0:f2ede00aed8a | 14 | void MotorShield::init() { |
elijahsj | 0:f2ede00aed8a | 15 | /** Initial config for the STM32H743 **/ |
elijahsj | 1:4c3c2b7337a6 | 16 | |
elijahsj | 3:2f46953e7c8b | 17 | initHardware(periodTickVal); // Setup PWM |
elijahsj | 3:2f46953e7c8b | 18 | wait_us(100); |
elijahsj | 1:4c3c2b7337a6 | 19 | |
elijahsj | 0:f2ede00aed8a | 20 | } |
elijahsj | 0:f2ede00aed8a | 21 | |
elijahsj | 3:2f46953e7c8b | 22 | void MotorShield::motorAWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 23 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 24 | TIM12->CCR2 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 25 | TIM12->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 26 | } |
elijahsj | 3:2f46953e7c8b | 27 | else { |
elijahsj | 3:2f46953e7c8b | 28 | TIM12->CCR2 = 0; |
elijahsj | 3:2f46953e7c8b | 29 | TIM12->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 30 | } |
elijahsj | 3:2f46953e7c8b | 31 | |
elijahsj | 0:f2ede00aed8a | 32 | } |
elijahsj | 0:f2ede00aed8a | 33 | |
elijahsj | 3:2f46953e7c8b | 34 | void MotorShield::motorBWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 35 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 36 | TIM15->CCR2 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 37 | TIM15->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 38 | } |
elijahsj | 3:2f46953e7c8b | 39 | else { |
elijahsj | 3:2f46953e7c8b | 40 | TIM15->CCR2 = 0; |
elijahsj | 3:2f46953e7c8b | 41 | TIM15->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 42 | } |
elijahsj | 0:f2ede00aed8a | 43 | } |
elijahsj | 0:f2ede00aed8a | 44 | |
elijahsj | 3:2f46953e7c8b | 45 | void MotorShield::motorCWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 46 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 47 | TIM13->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 48 | TIM14->CCR1 = 0; |
elijahsj | 0:f2ede00aed8a | 49 | } |
elijahsj | 3:2f46953e7c8b | 50 | else { |
elijahsj | 3:2f46953e7c8b | 51 | TIM13->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 52 | TIM14->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 0:f2ede00aed8a | 53 | } |
elijahsj | 0:f2ede00aed8a | 54 | } |
elijahsj | 3:2f46953e7c8b | 55 | void MotorShield::motorDWrite(double duty_cycle, int direction) { |
elijahsj | 3:2f46953e7c8b | 56 | if (direction){ |
elijahsj | 3:2f46953e7c8b | 57 | TIM16->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 58 | TIM17->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 59 | } |
elijahsj | 3:2f46953e7c8b | 60 | else { |
elijahsj | 3:2f46953e7c8b | 61 | TIM16->CCR1 = 0; |
elijahsj | 3:2f46953e7c8b | 62 | TIM17->CCR1 = int(periodTickVal * duty_cycle); |
elijahsj | 3:2f46953e7c8b | 63 | } |
elijahsj | 3:2f46953e7c8b | 64 | } |
elijahsj | 3:2f46953e7c8b | 65 | |
elijahsj | 3:2f46953e7c8b | 66 | void MotorShield::changePeriod(int periodTicks){ |
elijahsj | 3:2f46953e7c8b | 67 | periodTickVal = periodTicks; |
elijahsj | 3:2f46953e7c8b | 68 | init(); |
elijahsj | 3:2f46953e7c8b | 69 | } |
elijahsj | 3:2f46953e7c8b | 70 |