![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Tutorial for first mbed project
Dependencies: mbed lib_workshop_2019
src/test_motor/ft_run_motor.cpp@9:19255172362f, 2019-10-28 (annotated)
- Committer:
- gvaquette
- Date:
- Mon Oct 28 05:29:08 2019 +0000
- Revision:
- 9:19255172362f
- Parent:
- 7:c7ff04228b11
Version pour whorkshop v2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gvaquette | 4:bf5caf4c0c88 | 1 | #include "mbed.h" |
gvaquette | 4:bf5caf4c0c88 | 2 | #include "test_motor.h" |
gvaquette | 9:19255172362f | 3 | #include "console_output.h" |
gvaquette | 9:19255172362f | 4 | |
gvaquette | 4:bf5caf4c0c88 | 5 | |
gvaquette | 5:d99659a45cab | 6 | /**********************************************************************/ |
gvaquette | 5:d99659a45cab | 7 | /** ft_run_motor **/ |
gvaquette | 5:d99659a45cab | 8 | /* Args : - direction (e_direction): direction to turn the motor */ |
gvaquette | 5:d99659a45cab | 9 | /* - duty_cycle (double): duty cycle for the PWM */ |
gvaquette | 5:d99659a45cab | 10 | /* - pwm_mot (PwmOut): pin to set the PWM */ |
gvaquette | 5:d99659a45cab | 11 | /* - dirA and dirB (DigitalOut): Pins to set the direction */ |
gvaquette | 5:d99659a45cab | 12 | /**********************************************************************/ |
gvaquette | 4:bf5caf4c0c88 | 13 | void ft_run_motor( e_direction direction, double duty_cycle, |
gvaquette | 4:bf5caf4c0c88 | 14 | PwmOut pwm_mot, DigitalOut dirA, DigitalOut dirB) |
gvaquette | 4:bf5caf4c0c88 | 15 | { |
gvaquette | 4:bf5caf4c0c88 | 16 | |
gvaquette | 4:bf5caf4c0c88 | 17 | /* TO DO : define direction */ |
gvaquette | 4:bf5caf4c0c88 | 18 | |
gvaquette | 4:bf5caf4c0c88 | 19 | if (direction == FORWARD) |
gvaquette | 4:bf5caf4c0c88 | 20 | { |
gvaquette | 7:c7ff04228b11 | 21 | |
gvaquette | 7:c7ff04228b11 | 22 | |
gvaquette | 4:bf5caf4c0c88 | 23 | } |
gvaquette | 4:bf5caf4c0c88 | 24 | else // direction == BACKWARD |
gvaquette | 4:bf5caf4c0c88 | 25 | { |
gvaquette | 7:c7ff04228b11 | 26 | |
gvaquette | 7:c7ff04228b11 | 27 | |
gvaquette | 4:bf5caf4c0c88 | 28 | } |
gvaquette | 4:bf5caf4c0c88 | 29 | |
gvaquette | 5:d99659a45cab | 30 | /* TO DO : apply duty cycle to pwm_mot */ |
gvaquette | 7:c7ff04228b11 | 31 | |
gvaquette | 4:bf5caf4c0c88 | 32 | } |
gvaquette | 9:19255172362f | 33 | |
gvaquette | 9:19255172362f | 34 | |
gvaquette | 9:19255172362f | 35 | double ft_get_duty_cycle_choice(Serial pc) { |
gvaquette | 9:19255172362f | 36 | double duty_cycle = 0; |
gvaquette | 9:19255172362f | 37 | |
gvaquette | 9:19255172362f | 38 | if (pc.readable() ) |
gvaquette | 9:19255172362f | 39 | { |
gvaquette | 9:19255172362f | 40 | int user_int = ft_get_user_int(pc); |
gvaquette | 9:19255172362f | 41 | if (user_int < 10 && user_int >= 0) duty_cycle = user_int/10.0; |
gvaquette | 9:19255172362f | 42 | } |
gvaquette | 9:19255172362f | 43 | return duty_cycle; |
gvaquette | 9:19255172362f | 44 | } |
gvaquette | 9:19255172362f | 45 | |
gvaquette | 9:19255172362f | 46 | void ft_test_motor(PwmOut pin_pwm_mot, DigitalOut pin_dirA, DigitalOut pin_dirB, |
gvaquette | 9:19255172362f | 47 | Serial &pc) |
gvaquette | 9:19255172362f | 48 | { |
gvaquette | 9:19255172362f | 49 | e_direction direction = FORWARD; |
gvaquette | 9:19255172362f | 50 | |
gvaquette | 9:19255172362f | 51 | double duty_cycle =0.1; |
gvaquette | 9:19255172362f | 52 | |
gvaquette | 9:19255172362f | 53 | do{ |
gvaquette | 9:19255172362f | 54 | duty_cycle = ft_get_duty_cycle_choice(pc); |
gvaquette | 9:19255172362f | 55 | ft_run_motor(direction, duty_cycle, pin_pwm_mot, pin_dirA, pin_dirB); |
gvaquette | 9:19255172362f | 56 | } while (duty_cycle > 0.0); |
gvaquette | 9:19255172362f | 57 | |
gvaquette | 9:19255172362f | 58 | return; |
gvaquette | 9:19255172362f | 59 | } |