Javier Sanchez Peña
/
motor_prueva
control de velocidad progresivo de motor CC en ambos sentidos
main.cpp@0:f3b48db841cd, 2017-03-22 (annotated)
- Committer:
- javierjsp
- Date:
- Wed Mar 22 17:28:28 2017 +0000
- Revision:
- 0:f3b48db841cd
version 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
javierjsp | 0:f3b48db841cd | 1 | |
javierjsp | 0:f3b48db841cd | 2 | #include "mbed.h" |
javierjsp | 0:f3b48db841cd | 3 | |
javierjsp | 0:f3b48db841cd | 4 | PwmOut PWM1(PF_8); // pwm outputs |
javierjsp | 0:f3b48db841cd | 5 | PwmOut PWM2(PF_7); |
javierjsp | 0:f3b48db841cd | 6 | DigitalOut led1(LED1); |
javierjsp | 0:f3b48db841cd | 7 | |
javierjsp | 0:f3b48db841cd | 8 | /* Function prototype */ |
javierjsp | 0:f3b48db841cd | 9 | void motorControl(bool dir, float pwmVal); |
javierjsp | 0:f3b48db841cd | 10 | |
javierjsp | 0:f3b48db841cd | 11 | int main() |
javierjsp | 0:f3b48db841cd | 12 | { |
javierjsp | 0:f3b48db841cd | 13 | |
javierjsp | 0:f3b48db841cd | 14 | led1 = 1; |
javierjsp | 0:f3b48db841cd | 15 | wait(1); |
javierjsp | 0:f3b48db841cd | 16 | led1 = 0; |
javierjsp | 0:f3b48db841cd | 17 | PWM1.write(0); // Duty cycles are initially zero |
javierjsp | 0:f3b48db841cd | 18 | PWM2.write(0); |
javierjsp | 0:f3b48db841cd | 19 | PWM1.period(0.02f); // 1 kHz pwm frequency |
javierjsp | 0:f3b48db841cd | 20 | PWM2.period(0.02f); |
javierjsp | 0:f3b48db841cd | 21 | |
javierjsp | 0:f3b48db841cd | 22 | while(1) |
javierjsp | 0:f3b48db841cd | 23 | { |
javierjsp | 0:f3b48db841cd | 24 | for(float i=0;i<1;i=i+0.01f) // increase speed from 0 to 1 in 5 seconds (50ms*100). |
javierjsp | 0:f3b48db841cd | 25 | { |
javierjsp | 0:f3b48db841cd | 26 | motorControl(1,i); |
javierjsp | 0:f3b48db841cd | 27 | wait(0.05f); |
javierjsp | 0:f3b48db841cd | 28 | } |
javierjsp | 0:f3b48db841cd | 29 | for(float i=1;i>0;i=i-0.01f) // decrease speed from 1 to 0 in 5 seconds (50ms*100). |
javierjsp | 0:f3b48db841cd | 30 | { |
javierjsp | 0:f3b48db841cd | 31 | motorControl(0,i); |
javierjsp | 0:f3b48db841cd | 32 | wait(0.05f); |
javierjsp | 0:f3b48db841cd | 33 | } |
javierjsp | 0:f3b48db841cd | 34 | led1 = 1; |
javierjsp | 0:f3b48db841cd | 35 | wait(0.5); |
javierjsp | 0:f3b48db841cd | 36 | led1 = 0; |
javierjsp | 0:f3b48db841cd | 37 | } |
javierjsp | 0:f3b48db841cd | 38 | } |
javierjsp | 0:f3b48db841cd | 39 | |
javierjsp | 0:f3b48db841cd | 40 | // This is a motor control function which controls the speed and direction of |
javierjsp | 0:f3b48db841cd | 41 | // a motor. Motor drivers like L293D can be used. |
javierjsp | 0:f3b48db841cd | 42 | void motorControl(bool dir, float pwmVal) |
javierjsp | 0:f3b48db841cd | 43 | { |
javierjsp | 0:f3b48db841cd | 44 | if(dir==1) // forward direction |
javierjsp | 0:f3b48db841cd | 45 | { |
javierjsp | 0:f3b48db841cd | 46 | PWM2.write(0); |
javierjsp | 0:f3b48db841cd | 47 | PWM1.write(pwmVal); |
javierjsp | 0:f3b48db841cd | 48 | } |
javierjsp | 0:f3b48db841cd | 49 | else if(dir==0) // reverse direction |
javierjsp | 0:f3b48db841cd | 50 | { |
javierjsp | 0:f3b48db841cd | 51 | PWM1.write(0); |
javierjsp | 0:f3b48db841cd | 52 | PWM2.write(pwmVal); |
javierjsp | 0:f3b48db841cd | 53 | } |
javierjsp | 0:f3b48db841cd | 54 | } |