Updated for the next revision of the motor board
MotorShield.cpp@1:4c3c2b7337a6, 2020-08-25 (annotated)
- Committer:
- elijahsj
- Date:
- Tue Aug 25 23:38:26 2020 +0000
- Revision:
- 1:4c3c2b7337a6
- Parent:
- 0:f2ede00aed8a
- Child:
- 3:2f46953e7c8b
Fixed HAL libraries;
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 | 0:f2ede00aed8a | 2 | ** Uses a modified version of FastPWM library to improve PWM accuracy |
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 | 1:4c3c2b7337a6 | 9 | //GPIOStruct gpio; |
elijahsj | 0:f2ede00aed8a | 10 | |
elijahsj | 0:f2ede00aed8a | 11 | MotorShield::MotorShield(PinName forwardPin, PinName reversePin) { |
elijahsj | 1:4c3c2b7337a6 | 12 | |
elijahsj | 0:f2ede00aed8a | 13 | init(); |
elijahsj | 0:f2ede00aed8a | 14 | } |
elijahsj | 0:f2ede00aed8a | 15 | |
elijahsj | 0:f2ede00aed8a | 16 | void MotorShield::init() { |
elijahsj | 0:f2ede00aed8a | 17 | /** Initial config for the STM32H743 **/ |
elijahsj | 1:4c3c2b7337a6 | 18 | |
elijahsj | 1:4c3c2b7337a6 | 19 | //Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
elijahsj | 1:4c3c2b7337a6 | 20 | //wait_us(100); |
elijahsj | 1:4c3c2b7337a6 | 21 | |
elijahsj | 1:4c3c2b7337a6 | 22 | //TIM12->CCR2 = (PWM_ARR>>1)*(0.5f); |
elijahsj | 1:4c3c2b7337a6 | 23 | //TIM12->CCR1 = (PWM_ARR>>1)*(0.5f); |
elijahsj | 1:4c3c2b7337a6 | 24 | |
elijahsj | 0:f2ede00aed8a | 25 | direction_val = 0; |
elijahsj | 0:f2ede00aed8a | 26 | duty_cycle_val = 0; |
elijahsj | 0:f2ede00aed8a | 27 | period_val = 10.0; |
elijahsj | 0:f2ede00aed8a | 28 | |
elijahsj | 0:f2ede00aed8a | 29 | } |
elijahsj | 0:f2ede00aed8a | 30 | |
elijahsj | 0:f2ede00aed8a | 31 | void MotorShield::write(double duty_cycle) { |
elijahsj | 0:f2ede00aed8a | 32 | duty_cycle_val = duty_cycle; |
elijahsj | 0:f2ede00aed8a | 33 | writePWM(); |
elijahsj | 0:f2ede00aed8a | 34 | } |
elijahsj | 0:f2ede00aed8a | 35 | |
elijahsj | 0:f2ede00aed8a | 36 | void MotorShield::period(double period) { |
elijahsj | 0:f2ede00aed8a | 37 | period_val = period; |
elijahsj | 0:f2ede00aed8a | 38 | writePWM(); |
elijahsj | 0:f2ede00aed8a | 39 | } |
elijahsj | 0:f2ede00aed8a | 40 | |
elijahsj | 0:f2ede00aed8a | 41 | void MotorShield::direction(int direction) { |
elijahsj | 0:f2ede00aed8a | 42 | direction_val = direction; |
elijahsj | 0:f2ede00aed8a | 43 | writePWM(); |
elijahsj | 0:f2ede00aed8a | 44 | } |
elijahsj | 0:f2ede00aed8a | 45 | |
elijahsj | 0:f2ede00aed8a | 46 | void MotorShield::writePWM(){ |
elijahsj | 0:f2ede00aed8a | 47 | if (direction_val == 0){ |
elijahsj | 1:4c3c2b7337a6 | 48 | //forward.period_us(period_val); |
elijahsj | 1:4c3c2b7337a6 | 49 | //forward.write(duty_cycle_val); |
elijahsj | 1:4c3c2b7337a6 | 50 | //reverse.period_us(period_val); |
elijahsj | 1:4c3c2b7337a6 | 51 | //reverse.write(0); |
elijahsj | 0:f2ede00aed8a | 52 | } |
elijahsj | 0:f2ede00aed8a | 53 | else{ |
elijahsj | 1:4c3c2b7337a6 | 54 | //reverse.period_us(period_val); |
elijahsj | 1:4c3c2b7337a6 | 55 | //reverse.write(duty_cycle_val); |
elijahsj | 1:4c3c2b7337a6 | 56 | //forward.period_us(period_val); |
elijahsj | 1:4c3c2b7337a6 | 57 | //forward.write(0); |
elijahsj | 0:f2ede00aed8a | 58 | } |
elijahsj | 0:f2ede00aed8a | 59 | } |
elijahsj | 0:f2ede00aed8a | 60 |