Lectura del sensor TCS3200, para la identificación de colores.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "scolor_TCS3200.h" 00003 00004 00005 //****************************************************************************** 00006 // Definiciones 00007 00008 /// Puerto Serial 00009 Serial SerialPort(SERIAL_TX, SERIAL_RX, 9600); 00010 00011 /// PWM OUTPUTS 00012 PwmOut Buzzer(LED1); 00013 00014 // Temporizadores 00015 Ticker LengthTonoBuzzer; 00016 00017 // SENSOR DE COLOR 00018 scolor_TCS3200 SENSOR_COLOR (PA_8, PB_10, PB_4, PB_5, PB_3); 00019 00020 00021 // constantes 00022 //const int long_vector = 10; // longitud del vector 00023 00024 /// Global Variables 00025 00026 00027 00028 long red; 00029 long blue; 00030 long green; 00031 long clear; 00032 00033 float F_red; 00034 float F_blue; 00035 float F_green; 00036 float F_clear; 00037 00038 uint8_t coolterm_comand, i = 0; // Variable usada como Indice de los vectores, 00039 // indica la posicion del vector en donde se reciben 00040 // ó se almacenan los datos 00041 00042 float periodo_Buzzer = 1; 00043 uint8_t duracion_Tono = 1; 00044 00045 #define DO 3.78 /// VALOR DEL PERIODO EN MS 00046 #define RE 3.36 /// VALOR DEL PERIODO EN MS 00047 #define MI 3.03 /// VALOR DEL PERIODO EN MS 00048 #define SI 2.02 /// VALOR DEL PERIODO EN MS 00049 00050 00051 00052 //****************************************************************************** 00053 // COMANDOS 00054 00055 #define iniciar_telemetria 0xFE 00056 #define CMD_rojo 0x01 00057 #define CMD_azul 0x02 00058 #define CMD_verde 0x03 00059 #define CMD_clear 0x04 00060 #define ColorNoIdentificado 0x05 00061 00062 uint8_t color_identificado = ColorNoIdentificado; 00063 00064 //**************************************************************************** 00065 // prototipo de funciones 00066 00067 void ReadPort(void); // Lee el puerto Serial 00068 void MainConfig(void); // Configuracion Inicial de los Perifericos del uC 00069 void leer_color(void); // funcion que retorna los componentes 00070 // RGB y Clear del color leido 00071 //**************************************************************************** 00072 // 00073 00074 void ReadPort() 00075 { 00076 00077 coolterm_comand = SerialPort.getc(); 00078 00079 } 00080 00081 00082 int main() 00083 { 00084 Buzzer.write(0); 00085 SerialPort.attach(&ReadPort, Serial::RxIrq); 00086 SerialPort.printf("Hello World, System Run !!\n"); 00087 00088 while(1) 00089 { 00090 00091 while (coolterm_comand != iniciar_telemetria); 00092 SerialPort.printf("COMANDO RECIBIDO!!\n"); 00093 00094 leer_color(); 00095 coolterm_comand = 0; 00096 00097 00098 /* 00099 switch(coolterm_comand) 00100 { 00101 case CMD_rojo: leer_color(); 00102 break; 00103 00104 case CMD_azul: leer_color(); 00105 break; 00106 00107 case CMD_verde: leer_color(); 00108 break; 00109 00110 case CMD_clear: leer_color(); 00111 break; 00112 default : 00113 break; 00114 00115 00116 00117 }*/ 00118 00119 00120 00121 } 00122 00123 00124 } 00125 00126 00127 void leer_color() 00128 { 00129 00130 red = SENSOR_COLOR.ReadRed(); // OBTENEMOS EL TIEMPO DEL CICLO UTIL DE LA FRECUENCIA DE SALIDA 00131 green = SENSOR_COLOR.ReadGreen(); 00132 blue = SENSOR_COLOR.ReadBlue(); 00133 clear = SENSOR_COLOR.ReadClear(); 00134 00135 //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); 00136 00137 red *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo 00138 blue *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo 00139 green *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo 00140 clear *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo 00141 00142 //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); 00143 00144 F_red = (1 / ((float)red)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo rojo 00145 F_blue = (1 / ((float)blue)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo blue 00146 F_green = (1 / ((float)green)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo green 00147 F_clear = (1 / ((float)clear)) * (1000); /// calculando la FRECUENCIA de salida generada por el fotodiodo clear 00148 00149 00150 00151 //printf("RED: %5f GREEN: %5f BLUE: %5f CLEAR: %5f \n ", F_red, F_green, F_blue, F_clear); 00152 00153 00154 00155 ////////////////////////////////////////////////////////////// 00156 //// identificar azul 00157 00158 00159 if(red <=42 && red >=24) 00160 { 00161 if(green >= 20 && green <= 28 ) 00162 { 00163 if(blue >= 10 && blue <= 16) 00164 { 00165 color_identificado = CMD_azul; 00166 printf ( "0x0%1x\n ", CMD_azul); 00167 Buzzer.period_ms(DO); 00168 Buzzer.write(0.5); 00169 wait(4); 00170 Buzzer.write(0); 00171 00172 } 00173 } 00174 } 00175 00176 00177 00178 00179 ///////////////////////////////////////////////////////////// 00180 /// identificar rojo 00181 if(red <= 12 ) 00182 { 00183 if(green >= 10 && green <= 28 ) 00184 { 00185 if(blue >= 18 && blue <= 24) 00186 { 00187 color_identificado = CMD_rojo; 00188 printf ( "0x0%1x\n ", CMD_rojo ); 00189 Buzzer.period_ms(RE); 00190 Buzzer.write(0.5); //PERIODO UTIL 00191 wait(4); //TIEMPO ACTIVO DEL BUZZER 00192 Buzzer.write(0.0); 00193 } 00194 } 00195 00196 if(green < 10 && green >= 6 ) 00197 { 00198 if(blue <= 12 ) 00199 { 00200 color_identificado = CMD_clear; 00201 printf ( "0x0%1x \n ", CMD_clear ); 00202 Buzzer.period_ms(MI); 00203 Buzzer.write(0.5); 00204 wait(4); 00205 Buzzer.write(0); 00206 } 00207 00208 } 00209 00210 } 00211 00212 00213 ////////////////////////////////////////////////////////////// 00214 //// identificar cafe 00215 00216 00217 // if(red >=14 && red <= 18 ) 00218 // printf ( "0x0 %1x \n ", CMD_verde ); 00219 00220 00221 ////////////////////////////////////////////////////////////// 00222 //// identificar verde 00223 00224 if(green >= 36 && green <= 44 ) 00225 { 00226 if(red >= 40 && red <= 50 ) 00227 00228 { 00229 color_identificado = CMD_verde; 00230 printf ( "0x0%1x \n ", CMD_verde ); 00231 Buzzer.period_ms(SI); 00232 Buzzer.write(0.5); 00233 wait(4); 00234 Buzzer.write(0); 00235 00236 00237 } 00238 } 00239 00240 if (color_identificado == ColorNoIdentificado) 00241 { 00242 00243 00244 printf ( "0x0%1x \n ", ColorNoIdentificado); 00245 Buzzer.period_ms(10); 00246 Buzzer.write(0.5); 00247 wait(4); 00248 Buzzer.write(0); 00249 00250 00251 } 00252 00253 color_identificado = ColorNoIdentificado; 00254 } 00255 00256 00257
Generated on Mon Aug 29 2022 03:11:49 by
1.7.2