Tomas Zuran
/
TPEmbebidos-1
Sistema de control de un cooler a lazo abierto y cerrado
main.cpp@1:472a0e143e52, 2019-05-29 (annotated)
- Committer:
- tzuran
- Date:
- Wed May 29 22:07:41 2019 +0000
- Revision:
- 1:472a0e143e52
- Parent:
- 0:da7b1c04a659
Version final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
s_inoue_mbed | 0:da7b1c04a659 | 1 | #include "mbed.h" |
tzuran | 1:472a0e143e52 | 2 | #include "Dht11.h" |
tzuran | 1:472a0e143e52 | 3 | |
tzuran | 1:472a0e143e52 | 4 | //TICKERS CON FUNCIONES Y VARIABLES QUE SE USAN CON EL |
tzuran | 1:472a0e143e52 | 5 | Ticker sec; |
tzuran | 1:472a0e143e52 | 6 | void segundo(); |
tzuran | 1:472a0e143e52 | 7 | int mandar = 0; |
tzuran | 1:472a0e143e52 | 8 | int contador = 0; |
tzuran | 1:472a0e143e52 | 9 | |
tzuran | 1:472a0e143e52 | 10 | //SENSOR DE TEMPERATURA |
tzuran | 1:472a0e143e52 | 11 | Dht11 sensor(D2); |
tzuran | 1:472a0e143e52 | 12 | int temperatura = 0; |
tzuran | 1:472a0e143e52 | 13 | |
tzuran | 1:472a0e143e52 | 14 | //ENTRADA DEL SENSOR DEL MOTOR |
tzuran | 1:472a0e143e52 | 15 | DigitalIn rpm(D3); |
tzuran | 1:472a0e143e52 | 16 | |
tzuran | 1:472a0e143e52 | 17 | //SALIDA PWM PARA LA INTERFAZ DEL MOTOR |
tzuran | 1:472a0e143e52 | 18 | PwmOut motor(D4); |
tzuran | 1:472a0e143e52 | 19 | |
tzuran | 1:472a0e143e52 | 20 | //ENTRADA ANALOGICA PARA EL PRESET |
tzuran | 1:472a0e143e52 | 21 | AnalogIn preset(A0); |
s_inoue_mbed | 0:da7b1c04a659 | 22 | |
tzuran | 1:472a0e143e52 | 23 | //BOTON DE RESTART |
tzuran | 1:472a0e143e52 | 24 | Ticker boton; |
tzuran | 1:472a0e143e52 | 25 | DigitalIn restartButton(PTB8); |
tzuran | 1:472a0e143e52 | 26 | void button_in(); |
tzuran | 1:472a0e143e52 | 27 | unsigned char state1 = 0x00; |
tzuran | 1:472a0e143e52 | 28 | int button_output = 0; |
s_inoue_mbed | 0:da7b1c04a659 | 29 | |
tzuran | 1:472a0e143e52 | 30 | //MAQUINA DE ESTADOS CUENTA DE VUELTAS |
tzuran | 1:472a0e143e52 | 31 | void ME_RPM(); |
tzuran | 1:472a0e143e52 | 32 | enum ME_RPM_ESTADOS { ESPERO_FA, ESPERO_FD }; |
tzuran | 1:472a0e143e52 | 33 | ME_RPM_ESTADOS ME_RPM_ESTADO; |
tzuran | 1:472a0e143e52 | 34 | int cuenta_vueltas = 0; |
s_inoue_mbed | 0:da7b1c04a659 | 35 | |
tzuran | 1:472a0e143e52 | 36 | //MAQUINA DE ESTADOS GENERAL |
tzuran | 1:472a0e143e52 | 37 | void ME_GENERAL(); |
tzuran | 1:472a0e143e52 | 38 | enum ME_GENERAL_ESTADOS { LAZO_A, LAZO_C }; |
tzuran | 1:472a0e143e52 | 39 | ME_GENERAL_ESTADOS ME_GENERAL_ESTADO; |
s_inoue_mbed | 0:da7b1c04a659 | 40 | |
tzuran | 1:472a0e143e52 | 41 | //VARIABLE QUE GUARDA EL NIVEL DE DUTY QUE CALCULA EL PROGRAMA |
tzuran | 1:472a0e143e52 | 42 | float duty = 0; |
tzuran | 1:472a0e143e52 | 43 | |
s_inoue_mbed | 0:da7b1c04a659 | 44 | int main() |
s_inoue_mbed | 0:da7b1c04a659 | 45 | { |
tzuran | 1:472a0e143e52 | 46 | //INICIO EL MOTOR PRENDIDO AL MAXIMO PARA QUE SE LE PUEDA BAJAR EL DUTY POSTERIORMENTE |
tzuran | 1:472a0e143e52 | 47 | |
tzuran | 1:472a0e143e52 | 48 | motor = 1.0; |
tzuran | 1:472a0e143e52 | 49 | wait(2); |
tzuran | 1:472a0e143e52 | 50 | |
tzuran | 1:472a0e143e52 | 51 | //PULLUP PARA LA ENTRADA DEL BOTON DE CAMBIO DE MODO |
tzuran | 1:472a0e143e52 | 52 | restartButton.mode(PullUp); |
s_inoue_mbed | 0:da7b1c04a659 | 53 | |
tzuran | 1:472a0e143e52 | 54 | //TICKERS |
tzuran | 1:472a0e143e52 | 55 | sec.attach(&segundo, 1); |
tzuran | 1:472a0e143e52 | 56 | boton.attach(&button_in, 0.01); |
s_inoue_mbed | 0:da7b1c04a659 | 57 | |
tzuran | 1:472a0e143e52 | 58 | //ESTADO INICIAL MAQUINAS DE ESTADO |
tzuran | 1:472a0e143e52 | 59 | ME_RPM_ESTADO = ESPERO_FA; |
tzuran | 1:472a0e143e52 | 60 | ME_GENERAL_ESTADO = LAZO_A; |
s_inoue_mbed | 0:da7b1c04a659 | 61 | |
tzuran | 1:472a0e143e52 | 62 | while(1) { |
tzuran | 1:472a0e143e52 | 63 | ME_RPM(); |
tzuran | 1:472a0e143e52 | 64 | ME_GENERAL(); |
tzuran | 1:472a0e143e52 | 65 | |
tzuran | 1:472a0e143e52 | 66 | //CALCULO DE DUTY PARA CADA LAZO |
tzuran | 1:472a0e143e52 | 67 | if(ME_GENERAL_ESTADO == LAZO_A) { |
tzuran | 1:472a0e143e52 | 68 | if(preset < 0.035) { |
tzuran | 1:472a0e143e52 | 69 | duty = 0.035; // 200 RPM - 0.035 |
tzuran | 1:472a0e143e52 | 70 | } else { |
tzuran | 1:472a0e143e52 | 71 | duty = preset; |
tzuran | 1:472a0e143e52 | 72 | } |
s_inoue_mbed | 0:da7b1c04a659 | 73 | } else { |
tzuran | 1:472a0e143e52 | 74 | duty = temperatura < 20 ? 0.035 : temperatura / 70.0; |
s_inoue_mbed | 0:da7b1c04a659 | 75 | } |
tzuran | 1:472a0e143e52 | 76 | |
tzuran | 1:472a0e143e52 | 77 | //ENVIO DE DATOS A LA PC |
tzuran | 1:472a0e143e52 | 78 | if(mandar) { |
tzuran | 1:472a0e143e52 | 79 | printf("T: %d, Modo: %d\r\n", sensor.getCelsius(), ME_GENERAL_ESTADO); |
tzuran | 1:472a0e143e52 | 80 | printf("RPM: %d Duty: %f\r\n", cuenta_vueltas * 60, duty * 100); |
tzuran | 1:472a0e143e52 | 81 | printf("Boton: %d\n\r", button_output); |
tzuran | 1:472a0e143e52 | 82 | cuenta_vueltas = 0; |
tzuran | 1:472a0e143e52 | 83 | mandar = 0; |
tzuran | 1:472a0e143e52 | 84 | motor = duty; |
tzuran | 1:472a0e143e52 | 85 | } |
s_inoue_mbed | 0:da7b1c04a659 | 86 | } |
s_inoue_mbed | 0:da7b1c04a659 | 87 | } |
tzuran | 1:472a0e143e52 | 88 | |
tzuran | 1:472a0e143e52 | 89 | //TICKER 1 seg |
tzuran | 1:472a0e143e52 | 90 | void segundo() |
tzuran | 1:472a0e143e52 | 91 | { |
tzuran | 1:472a0e143e52 | 92 | //FLAG DE ENVIO DE DATOS A LA PC |
tzuran | 1:472a0e143e52 | 93 | mandar = 1; |
tzuran | 1:472a0e143e52 | 94 | |
tzuran | 1:472a0e143e52 | 95 | //LECTURA DE LA TEMEPERATURA DEL SENSOR |
tzuran | 1:472a0e143e52 | 96 | sensor.read(); |
tzuran | 1:472a0e143e52 | 97 | temperatura = sensor.getCelsius(); |
tzuran | 1:472a0e143e52 | 98 | |
tzuran | 1:472a0e143e52 | 99 | //CONTADOR 1 seg |
tzuran | 1:472a0e143e52 | 100 | contador++; |
tzuran | 1:472a0e143e52 | 101 | } |
tzuran | 1:472a0e143e52 | 102 | |
tzuran | 1:472a0e143e52 | 103 | void ME_RPM() |
tzuran | 1:472a0e143e52 | 104 | { |
tzuran | 1:472a0e143e52 | 105 | switch(ME_RPM_ESTADO) { |
tzuran | 1:472a0e143e52 | 106 | case ESPERO_FA: |
tzuran | 1:472a0e143e52 | 107 | //SI SE DETECTA UN FLANCO ASCENDENTE CAMBIO DE MODO |
tzuran | 1:472a0e143e52 | 108 | if(rpm) { |
tzuran | 1:472a0e143e52 | 109 | ME_RPM_ESTADO = ESPERO_FD; |
tzuran | 1:472a0e143e52 | 110 | cuenta_vueltas++; |
tzuran | 1:472a0e143e52 | 111 | } |
tzuran | 1:472a0e143e52 | 112 | break; |
tzuran | 1:472a0e143e52 | 113 | case ESPERO_FD: |
tzuran | 1:472a0e143e52 | 114 | //SI SE DETECTA UN FLANCO DESCENDENTE CAMBIO DE MODO |
tzuran | 1:472a0e143e52 | 115 | if(!rpm) { |
tzuran | 1:472a0e143e52 | 116 | ME_RPM_ESTADO = ESPERO_FA; |
tzuran | 1:472a0e143e52 | 117 | } |
tzuran | 1:472a0e143e52 | 118 | break; |
tzuran | 1:472a0e143e52 | 119 | } |
tzuran | 1:472a0e143e52 | 120 | |
tzuran | 1:472a0e143e52 | 121 | } |
tzuran | 1:472a0e143e52 | 122 | |
tzuran | 1:472a0e143e52 | 123 | void ME_GENERAL() |
tzuran | 1:472a0e143e52 | 124 | { |
tzuran | 1:472a0e143e52 | 125 | switch(ME_GENERAL_ESTADO) { |
tzuran | 1:472a0e143e52 | 126 | case LAZO_A: |
tzuran | 1:472a0e143e52 | 127 | //SI SE APRIETA EL BOTON Y YA PASO UN SEGUNDO DESDE EL ULTIMO CAMBIO DE MODO SE PRODUCE UN CAMBIO |
tzuran | 1:472a0e143e52 | 128 | if(button_output == 1 && contador > 1) { |
tzuran | 1:472a0e143e52 | 129 | ME_GENERAL_ESTADO = LAZO_C; |
tzuran | 1:472a0e143e52 | 130 | contador = 0; |
tzuran | 1:472a0e143e52 | 131 | } |
tzuran | 1:472a0e143e52 | 132 | break; |
tzuran | 1:472a0e143e52 | 133 | case LAZO_C: |
tzuran | 1:472a0e143e52 | 134 | //SI SE APRIETA EL BOTON Y YA PASO UN SEGUNDO DESDE EL ULTIMO CAMBIO DE MODO SE PRODUCE UN CAMBIO |
tzuran | 1:472a0e143e52 | 135 | if(button_output == 1 && contador > 1) { |
tzuran | 1:472a0e143e52 | 136 | ME_GENERAL_ESTADO = LAZO_A; |
tzuran | 1:472a0e143e52 | 137 | contador = 0; |
tzuran | 1:472a0e143e52 | 138 | } |
tzuran | 1:472a0e143e52 | 139 | break; |
tzuran | 1:472a0e143e52 | 140 | } |
tzuran | 1:472a0e143e52 | 141 | } |
tzuran | 1:472a0e143e52 | 142 | |
tzuran | 1:472a0e143e52 | 143 | //MUESTREO DEL BOTON PARA ELIMINACION DEL REBOTE |
tzuran | 1:472a0e143e52 | 144 | void button_in() |
tzuran | 1:472a0e143e52 | 145 | { |
tzuran | 1:472a0e143e52 | 146 | state1 = state1 >> 1; |
tzuran | 1:472a0e143e52 | 147 | |
tzuran | 1:472a0e143e52 | 148 | if(!restartButton) { |
tzuran | 1:472a0e143e52 | 149 | state1 |= 0x80; |
tzuran | 1:472a0e143e52 | 150 | } |
tzuran | 1:472a0e143e52 | 151 | if(state1 == 0xFF) |
tzuran | 1:472a0e143e52 | 152 | button_output = 1; |
tzuran | 1:472a0e143e52 | 153 | else if(state1 == 0) |
tzuran | 1:472a0e143e52 | 154 | button_output = 0; |
tzuran | 1:472a0e143e52 | 155 | } |