Marcelo Costanzo Miranda
/
idle_control_stepper
Controle P de atuador de marcha lenta
main.cpp@2:aee0cbc8b7dd, 2019-04-08 (annotated)
- Committer:
- Marcelocostanzo
- Date:
- Mon Apr 08 13:59:52 2019 +0000
- Revision:
- 2:aee0cbc8b7dd
- Parent:
- 0:b5a9e0614efd
Controle de atuador de marcha lenta (motor de passo)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:b5a9e0614efd | 1 | #include "mbed.h" |
mbed_official | 0:b5a9e0614efd | 2 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 3 | #define setpoint 1000 //alvo de rotação |
Marcelocostanzo | 2:aee0cbc8b7dd | 4 | #define KP 0.0007 //ganho proporcional |
Marcelocostanzo | 2:aee0cbc8b7dd | 5 | #define close 0 //sentido dos passos |
Marcelocostanzo | 2:aee0cbc8b7dd | 6 | #define open 1 //sentido dos passos |
Marcelocostanzo | 2:aee0cbc8b7dd | 7 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 8 | //----Configurando pinos de saida----------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 9 | DigitalOut DIR_STEP(D6); |
Marcelocostanzo | 2:aee0cbc8b7dd | 10 | DigitalOut STEP(D7); |
Marcelocostanzo | 2:aee0cbc8b7dd | 11 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 12 | //-----Configurando pino de interrupção externa----------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 13 | InterruptIn crank_sensor(D8); |
Marcelocostanzo | 2:aee0cbc8b7dd | 14 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 15 | //-----Inclusão da função timer------------------ |
Marcelocostanzo | 2:aee0cbc8b7dd | 16 | Timer t1; |
Marcelocostanzo | 2:aee0cbc8b7dd | 17 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 18 | //Serial pc(USBTX, USBRX); |
Marcelocostanzo | 2:aee0cbc8b7dd | 19 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 20 | //------Variavel global------------------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 21 | float time_rpm = 0; |
Marcelocostanzo | 2:aee0cbc8b7dd | 22 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 23 | //----------Rotina de para ler o tempo entre bordas de descida-------------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 24 | void timer_read() |
Marcelocostanzo | 2:aee0cbc8b7dd | 25 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 26 | t1.stop(); |
Marcelocostanzo | 2:aee0cbc8b7dd | 27 | time_rpm = t1.read(); |
Marcelocostanzo | 2:aee0cbc8b7dd | 28 | t1.reset(); |
Marcelocostanzo | 2:aee0cbc8b7dd | 29 | t1.start(); |
Marcelocostanzo | 2:aee0cbc8b7dd | 30 | } |
Marcelocostanzo | 2:aee0cbc8b7dd | 31 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 32 | //-------Rotina para referenciar o motor de passso----------------------------------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 33 | void ref() |
Marcelocostanzo | 2:aee0cbc8b7dd | 34 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 35 | for(int i = 0; i < 300; i++)//curso de 250 passos, 300 para garantia de referenciamento |
Marcelocostanzo | 2:aee0cbc8b7dd | 36 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 37 | DIR_STEP = close; |
Marcelocostanzo | 2:aee0cbc8b7dd | 38 | STEP = 1; |
Marcelocostanzo | 2:aee0cbc8b7dd | 39 | wait_us(10); |
Marcelocostanzo | 2:aee0cbc8b7dd | 40 | STEP = 0; |
Marcelocostanzo | 2:aee0cbc8b7dd | 41 | wait_ms(5); |
Marcelocostanzo | 2:aee0cbc8b7dd | 42 | } |
Marcelocostanzo | 2:aee0cbc8b7dd | 43 | } |
mbed_official | 0:b5a9e0614efd | 44 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 45 | int main() |
Marcelocostanzo | 2:aee0cbc8b7dd | 46 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 47 | ref();//chama a rotina para refernciar |
Marcelocostanzo | 2:aee0cbc8b7dd | 48 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 49 | //----------variaveis locais---------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 50 | float rpm = 0, error = 0, delay = 0; |
Marcelocostanzo | 2:aee0cbc8b7dd | 51 | int steps_counter = 0; |
Marcelocostanzo | 2:aee0cbc8b7dd | 52 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 53 | //--------habilita a interrupção externa e aponta para o endereço da rotina de medição de tempo-------------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 54 | crank_sensor.fall(&timer_read); |
Marcelocostanzo | 2:aee0cbc8b7dd | 55 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 56 | while(1) |
Marcelocostanzo | 2:aee0cbc8b7dd | 57 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 58 | rpm = 1 / time_rpm; //converte tempo em frequencia |
Marcelocostanzo | 2:aee0cbc8b7dd | 59 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 60 | error = setpoint - rpm; //calcula o erro entre alvo e feedback |
Marcelocostanzo | 2:aee0cbc8b7dd | 61 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 62 | //-----Rotina caso o erro seja positivo--------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 63 | if(error > 30) |
Marcelocostanzo | 2:aee0cbc8b7dd | 64 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 65 | DIR_STEP = close; |
Marcelocostanzo | 2:aee0cbc8b7dd | 66 | STEP = 1; |
Marcelocostanzo | 2:aee0cbc8b7dd | 67 | wait_us(10); |
Marcelocostanzo | 2:aee0cbc8b7dd | 68 | steps_counter--; |
Marcelocostanzo | 2:aee0cbc8b7dd | 69 | STEP = 0; |
Marcelocostanzo | 2:aee0cbc8b7dd | 70 | delay = 1 / (error * KP); |
Marcelocostanzo | 2:aee0cbc8b7dd | 71 | wait_ms(delay); |
Marcelocostanzo | 2:aee0cbc8b7dd | 72 | } |
Marcelocostanzo | 2:aee0cbc8b7dd | 73 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 74 | //-----Rotina caso o erro seja negativo--------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 75 | if(error < -30) |
Marcelocostanzo | 2:aee0cbc8b7dd | 76 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 77 | error = error * -1.0f; |
Marcelocostanzo | 2:aee0cbc8b7dd | 78 | DIR_STEP = open; |
Marcelocostanzo | 2:aee0cbc8b7dd | 79 | STEP = 1; |
Marcelocostanzo | 2:aee0cbc8b7dd | 80 | wait_us(10); |
Marcelocostanzo | 2:aee0cbc8b7dd | 81 | steps_counter++; |
Marcelocostanzo | 2:aee0cbc8b7dd | 82 | STEP = 0; |
Marcelocostanzo | 2:aee0cbc8b7dd | 83 | delay = 1 / (error * KP); |
Marcelocostanzo | 2:aee0cbc8b7dd | 84 | wait_ms(delay); |
Marcelocostanzo | 2:aee0cbc8b7dd | 85 | } |
Marcelocostanzo | 2:aee0cbc8b7dd | 86 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 87 | //-----Rotina caso o erro seja neutro--------- |
Marcelocostanzo | 2:aee0cbc8b7dd | 88 | else |
Marcelocostanzo | 2:aee0cbc8b7dd | 89 | { |
Marcelocostanzo | 2:aee0cbc8b7dd | 90 | STEP = 0; |
Marcelocostanzo | 2:aee0cbc8b7dd | 91 | } |
Marcelocostanzo | 2:aee0cbc8b7dd | 92 | |
Marcelocostanzo | 2:aee0cbc8b7dd | 93 | //pc.printf("\n\rerror %f",error); |
Marcelocostanzo | 2:aee0cbc8b7dd | 94 | //pc.printf("\n\rdelay %f",delay); |
mbed_official | 0:b5a9e0614efd | 95 | } |
mbed_official | 0:b5a9e0614efd | 96 | } |