cooler
Dependencies: mbed tsi_sensor DHT11
main.cpp@0:488e4bd141d5, 2019-06-04 (annotated)
- Committer:
- juanijc
- Date:
- Tue Jun 04 13:43:28 2019 +0000
- Revision:
- 0:488e4bd141d5
tp1_cooler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
juanijc | 0:488e4bd141d5 | 1 | #include "mbed.h" |
juanijc | 0:488e4bd141d5 | 2 | #include "Dht11.h" |
juanijc | 0:488e4bd141d5 | 3 | #include "tsi_sensor.h" |
juanijc | 0:488e4bd141d5 | 4 | |
juanijc | 0:488e4bd141d5 | 5 | #define m 0.04 |
juanijc | 0:488e4bd141d5 | 6 | #define b -0.6 |
juanijc | 0:488e4bd141d5 | 7 | |
juanijc | 0:488e4bd141d5 | 8 | //tsi |
juanijc | 0:488e4bd141d5 | 9 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
juanijc | 0:488e4bd141d5 | 10 | #define ELEC0 9 |
juanijc | 0:488e4bd141d5 | 11 | #define ELEC1 10 |
juanijc | 0:488e4bd141d5 | 12 | #elif defined (TARGET_KL05Z) |
juanijc | 0:488e4bd141d5 | 13 | #define ELEC0 9 |
juanijc | 0:488e4bd141d5 | 14 | #define ELEC1 8 |
juanijc | 0:488e4bd141d5 | 15 | #else |
juanijc | 0:488e4bd141d5 | 16 | #error TARGET NOT DEFINED |
juanijc | 0:488e4bd141d5 | 17 | #endif |
juanijc | 0:488e4bd141d5 | 18 | TSIAnalogSlider tsi(ELEC0, ELEC1, 40); |
juanijc | 0:488e4bd141d5 | 19 | |
juanijc | 0:488e4bd141d5 | 20 | //funciones y maquinas de estado |
juanijc | 0:488e4bd141d5 | 21 | void me_modo(); |
juanijc | 0:488e4bd141d5 | 22 | void timer(); |
juanijc | 0:488e4bd141d5 | 23 | |
juanijc | 0:488e4bd141d5 | 24 | enum me_modo_estados {abierto,cerrado}; |
juanijc | 0:488e4bd141d5 | 25 | enum me_modo_estados me_modo_estado=abierto; |
juanijc | 0:488e4bd141d5 | 26 | |
juanijc | 0:488e4bd141d5 | 27 | //entradas y salidas |
juanijc | 0:488e4bd141d5 | 28 | Dht11 sensor(PTB3); |
juanijc | 0:488e4bd141d5 | 29 | AnalogIn preset(PTB2); |
juanijc | 0:488e4bd141d5 | 30 | DigitalIn rpm(PTD4); |
juanijc | 0:488e4bd141d5 | 31 | |
juanijc | 0:488e4bd141d5 | 32 | PwmOut cooler(PTC2); |
juanijc | 0:488e4bd141d5 | 33 | |
juanijc | 0:488e4bd141d5 | 34 | |
juanijc | 0:488e4bd141d5 | 35 | Ticker t; |
juanijc | 0:488e4bd141d5 | 36 | |
juanijc | 0:488e4bd141d5 | 37 | //variables utilizadas |
juanijc | 0:488e4bd141d5 | 38 | int pres, pres_ant=0; |
juanijc | 0:488e4bd141d5 | 39 | int tmo=0, tmo1=0, tmo2, rebote=0; |
juanijc | 0:488e4bd141d5 | 40 | int primera=0; |
juanijc | 0:488e4bd141d5 | 41 | int flanco=0; |
juanijc | 0:488e4bd141d5 | 42 | int rpm_ant=0; |
juanijc | 0:488e4bd141d5 | 43 | int revoluciones=0; |
juanijc | 0:488e4bd141d5 | 44 | |
juanijc | 0:488e4bd141d5 | 45 | int main() |
juanijc | 0:488e4bd141d5 | 46 | { |
juanijc | 0:488e4bd141d5 | 47 | //llamo a la funcion timer() cada 0.01 seg |
juanijc | 0:488e4bd141d5 | 48 | t.attach(&timer,0.01); |
juanijc | 0:488e4bd141d5 | 49 | while(1) { |
juanijc | 0:488e4bd141d5 | 50 | me_modo(); |
juanijc | 0:488e4bd141d5 | 51 | } |
juanijc | 0:488e4bd141d5 | 52 | } |
juanijc | 0:488e4bd141d5 | 53 | //timer |
juanijc | 0:488e4bd141d5 | 54 | void timer() |
juanijc | 0:488e4bd141d5 | 55 | { |
juanijc | 0:488e4bd141d5 | 56 | if(tmo!=0) { |
juanijc | 0:488e4bd141d5 | 57 | tmo--; |
juanijc | 0:488e4bd141d5 | 58 | } |
juanijc | 0:488e4bd141d5 | 59 | if(tmo1!=0) { |
juanijc | 0:488e4bd141d5 | 60 | tmo1--; |
juanijc | 0:488e4bd141d5 | 61 | } |
juanijc | 0:488e4bd141d5 | 62 | if(tmo2!=0) { |
juanijc | 0:488e4bd141d5 | 63 | tmo2--; |
juanijc | 0:488e4bd141d5 | 64 | } |
juanijc | 0:488e4bd141d5 | 65 | if(rebote!=0) |
juanijc | 0:488e4bd141d5 | 66 | rebote--; |
juanijc | 0:488e4bd141d5 | 67 | } |
juanijc | 0:488e4bd141d5 | 68 | //maquina de estado del modo |
juanijc | 0:488e4bd141d5 | 69 | void me_modo() |
juanijc | 0:488e4bd141d5 | 70 | { |
juanijc | 0:488e4bd141d5 | 71 | switch(me_modo_estado) { |
juanijc | 0:488e4bd141d5 | 72 | |
juanijc | 0:488e4bd141d5 | 73 | case abierto: |
juanijc | 0:488e4bd141d5 | 74 | if(primera==0) { |
juanijc | 0:488e4bd141d5 | 75 | printf("Lazo abierto\n"); |
juanijc | 0:488e4bd141d5 | 76 | primera=1; |
juanijc | 0:488e4bd141d5 | 77 | } |
juanijc | 0:488e4bd141d5 | 78 | pres=preset*50; |
juanijc | 0:488e4bd141d5 | 79 | //detenccion de flancos descendentes |
juanijc | 0:488e4bd141d5 | 80 | if(rpm==0 && rpm_ant==1) { |
juanijc | 0:488e4bd141d5 | 81 | flanco++; |
juanijc | 0:488e4bd141d5 | 82 | rpm_ant=0; |
juanijc | 0:488e4bd141d5 | 83 | } |
juanijc | 0:488e4bd141d5 | 84 | if(rpm==1 && rpm_ant==0) { |
juanijc | 0:488e4bd141d5 | 85 | rpm_ant=1; |
juanijc | 0:488e4bd141d5 | 86 | } |
juanijc | 0:488e4bd141d5 | 87 | //calculo las RPM a partir de los flancos medidos en 1 seg |
juanijc | 0:488e4bd141d5 | 88 | if(tmo==0) { |
juanijc | 0:488e4bd141d5 | 89 | tmo=100; |
juanijc | 0:488e4bd141d5 | 90 | revoluciones=flanco*60; |
juanijc | 0:488e4bd141d5 | 91 | printf("RPM= %i \n",revoluciones); |
juanijc | 0:488e4bd141d5 | 92 | flanco=0; |
juanijc | 0:488e4bd141d5 | 93 | //mido el preset cada 1 seg para no variar el duty de salida constantemente |
juanijc | 0:488e4bd141d5 | 94 | if(preset.read()<0.1) |
juanijc | 0:488e4bd141d5 | 95 | cooler=0.1; |
juanijc | 0:488e4bd141d5 | 96 | else |
juanijc | 0:488e4bd141d5 | 97 | cooler=preset.read(); |
juanijc | 0:488e4bd141d5 | 98 | |
juanijc | 0:488e4bd141d5 | 99 | } |
juanijc | 0:488e4bd141d5 | 100 | //tsi sin rebote. Para cambiar al lazo cerrado |
juanijc | 0:488e4bd141d5 | 101 | if(tsi.readPercentage() && rebote==0) { |
juanijc | 0:488e4bd141d5 | 102 | me_modo_estado=cerrado; |
juanijc | 0:488e4bd141d5 | 103 | rebote=100; |
juanijc | 0:488e4bd141d5 | 104 | primera=0; |
juanijc | 0:488e4bd141d5 | 105 | } |
juanijc | 0:488e4bd141d5 | 106 | |
juanijc | 0:488e4bd141d5 | 107 | break; |
juanijc | 0:488e4bd141d5 | 108 | |
juanijc | 0:488e4bd141d5 | 109 | case cerrado: |
juanijc | 0:488e4bd141d5 | 110 | |
juanijc | 0:488e4bd141d5 | 111 | if(primera==0) { |
juanijc | 0:488e4bd141d5 | 112 | printf("Lazo cerrado\n"); |
juanijc | 0:488e4bd141d5 | 113 | primera=1; |
juanijc | 0:488e4bd141d5 | 114 | } |
juanijc | 0:488e4bd141d5 | 115 | //actualizo la informacion del sensor y mido las RPM cada 1 seg. |
juanijc | 0:488e4bd141d5 | 116 | if(tmo1==0) { |
juanijc | 0:488e4bd141d5 | 117 | tmo1=100; |
juanijc | 0:488e4bd141d5 | 118 | sensor.read(); |
juanijc | 0:488e4bd141d5 | 119 | revoluciones=flanco*60; |
juanijc | 0:488e4bd141d5 | 120 | printf("RPM= %i TEMP= %i\n",revoluciones, sensor.getCelsius()); |
juanijc | 0:488e4bd141d5 | 121 | flanco=0; |
juanijc | 0:488e4bd141d5 | 122 | } |
juanijc | 0:488e4bd141d5 | 123 | //Deteccion de flancos descendentes |
juanijc | 0:488e4bd141d5 | 124 | if(rpm==0 && rpm_ant==1) { |
juanijc | 0:488e4bd141d5 | 125 | flanco++; |
juanijc | 0:488e4bd141d5 | 126 | rpm_ant=0; |
juanijc | 0:488e4bd141d5 | 127 | } |
juanijc | 0:488e4bd141d5 | 128 | if(rpm==1 && rpm_ant==0) { |
juanijc | 0:488e4bd141d5 | 129 | rpm_ant=1; |
juanijc | 0:488e4bd141d5 | 130 | } |
juanijc | 0:488e4bd141d5 | 131 | //transformo la informacion de temperatura y la entrego al cooler |
juanijc | 0:488e4bd141d5 | 132 | if(tmo2==0) { |
juanijc | 0:488e4bd141d5 | 133 | tmo2=100; |
juanijc | 0:488e4bd141d5 | 134 | //se hace hasta 50 porque es el maximo que soporta el sensor |
juanijc | 0:488e4bd141d5 | 135 | if(sensor.getCelsius()>20 && 50>sensor.getCelsius()) { |
juanijc | 0:488e4bd141d5 | 136 | //recta que vincula la temperatura sensada entre 20 y 50 con el puerto de salida PWM |
juanijc | 0:488e4bd141d5 | 137 | cooler = sensor.getCelsius() * m + b; |
juanijc | 0:488e4bd141d5 | 138 | } |
juanijc | 0:488e4bd141d5 | 139 | //casos extremos de temperatura |
juanijc | 0:488e4bd141d5 | 140 | if(20>=sensor.getCelsius()) |
juanijc | 0:488e4bd141d5 | 141 | cooler=0.1; |
juanijc | 0:488e4bd141d5 | 142 | if(sensor.getCelsius()>=50) |
juanijc | 0:488e4bd141d5 | 143 | cooler=1; |
juanijc | 0:488e4bd141d5 | 144 | } |
juanijc | 0:488e4bd141d5 | 145 | //tsi sin rebote. Para cambiar al lazo abierto |
juanijc | 0:488e4bd141d5 | 146 | if(tsi.readPercentage() && rebote==0) { |
juanijc | 0:488e4bd141d5 | 147 | me_modo_estado=abierto; |
juanijc | 0:488e4bd141d5 | 148 | rebote=100; |
juanijc | 0:488e4bd141d5 | 149 | primera=0; |
juanijc | 0:488e4bd141d5 | 150 | } |
juanijc | 0:488e4bd141d5 | 151 | break; |
juanijc | 0:488e4bd141d5 | 152 | |
juanijc | 0:488e4bd141d5 | 153 | } |
juanijc | 0:488e4bd141d5 | 154 | } |