Se leen comando por el puerto serial realiza las siguientes funciones según el comando: - Genera distintos tonos por un buzzer. - Controla el movimiento de un carro (con 2 motores) con comandos - Controla el movimiento de un carro (con 2 motores) con Joystick. - Lee y envía el color leido por el puerto serial al PC
corregido.txt@0:3a37f6734913, 2021-09-03 (annotated)
- Committer:
- CCastrop1012
- Date:
- Fri Sep 03 05:22:19 2021 +0000
- Revision:
- 0:3a37f6734913
Programa finalizado y funcional.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CCastrop1012 | 0:3a37f6734913 | 1 | #include "mbed.h" |
CCastrop1012 | 0:3a37f6734913 | 2 | #include "Motor.h" |
CCastrop1012 | 0:3a37f6734913 | 3 | #include "scolor_TCS3200.h" |
CCastrop1012 | 0:3a37f6734913 | 4 | |
CCastrop1012 | 0:3a37f6734913 | 5 | // Puerto de comunicacion Serial |
CCastrop1012 | 0:3a37f6734913 | 6 | Serial CoolTerm(USBTX, USBRX); |
CCastrop1012 | 0:3a37f6734913 | 7 | |
CCastrop1012 | 0:3a37f6734913 | 8 | // Motores m1step |
CCastrop1012 | 0:3a37f6734913 | 9 | TraccionD Motores(PB_5, PB_3, PB_10, PB_4, 200, 3.75, 15.5) ; |
CCastrop1012 | 0:3a37f6734913 | 10 | |
CCastrop1012 | 0:3a37f6734913 | 11 | /// PWM OUTPUTS |
CCastrop1012 | 0:3a37f6734913 | 12 | PwmOut Buzzer(D10); // LED1 |
CCastrop1012 | 0:3a37f6734913 | 13 | |
CCastrop1012 | 0:3a37f6734913 | 14 | // Temporizadores |
CCastrop1012 | 0:3a37f6734913 | 15 | Ticker MuestrearCOLOR; |
CCastrop1012 | 0:3a37f6734913 | 16 | |
CCastrop1012 | 0:3a37f6734913 | 17 | // SENSOR DE COLOR |
CCastrop1012 | 0:3a37f6734913 | 18 | scolor_TCS3200 SENSOR_COLOR (PA_9, PC_7, PB_6, PA_7, PA_8); |
CCastrop1012 | 0:3a37f6734913 | 19 | |
CCastrop1012 | 0:3a37f6734913 | 20 | |
CCastrop1012 | 0:3a37f6734913 | 21 | // Lecturas Analogas de Joystick |
CCastrop1012 | 0:3a37f6734913 | 22 | AnalogIn JEjeX(A0); |
CCastrop1012 | 0:3a37f6734913 | 23 | AnalogIn JEjeY(A1); |
CCastrop1012 | 0:3a37f6734913 | 24 | |
CCastrop1012 | 0:3a37f6734913 | 25 | |
CCastrop1012 | 0:3a37f6734913 | 26 | // Salidas digitales |
CCastrop1012 | 0:3a37f6734913 | 27 | DigitalOut LED(PA_5); |
CCastrop1012 | 0:3a37f6734913 | 28 | |
CCastrop1012 | 0:3a37f6734913 | 29 | |
CCastrop1012 | 0:3a37f6734913 | 30 | //****************************************************************************** |
CCastrop1012 | 0:3a37f6734913 | 31 | /// Declarar Variables Globales |
CCastrop1012 | 0:3a37f6734913 | 32 | |
CCastrop1012 | 0:3a37f6734913 | 33 | |
CCastrop1012 | 0:3a37f6734913 | 34 | /// Variables sensor de color |
CCastrop1012 | 0:3a37f6734913 | 35 | long red; //Almacenan el Tiempo que dura el CicloUtil de la frecuencia |
CCastrop1012 | 0:3a37f6734913 | 36 | long blue; //Generada por el Sensor TSC3200, al leer el componente |
CCastrop1012 | 0:3a37f6734913 | 37 | long green; //R, G, B o Clear del color que tenga en Frente |
CCastrop1012 | 0:3a37f6734913 | 38 | long clear; |
CCastrop1012 | 0:3a37f6734913 | 39 | |
CCastrop1012 | 0:3a37f6734913 | 40 | |
CCastrop1012 | 0:3a37f6734913 | 41 | /// Constantes Sensor de Color |
CCastrop1012 | 0:3a37f6734913 | 42 | #define CMD_rojo 0x01 |
CCastrop1012 | 0:3a37f6734913 | 43 | #define CMD_azul 0x02 |
CCastrop1012 | 0:3a37f6734913 | 44 | #define CMD_verde 0x03 |
CCastrop1012 | 0:3a37f6734913 | 45 | #define CMD_clear 0x04 |
CCastrop1012 | 0:3a37f6734913 | 46 | #define ColorNoIdentificado 0x00 |
CCastrop1012 | 0:3a37f6734913 | 47 | |
CCastrop1012 | 0:3a37f6734913 | 48 | |
CCastrop1012 | 0:3a37f6734913 | 49 | uint8_t color_identificado = ColorNoIdentificado; |
CCastrop1012 | 0:3a37f6734913 | 50 | |
CCastrop1012 | 0:3a37f6734913 | 51 | |
CCastrop1012 | 0:3a37f6734913 | 52 | |
CCastrop1012 | 0:3a37f6734913 | 53 | /// Varialbles Buzzer |
CCastrop1012 | 0:3a37f6734913 | 54 | uint8_t duracion_tono = 1; // Variable que almacena el Tiempo que es ESCUCHARÁ el Tono |
CCastrop1012 | 0:3a37f6734913 | 55 | uint8_t tipo_tono = 1; // Variable que almacena el Tono que se desee escuchar |
CCastrop1012 | 0:3a37f6734913 | 56 | |
CCastrop1012 | 0:3a37f6734913 | 57 | /// Constantes Buzzer |
CCastrop1012 | 0:3a37f6734913 | 58 | #define TONO_DO 0x01 /// si tipo_tono == 0x01, se escuchará un DO |
CCastrop1012 | 0:3a37f6734913 | 59 | #define TONO_RE 0x02 /// si tipo_tono == 0x02, se escuchará un RE |
CCastrop1012 | 0:3a37f6734913 | 60 | #define TONO_MI 0x03 /// si tipo_tono == 0x03, se escuchará un MI |
CCastrop1012 | 0:3a37f6734913 | 61 | #define TONO_SI 0x04 /// si tipo_tono == 0x04, se escuchará un SI |
CCastrop1012 | 0:3a37f6734913 | 62 | |
CCastrop1012 | 0:3a37f6734913 | 63 | #define DO 3.78 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN DO |
CCastrop1012 | 0:3a37f6734913 | 64 | #define RE 3.36 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN RE |
CCastrop1012 | 0:3a37f6734913 | 65 | #define MI 3.03 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN MI |
CCastrop1012 | 0:3a37f6734913 | 66 | #define SI 2.82 /// Duración del periodo en ms, que se pondrá en el Buzzer.period_ms() PARA ESCUCHAR UN SI |
CCastrop1012 | 0:3a37f6734913 | 67 | |
CCastrop1012 | 0:3a37f6734913 | 68 | |
CCastrop1012 | 0:3a37f6734913 | 69 | |
CCastrop1012 | 0:3a37f6734913 | 70 | |
CCastrop1012 | 0:3a37f6734913 | 71 | #define limite 0.727 |
CCastrop1012 | 0:3a37f6734913 | 72 | |
CCastrop1012 | 0:3a37f6734913 | 73 | // variables de control y flujo de programa |
CCastrop1012 | 0:3a37f6734913 | 74 | |
CCastrop1012 | 0:3a37f6734913 | 75 | uint8_t programa_ejecutar = 0; // Variable que almacena la ORDEN (Telemetria ó Telecomando) |
CCastrop1012 | 0:3a37f6734913 | 76 | // enviada desde el CoolTerm |
CCastrop1012 | 0:3a37f6734913 | 77 | uint8_t coolterm_data; // Se almacenan Todos los Caracteres recividos por el CoolTerm. |
CCastrop1012 | 0:3a37f6734913 | 78 | |
CCastrop1012 | 0:3a37f6734913 | 79 | |
CCastrop1012 | 0:3a37f6734913 | 80 | // Constantes de velocidad |
CCastrop1012 | 0:3a37f6734913 | 81 | |
CCastrop1012 | 0:3a37f6734913 | 82 | #define VelAlta 300 |
CCastrop1012 | 0:3a37f6734913 | 83 | #define VelMedia 200 |
CCastrop1012 | 0:3a37f6734913 | 84 | #define VelBaja 100 |
CCastrop1012 | 0:3a37f6734913 | 85 | |
CCastrop1012 | 0:3a37f6734913 | 86 | |
CCastrop1012 | 0:3a37f6734913 | 87 | //****************************************************************************** |
CCastrop1012 | 0:3a37f6734913 | 88 | // COMANDOS |
CCastrop1012 | 0:3a37f6734913 | 89 | |
CCastrop1012 | 0:3a37f6734913 | 90 | #define iniciar_telemetria 0xFE |
CCastrop1012 | 0:3a37f6734913 | 91 | #define iniciar_telecomando 0xFF |
CCastrop1012 | 0:3a37f6734913 | 92 | |
CCastrop1012 | 0:3a37f6734913 | 93 | #define telemetria_1 0x01 // |
CCastrop1012 | 0:3a37f6734913 | 94 | #define telemcomando_1 0x01 |
CCastrop1012 | 0:3a37f6734913 | 95 | |
CCastrop1012 | 0:3a37f6734913 | 96 | #define C_LeerColor 0x00 |
CCastrop1012 | 0:3a37f6734913 | 97 | #define C_Sonido1 0x01 |
CCastrop1012 | 0:3a37f6734913 | 98 | #define C_Sonido2 0x02 |
CCastrop1012 | 0:3a37f6734913 | 99 | #define C_Sonido3 0x03 |
CCastrop1012 | 0:3a37f6734913 | 100 | #define C_Sonido4 0x04 |
CCastrop1012 | 0:3a37f6734913 | 101 | #define C_Adelante 0x05 |
CCastrop1012 | 0:3a37f6734913 | 102 | #define C_Atras 0x06 |
CCastrop1012 | 0:3a37f6734913 | 103 | #define C_Izquierda 0x07 |
CCastrop1012 | 0:3a37f6734913 | 104 | #define C_Derecha 0x08 |
CCastrop1012 | 0:3a37f6734913 | 105 | #define C_Velocidad 0x09 |
CCastrop1012 | 0:3a37f6734913 | 106 | #define C_Joistck 0x0A |
CCastrop1012 | 0:3a37f6734913 | 107 | int comando_joystick = 0; |
CCastrop1012 | 0:3a37f6734913 | 108 | |
CCastrop1012 | 0:3a37f6734913 | 109 | |
CCastrop1012 | 0:3a37f6734913 | 110 | // variables y constantes del Joystick |
CCastrop1012 | 0:3a37f6734913 | 111 | |
CCastrop1012 | 0:3a37f6734913 | 112 | uint8_t estado_x; |
CCastrop1012 | 0:3a37f6734913 | 113 | uint8_t estado_y; |
CCastrop1012 | 0:3a37f6734913 | 114 | #define E_Derecha 1 |
CCastrop1012 | 0:3a37f6734913 | 115 | #define E_Izquier 0 |
CCastrop1012 | 0:3a37f6734913 | 116 | #define E_Adelante 1 |
CCastrop1012 | 0:3a37f6734913 | 117 | #define E_Atras 0 |
CCastrop1012 | 0:3a37f6734913 | 118 | #define E_Centro 3 |
CCastrop1012 | 0:3a37f6734913 | 119 | #define Lim_JKIn 150 |
CCastrop1012 | 0:3a37f6734913 | 120 | #define Lim_JKSup 180 |
CCastrop1012 | 0:3a37f6734913 | 121 | |
CCastrop1012 | 0:3a37f6734913 | 122 | //**************************************************************************** |
CCastrop1012 | 0:3a37f6734913 | 123 | // Prototipo de funciones |
CCastrop1012 | 0:3a37f6734913 | 124 | |
CCastrop1012 | 0:3a37f6734913 | 125 | void ReadPort(void); // Lee el puerto Serial |
CCastrop1012 | 0:3a37f6734913 | 126 | void MainConfig(void); // Configuracion Inicial de los Perifericos del uC |
CCastrop1012 | 0:3a37f6734913 | 127 | void Buzzer_Tone(uint8_t tipo_tono, uint8_t duracion_tono); // configura el tono y la duracion escuchada a travez del Buzzer |
CCastrop1012 | 0:3a37f6734913 | 128 | void leer_color(void); // funcion que retorna los componentes |
CCastrop1012 | 0:3a37f6734913 | 129 | // RGB y Clear del color leido |
CCastrop1012 | 0:3a37f6734913 | 130 | void leer_Joystick(); // Ejerce control sobre el Movimiento del carro desde el Joistick |
CCastrop1012 | 0:3a37f6734913 | 131 | |
CCastrop1012 | 0:3a37f6734913 | 132 | //**************************************************************************** |
CCastrop1012 | 0:3a37f6734913 | 133 | // |
CCastrop1012 | 0:3a37f6734913 | 134 | #define valorInicial 0xAA |
CCastrop1012 | 0:3a37f6734913 | 135 | uint8_t n_interrupcion = 0; |
CCastrop1012 | 0:3a37f6734913 | 136 | uint8_t ComandoRecivido = valorInicial, Parametro = valorInicial; |
CCastrop1012 | 0:3a37f6734913 | 137 | |
CCastrop1012 | 0:3a37f6734913 | 138 | bool comm_pendi=false; |
CCastrop1012 | 0:3a37f6734913 | 139 | void ReadPort() |
CCastrop1012 | 0:3a37f6734913 | 140 | { |
CCastrop1012 | 0:3a37f6734913 | 141 | uint8_t tm=CoolTerm.getc(); |
CCastrop1012 | 0:3a37f6734913 | 142 | //if(CoolTerm.writable()) CoolTerm.abort_write(); // ELIMINA LO QUE ESTEMOS ESCRIBIENDO AL COOLTERM |
CCastrop1012 | 0:3a37f6734913 | 143 | if (comm_pendi==false){ |
CCastrop1012 | 0:3a37f6734913 | 144 | switch(n_interrupcion){ |
CCastrop1012 | 0:3a37f6734913 | 145 | case 00: |
CCastrop1012 | 0:3a37f6734913 | 146 | coolterm_data = tm; |
CCastrop1012 | 0:3a37f6734913 | 147 | if (coolterm_data == iniciar_telecomando) |
CCastrop1012 | 0:3a37f6734913 | 148 | n_interrupcion=1; |
CCastrop1012 | 0:3a37f6734913 | 149 | break; |
CCastrop1012 | 0:3a37f6734913 | 150 | case 01:ComandoRecivido = tm; n_interrupcion=2; break; |
CCastrop1012 | 0:3a37f6734913 | 151 | case 02: |
CCastrop1012 | 0:3a37f6734913 | 152 | Parametro = tm; |
CCastrop1012 | 0:3a37f6734913 | 153 | n_interrupcion = 0; |
CCastrop1012 | 0:3a37f6734913 | 154 | comm_pendi=true; |
CCastrop1012 | 0:3a37f6734913 | 155 | break; |
CCastrop1012 | 0:3a37f6734913 | 156 | } |
CCastrop1012 | 0:3a37f6734913 | 157 | |
CCastrop1012 | 0:3a37f6734913 | 158 | } |
CCastrop1012 | 0:3a37f6734913 | 159 | // printf("%d",tm); |
CCastrop1012 | 0:3a37f6734913 | 160 | } |
CCastrop1012 | 0:3a37f6734913 | 161 | |
CCastrop1012 | 0:3a37f6734913 | 162 | ///******************************************+ |
CCastrop1012 | 0:3a37f6734913 | 163 | |
CCastrop1012 | 0:3a37f6734913 | 164 | |
CCastrop1012 | 0:3a37f6734913 | 165 | void leer_Joystick() |
CCastrop1012 | 0:3a37f6734913 | 166 | { |
CCastrop1012 | 0:3a37f6734913 | 167 | |
CCastrop1012 | 0:3a37f6734913 | 168 | /// Variables Joystick |
CCastrop1012 | 0:3a37f6734913 | 169 | float EjeX; |
CCastrop1012 | 0:3a37f6734913 | 170 | float EjeY; |
CCastrop1012 | 0:3a37f6734913 | 171 | float Vx; |
CCastrop1012 | 0:3a37f6734913 | 172 | float Vy; |
CCastrop1012 | 0:3a37f6734913 | 173 | |
CCastrop1012 | 0:3a37f6734913 | 174 | while (comando_joystick == 1) // |
CCastrop1012 | 0:3a37f6734913 | 175 | { |
CCastrop1012 | 0:3a37f6734913 | 176 | |
CCastrop1012 | 0:3a37f6734913 | 177 | EjeX = JEjeX.read(); |
CCastrop1012 | 0:3a37f6734913 | 178 | Vx = EjeX * 3300; |
CCastrop1012 | 0:3a37f6734913 | 179 | wait (0.1); |
CCastrop1012 | 0:3a37f6734913 | 180 | EjeY = JEjeY.read(); |
CCastrop1012 | 0:3a37f6734913 | 181 | Vy = EjeY * 3300; |
CCastrop1012 | 0:3a37f6734913 | 182 | |
CCastrop1012 | 0:3a37f6734913 | 183 | // CoolTerm.printf ("ejex: %f ejey: %f Vx: %f Vy: %f \n ", EjeX, EjeY, Vx, Vy); |
CCastrop1012 | 0:3a37f6734913 | 184 | |
CCastrop1012 | 0:3a37f6734913 | 185 | if(int(Vx/10) > Lim_JKIn && int(Vx/10) < Lim_JKSup) {estado_x = E_Centro; }//(CoolTerm.printf ("Estado X Centro \n"); } |
CCastrop1012 | 0:3a37f6734913 | 186 | if(int(Vy/10) > Lim_JKIn && int(Vy/10) < Lim_JKSup) {estado_y = E_Centro; }//CoolTerm.printf ("Estado Y Centro \n"); } |
CCastrop1012 | 0:3a37f6734913 | 187 | |
CCastrop1012 | 0:3a37f6734913 | 188 | if(int(Vx/10) > Lim_JKSup && estado_y == E_Centro){ estado_x = E_Izquier; }// CoolTerm.printf ("Estado X Izquierda\n"); } |
CCastrop1012 | 0:3a37f6734913 | 189 | if(int(Vy/10) > Lim_JKSup && estado_x == E_Centro){ estado_y = E_Atras; }// CoolTerm.printf ("Estado Y Adelante\n"); } |
CCastrop1012 | 0:3a37f6734913 | 190 | |
CCastrop1012 | 0:3a37f6734913 | 191 | if(int(Vx/10) < Lim_JKIn && estado_y == E_Centro){ estado_x = E_Derecha; } //CoolTerm.printf ("Estado X Derecha\n"); } |
CCastrop1012 | 0:3a37f6734913 | 192 | if(int(Vy/10) < Lim_JKIn && estado_x == E_Centro){ estado_y = E_Adelante; } //CoolTerm.printf ("Estado Y Atras\n"); } |
CCastrop1012 | 0:3a37f6734913 | 193 | |
CCastrop1012 | 0:3a37f6734913 | 194 | |
CCastrop1012 | 0:3a37f6734913 | 195 | // CoolTerm.printf ("\n\n X = %d Y = %d \n",estado_x , estado_y); |
CCastrop1012 | 0:3a37f6734913 | 196 | // wait(2); |
CCastrop1012 | 0:3a37f6734913 | 197 | // Combinacion de estados para STOP |
CCastrop1012 | 0:3a37f6734913 | 198 | if( estado_x == E_Centro && estado_y == E_Centro){ Motores.Stop(); CoolTerm.printf ("MOTORES STOP\n"); } |
CCastrop1012 | 0:3a37f6734913 | 199 | |
CCastrop1012 | 0:3a37f6734913 | 200 | // Combinacion de estados para ADELANTE |
CCastrop1012 | 0:3a37f6734913 | 201 | if(estado_x == E_Centro && estado_y == E_Adelante) { Motores.Back(); Motores.Run(1);CoolTerm.printf ("MOTORES BACK\n"); } |
CCastrop1012 | 0:3a37f6734913 | 202 | |
CCastrop1012 | 0:3a37f6734913 | 203 | // Combinacion de estados para ATRAS |
CCastrop1012 | 0:3a37f6734913 | 204 | if(estado_x == E_Centro && estado_y == E_Atras) { Motores.Forward(); Motores.Run(1);CoolTerm.printf ("MOTORES FORWARD\n"); } |
CCastrop1012 | 0:3a37f6734913 | 205 | |
CCastrop1012 | 0:3a37f6734913 | 206 | |
CCastrop1012 | 0:3a37f6734913 | 207 | // Combinacion de estados para DERECHA |
CCastrop1012 | 0:3a37f6734913 | 208 | if(estado_y == E_Centro && estado_x == E_Derecha) { Motores.Giro(15, false); Motores.Run(1); CoolTerm.printf ("MOTORES DERECHA\n"); } |
CCastrop1012 | 0:3a37f6734913 | 209 | |
CCastrop1012 | 0:3a37f6734913 | 210 | // Combinacion de estados para IZQUIERDA |
CCastrop1012 | 0:3a37f6734913 | 211 | if(estado_y == E_Centro && estado_x == E_Izquier) { Motores.Giro(15, true); Motores.Run(1); CoolTerm.printf ("MOTORES IZQUIERDA\n"); } |
CCastrop1012 | 0:3a37f6734913 | 212 | //wait(1.5); |
CCastrop1012 | 0:3a37f6734913 | 213 | } |
CCastrop1012 | 0:3a37f6734913 | 214 | comando_joystick = 0; |
CCastrop1012 | 0:3a37f6734913 | 215 | |
CCastrop1012 | 0:3a37f6734913 | 216 | } |
CCastrop1012 | 0:3a37f6734913 | 217 | |
CCastrop1012 | 0:3a37f6734913 | 218 | |
CCastrop1012 | 0:3a37f6734913 | 219 | int main() { |
CCastrop1012 | 0:3a37f6734913 | 220 | |
CCastrop1012 | 0:3a37f6734913 | 221 | CoolTerm.printf("inicio"); |
CCastrop1012 | 0:3a37f6734913 | 222 | Buzzer.write(0); ///configura el ciclo util |
CCastrop1012 | 0:3a37f6734913 | 223 | Motores.StepFreq(VelMedia); |
CCastrop1012 | 0:3a37f6734913 | 224 | CoolTerm.attach(&ReadPort, Serial::RxIrq); //// se Habilita la interrupcion serial o recepcion de datos |
CCastrop1012 | 0:3a37f6734913 | 225 | MuestrearCOLOR.attach(&leer_color, 0.6); |
CCastrop1012 | 0:3a37f6734913 | 226 | |
CCastrop1012 | 0:3a37f6734913 | 227 | //CoolTerm.printf("Hello World, System Run !!\n"); // mensaje inicial |
CCastrop1012 | 0:3a37f6734913 | 228 | //leer_Joystick (); |
CCastrop1012 | 0:3a37f6734913 | 229 | |
CCastrop1012 | 0:3a37f6734913 | 230 | while(1) |
CCastrop1012 | 0:3a37f6734913 | 231 | { |
CCastrop1012 | 0:3a37f6734913 | 232 | |
CCastrop1012 | 0:3a37f6734913 | 233 | /// Espera hasta recivir OxFF |
CCastrop1012 | 0:3a37f6734913 | 234 | // while(programa_ejecutar != iniciar_telecomando); |
CCastrop1012 | 0:3a37f6734913 | 235 | |
CCastrop1012 | 0:3a37f6734913 | 236 | /// Espera hasta que reciva algo diferente de 0xAA |
CCastrop1012 | 0:3a37f6734913 | 237 | // while(ComandoRecivido == valorInicial); |
CCastrop1012 | 0:3a37f6734913 | 238 | |
CCastrop1012 | 0:3a37f6734913 | 239 | /// Espera hasta que reciva algo diferente de 0xAA |
CCastrop1012 | 0:3a37f6734913 | 240 | // while(Parametro == valorInicial); |
CCastrop1012 | 0:3a37f6734913 | 241 | |
CCastrop1012 | 0:3a37f6734913 | 242 | while(comm_pendi ==false); |
CCastrop1012 | 0:3a37f6734913 | 243 | |
CCastrop1012 | 0:3a37f6734913 | 244 | printf("okey comando :%d %d ", ComandoRecivido, Parametro); |
CCastrop1012 | 0:3a37f6734913 | 245 | |
CCastrop1012 | 0:3a37f6734913 | 246 | //// Desactivamos la interrupcion serial o recepcion de datos PORQUE NO NECESITAMOS recibir mas datos por AHORA |
CCastrop1012 | 0:3a37f6734913 | 247 | // CoolTerm.attach(NULL, Serial::RxIrq); |
CCastrop1012 | 0:3a37f6734913 | 248 | comm_pendi= false; |
CCastrop1012 | 0:3a37f6734913 | 249 | |
CCastrop1012 | 0:3a37f6734913 | 250 | switch(ComandoRecivido) |
CCastrop1012 | 0:3a37f6734913 | 251 | { |
CCastrop1012 | 0:3a37f6734913 | 252 | |
CCastrop1012 | 0:3a37f6734913 | 253 | //case C_LeerColor: // Ejecutamos la Funcion LeerColor(); |
CCastrop1012 | 0:3a37f6734913 | 254 | // leer_color(); |
CCastrop1012 | 0:3a37f6734913 | 255 | //break; |
CCastrop1012 | 0:3a37f6734913 | 256 | case C_Sonido1: //CoolTerm.printf("SONIDO 1\n"); |
CCastrop1012 | 0:3a37f6734913 | 257 | duracion_tono = Parametro; // lo almacenamos en: duracion_tono |
CCastrop1012 | 0:3a37f6734913 | 258 | tipo_tono = TONO_DO; |
CCastrop1012 | 0:3a37f6734913 | 259 | Buzzer_Tone(tipo_tono, duracion_tono); |
CCastrop1012 | 0:3a37f6734913 | 260 | break; |
CCastrop1012 | 0:3a37f6734913 | 261 | case C_Sonido2: //CoolTerm.printf("SONIDO 2\n"); |
CCastrop1012 | 0:3a37f6734913 | 262 | duracion_tono = Parametro; // lo almacenamos en: duracion_tono |
CCastrop1012 | 0:3a37f6734913 | 263 | tipo_tono = TONO_RE; |
CCastrop1012 | 0:3a37f6734913 | 264 | Buzzer_Tone(tipo_tono, duracion_tono); |
CCastrop1012 | 0:3a37f6734913 | 265 | break; |
CCastrop1012 | 0:3a37f6734913 | 266 | case C_Sonido3: //CoolTerm.printf("SONIDO 3\n"); |
CCastrop1012 | 0:3a37f6734913 | 267 | duracion_tono = Parametro; // lo almacenamos en: duracion_tono |
CCastrop1012 | 0:3a37f6734913 | 268 | tipo_tono = TONO_MI; |
CCastrop1012 | 0:3a37f6734913 | 269 | Buzzer_Tone(tipo_tono, duracion_tono); |
CCastrop1012 | 0:3a37f6734913 | 270 | break; |
CCastrop1012 | 0:3a37f6734913 | 271 | case C_Sonido4: //CoolTerm.printf("SONIDO 4\n"); |
CCastrop1012 | 0:3a37f6734913 | 272 | duracion_tono = Parametro; // lo almacenamos en: duracion_tono |
CCastrop1012 | 0:3a37f6734913 | 273 | tipo_tono = TONO_SI; |
CCastrop1012 | 0:3a37f6734913 | 274 | Buzzer_Tone(tipo_tono, duracion_tono); |
CCastrop1012 | 0:3a37f6734913 | 275 | break; |
CCastrop1012 | 0:3a37f6734913 | 276 | case C_Adelante: Motores.Forward(); Motores.RunRound(Parametro); |
CCastrop1012 | 0:3a37f6734913 | 277 | break; |
CCastrop1012 | 0:3a37f6734913 | 278 | case C_Atras: Motores.Back(); Motores.RunRound(Parametro); |
CCastrop1012 | 0:3a37f6734913 | 279 | break; |
CCastrop1012 | 0:3a37f6734913 | 280 | case C_Izquierda: Motores.Giro(65, true); |
CCastrop1012 | 0:3a37f6734913 | 281 | break; |
CCastrop1012 | 0:3a37f6734913 | 282 | case C_Derecha: Motores.Giro(65, false); |
CCastrop1012 | 0:3a37f6734913 | 283 | break; |
CCastrop1012 | 0:3a37f6734913 | 284 | case C_Velocidad: if(Parametro == 0x01)Motores.StepFreq(VelBaja); |
CCastrop1012 | 0:3a37f6734913 | 285 | if(Parametro == 0x02)Motores.StepFreq(VelMedia); |
CCastrop1012 | 0:3a37f6734913 | 286 | if(Parametro == 0x03)Motores.StepFreq(VelAlta); |
CCastrop1012 | 0:3a37f6734913 | 287 | break; |
CCastrop1012 | 0:3a37f6734913 | 288 | case C_Joistck: comando_joystick = 1; leer_Joystick(); |
CCastrop1012 | 0:3a37f6734913 | 289 | break; |
CCastrop1012 | 0:3a37f6734913 | 290 | default: break; |
CCastrop1012 | 0:3a37f6734913 | 291 | |
CCastrop1012 | 0:3a37f6734913 | 292 | } |
CCastrop1012 | 0:3a37f6734913 | 293 | comm_pendi= false; |
CCastrop1012 | 0:3a37f6734913 | 294 | //CoolTerm.printf("ProgramaFinalizado!!\n"); |
CCastrop1012 | 0:3a37f6734913 | 295 | |
CCastrop1012 | 0:3a37f6734913 | 296 | // Re inicializamos nuestras variables de control a sus valores iniciales |
CCastrop1012 | 0:3a37f6734913 | 297 | // Para no seguir entrando a las sentencias IF |
CCastrop1012 | 0:3a37f6734913 | 298 | // programa_ejecutar = 0; coolterm_data = 0; |
CCastrop1012 | 0:3a37f6734913 | 299 | // ComandoRecivido = valorInicial; Parametro = valorInicial; |
CCastrop1012 | 0:3a37f6734913 | 300 | //// HABILITAMOS NUEVAMENTE la interrupcion serial o recepcion de datos |
CCastrop1012 | 0:3a37f6734913 | 301 | // CoolTerm.attach(&ReadPort, Serial::RxIrq); |
CCastrop1012 | 0:3a37f6734913 | 302 | |
CCastrop1012 | 0:3a37f6734913 | 303 | } |
CCastrop1012 | 0:3a37f6734913 | 304 | |
CCastrop1012 | 0:3a37f6734913 | 305 | } |
CCastrop1012 | 0:3a37f6734913 | 306 | |
CCastrop1012 | 0:3a37f6734913 | 307 | |
CCastrop1012 | 0:3a37f6734913 | 308 | |
CCastrop1012 | 0:3a37f6734913 | 309 | void Buzzer_Tone(uint8_t tipo_tono, uint8_t duracion_tono) |
CCastrop1012 | 0:3a37f6734913 | 310 | { |
CCastrop1012 | 0:3a37f6734913 | 311 | |
CCastrop1012 | 0:3a37f6734913 | 312 | |
CCastrop1012 | 0:3a37f6734913 | 313 | switch (tipo_tono) |
CCastrop1012 | 0:3a37f6734913 | 314 | { |
CCastrop1012 | 0:3a37f6734913 | 315 | |
CCastrop1012 | 0:3a37f6734913 | 316 | case TONO_DO: Buzzer.period_ms(DO); |
CCastrop1012 | 0:3a37f6734913 | 317 | //CoolTerm.printf("Tono Seleccionado DO!!\n"); |
CCastrop1012 | 0:3a37f6734913 | 318 | |
CCastrop1012 | 0:3a37f6734913 | 319 | break; // salimos del SWITCH |
CCastrop1012 | 0:3a37f6734913 | 320 | |
CCastrop1012 | 0:3a37f6734913 | 321 | case TONO_RE: Buzzer.period_ms(RE); |
CCastrop1012 | 0:3a37f6734913 | 322 | //CoolTerm.printf("Tono Seleccionado RE!!\n"); |
CCastrop1012 | 0:3a37f6734913 | 323 | |
CCastrop1012 | 0:3a37f6734913 | 324 | break; // salimos del SWITCH |
CCastrop1012 | 0:3a37f6734913 | 325 | |
CCastrop1012 | 0:3a37f6734913 | 326 | case TONO_MI: Buzzer.period_ms(MI); |
CCastrop1012 | 0:3a37f6734913 | 327 | //CoolTerm.printf("Tono Seleccionado MI!!\n"); |
CCastrop1012 | 0:3a37f6734913 | 328 | |
CCastrop1012 | 0:3a37f6734913 | 329 | break; // salimos del SWITCH |
CCastrop1012 | 0:3a37f6734913 | 330 | |
CCastrop1012 | 0:3a37f6734913 | 331 | case TONO_SI: Buzzer.period_ms(SI); |
CCastrop1012 | 0:3a37f6734913 | 332 | //CoolTerm.printf("Tono Seleccionado SI!!\n"); |
CCastrop1012 | 0:3a37f6734913 | 333 | |
CCastrop1012 | 0:3a37f6734913 | 334 | break; // salimos del SWITCH |
CCastrop1012 | 0:3a37f6734913 | 335 | |
CCastrop1012 | 0:3a37f6734913 | 336 | // si no fue ninguno de los valores anteriores entonces: |
CCastrop1012 | 0:3a37f6734913 | 337 | default: //CoolTerm.printf("teleComando desconocido, inicie nuevamente !!\n"); |
CCastrop1012 | 0:3a37f6734913 | 338 | |
CCastrop1012 | 0:3a37f6734913 | 339 | break; // salimos del SWITCH |
CCastrop1012 | 0:3a37f6734913 | 340 | |
CCastrop1012 | 0:3a37f6734913 | 341 | } |
CCastrop1012 | 0:3a37f6734913 | 342 | // COMO EL CICLO UTIL DEL BUZZER ESTABA EN 0, POR LO CUAL NO SONABA |
CCastrop1012 | 0:3a37f6734913 | 343 | // SE PONE AL 50% DEL PERIODO |
CCastrop1012 | 0:3a37f6734913 | 344 | Buzzer.write(0.5); |
CCastrop1012 | 0:3a37f6734913 | 345 | // SE ESPERA DURANTE EN TIEMPO INGRESADO (EN SEGUNDOS ) |
CCastrop1012 | 0:3a37f6734913 | 346 | wait(duracion_tono); |
CCastrop1012 | 0:3a37f6734913 | 347 | |
CCastrop1012 | 0:3a37f6734913 | 348 | // Se Reinicializa el Periodo y el Ciclo útil de la señal PWM |
CCastrop1012 | 0:3a37f6734913 | 349 | // que va al Buzzer |
CCastrop1012 | 0:3a37f6734913 | 350 | Buzzer.period_ms(1); |
CCastrop1012 | 0:3a37f6734913 | 351 | Buzzer.write(0); |
CCastrop1012 | 0:3a37f6734913 | 352 | |
CCastrop1012 | 0:3a37f6734913 | 353 | |
CCastrop1012 | 0:3a37f6734913 | 354 | |
CCastrop1012 | 0:3a37f6734913 | 355 | } |
CCastrop1012 | 0:3a37f6734913 | 356 | |
CCastrop1012 | 0:3a37f6734913 | 357 | |
CCastrop1012 | 0:3a37f6734913 | 358 | |
CCastrop1012 | 0:3a37f6734913 | 359 | void leer_color() |
CCastrop1012 | 0:3a37f6734913 | 360 | { |
CCastrop1012 | 0:3a37f6734913 | 361 | |
CCastrop1012 | 0:3a37f6734913 | 362 | red = SENSOR_COLOR.ReadRed(); // OBTENEMOS EL TIEMPO DEL CICLO UTIL DE LA FRECUENCIA DE SALIDA |
CCastrop1012 | 0:3a37f6734913 | 363 | green = SENSOR_COLOR.ReadGreen(); |
CCastrop1012 | 0:3a37f6734913 | 364 | blue = SENSOR_COLOR.ReadBlue(); |
CCastrop1012 | 0:3a37f6734913 | 365 | clear = SENSOR_COLOR.ReadClear(); |
CCastrop1012 | 0:3a37f6734913 | 366 | |
CCastrop1012 | 0:3a37f6734913 | 367 | //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); |
CCastrop1012 | 0:3a37f6734913 | 368 | |
CCastrop1012 | 0:3a37f6734913 | 369 | red *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:3a37f6734913 | 370 | blue *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:3a37f6734913 | 371 | green *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:3a37f6734913 | 372 | clear *= 2; // Calculamos EL PERIODO de la frecuencia generada por la lectura del fotodiodo rojo |
CCastrop1012 | 0:3a37f6734913 | 373 | |
CCastrop1012 | 0:3a37f6734913 | 374 | //printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); |
CCastrop1012 | 0:3a37f6734913 | 375 | |
CCastrop1012 | 0:3a37f6734913 | 376 | |
CCastrop1012 | 0:3a37f6734913 | 377 | ////////////////////////////////////////////////////////////// |
CCastrop1012 | 0:3a37f6734913 | 378 | //// identificar azul |
CCastrop1012 | 0:3a37f6734913 | 379 | |
CCastrop1012 | 0:3a37f6734913 | 380 | |
CCastrop1012 | 0:3a37f6734913 | 381 | if(red <=42 && red >=24) |
CCastrop1012 | 0:3a37f6734913 | 382 | { |
CCastrop1012 | 0:3a37f6734913 | 383 | if(green >= 20 && green <= 28 ) |
CCastrop1012 | 0:3a37f6734913 | 384 | { |
CCastrop1012 | 0:3a37f6734913 | 385 | if(blue >= 10 && blue <= 16) |
CCastrop1012 | 0:3a37f6734913 | 386 | { |
CCastrop1012 | 0:3a37f6734913 | 387 | color_identificado = CMD_azul; |
CCastrop1012 | 0:3a37f6734913 | 388 | CoolTerm.putc( iniciar_telemetria); |
CCastrop1012 | 0:3a37f6734913 | 389 | CoolTerm.putc( CMD_azul ); |
CCastrop1012 | 0:3a37f6734913 | 390 | //Buzzer.period_ms(DO); |
CCastrop1012 | 0:3a37f6734913 | 391 | //Buzzer.write(0.5); |
CCastrop1012 | 0:3a37f6734913 | 392 | //wait(4); |
CCastrop1012 | 0:3a37f6734913 | 393 | //Buzzer.write(0); |
CCastrop1012 | 0:3a37f6734913 | 394 | |
CCastrop1012 | 0:3a37f6734913 | 395 | } |
CCastrop1012 | 0:3a37f6734913 | 396 | } |
CCastrop1012 | 0:3a37f6734913 | 397 | } |
CCastrop1012 | 0:3a37f6734913 | 398 | |
CCastrop1012 | 0:3a37f6734913 | 399 | |
CCastrop1012 | 0:3a37f6734913 | 400 | |
CCastrop1012 | 0:3a37f6734913 | 401 | |
CCastrop1012 | 0:3a37f6734913 | 402 | ///////////////////////////////////////////////////////////// |
CCastrop1012 | 0:3a37f6734913 | 403 | /// identificar rojo |
CCastrop1012 | 0:3a37f6734913 | 404 | if(red <= 12 ) |
CCastrop1012 | 0:3a37f6734913 | 405 | { |
CCastrop1012 | 0:3a37f6734913 | 406 | if(green >= 10 && green <= 28 ) |
CCastrop1012 | 0:3a37f6734913 | 407 | { |
CCastrop1012 | 0:3a37f6734913 | 408 | if(blue >= 18 && blue <= 24) |
CCastrop1012 | 0:3a37f6734913 | 409 | { |
CCastrop1012 | 0:3a37f6734913 | 410 | color_identificado = CMD_rojo; |
CCastrop1012 | 0:3a37f6734913 | 411 | CoolTerm.putc( iniciar_telemetria); |
CCastrop1012 | 0:3a37f6734913 | 412 | CoolTerm.putc( CMD_rojo ); |
CCastrop1012 | 0:3a37f6734913 | 413 | //Buzzer.period_ms(RE); |
CCastrop1012 | 0:3a37f6734913 | 414 | //Buzzer.write(0.5); //PERIODO UTIL |
CCastrop1012 | 0:3a37f6734913 | 415 | //wait(4); //TIEMPO ACTIVO DEL BUZZER |
CCastrop1012 | 0:3a37f6734913 | 416 | //Buzzer.write(0.0); |
CCastrop1012 | 0:3a37f6734913 | 417 | } |
CCastrop1012 | 0:3a37f6734913 | 418 | } |
CCastrop1012 | 0:3a37f6734913 | 419 | |
CCastrop1012 | 0:3a37f6734913 | 420 | if(green < 10 && green >= 6 ) |
CCastrop1012 | 0:3a37f6734913 | 421 | { |
CCastrop1012 | 0:3a37f6734913 | 422 | if(blue <= 12 ) |
CCastrop1012 | 0:3a37f6734913 | 423 | { |
CCastrop1012 | 0:3a37f6734913 | 424 | color_identificado = CMD_clear; |
CCastrop1012 | 0:3a37f6734913 | 425 | CoolTerm.putc( iniciar_telemetria); |
CCastrop1012 | 0:3a37f6734913 | 426 | CoolTerm.putc( CMD_clear ); |
CCastrop1012 | 0:3a37f6734913 | 427 | //Buzzer.period_ms(MI); |
CCastrop1012 | 0:3a37f6734913 | 428 | //Buzzer.write(0.5); |
CCastrop1012 | 0:3a37f6734913 | 429 | //wait(4); |
CCastrop1012 | 0:3a37f6734913 | 430 | //Buzzer.write(0); |
CCastrop1012 | 0:3a37f6734913 | 431 | } |
CCastrop1012 | 0:3a37f6734913 | 432 | |
CCastrop1012 | 0:3a37f6734913 | 433 | } |
CCastrop1012 | 0:3a37f6734913 | 434 | |
CCastrop1012 | 0:3a37f6734913 | 435 | } |
CCastrop1012 | 0:3a37f6734913 | 436 | |
CCastrop1012 | 0:3a37f6734913 | 437 | |
CCastrop1012 | 0:3a37f6734913 | 438 | if(green >= 36 && green <= 44 ) |
CCastrop1012 | 0:3a37f6734913 | 439 | { |
CCastrop1012 | 0:3a37f6734913 | 440 | if(red >= 40 && red <= 50 ) |
CCastrop1012 | 0:3a37f6734913 | 441 | |
CCastrop1012 | 0:3a37f6734913 | 442 | { |
CCastrop1012 | 0:3a37f6734913 | 443 | color_identificado = CMD_verde; |
CCastrop1012 | 0:3a37f6734913 | 444 | CoolTerm.putc( iniciar_telemetria); |
CCastrop1012 | 0:3a37f6734913 | 445 | CoolTerm.putc( CMD_verde ); |
CCastrop1012 | 0:3a37f6734913 | 446 | //Buzzer.period_ms(SI); |
CCastrop1012 | 0:3a37f6734913 | 447 | //Buzzer.write(0.5); |
CCastrop1012 | 0:3a37f6734913 | 448 | //wait(4); |
CCastrop1012 | 0:3a37f6734913 | 449 | //Buzzer.write(0); |
CCastrop1012 | 0:3a37f6734913 | 450 | |
CCastrop1012 | 0:3a37f6734913 | 451 | |
CCastrop1012 | 0:3a37f6734913 | 452 | } |
CCastrop1012 | 0:3a37f6734913 | 453 | } |
CCastrop1012 | 0:3a37f6734913 | 454 | |
CCastrop1012 | 0:3a37f6734913 | 455 | if (color_identificado == ColorNoIdentificado) |
CCastrop1012 | 0:3a37f6734913 | 456 | { |
CCastrop1012 | 0:3a37f6734913 | 457 | |
CCastrop1012 | 0:3a37f6734913 | 458 | |
CCastrop1012 | 0:3a37f6734913 | 459 | CoolTerm.putc( iniciar_telemetria); |
CCastrop1012 | 0:3a37f6734913 | 460 | CoolTerm.putc( ColorNoIdentificado); |
CCastrop1012 | 0:3a37f6734913 | 461 | //Buzzer.period_ms(10); |
CCastrop1012 | 0:3a37f6734913 | 462 | //Buzzer.write(0.5); |
CCastrop1012 | 0:3a37f6734913 | 463 | //wait(4); |
CCastrop1012 | 0:3a37f6734913 | 464 | //Buzzer.write(0); |
CCastrop1012 | 0:3a37f6734913 | 465 | |
CCastrop1012 | 0:3a37f6734913 | 466 | |
CCastrop1012 | 0:3a37f6734913 | 467 | } |
CCastrop1012 | 0:3a37f6734913 | 468 | |
CCastrop1012 | 0:3a37f6734913 | 469 | color_identificado = ColorNoIdentificado; |
CCastrop1012 | 0:3a37f6734913 | 470 | } |
CCastrop1012 | 0:3a37f6734913 | 471 | |
CCastrop1012 | 0:3a37f6734913 | 472 |