Projeto Mecatrônico
/
ProjetoMecatronico
testando
main_PM.cpp
- Committer:
- enricoan
- Date:
- 2020-05-28
- Revision:
- 43:cfc3da0299ff
- Parent:
- 42:a6fbbe536814
- Child:
- 44:f0b7a2fe28d9
File content as of revision 43:cfc3da0299ff:
//local para inclusao de bibliotecas #include "mbed.h" #include "TextLCD.h" #include "Keypad.h" #include "funcoes_ihm.h" #include "classe.h" #include <stdio.h> #include <string> //local para a declaracao de varioveis float t_motor = 0.002; //delay entre o acionamento de uma bobina do motor e a outra float t_teclado = 0.01; //delay entre a leitura de um botão do teclado, necessário para que um click nao gere mais de uma saída int hor[] = {12,6,3,9}; //valores que, ao serem convertidos em binário, geram a sequência de acionamento do motor no sentido horário enviando a sequência de 0's e 1's às bobinas int antihor[] = {3,6,12,9}; //valores que, ao serem convertidos em binário, geram a sequência de acionamento do motor no sentido anti-horário enviando a sequência de 0's e 1's às bobinas int parado[] = {0,0,0,0}; //valores enviados às bobinas do motor para deixá-lo parado char tecla; //variável que guarda a tecla apertada no teclado float coord_x = 0; //variável que guardará a posição linear do eixo x, ela é incrementada com a conta do deslocamento linear dado um deslocamento angular sempre que o eixo é movimentado depois do referencimaneto float coord_y = 0; //variável que guardará a posição linear do eixo y, ela é incrementada com a conta do deslocamento linear dado um deslocamento angular sempre que o eixo é movimentado depois do referencimaneto float coord_z = 0; //variável que guardará a posição linear do eixo z, ela é incrementada com a conta do deslocamento linear dado um deslocamento angular sempre que o eixo é movimentado depois do referencimaneto int est_x = 0; //variável que indicará o estado do eixo x, pemitindo ou não que ele possa ser avançado depois de atingir o fim de curso int est_y = 0; //variável que indicará o estado do eixo y, pemitindo ou não que ele possa ser avançado depois de atingir o fim de curso int est_z = 0; //variável que indicará o estado do eixo z, pemitindo ou não que ele possa ser avançado depois de atingir o fim de curso //pinos usados pelos botoes de fim de curso DigitalIn fdc_x(PC_10); DigitalIn fdc_y(PC_11); DigitalIn fdc_z(PC_12); //Iterrupcao botao de emergência InterruptIn interrupcao(PC_14); //pinos do rele, saida digital para acionar o rele se necessario, dada uma entrada no pino da emergencia DigitalOut rele1(PC_5); DigitalOut rele2(PB_9); //botoes para movimentacao eixo a eixo DigitalIn x_mais(PA_0); DigitalIn x_menos(PA_1); DigitalIn y_mais(PC_0); DigitalIn y_menos(PC_1); DigitalIn z_mais(PC_2); DigitalIn z_menos(PC_3); //pinos da placa usados na biblioteca do LCD //TextLCD lcd(D4, D5, D6, D7, D8, D9); //pinos do teclado DigitalIn b(PC_4); DigitalIn a(PB_0); DigitalIn t_0(PC_13); DigitalIn t_1(PB_7); DigitalIn t_2(PD_2); DigitalIn t_3(PA_4); DigitalIn t_4(PB_12); DigitalIn t_5(PB_15); DigitalIn t_6(PA_7); DigitalIn t_7(PB_14); DigitalIn t_8(PA_6); DigitalIn t_9(PB_13); //pinos de saída para os motores //eixo x BusOut eixox(PA_15,PA_14,PA_13,PA_12); //eixo y BusOut eixoy(PA_11,PA_10,PB_2,PB_1); //eixo z BusOut eixoz(PC_9,PC_8,PB_8,PC_6); //funcao para receber inputs do teclado char pega_tecla(){ if (b == 0)return ('A'); if (a == 0)return('B'); if (t_0 == 0)return('0'); if (t_1 == 0)return('1'); if (t_2 == 0)return('2'); if (t_3 == 0)return('3'); if (t_4 == 0)return('4'); if (t_5 == 0)return('5'); if (t_6 == 0)return('6'); if (t_7 == 0)return('7'); if (t_8 == 0)return('8'); if (t_9 == 0)return('9'); return('nada');} //funcao de movimentacao dos motores void mov_x_menos(){for(int i = 0; i < 4; i++){eixox = antihor[i];wait(t_motor);coord_x -= (5.625*5/32)/360;est_x = 0;}} void mov_y_menos(){for(int i = 0; i < 4; i++){eixoy = antihor[i];wait(t_motor);coord_y -= (5.625*5/32)/360;est_y = 0;}} void mov_z_menos(){for(int i = 0; i < 4; i++){eixoz = antihor[i];wait(t_motor);coord_z -= (5.625*5/32)/360;est_z = 0;}} void mov_x_mais() {for(int i = 0; i < 4; i++){eixox = hor[i];wait(t_motor);coord_x += (5.625*5/32)/360;est_x = 0;}} void mov_y_mais() {for(int i = 0; i < 4; i++){eixoy = hor[i];wait(t_motor);coord_y += (5.625*5/32)/360;est_y = 0;}} void mov_z_mais() {for(int i = 0; i < 4; i++){eixoz = hor[i];wait(t_motor);coord_z += (5.625*5/32)/360;est_z = 0;}} //Funcao de interrupcao void emergencia(){TextLCD lcd(D4, D5, D6, D7, D8, D9); while(interrupcao == 0){ rele1 = 0; eixox = 0b0000; eixoy = 0b0000; eixoz = 0b0000; lcd.cls(); lcd.printf("Botao Emergencia"); lcd.locate(0,1); lcd.printf(" acionado "); lcd.locate(0,2); lcd.printf("Operacao pausada");}} //Funcao de zeramento void zeramento(){ while (fdc_x != 0){for(int i = 0; i < 4; i++){eixox = hor[i];wait(t_motor);est_x = 1;}} while (fdc_y != 0){for(int i = 0; i < 4; i++){eixoy = hor[i];wait(t_motor);est_y = 1;}} while (fdc_z != 0){for(int i = 0; i < 4; i++){eixoz = hor[i];wait(t_motor);est_z = 1;}} /* printf("\n%i",est_x); printf("\n%i",est_y); printf("\n%i",est_z);*/} //Varioveis para operacao IHM using std::string; string status_tela = "zeramento"; int programa = 0; int main() { TextLCD lcd(D4, D5, D6, D7, D8, D9); printf("\n%i",est_x); printf("\n%i",est_y); printf("\n%i",est_z); lcd.printf("Bem-vindo ao Pipemax"); lcd.locate(0,1); lcd.printf("o seu sistema"); lcd.locate(0,2); lcd.printf("automatizado"); lcd.locate(0,3); lcd.printf("de pipetagem"); wait(3); lcd.cls(); lcd.printf("Por favor, aceite"); lcd.locate(0,1); lcd.printf("o zeramento do seu"); lcd.locate(0,2); lcd.printf("sistema"); lcd.locate(0,3); lcd.printf("A - proximo"); interrupcao.fall(&emergencia); while(1) { tecla = pega_tecla(); if(tecla == 'A' && status_tela == "zeramento"){ zeramento(); status_tela = "inicio"; funcao_inicio();} /* tentativa de fazer o motor parar ao apertar algum dos fins de curso if(fdc_x == 0 || fdc_y == 0 || fdc_z == 0){ for(int i = 0; i < 4; i++){eixox = parado[i];wait(t_motor);est_x = 1;} for(int i = 0; i < 4; i++){eixoy = parado[i];wait(t_motor);est_y = 1;} for(int i = 0; i < 4; i++){eixoz = parado[i];wait(t_motor);est_z = 1;} */ if (x_mais == 0 && est_x == 1){for(int i = 0; i < 4; i++){eixox = parado[i];wait(t_motor);coord_x += 0;lcd.cls();lcd.printf("Limite de x atingido");}} if (y_mais == 0 && est_y == 1){for(int i = 0; i < 4; i++){eixoy = parado[i];wait(t_motor);coord_y += 0;lcd.cls();lcd.printf("Limite de y atingido");}} if (z_mais == 0 && est_z == 1){for(int i = 0; i < 4; i++){eixoz = parado[i];wait(t_motor);coord_z += 0;lcd.cls();lcd.printf("Limite de z atingido");}} while(x_menos == 0){mov_x_menos(); lcd.cls();lcd.printf("X: %4.1f", coord_x); lcd.locate(0,1);lcd.printf("Y: %4.1f", coord_y); lcd.locate(0,2);lcd.printf("Z: %4.1f", coord_z);} while(y_menos == 0){mov_y_menos(); lcd.cls();lcd.printf("X: %4.1f", coord_x); lcd.locate(0,1);lcd.printf("Y: %4.1f", coord_y); lcd.locate(0,2);lcd.printf("Z: %4.1f", coord_z);} while(z_menos == 0){mov_z_menos(); lcd.cls();lcd.printf("X: %4.1f", coord_x); lcd.locate(0,1);lcd.printf("Y: %4.1f", coord_y); lcd.locate(0,2);lcd.printf("Z: %4.1f", coord_z);} while(x_mais == 0){mov_x_mais(); lcd.cls();lcd.printf("X: %4.1f", coord_x); lcd.locate(0,1);lcd.printf("Y: %4.1f", coord_y); lcd.locate(0,2);lcd.printf("Z: %4.1f", coord_z);} while(y_mais == 0){mov_y_mais(); lcd.cls();lcd.printf("X: %4.1f", coord_x); lcd.locate(0,1);lcd.printf("Y: %4.1f", coord_y); lcd.locate(0,2);lcd.printf("Z: %4.1f", coord_z);} while(z_mais == 0){mov_z_mais(); lcd.cls();lcd.printf("X: %4.1f", coord_x); lcd.locate(0,1);lcd.printf("Y: %4.1f", coord_y); lcd.locate(0,2);lcd.printf("Z: %4.1f", coord_z);} if(x_mais && x_menos && y_mais && y_menos && z_mais && z_menos != 0){ for(int i = 0; i < 4; i++){eixox = parado[i];} for(int i = 0; i < 4; i++){eixoy = parado[i];} for(int i = 0; i < 4; i++){eixoz = parado[i];}} tecla = pega_tecla(); if(tecla =='1' && status_tela == "inicio"){ funcao_pega1(); wait(1); funcao_pega2(); status_tela = "novo";} else if(tecla == '2' && status_tela == "inicio"){ funcao_salvo1(); wait(1); funcao_salvo2(); status_tela = "salvo";} tecla = pega_tecla(); if(tecla =='1' && status_tela =="salvo"){ funcao_salvo_agree1(); wait(1); funcao_salvo_agree2(); programa = 1; status_tela = "salvo_confirmacao";} else if(tecla =='2' && status_tela =="salvo"){ funcao_salvo_agree1(); wait(1); funcao_salvo_agree2(); programa = 2; status_tela = "salvo_confirmacao";} else if(tecla =='3' && status_tela =="salvo"){ funcao_salvo_agree1(); wait(1); funcao_salvo_agree2(); programa = 3; status_tela = "salvo_confirmacao";} else if(tecla =='4' && status_tela =="salvo"){ funcao_salvo_agree1(); wait(1); funcao_salvo_agree2(); programa = 4; status_tela = "salvo_confirmacao";} else if(tecla =='5' && status_tela =="salvo"){ funcao_salvo_agree1(); wait(1); funcao_salvo_agree2(); programa = 5; status_tela = "salvo_confirmacao";} else if(tecla =='B' && status_tela =="salvo"){ funcao_salvo1(); wait(1); funcao_salvo2(); status_tela = "inicio";} tecla = pega_tecla(); if(tecla =='1' && status_tela =="salvo_confirmacao"){ funcao_salvo_agree_pos_aviso(); //posicionar no local da pega salvo; (motor -> programa.pos_x_pega e motor -> programa.pos_y_pega) funcao_salvo_agree_pos_certo(); status_tela="salvo_pos_certo" ;} else if(tecla =='2' && status_tela =="salvo_confirmacao"){ funcao_salvo_agree_solta_aviso(); //posicionar no local de solta para confirmação; (motor -> programa.pos_x_solta e motor -> programa.pos_y_solta) funcao_salvo_agree_pos_certo(); status_tela = "salvo_pos_certo";} else if(tecla =='3' && status_tela =="salvo_confirmacao"){ funcao_salvo_agree_pega_vol(); //programação de volume igual da de cima (programa.vol_pega) status_tela="salvo_confirmacao";} else if(tecla =='4' && status_tela =="salvo_confirmacao"){ funcao_salvo_agree_solta_vol(); //volumes configurados por cada pipeta. (programa.vol_solta_pipeta) status_tela="salvo_conf_vol_solta";} else if(tecla =='B' && status_tela =="salvo_confirmacao"){ funcao_salvo_agree1(); wait(1); funcao_salvo_agree2(); programa = 1; status_tela = "salvo";} tecla = pega_tecla(); if(tecla =='1' && status_tela =="salvo_pos_certo"){}//salva essa posição como correta else if(tecla =='2' && status_tela =="salvo_pos_certo"){} else if(tecla =='B' && status_tela =="salvo_pos_certo"){ funcao_salvo_agree1(); wait(1); funcao_salvo_agree2(); status_tela = "salvo_confirmacao";} tecla = pega_tecla(); if(tecla =='1' && status_tela =="novo"){ funcao_pega_forma(); status_tela ="forma";} else if(tecla =='2' && status_tela == "novo"){ funcao_pega_volume(); status_tela = "volume"; bool continuar = 1; int volume = 0; int multiplicador = 1; while(continuar){ tecla = pega_tecla(); if(tecla!= 'A' && tecla!='B' && tecla!='C' && tecla!='D' && tecla!='#' && tecla != '*'){ volume = multiplicador*volume + (int)tecla; multiplicador*=10; } else if(tecla == 'A'){ lcd.printf("Volume: %d", volume); continuar = 0; } } } else if(tecla == '3' && status_tela == "novo"){ funcao_pega_pos_x(); status_tela = "pos_x";} else if(tecla == 'B' && status_tela == "novo"){ funcao_inicio(); status_tela = "inicio";} else if(tecla == '1' && status_tela == "salvo"){ funcao_salvo_agree1(); status_tela = "confirmacao";} else if(tecla =='2' && status_tela == "salvo"){ funcao_salvo_agree1(); status_tela = "confirmacao";} else if(tecla =='B' && status_tela == "salvo"){ funcao_inicio(); status_tela = "inicio";} tecla = pega_tecla(); if(tecla == '1' && status_tela == "forma"){ funcao_pega_forma_quad(); status_tela ="forma_quad";} else if(tecla == '2' && status_tela == "forma"){ funcao_pega_forma_red(); status_tela = "forma_quad";} else if(tecla == 'B' && status_tela == "forma"){ funcao_pega_forma(); status_tela = "novo";} tecla = pega_tecla(); if(tecla == 'A' && status_tela == "forma_quad"){ funcao_pega1(); wait(1); funcao_pega2(); status_tela ="inicio";} else if(tecla == 'B' && status_tela == "forma_quad"){ funcao_pega_forma(); status_tela = "forma";} else if(tecla == 'A' && status_tela == "forma_red"){ funcao_pega1(); wait(1); funcao_pega2(); status_tela ="inicio";} else if(tecla == 'B' && status_tela == "forma_red"){ funcao_pega_forma(); status_tela = "forma";} else if(tecla == 'A' && status_tela == "volume"){ funcao_pega1(); wait(1); funcao_pega2(); status_tela ="inicio";} else if( tecla == 'B' && status_tela == "volume"){ funcao_pega_forma(); status_tela = "forma";} else if(tecla == 'A' && status_tela == "pos_x"){ funcao_pega_pos_y(); //colocar programacao que contem o posicionamento do eixo y status_tela = "pos_y";} else if(tecla == 'B' && status_tela == "pos_x"){ funcao_pega_forma(); status_tela = "forma";} tecla = pega_tecla(); if(tecla == 'A' && status_tela == "pos_y"){ funcao_pega1(); wait(1); funcao_pega2(); status_tela ="inicio";} else if(tecla == 'B' && status_tela == "pos_y"){ funcao_pega_forma(); status_tela = "forma";} } }