Dependencies:   mbed TextLCD

Revision:
2:3eb5c816d496
Parent:
1:e8be78899409
Child:
3:8f569cc10b95
--- a/Referenciamneto.cpp	Thu May 20 18:59:02 2021 +0000
+++ b/Referenciamneto.cpp	Fri May 21 01:03:03 2021 +0000
@@ -6,7 +6,7 @@
 AnalogIn eixo_Z(PC_2); //Joystick eixo Z
 AnalogIn botao_SELECT(A0); //Botão SELECT da IHM
 InterruptIn posicao_salva(PA_15); //Botão de salva posição Joystick
-InterruptIn botao_emergencia(PA_14); //Botão de emergência
+InterruptIn botao_emergencia_in(PC_4); //Botão de emergência para detectar acionamento
 InterruptIn botao_fim_curso(PC_12); //Chave de fim de curso
 DigitalIn botao_indicador_fonte_externa(PC_10); //Indica se os componentes dependentes da fonte externa estão energizados
 
@@ -20,20 +20,27 @@
 int pulsos_horario, pulsos_anti_horario;
 int i;
 int J_Z;
-float deslocamento_horario_Z, deslocamento_anti_horario_Z, deslocamento_total_Z;
-float velocidade_Z;
-float passo_motor = 18;//5.625/32;
+float deslocamento_horario_Z = 0;
+float deslocamento_anti_horario_Z = 0;
+float deslocamento_Z = 0;
+float deslocamento_max_Z = 0;
+
+float passo_motor = 22.5;//5.625/32;
 int passo_fuso = 5;
 int pegaZ;
 int posicao_salva_estado;
 int fonte_externa;
 int SELECT;
 int contador_SELECT = 0;
+int contador_emergencia = 0;
 bool valor_SELECT = false;
 bool estado_referenciamento = false;
-bool estodo_botao_emergencia = false;
+bool estado_botao_emergencia_in = false;
+bool motor_referenciamento = false;
+
+
 void rotina_posicao_salva(void);
-void rotina_emergencia(void);
+void rotina_emergencia_in(void);
 void rotina_velocidade_eixo_Z(void);
 void rotina_deslocamento_eixo_Z (void);
 void rotina_JOG_Z (void);
@@ -48,7 +55,7 @@
     pc.baud(115200);
     motor_z = 0x00;
     posicao_salva.rise(&rotina_posicao_salva);
-    botao_emergencia.fall(&rotina_emergencia);
+    botao_emergencia_in.rise(&rotina_emergencia_in);
     botao_fim_curso.rise(&rotina_referenciamento);
     wait_ms(500);
     lcd.locate(2,0);
@@ -62,14 +69,15 @@
         fonte_externa = botao_indicador_fonte_externa.read();
         SELECT = botao_SELECT.read_u16();
 
-//        pc.printf("PosicaoZ=%4f,Posicao_Salva=%d\r\n", deslocamento_total_Z, pegaZ);
-//        pc.printf("Z=%4d, PegaZ=%4d, Posicao_salva_estado=%d, Botao_emergencia_estado=%d, Tempo1=%4f, Tempo2=%4f, Pulsos=%d, Deslocamento_Z=%f, Velocidade_Z=%f\r\n", J_Z, pegaZ, posicao_salva_estado, botao_emergencia_estado, tempo_horario, tempo_anti_horario, pulsos_anti_horario, deslocamento_total_Z, velocidade_Z);
-        pc.printf("estado_referenciamento=%d\r\n", estado_referenciamento);
-        if(fonte_externa == 0 & estado_referenciamento == true & estodo_botao_emergencia == false) {
+//        pc.printf("PosicaoZ=%4f,Posicao_Salva=%d\r\n", deslocamento_Z, pegaZ);
+//        pc.printf("Z=%4d, PegaZ=%4d, Posicao_salva_estado=%d, Botao_emergencia_estado=%d, Tempo1=%4f, Tempo2=%4f, Pulsos=%d, Deslocamento_Z=%f, Velocidade_Z=%f\r\n", J_Z, pegaZ, posicao_salva_estado, botao_emergencia_estado, tempo_horario, tempo_anti_horario, pulsos_anti_horario, deslocamento_Z, velocidade_Z);
+        pc.printf("estado_referenciamento=%d, estado_botao_emergencia_in=%d, contador_emergencia=%d\r\n", estado_referenciamento, estado_botao_emergencia_in, contador_emergencia);
+        if(fonte_externa == 0 & estado_referenciamento == true & estado_botao_emergencia_in == true) {
+
             lcd.locate(2,0);
             lcd.printf("PosicaoZ:");
             lcd.locate(0,1);
-            lcd.printf("%4fmm",deslocamento_total_Z);
+            lcd.printf("%4fmm",deslocamento_Z);
 //            pc.printf("Fonte=%d\r\n", fonte_externa);
             rotina_JOG_Z();
 
@@ -82,7 +90,16 @@
                 lcd.printf("Iniciando");
                 lcd.locate(0,1);
                 lcd.printf("referenciamento");
-                //rotina_referenciamento();
+                while(motor_referenciamento == false){
+                for(i = 0; i < 4; i++) {
+                    motor_z = 1 << i;
+                    //rotina_velocidade_eixo_Z();
+                    //rotina_deslocamento_eixo_Z();
+                    wait(0.003);
+                    //pulsos_horario+=1;
+                    }
+
+                }
                 pc.printf("estado_referenciamento=%d\r\n", estado_referenciamento);
             }
         }
