Cristian Castro
/
E01-Entrega_1
Lectura del sensor TCS3200, para la identificación de colores.
main.cpp@0:19d20daef799, 2021-09-03 (annotated)
- Committer:
- CCastrop1012
- Date:
- Fri Sep 03 05:16:11 2021 +0000
- Revision:
- 0:19d20daef799
Lectura del sensor TCS3200, para la identificacion de colores
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CCastrop1012 | 0:19d20daef799 | 1 | #include "mbed.h" |
CCastrop1012 | 0:19d20daef799 | 2 | #include "scolor_TCS3200.h" |
CCastrop1012 | 0:19d20daef799 | 3 | |
CCastrop1012 | 0:19d20daef799 | 4 | |
CCastrop1012 | 0:19d20daef799 | 5 | //****************************************************************************** |
CCastrop1012 | 0:19d20daef799 | 6 | // Definiciones |
CCastrop1012 | 0:19d20daef799 | 7 | |
CCastrop1012 | 0:19d20daef799 | 8 | /// Puerto Serial |
CCastrop1012 | 0:19d20daef799 | 9 | Serial SerialPort(SERIAL_TX, SERIAL_RX, 9600); |
CCastrop1012 | 0:19d20daef799 | 10 | |
CCastrop1012 | 0:19d20daef799 | 11 | /// PWM OUTPUTS |
CCastrop1012 | 0:19d20daef799 | 12 | PwmOut Buzzer(LED1); |
CCastrop1012 | 0:19d20daef799 | 13 | |
CCastrop1012 | 0:19d20daef799 | 14 | // Temporizadores |
CCastrop1012 | 0:19d20daef799 | 15 | Ticker LengthTonoBuzzer; |
CCastrop1012 | 0:19d20daef799 | 16 | |
CCastrop1012 | 0:19d20daef799 | 17 | // SENSOR DE COLOR |
CCastrop1012 | 0:19d20daef799 | 18 | scolor_TCS3200 SENSOR_COLOR (PA_8, PB_10, PB_4, PB_5, PB_3); |
CCastrop1012 | 0:19d20daef799 | 19 | |
CCastrop1012 | 0:19d20daef799 | 20 | |
CCastrop1012 | 0:19d20daef799 | 21 | // constantes |
CCastrop1012 | 0:19d20daef799 | 22 | //const int long_vector = 10; // longitud del vector |
CCastrop1012 | 0:19d20daef799 | 23 | |
CCastrop1012 | 0:19d20daef799 | 24 | /// Global Variables |
CCastrop1012 | 0:19d20daef799 | 25 | |
CCastrop1012 | 0:19d20daef799 | 26 | |
CCastrop1012 | 0:19d20daef799 | 27 | |
CCastrop1012 | 0:19d20daef799 | 28 | long red; |
CCastrop1012 | 0:19d20daef799 | 29 | long blue; |
CCastrop1012 | 0:19d20daef799 | 30 | long green; |
CCastrop1012 | 0:19d20daef799 | 31 | long clear; |
CCastrop1012 | 0:19d20daef799 | 32 | |
CCastrop1012 | 0:19d20daef799 | 33 | float F_red; |
CCastrop1012 | 0:19d20daef799 | 34 | float F_blue; |
CCastrop1012 | 0:19d20daef799 | 35 | float F_green; |
CCastrop1012 | 0:19d20daef799 | 36 | float F_clear; |
CCastrop1012 | 0:19d20daef799 | 37 | |
CCastrop1012 | 0:19d20daef799 | 38 | uint8_t coolterm_comand, i = 0; // Variable usada como Indice de los vectores, |
CCastrop1012 | 0:19d20daef799 | 39 | // indica la posicion del vector en donde se reciben |
CCastrop1012 | 0:19d20daef799 | 40 | // ó se almacenan los datos |
CCastrop1012 | 0:19d20daef799 | 41 | |
CCastrop1012 | 0:19d20daef799 | 42 | float periodo_Buzzer = 1; |
CCastrop1012 | 0:19d20daef799 | 43 | uint8_t duracion_Tono = 1; |
CCastrop1012 | 0:19d20daef799 | 44 | |
CCastrop1012 | 0:19d20daef799 | 45 | #define DO 3.78 /// VALOR DEL PERIODO EN MS |
CCastrop1012 | 0:19d20daef799 | 46 | #define RE 3.36 /// VALOR DEL PERIODO EN MS |
CCastrop1012 | 0:19d20daef799 | 47 | #define MI 3.03 /// VALOR DEL PERIODO EN MS |
CCastrop1012 | 0:19d20daef799 | 48 | #define SI 2.02 /// VALOR DEL PERIODO EN MS |
CCastrop1012 | 0:19d20daef799 | 49 | |
CCastrop1012 | 0:19d20daef799 | 50 | |
CCastrop1012 | 0:19d20daef799 | 51 | |
CCastrop1012 | 0:19d20daef799 | 52 | //****************************************************************************** |
CCastrop1012 | 0:19d20daef799 | 53 | // COMANDOS |
CCastrop1012 | 0:19d20daef799 | 54 | |
CCastrop1012 | 0:19d20daef799 | 55 | #define iniciar_telemetria 0xFE |
CCastrop1012 | 0:19d20daef799 | 56 | #define CMD_rojo 0x01 |
CCastrop1012 | 0:19d20daef799 | 57 | #define CMD_azul 0x02 |
CCastrop1012 | 0:19d20daef799 | 58 | #define CMD_verde 0x03 |
CCastrop1012 | 0:19d20daef799 | 59 | #define CMD_clear 0x04 |
CCastrop1012 | 0:19d20daef799 | 60 | #define ColorNoIdentificado 0x05 |
CCastrop1012 | 0:19d20daef799 | 61 | |
CCastrop1012 | 0:19d20daef799 | 62 | uint8_t color_identificado = ColorNoIdentificado; |
CCastrop1012 | 0:19d20daef799 | 63 | |
CCastrop1012 | 0:19d20daef799 | 64 | //**************************************************************************** |
CCastrop1012 | 0:19d20daef799 | 65 | // prototipo de funciones |
CCastrop1012 | 0:19d20daef799 | 66 | |
CCastrop1012 | 0:19d20daef799 | 67 | void ReadPort(void); // Lee el puerto Serial |
CCastrop1012 | 0:19d20daef799 | 68 | void MainConfig(void); // Configuracion Inicial de los Perifericos del uC |
CCastrop1012 | 0:19d20daef799 | 69 | void leer_color(void); // funcion que retorna los componentes |
CCastrop1012 | 0:19d20daef799 | 70 | // RGB y Clear del color leido |
CCastrop1012 | 0:19d20daef799 | 71 | //**************************************************************************** |
CCastrop1012 | 0:19d20daef799 | 72 | // |
CCastrop1012 | 0:19d20daef799 | 73 | |
CCastrop1012 | 0:19d20daef799 | 74 | void ReadPort() |
CCastrop1012 | 0:19d20daef799 | 75 | { |
CCastrop1012 | 0:19d20daef799 | 76 | |
CCastrop1012 | 0:19d20daef799 | 77 | coolterm_comand = SerialPort.getc(); |
CCastrop1012 | 0:19d20daef799 | 78 | |
CCastrop1012 | 0:19d20daef799 | 79 | } |
CCastrop1012 | 0:19d20daef799 | 80 | |
CCastrop1012 | 0:19d20daef799 | 81 | |
CCastrop1012 | 0:19d20daef799 | 82 | int main() |
CCastrop1012 | 0:19d20daef799 | 83 | { |
CCastrop1012 | 0:19d20daef799 | 84 | Buzzer.write(0); |
CCastrop1012 | 0:19d20daef799 | 85 | SerialPort.attach(&ReadPort, Serial::RxIrq); |
CCastrop1012 | 0:19d20daef799 | 86 | SerialPort.printf("Hello World, System Run !!\n"); |
CCastrop1012 | 0:19d20daef799 | 87 | |
CCastrop1012 | 0:19d20daef799 | 88 | while(1) |
CCastrop1012 | 0:19d20daef799 | 89 | { |
CCastrop1012 | 0:19d20daef799 | 90 | |
CCastrop1012 | 0:19d20daef799 | 91 | while (coolterm_comand != iniciar_telemetria); |
CCastrop1012 | 0:19d20daef799 | 92 | SerialPort.printf("COMANDO RECIBIDO!!\n"); |
CCastrop1012 | 0:19d20daef799 | 93 | |
CCastrop1012 | 0:19d20daef799 | 94 | leer_color(); |
CCastrop1012 | 0:19d20daef799 | 95 | coolterm_comand = 0; |
CCastrop1012 | 0:19d20daef799 | 96 | |
CCastrop1012 | 0:19d20daef799 | 97 | |
CCastrop1012 | 0:19d20daef799 | 98 | /* |
CCastrop1012 | 0:19d20daef799 | 99 | switch(coolterm_comand) |
CCastrop1012 | 0:19d20daef799 | 100 | { |
CCastrop1012 | 0:19d20daef799 | 101 | case CMD_rojo: leer_color(); |
CCastrop1012 | 0:19d20daef799 | 102 | break; |
CCastrop1012 | 0:19d20daef799 | 103 | |
CCastrop1012 | 0:19d20daef799 | 104 | case CMD_azul: leer_color(); |
CCastrop1012 | 0:19d20daef799 | 105 | break; |
CCastrop1012 | 0:19d20daef799 | 106 | |
CCastrop1012 | 0:19d20daef799 | 107 | case CMD_verde: leer_color(); |
CCastrop1012 | 0:19d20daef799 | 108 | break; |
CCastrop1012 | 0:19d20daef799 | 109 | |
CCastrop1012 | 0:19d20daef799 | 110 | case CMD_clear: leer_color(); |
CCastrop1012 | 0:19d20daef799 | 111 | break; |
CCastrop1012 | 0:19d20daef799 | 112 | default : |
CCastrop1012 | 0:19d20daef799 | 113 | break; |
CCastrop1012 | 0:19d20daef799 | 114 | |
CCastrop1012 | 0:19d20daef799 | 115 | |
CCastrop1012 | 0:19d20daef799 | 116 | |
CCastrop1012 | 0:19d20daef799 | 117 | }*/ |
CCastrop1012 | 0:19d20daef799 | 118 | |
CCastrop1012 | 0:19d20daef799 | 119 | |
CCastrop1012 | 0:19d20daef799 | 120 | |
CCastrop1012 | 0:19d20daef799 | 121 | } |
CCastrop1012 | 0:19d20daef799 | 122 | |
CCastrop1012 | 0:19d20daef799 | 123 | |
CCastrop1012 | 0:19d20daef799 | 124 | } |
CCastrop1012 | 0:19d20daef799 | 125 | |
CCastrop1012 | 0:19d20daef799 | 126 | |
CCastrop1012 | 0:19d20daef799 | 127 | void leer_color() |
CCastrop1012 | 0:19d20daef799 | 128 | { |
CCastrop1012 | 0:19d20daef799 | 129 | |
CCastrop1012 | 0:19d20daef799 | 130 | red = SENSOR_COLOR.ReadRed(); // OBTENEMOS EL TIEMPO DEL CICLO UTIL DE LA FRECUENCIA DE SALIDA |
CCastrop1012 | 0:19d20daef799 | 131 | green = SENSOR_COLOR.ReadGreen(); |
CCastrop1012 | 0:19d20daef799 | 132 | blue = SENSOR_COLOR.ReadBlue(); |
CCastrop1012 | 0:19d20daef799 | 133 | clear = SENSOR_COLOR.ReadClear(); |
CCastrop1012 | 0:19d20daef799 | 134 | |
CCastrop1012 | 0:19d20daef799 | 135 | //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); |
CCastrop1012 | 0:19d20daef799 | 136 | |
CCastrop1012 | 0:19d20daef799 | 137 | red *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:19d20daef799 | 138 | blue *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:19d20daef799 | 139 | green *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:19d20daef799 | 140 | clear *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:19d20daef799 | 141 | |
CCastrop1012 | 0:19d20daef799 | 142 | //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); |
CCastrop1012 | 0:19d20daef799 | 143 | |
CCastrop1012 | 0:19d20daef799 | 144 | F_red = (1 / ((float)red)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo rojo |
CCastrop1012 | 0:19d20daef799 | 145 | F_blue = (1 / ((float)blue)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo blue |
CCastrop1012 | 0:19d20daef799 | 146 | F_green = (1 / ((float)green)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo green |
CCastrop1012 | 0:19d20daef799 | 147 | F_clear = (1 / ((float)clear)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo clear |
CCastrop1012 | 0:19d20daef799 | 148 | |
CCastrop1012 | 0:19d20daef799 | 149 | |
CCastrop1012 | 0:19d20daef799 | 150 | |
CCastrop1012 | 0:19d20daef799 | 151 | //printf("RED: %5f GREEN: %5f BLUE: %5f CLEAR: %5f \n ", F_red, F_green, F_blue, F_clear); |
CCastrop1012 | 0:19d20daef799 | 152 | |
CCastrop1012 | 0:19d20daef799 | 153 | |
CCastrop1012 | 0:19d20daef799 | 154 | |
CCastrop1012 | 0:19d20daef799 | 155 | ////////////////////////////////////////////////////////////// |
CCastrop1012 | 0:19d20daef799 | 156 | //// identificar azul |
CCastrop1012 | 0:19d20daef799 | 157 | |
CCastrop1012 | 0:19d20daef799 | 158 | |
CCastrop1012 | 0:19d20daef799 | 159 | if(red <=42 && red >=24) |
CCastrop1012 | 0:19d20daef799 | 160 | { |
CCastrop1012 | 0:19d20daef799 | 161 | if(green >= 20 && green <= 28 ) |
CCastrop1012 | 0:19d20daef799 | 162 | { |
CCastrop1012 | 0:19d20daef799 | 163 | if(blue >= 10 && blue <= 16) |
CCastrop1012 | 0:19d20daef799 | 164 | { |
CCastrop1012 | 0:19d20daef799 | 165 | color_identificado = CMD_azul; |
CCastrop1012 | 0:19d20daef799 | 166 | printf ( "0x0%1x\n ", CMD_azul); |
CCastrop1012 | 0:19d20daef799 | 167 | Buzzer.period_ms(DO); |
CCastrop1012 | 0:19d20daef799 | 168 | Buzzer.write(0.5); |
CCastrop1012 | 0:19d20daef799 | 169 | wait(4); |
CCastrop1012 | 0:19d20daef799 | 170 | Buzzer.write(0); |
CCastrop1012 | 0:19d20daef799 | 171 | |
CCastrop1012 | 0:19d20daef799 | 172 | } |
CCastrop1012 | 0:19d20daef799 | 173 | } |
CCastrop1012 | 0:19d20daef799 | 174 | } |
CCastrop1012 | 0:19d20daef799 | 175 | |
CCastrop1012 | 0:19d20daef799 | 176 | |
CCastrop1012 | 0:19d20daef799 | 177 | |
CCastrop1012 | 0:19d20daef799 | 178 | |
CCastrop1012 | 0:19d20daef799 | 179 | ///////////////////////////////////////////////////////////// |
CCastrop1012 | 0:19d20daef799 | 180 | /// identificar rojo |
CCastrop1012 | 0:19d20daef799 | 181 | if(red <= 12 ) |
CCastrop1012 | 0:19d20daef799 | 182 | { |
CCastrop1012 | 0:19d20daef799 | 183 | if(green >= 10 && green <= 28 ) |
CCastrop1012 | 0:19d20daef799 | 184 | { |
CCastrop1012 | 0:19d20daef799 | 185 | if(blue >= 18 && blue <= 24) |
CCastrop1012 | 0:19d20daef799 | 186 | { |
CCastrop1012 | 0:19d20daef799 | 187 | color_identificado = CMD_rojo; |
CCastrop1012 | 0:19d20daef799 | 188 | printf ( "0x0%1x\n ", CMD_rojo ); |
CCastrop1012 | 0:19d20daef799 | 189 | Buzzer.period_ms(RE); |
CCastrop1012 | 0:19d20daef799 | 190 | Buzzer.write(0.5); //PERIODO UTIL |
CCastrop1012 | 0:19d20daef799 | 191 | wait(4); //TIEMPO ACTIVO DEL BUZZER |
CCastrop1012 | 0:19d20daef799 | 192 | Buzzer.write(0.0); |
CCastrop1012 | 0:19d20daef799 | 193 | } |
CCastrop1012 | 0:19d20daef799 | 194 | } |
CCastrop1012 | 0:19d20daef799 | 195 | |
CCastrop1012 | 0:19d20daef799 | 196 | if(green < 10 && green >= 6 ) |
CCastrop1012 | 0:19d20daef799 | 197 | { |
CCastrop1012 | 0:19d20daef799 | 198 | if(blue <= 12 ) |
CCastrop1012 | 0:19d20daef799 | 199 | { |
CCastrop1012 | 0:19d20daef799 | 200 | color_identificado = CMD_clear; |
CCastrop1012 | 0:19d20daef799 | 201 | printf ( "0x0%1x \n ", CMD_clear ); |
CCastrop1012 | 0:19d20daef799 | 202 | Buzzer.period_ms(MI); |
CCastrop1012 | 0:19d20daef799 | 203 | Buzzer.write(0.5); |
CCastrop1012 | 0:19d20daef799 | 204 | wait(4); |
CCastrop1012 | 0:19d20daef799 | 205 | Buzzer.write(0); |
CCastrop1012 | 0:19d20daef799 | 206 | } |
CCastrop1012 | 0:19d20daef799 | 207 | |
CCastrop1012 | 0:19d20daef799 | 208 | } |
CCastrop1012 | 0:19d20daef799 | 209 | |
CCastrop1012 | 0:19d20daef799 | 210 | } |
CCastrop1012 | 0:19d20daef799 | 211 | |
CCastrop1012 | 0:19d20daef799 | 212 | |
CCastrop1012 | 0:19d20daef799 | 213 | ////////////////////////////////////////////////////////////// |
CCastrop1012 | 0:19d20daef799 | 214 | //// identificar cafe |
CCastrop1012 | 0:19d20daef799 | 215 | |
CCastrop1012 | 0:19d20daef799 | 216 | |
CCastrop1012 | 0:19d20daef799 | 217 | // if(red >=14 && red <= 18 ) |
CCastrop1012 | 0:19d20daef799 | 218 | // printf ( "0x0 %1x \n ", CMD_verde ); |
CCastrop1012 | 0:19d20daef799 | 219 | |
CCastrop1012 | 0:19d20daef799 | 220 | |
CCastrop1012 | 0:19d20daef799 | 221 | ////////////////////////////////////////////////////////////// |
CCastrop1012 | 0:19d20daef799 | 222 | //// identificar verde |
CCastrop1012 | 0:19d20daef799 | 223 | |
CCastrop1012 | 0:19d20daef799 | 224 | if(green >= 36 && green <= 44 ) |
CCastrop1012 | 0:19d20daef799 | 225 | { |
CCastrop1012 | 0:19d20daef799 | 226 | if(red >= 40 && red <= 50 ) |
CCastrop1012 | 0:19d20daef799 | 227 | |
CCastrop1012 | 0:19d20daef799 | 228 | { |
CCastrop1012 | 0:19d20daef799 | 229 | color_identificado = CMD_verde; |
CCastrop1012 | 0:19d20daef799 | 230 | printf ( "0x0%1x \n ", CMD_verde ); |
CCastrop1012 | 0:19d20daef799 | 231 | Buzzer.period_ms(SI); |
CCastrop1012 | 0:19d20daef799 | 232 | Buzzer.write(0.5); |
CCastrop1012 | 0:19d20daef799 | 233 | wait(4); |
CCastrop1012 | 0:19d20daef799 | 234 | Buzzer.write(0); |
CCastrop1012 | 0:19d20daef799 | 235 | |
CCastrop1012 | 0:19d20daef799 | 236 | |
CCastrop1012 | 0:19d20daef799 | 237 | } |
CCastrop1012 | 0:19d20daef799 | 238 | } |
CCastrop1012 | 0:19d20daef799 | 239 | |
CCastrop1012 | 0:19d20daef799 | 240 | if (color_identificado == ColorNoIdentificado) |
CCastrop1012 | 0:19d20daef799 | 241 | { |
CCastrop1012 | 0:19d20daef799 | 242 | |
CCastrop1012 | 0:19d20daef799 | 243 | |
CCastrop1012 | 0:19d20daef799 | 244 | printf ( "0x0%1x \n ", ColorNoIdentificado); |
CCastrop1012 | 0:19d20daef799 | 245 | Buzzer.period_ms(10); |
CCastrop1012 | 0:19d20daef799 | 246 | Buzzer.write(0.5); |
CCastrop1012 | 0:19d20daef799 | 247 | wait(4); |
CCastrop1012 | 0:19d20daef799 | 248 | Buzzer.write(0); |
CCastrop1012 | 0:19d20daef799 | 249 | |
CCastrop1012 | 0:19d20daef799 | 250 | |
CCastrop1012 | 0:19d20daef799 | 251 | } |
CCastrop1012 | 0:19d20daef799 | 252 | |
CCastrop1012 | 0:19d20daef799 | 253 | color_identificado = ColorNoIdentificado; |
CCastrop1012 | 0:19d20daef799 | 254 | } |
CCastrop1012 | 0:19d20daef799 | 255 | |
CCastrop1012 | 0:19d20daef799 | 256 | |
CCastrop1012 | 0:19d20daef799 | 257 |