![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Motor test
Dependencies: mbed
Fork of motor_reverser by
main.cpp@0:b54c5342b074, 2015-09-23 (annotated)
- Committer:
- bjornnijhuis
- Date:
- Wed Sep 23 08:14:49 2015 +0000
- Revision:
- 0:b54c5342b074
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bjornnijhuis | 0:b54c5342b074 | 1 | #include "mbed.h" |
bjornnijhuis | 0:b54c5342b074 | 2 | Ticker reverser; |
bjornnijhuis | 0:b54c5342b074 | 3 | |
bjornnijhuis | 0:b54c5342b074 | 4 | PwmOut motor_pwm(D5); |
bjornnijhuis | 0:b54c5342b074 | 5 | DigitalOut dir(D4); |
bjornnijhuis | 0:b54c5342b074 | 6 | |
bjornnijhuis | 0:b54c5342b074 | 7 | const int pwm_frequency = 10000; // PWM frequency |
bjornnijhuis | 0:b54c5342b074 | 8 | const float duty_cycle = 1; // Duty cycle (half speed) |
bjornnijhuis | 0:b54c5342b074 | 9 | bool direction = false; // Motor direction |
bjornnijhuis | 0:b54c5342b074 | 10 | |
bjornnijhuis | 0:b54c5342b074 | 11 | const float reverse_frequency = 0.25; // Reversion frequency |
bjornnijhuis | 0:b54c5342b074 | 12 | float reverse_period = 1/reverse_frequency; |
bjornnijhuis | 0:b54c5342b074 | 13 | |
bjornnijhuis | 0:b54c5342b074 | 14 | void dirswitch(){ |
bjornnijhuis | 0:b54c5342b074 | 15 | direction = !direction; // Reverse direction |
bjornnijhuis | 0:b54c5342b074 | 16 | dir.write(direction); // Sending direction to pin |
bjornnijhuis | 0:b54c5342b074 | 17 | } |
bjornnijhuis | 0:b54c5342b074 | 18 | |
bjornnijhuis | 0:b54c5342b074 | 19 | int main() |
bjornnijhuis | 0:b54c5342b074 | 20 | { |
bjornnijhuis | 0:b54c5342b074 | 21 | reverser.attach(dirswitch, reverse_period); |
bjornnijhuis | 0:b54c5342b074 | 22 | |
bjornnijhuis | 0:b54c5342b074 | 23 | dir.write(direction); // Write motor direction |
bjornnijhuis | 0:b54c5342b074 | 24 | motor_pwm.period(1/pwm_frequency); // Motor PWM period |
bjornnijhuis | 0:b54c5342b074 | 25 | motor_pwm.write(duty_cycle); // Set motor speed |
bjornnijhuis | 0:b54c5342b074 | 26 | |
bjornnijhuis | 0:b54c5342b074 | 27 | while(true){ |
bjornnijhuis | 0:b54c5342b074 | 28 | } |
bjornnijhuis | 0:b54c5342b074 | 29 | |
bjornnijhuis | 0:b54c5342b074 | 30 | |
bjornnijhuis | 0:b54c5342b074 | 31 | } |