@@ -93,37 +110,47 @@
 void rotina_posicao_salva()
 {
     debounce1.start();
-    if(debounce1.read_ms() >= 10) pegaZ = deslocamento_total_Z;
+    if(debounce1.read_ms() >= 10) pegaZ = deslocamento_Z;
     else pegaZ = 0;
     debounce1.reset();
 }
 
-void rotina_emergencia()
+void rotina_emergencia_in()
 {
     debounce2.start();
-    if(debounce2.read_ms() >= 10) {
-        
+    if(debounce2.read_ms() >= 120) {
+
         motor_z = 0x00;
         estado_referenciamento = false;
-        estodo_botao_emergencia = true;
+        estado_botao_emergencia_in =! estado_botao_emergencia_in;
+        contador_emergencia+=1;
 
-        
-        pc.printf("Rotina de emergencia \r\n");
     }
     debounce2.reset();
+    if(contador_emergencia > 1) {
+        lcd.cls();
+        wait_ms(500);
+        lcd.locate(0,0);
+        lcd.printf("Emergencia ON");
+        lcd.locate(0,1);
+        lcd.printf("Pressione RST");
+        exit(0);
+    }
 }
 
+
+
 void rotina_velocidade_eixo_Z()
 {
-    tempo_horario = -1*J_Z*0.0000028 +0.1875;
-    tempo_anti_horario = J_Z*0.000002785 +0.0025;
+    tempo_horario = -1*J_Z*0.0000028 + 0.1875;
+    tempo_anti_horario = J_Z*0.000002785 + 0.0025;
 }
 
 void rotina_deslocamento_eixo_Z()
 {
     deslocamento_horario_Z = (passo_fuso*pulsos_horario*passo_motor)/360;
     deslocamento_anti_horario_Z = (passo_fuso*pulsos_anti_horario*passo_motor)/360;
-    deslocamento_total_Z = deslocamento_anti_horario_Z - deslocamento_horario_Z;
+    deslocamento_Z = deslocamento_anti_horario_Z - deslocamento_horario_Z;
 }
 
 void rotina_botoes_IHM()
@@ -136,8 +163,10 @@
 {
 
     debounce3.start();
-    if(debounce3.read_ms() >= 10 & contador_SELECT == 1) {
+    if(debounce3.read_ms() >= 10 & contador_SELECT == 1 & estado_botao_emergencia_in == true) {
         estado_referenciamento =!  estado_referenciamento;
+        motor_referenciamento =! motor_referenciamento;
+        deslocamento_max_Z = 100;
         lcd.cls();
         wait_ms(50);
         lcd.locate(2,0);
@@ -169,7 +198,7 @@
         motor_z = 0x00;
 
     } else {
-        if (J_Z > 31000) {
+        if (J_Z > 31000 & deslocamento_Z > 0) {
             for(i = 0; i < 4; i++) {
                 motor_z = 1 << i;
                 rotina_velocidade_eixo_Z();
@@ -179,8 +208,9 @@
 
             }
         }
+        else motor_z = 0x00;
 
-        if(J_Z < 35000) {
+        if(J_Z < 35000 & deslocamento_Z < deslocamento_max_Z) {
             for(i = 3; i > -1; i--) {
                 motor_z = 1 << i;
                 rotina_velocidade_eixo_Z();
@@ -190,6 +220,7 @@
 
             }
         }
+        else motor_z = 0x00;
     }