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:
- 0:f2ede00aed8a
- Child:
- 1:4c3c2b7337a6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MotorShield.cpp Mon Aug 24 20:19:03 2020 +0000 @@ -0,0 +1,53 @@ +/* Library to interface with 2.74 Motor Shield +** Uses a modified version of FastPWM library to improve PWM accuracy +*/ + +#include "mbed.h" +#include "MotorShield.h" +#include "FastPWM.h" + +MotorShield::MotorShield(PinName forwardPin, PinName reversePin) { + + FastPWM forward(forwardPin, 1); + FastPWM reverse(reversePin, 1); + init(); +} + +void MotorShield::init() { + /** Initial config for the STM32H743 **/ + 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::period(double period) { + period_val = period; + writePWM(); +} + +void MotorShield::direction(int direction) { + direction_val = direction; + writePWM(); +} + +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); + } + else{ + reverse.period_us(period_val); + reverse.write(duty_cycle_val); + forward.period_us(period_val); + forward.write(0); + } +} +