Projeto Mecatrônico - 2020 - G3 / Mbed 2 deprecated Pipetagem

Dependencies:   mbed Pipetagem_automatica

Revision:
1:3dbb9b66b990
Child:
2:bce78210ffb1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/joystick.h	Mon Jun 29 13:41:37 2020 +0000
@@ -0,0 +1,289 @@
+#include "mbed.h"
+
+AnalogIn XAxis(PA_0);
+AnalogIn YAxis(PA_1);
+InterruptIn PARA_CARAI(PC_9);
+InterruptIn butao(PC_13);
+DigitalIn ZAxis_UP(PA_8);
+DigitalIn ZAxis_DOWN(PB_10);
+DigitalOut Fase_M1_A(PB_12);
+DigitalOut Fase_M1_B(PB_15);
+DigitalOut Fase_M1_C(PB_14);
+DigitalOut Fase_M1_D(PB_13);
+DigitalOut Fase_M2_A(PC_12);
+DigitalOut Fase_M2_B(PA_13);
+DigitalOut Fase_M2_C(PA_14);
+DigitalOut Fase_M2_D(PA_15);
+DigitalOut Fase_M3_A(PA_12);
+DigitalOut Fase_M3_B(PC_5);
+DigitalOut Fase_M3_C(PC_6);
+DigitalOut Fase_M3_D(PC_8);
+DigitalOut Emergencia(PB_9);
+Serial pc(USBTX, USBRX);
+Timer debounce;
+
+float tempo = 0.01;
+
+void Botao_Emergencia(void);    
+void PAROU(void);
+
+int ZUP = ZAxis_UP;
+int ZDOWN = ZAxis_DOWN;
+int limite1 = 0;
+int limite2 = 0;
+int limite3 = 0;
+int limite4 = 0;
+int limite5 = 0;
+int limite6 = 0;
+int PosicaoUsuario[] = {0,0,0};
+int estado = 1;
+
+void SubindoZ() {
+    if (debounce.read_ms()>200) {
+        Fase_M3_D = 1;
+        Fase_M3_C = 0;
+        Fase_M3_B = 0;
+        Fase_M3_A = 0;
+        wait(tempo);
+        Fase_M3_D = 0;
+        Fase_M3_C = 1;
+        Fase_M3_B = 0;
+        Fase_M3_A = 0;
+        wait(tempo);
+        Fase_M3_D = 0;
+        Fase_M3_C = 0;
+        Fase_M3_B = 1;
+        Fase_M3_A = 0;
+        wait(tempo);
+        Fase_M3_D = 0;
+        Fase_M3_C = 0;
+        Fase_M3_B = 0;
+        Fase_M3_A = 1;
+        wait(tempo);
+        limite1 = limite1 + 1;
+        if (limite1 == 50) {
+            PosicaoUsuario[2] = PosicaoUsuario[2] - 5;
+            }
+        }
+    }
+
+void DescendoZ() {
+    if (debounce.read_ms()>200) {
+        Fase_M3_A = 1;
+        Fase_M3_B = 0;
+        Fase_M3_C = 0;
+        Fase_M3_D = 0;
+        wait(tempo);
+        Fase_M3_A = 0;
+        Fase_M3_B = 1;
+        Fase_M3_C = 0;
+        Fase_M3_D = 0;
+        wait(tempo);
+        Fase_M3_A = 0;
+        Fase_M3_B = 0;
+        Fase_M3_C = 1;
+        Fase_M3_D = 0;
+        wait(tempo);
+        Fase_M3_A = 0;
+        Fase_M3_B = 0;
+        Fase_M3_C = 0;
+        Fase_M3_D = 1;
+        wait(tempo);
+        limite2 = limite2 + 1;
+        if (limite2 == 50) {
+            PosicaoUsuario[2] = PosicaoUsuario[2] + 5;
+            }
+        }
+    }   
+
+void joystick(){
+
+    int X,Y;
+    Emergencia = 0;
+    Fase_M1_A = 0;
+    Fase_M1_B = 0;
+    Fase_M1_C = 0;
+    Fase_M1_D = 0;
+    Fase_M2_A = 0;
+    Fase_M2_B = 0;
+    Fase_M2_C = 0;
+    Fase_M2_D = 0;
+    Fase_M3_A = 0;
+    Fase_M3_B = 0;
+    Fase_M3_C = 0;
+    Fase_M3_D = 0;
+    
+    debounce.start();
+    butao.rise(&Botao_Emergencia);
+    PARA_CARAI.rise(&PAROU);
+    
+    while (estado == 1) {
+        
+        X = XAxis.read() * 1000;
+        Y = YAxis.read() * 1000;
+        float tempo = 0.01;
+                        
+        if (ZAxis_UP != 0 && ZAxis_DOWN == 0) { // Quando o botão UP estiver pressionado e o botão DOWN não - Suba a pipeta - inverte o sentido de rotação do motor
+            SubindoZ();
+            }
+        
+        else if (ZAxis_DOWN != 0 && ZAxis_UP == 0 ) { // Quando o botão DOWN estiver pressionado e o botão UP não  - Desce a pipeta - sentido normal de rotação do motor
+            DescendoZ();
+            }
+        
+        if (100<X<900) { //delimitando range de atuação do joystick
+            Fase_M1_A = 0;
+            Fase_M1_B = 0;
+            Fase_M1_C = 0;
+            Fase_M1_D = 0;
+            Fase_M2_A = 0;
+            Fase_M2_B = 0;
+            Fase_M2_C = 0;
+            Fase_M2_D = 0;
+            Fase_M3_A = 0;
+            Fase_M3_B = 0;
+            Fase_M3_C = 0;
+            Fase_M3_D = 0;
+            }
+        
+        if (100<Y<900) { //delimitando range de atuação do joystick
+            Fase_M1_A = 0;
+            Fase_M1_B = 0;
+            Fase_M1_C = 0;
+            Fase_M1_D = 0;
+            Fase_M2_A = 0;
+            Fase_M2_B = 0;
+            Fase_M2_C = 0;
+            Fase_M2_D = 0;
+            Fase_M3_A = 0;
+            Fase_M3_B = 0;
+            Fase_M3_C = 0;
+            Fase_M3_D = 0;
+            }  
+    
+        if (X<100) { //seta esquerda - Inverte o sentido de rotação
+            
+            Fase_M1_D = 1;
+            Fase_M1_C = 0;
+            Fase_M1_B = 0;
+            Fase_M1_A = 0;
+            wait(tempo);
+            Fase_M1_D = 0;
+            Fase_M1_C = 1;
+            Fase_M1_B = 0;
+            Fase_M1_A = 0;
+            wait(tempo);
+            Fase_M1_D = 0;
+            Fase_M1_C = 0;
+            Fase_M1_B = 1;
+            Fase_M1_A = 0;
+            wait(tempo);
+            Fase_M1_D = 0;
+            Fase_M1_C = 0;
+            Fase_M1_B = 0;
+            Fase_M1_A = 1;
+            wait(tempo);
+            limite3 = limite3 + 1;
+            if (limite3 == 50) {
+                PosicaoUsuario[0] = PosicaoUsuario[0] - 5;
+                }
+            }
+        
+        else if (X>900) { // seta direita - sentido normal de rotação
+            
+            Fase_M1_A = 1;
+            Fase_M1_B = 0;
+            Fase_M1_C = 0;
+            Fase_M1_D = 0;
+            wait(tempo);
+            Fase_M1_A = 0;
+            Fase_M1_B = 1;
+            Fase_M1_C = 0;
+            Fase_M1_D = 0;
+            wait(tempo);
+            Fase_M1_A = 0;
+            Fase_M1_B = 0;
+            Fase_M1_C = 1;
+            Fase_M1_D = 0;
+            wait(tempo);
+            Fase_M1_A = 0;
+            Fase_M1_B = 0;
+            Fase_M1_C = 0;
+            Fase_M1_D = 1;
+            wait(tempo);
+            limite4 = limite4 + 1;
+            if (limite4 == 50) {
+                PosicaoUsuario[0] = PosicaoUsuario[0] + 5;
+                }
+            } 
+        
+        else if (Y<100) { //seta pra baixo - Inverte o sentido de rotação
+            
+            Fase_M2_D = 1;
+            Fase_M2_C = 0;
+            Fase_M2_B = 0;
+            Fase_M2_A = 0;
+            wait(tempo);
+            Fase_M2_D = 0;
+            Fase_M2_C = 1;
+            Fase_M2_B = 0;
+            Fase_M2_A = 0;
+            wait(tempo);
+            Fase_M2_D = 0;
+            Fase_M2_C = 0;
+            Fase_M2_B = 1;
+            Fase_M2_A = 0;
+            wait(tempo);
+            Fase_M2_D = 0;
+            Fase_M2_C = 0;
+            Fase_M2_B = 0;
+            Fase_M2_A = 1;
+            wait(tempo);
+            limite5 = limite5 + 1;
+            if (limite5 == 50) {
+                PosicaoUsuario[1] = PosicaoUsuario[1] - 5;
+                }
+            }
+        
+        else if (Y>900) { //seta pra cima - Sentido normal de rotação
+            
+            Fase_M2_A = 1;
+            Fase_M2_B = 0;
+            Fase_M2_C = 0;
+            Fase_M2_D = 0;
+            wait(tempo);
+            Fase_M2_A = 0;
+            Fase_M2_B = 1;
+            Fase_M2_C = 0;
+            Fase_M2_D = 0;
+            wait(tempo);
+            Fase_M2_A = 0;
+            Fase_M2_B = 0;
+            Fase_M2_C = 1;
+            Fase_M2_D = 0;
+            wait(tempo);
+            Fase_M2_A = 0;
+            Fase_M2_B = 0;
+            Fase_M2_C = 0;
+            Fase_M2_D = 1;
+            wait(tempo);
+            limite6 = limite6 + 1;
+            if (limite6 == 50) {
+                PosicaoUsuario[1] = PosicaoUsuario[1] + 5;
+                }
+            }
+        }
+    }
+
+void Botao_Emergencia() {
+    if (debounce.read_ms()>200) {
+        Emergencia = 1;
+        debounce.reset();
+        }
+    }
+    
+void PAROU() {
+    estado = 0;
+    }
+
+