//////////////////////////////////// // Constantes // //////////////////////////////////// #define NUM_OPCOES 5 #define FAZ_QUAD 4 #define FAZ_CIRC 3 #define CALIBRACAO_RETA 2 #define CALIBRACAO_QUAD 1 #define CALIBRACAO_CIRC 0 #define WIDTH 15.5 //////////////////////////////////// // Variaveis Globais (calibracao) // //////////////////////////////////// // Motor 1 persistent int motor1 = 70; // Motor 2 persistent int motor2 = 68; // Motor 1 circulo persistent int motor1circ = 100; // Motor 2 circulo persistent int motor2circ = 32; // Aresta 30 cm persistent float tempo_aresta = 2.70; // Canto 90 graus persistent float tempo_canto = 1.24; // Circulo 30 cm raio persistent float tempo_circ = 17.5; //////////////////////////////////// // Funcoes // //////////////////////////////////// /**********************************/ // Desenha o quadrado // /**********************************/ void quadrado(){ int lado, lado_old; int i; float tempo; while(1) { lado_old = -1; do { lado = knob()*50/255; if (lado != lado_old) { printf ("LADO: Escolha o Lado %d\n", lado); lado_old = lado; } } while(!start_button() && !stop_button()); // Volta para o menu inicial if (stop_button()) { while(stop_button()); return; } while(start_button()); printf("Desenhando quadrado de lado %d\n", lado); tempo = ((float)lado*tempo_aresta)/30.0; for (i = 0; i < 12 && !stop_button(); ++i) { motor(1, motor1); motor(2, motor2); sleep(tempo); alloff(); sleep(0.5); motor(1, motor1); motor(2, -motor2); sleep(tempo_canto); alloff(); sleep(0.5); } while(stop_button()); } } /**********************************/ // Desenha o circulo // /**********************************/ void circulo(){ int raio, raio_old; int i; int pot1, pot2; float k, tempo; while(1) { raio_old = -1; do { raio = knob()*50/255; if (raio != raio_old) { printf ("RAIO: Escolha o Raio %d\n", raio); raio_old = raio; } } while(!start_button() && !stop_button()); // Volta para o menu inicial if (stop_button()) { while(stop_button()); return; } while(start_button()); printf("Desenhando circ de raio %d\n", raio); // Nao implementado pot1 = motor1circ; pot2 = motor2circ; tempo = 3.0*tempo_circ; motor(1, pot1); motor(2, pot2); sleep(tempo); alloff(); } } /**********************************/ // Calibracao da reta // /**********************************/ void calibracao_reta() { int pot1, pot_old1; int pot2, pot_old2; float tempo, tempo_old; int voltar; while(1) { voltar = 0; pot_old1 = 200; pot_old2 = 200; tempo_old = -1.0; printf("Calibrando reta de 30 cm\n"); // Volta para o menu inicial while(!start_button() && !stop_button()); if (stop_button()){ while(stop_button()); return; } while(start_button()); do { pot1 = ((knob() - 127)*100)/127; if (pot_old1 != pot1) { printf("Potencia motor 1 = %d\n", pot1); pot_old1 = pot1; } } while(!start_button() && !stop_button()); if (stop_button()) voltar = 1; while(start_button() && !voltar); do { pot2 = ((knob() - 127)*100)/127; if (pot_old2 != pot2) { printf("Potencia motor 2 = %d\n", pot2); pot_old2 = pot2; } } while((!start_button() && !stop_button()) && !voltar); if (stop_button()) voltar = 1; while(start_button() && !voltar); do { tempo = ((float)knob()*7.)/255.; if (tempo_old != tempo) { printf("Tempo = %f\n", tempo); tempo_old = tempo; } } while((!start_button() && !stop_button()) && !voltar); if (stop_button()) voltar = 1; while(start_button() && !voltar); while(!voltar) { printf("Pressione START ou STOP\n"); while(!start_button() && !stop_button()); if (stop_button()){ while(stop_button()); break; } while(start_button()); printf("p1=%d p2=%d t = %f\n", pot1, pot2, tempo); // Atualiza as variaveis de calibracao motor1 = pot1; motor2 = pot2; tempo_aresta = tempo; motor(1, pot1); motor(2, pot2); sleep(tempo); alloff(); } } } /**********************************/ // Calibracao do quadrado // /**********************************/ void calibracao_quadrado() { float tempo1, tempo_old1; float tempo2, tempo_old2; int voltar, i; while(1) { voltar = 0; tempo_old1 = -1.0; tempo_old2 = -1.0; printf("Calibrando tempos quadrado.\n"); while(!start_button() && !stop_button()); // Volta para o menu inicial if (stop_button()) { while(stop_button()); return; } while(start_button()); do { tempo1 = ((float)knob()*15.)/255.; if (tempo_old1 != tempo1) { printf("Tempo aresta = %f\n", tempo1); tempo_old1 = tempo1; } } while((!start_button() && !stop_button()) && !voltar); if (stop_button()) voltar = 1; while(start_button() && !voltar); do { tempo2 = ((float)knob()*7.)/255.; if (tempo_old2 != tempo2) { printf("Tempo canto = %f\n", tempo2); tempo_old2 = tempo2; } } while((!start_button() && !stop_button()) && !voltar); if (stop_button()) voltar = 1; while(start_button() && !voltar); while(!voltar) { printf("Pressione START ou STOP\n"); while(!start_button() && !stop_button()); if (stop_button()) break; while(start_button()); printf("aresta=%f canto=%f\n", tempo1, tempo2); // Atualiza as variaveis de calibracao tempo_aresta = tempo1; tempo_canto = tempo2; for (i = 0; i < 4 && !stop_button(); ++i) { motor(1, motor1); motor(2, motor2); sleep(tempo1); alloff(); sleep(0.3); motor(1, -motor1); sleep(0.2); off(1); motor(1, -motor1); motor(2, motor2); sleep(tempo2); alloff(); sleep(0.3); } if (stop_button()) { while(stop_button()); voltar = 1; } } } } /**********************************/ // Calibracao do circulo // /**********************************/ void calibracao_circulo() { int pot1, pot_old1; int pot2, pot_old2; float tempo, tempo_old; int voltar; while(1) { voltar = 0; pot_old1 = 200; pot_old2 = 200; tempo_old = -1.0; printf("Calibrando circ de raio 30 cm\n"); // Volta para o menu inicial while(!start_button() && !stop_button()); if (stop_button()){ while(stop_button()); return; } while(start_button()); do { pot1 = ((knob() - 127)*100)/127; if (pot_old1 != pot1) { printf("Potencia motor 1 = %d\n", pot1); pot_old1 = pot1; } } while(!start_button() && !stop_button()); if (stop_button()) voltar = 1; while(start_button() && !voltar); do { pot2 = ((knob() - 127)*100)/127; if (pot_old2 != pot2) { printf("Potencia motor 2 = %d\n", pot2); pot_old2 = pot2; } } while((!start_button() && !stop_button()) && !voltar); if (stop_button()) voltar = 1; while(start_button() && !voltar); do { tempo = ((float)knob()*40.)/255.; if (tempo_old != tempo) { printf("Tempo = %f\n", tempo); tempo_old = tempo; } } while((!start_button() && !stop_button()) && !voltar); if (stop_button()) voltar = 1; while(start_button() && !voltar); while(!voltar) { printf("Pressione START ou STOP\n"); while(!start_button() && !stop_button()); if (stop_button()){ while(stop_button()); break; } while(start_button()); printf("p1=%d p2=%d t = %f\n", pot1, pot2, tempo); // Atualiza as variaveis de calibracao motor1circ = pot1; motor2circ = pot2; tempo_circ = tempo; motor(1, pot1); motor(2, pot2); sleep(tempo); alloff(); } } } /**********************************/ // Programa Principal // /**********************************/ int main(){ int modo, modo_old; while(1) { modo_old = -1; printf ("T-800 TP1 Aperte START \n"); while(!start_button()); while(start_button()); do { modo = knob()*(NUM_OPCOES - 1)/255; if (modo != modo_old) { modo_old = modo; switch(modo){ case FAZ_QUAD: { printf("Faz quadrado Aperte START\n"); } break; case FAZ_CIRC: { printf("Faz circulo Aperte START\n"); } break; case CALIBRACAO_RETA: { printf("Calibracao reta Aperte START\n"); } break; case CALIBRACAO_QUAD: { printf("Calibracao quad Aperte START\n"); } break; case CALIBRACAO_CIRC: { printf("Calibracao circ Aperte START\n"); } break; } } } while(!start_button()); while(start_button()); switch(modo){ case FAZ_QUAD: { quadrado(); } break; case FAZ_CIRC: { circulo(); } break; case CALIBRACAO_RETA: { calibracao_reta(); } break; case CALIBRACAO_QUAD: { calibracao_quadrado(); } break; case CALIBRACAO_CIRC: { calibracao_circulo(); } break; } } }