Motor Shield Example code for 2.74 Class @ MIT
Dependents: experiment_example motor_shield_example Lab3_experiment_example jumping_leg_clicky
MotorShield.cpp@0:f2ede00aed8a, 2020-08-24 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Aug 24 20:19:03 2020 +0000
- Revision:
- 0:f2ede00aed8a
- Child:
- 1:4c3c2b7337a6
Initial class structure;
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 | 0:f2ede00aed8a | 7 | #include "FastPWM.h" |
elijahsj | 0:f2ede00aed8a | 8 | |
elijahsj | 0:f2ede00aed8a | 9 | MotorShield::MotorShield(PinName forwardPin, PinName reversePin) { |
elijahsj | 0:f2ede00aed8a | 10 | |
elijahsj | 0:f2ede00aed8a | 11 | FastPWM forward(forwardPin, 1); |
elijahsj | 0:f2ede00aed8a | 12 | FastPWM reverse(reversePin, 1); |
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 | 0:f2ede00aed8a | 18 | direction_val = 0; |
elijahsj | 0:f2ede00aed8a | 19 | duty_cycle_val = 0; |
elijahsj | 0:f2ede00aed8a | 20 | period_val = 10.0; |
elijahsj | 0:f2ede00aed8a | 21 | |
elijahsj | 0:f2ede00aed8a | 22 | } |
elijahsj | 0:f2ede00aed8a | 23 | |
elijahsj | 0:f2ede00aed8a | 24 | void MotorShield::write(double duty_cycle) { |
elijahsj | 0:f2ede00aed8a | 25 | duty_cycle_val = duty_cycle; |
elijahsj | 0:f2ede00aed8a | 26 | writePWM(); |
elijahsj | 0:f2ede00aed8a | 27 | } |
elijahsj | 0:f2ede00aed8a | 28 | |
elijahsj | 0:f2ede00aed8a | 29 | void MotorShield::period(double period) { |
elijahsj | 0:f2ede00aed8a | 30 | period_val = period; |
elijahsj | 0:f2ede00aed8a | 31 | writePWM(); |
elijahsj | 0:f2ede00aed8a | 32 | } |
elijahsj | 0:f2ede00aed8a | 33 | |
elijahsj | 0:f2ede00aed8a | 34 | void MotorShield::direction(int direction) { |
elijahsj | 0:f2ede00aed8a | 35 | direction_val = direction; |
elijahsj | 0:f2ede00aed8a | 36 | writePWM(); |
elijahsj | 0:f2ede00aed8a | 37 | } |
elijahsj | 0:f2ede00aed8a | 38 | |
elijahsj | 0:f2ede00aed8a | 39 | void MotorShield::writePWM(){ |
elijahsj | 0:f2ede00aed8a | 40 | if (direction_val == 0){ |
elijahsj | 0:f2ede00aed8a | 41 | forward.period_us(period_val); |
elijahsj | 0:f2ede00aed8a | 42 | forward.write(duty_cycle_val); |
elijahsj | 0:f2ede00aed8a | 43 | reverse.period_us(period_val); |
elijahsj | 0:f2ede00aed8a | 44 | reverse.write(0); |
elijahsj | 0:f2ede00aed8a | 45 | } |
elijahsj | 0:f2ede00aed8a | 46 | else{ |
elijahsj | 0:f2ede00aed8a | 47 | reverse.period_us(period_val); |
elijahsj | 0:f2ede00aed8a | 48 | reverse.write(duty_cycle_val); |
elijahsj | 0:f2ede00aed8a | 49 | forward.period_us(period_val); |
elijahsj | 0:f2ede00aed8a | 50 | forward.write(0); |
elijahsj | 0:f2ede00aed8a | 51 | } |
elijahsj | 0:f2ede00aed8a | 52 | } |
elijahsj | 0:f2ede00aed8a | 53 |