Motor Shield Example code for 2.74 Class @ MIT
Dependents: experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky
Diff: MotorShield.cpp
- Revision:
- 3:2f46953e7c8b
- Parent:
- 1:4c3c2b7337a6
- Child:
- 4:2b45973bdc67
--- a/MotorShield.cpp Wed Aug 26 00:24:51 2020 +0000 +++ b/MotorShield.cpp Wed Aug 26 02:43:57 2020 +0000 @@ -1,60 +1,70 @@ /* Library to interface with 2.74 Motor Shield -** Uses a modified version of FastPWM library to improve PWM accuracy +** Uses low level HAL libraries to enable high speed PWM */ #include "mbed.h" #include "MotorShield.h" #include "HardwareSetup.h" -//GPIOStruct gpio; - -MotorShield::MotorShield(PinName forwardPin, PinName reversePin) { - +MotorShield::MotorShield(int periodTicks) { + periodTickVal = periodTicks; init(); } void MotorShield::init() { /** Initial config for the STM32H743 **/ - //Init_All_HW(&gpio); // Setup PWM, ADC, GPIO - //wait_us(100); - - //TIM12->CCR2 = (PWM_ARR>>1)*(0.5f); - //TIM12->CCR1 = (PWM_ARR>>1)*(0.5f); + initHardware(periodTickVal); // Setup PWM + wait_us(100); - direction_val = 0; - duty_cycle_val = 0; - period_val = 10.0; - } -void MotorShield::write(double duty_cycle) { - duty_cycle_val = duty_cycle; - writePWM(); +void MotorShield::motorAWrite(double duty_cycle, int direction) { + if (direction){ + TIM12->CCR2 = int(periodTickVal * duty_cycle); + TIM12->CCR1 = 0; + } + else { + TIM12->CCR2 = 0; + TIM12->CCR1 = int(periodTickVal * duty_cycle); + } + } -void MotorShield::period(double period) { - period_val = period; - writePWM(); -} - -void MotorShield::direction(int direction) { - direction_val = direction; - writePWM(); +void MotorShield::motorBWrite(double duty_cycle, int direction) { + if (direction){ + TIM15->CCR2 = int(periodTickVal * duty_cycle); + TIM15->CCR1 = 0; + } + else { + TIM15->CCR2 = 0; + TIM15->CCR1 = int(periodTickVal * duty_cycle); + } } -void MotorShield::writePWM(){ - if (direction_val == 0){ - //forward.period_us(period_val); - //forward.write(duty_cycle_val); - //reverse.period_us(period_val); - //reverse.write(0); +void MotorShield::motorCWrite(double duty_cycle, int direction) { + if (direction){ + TIM13->CCR1 = int(periodTickVal * duty_cycle); + TIM14->CCR1 = 0; } - else{ - //reverse.period_us(period_val); - //reverse.write(duty_cycle_val); - //forward.period_us(period_val); - //forward.write(0); + else { + TIM13->CCR1 = 0; + TIM14->CCR1 = int(periodTickVal * duty_cycle); } } - +void MotorShield::motorDWrite(double duty_cycle, int direction) { + if (direction){ + TIM16->CCR1 = int(periodTickVal * duty_cycle); + TIM17->CCR1 = 0; + } + else { + TIM16->CCR1 = 0; + TIM17->CCR1 = int(periodTickVal * duty_cycle); + } +} + +void MotorShield::changePeriod(int periodTicks){ + periodTickVal = periodTicks; + init(); +} +