Sz_Insper / Mbed 2 deprecated L6230_Hbridge

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Marcelocostanzo
Date:
Wed Oct 28 15:01:54 2020 +0000
Parent:
5:6c5f54fc5cbd
Commit message:
Ponte H com l6230

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Oct 28 13:05:28 2020 +0000
+++ b/main.cpp	Wed Oct 28 15:01:54 2020 +0000
@@ -1,5 +1,10 @@
 #include "mbed.h"
 
+#define CW      0
+#define CCW     1
+#define BRAKED  2
+#define HIGH_Z  3
+
 //---Pinos de enable dos braços----
 DigitalOut enable_ch1(PC_10);
 DigitalOut enable_ch2(PC_11);
@@ -23,8 +28,7 @@
 Serial pc(USBTX, USBRX);
 
 //---Prototipo das Funções----
-void motor_start(float SPEED, unsigned int MOTOR);
-void motor_stop(unsigned int MOTOR);
+void bridge(float DUTY, unsigned int DIRECTION);
 
 //---Programa principal-------
 int main() 
@@ -51,63 +55,60 @@
     
     while(1) 
     {
-        motor_start(0.5, 1); //Liga o motor 1 com 50% de duty-cicle
-        wait_ms(500);
-        motor_start(0.5, 2); //Liga o motor 2 com 50% de duty-cicle
-        wait_ms(500);
-        motor_start(0.5, 3); //Liga o motor 3 com 50% de duty-cicle
-        wait_ms(500);
+        pc.printf("CW\r");
+        bridge(0.5, CW);
+        wait_ms(5000);
+        
+        pc.printf("CCW\r");
+        bridge(0.5, CCW);
+        wait_ms(5000);
         
-        motor_stop(1);  //Desliga o motor 1
-        wait_ms(500);
-        motor_stop(2);  //Desliga o motor 2
-        wait_ms(500);
-        motor_stop(3);  //Desliga o motor 3
-        wait_ms(500);
+        pc.printf("BRAKED\r");
+        bridge(0.5, BRAKED);
+        wait_ms(5000);
+        
+        pc.printf("HIGH Z\r");
+        bridge(0.5, HIGH_Z);
+        wait_ms(5000);
     }
 }
 
 //---Função de controle dos braços----------
-void motor_start(float SPEED, unsigned int MOTOR)
+void bridge(float DUTY, unsigned int DIRECTION)
 {
-    if(MOTOR == 1)
+    //Direção for horaria
+    if(DIRECTION == CW)
     {
         enable_ch1 = 1;
-        in_ch1.write(SPEED);
-    }
-    
-    if(MOTOR == 2)
-    {
+        in_ch1.write(DUTY);
         enable_ch2 = 1;
-        in_ch2.write(SPEED);
-    }
-    
-    if(MOTOR == 3)
-    {
-        enable_ch3 = 1;
-        in_ch3.write(SPEED);
-    }
-}
-
-//---Função de desligamento dos braços----------
-void motor_stop(unsigned int MOTOR)
-{
-  if(MOTOR == 1)
-    {
-        enable_ch1 = 2; //alta impedancia - Z
-        in_ch1.write(0);
-    }
-    
-    if(MOTOR == 2)
-    {
-        enable_ch2 = 2; //alta impedancia - Z
         in_ch2.write(0);
     }
     
-    if(MOTOR == 3)
+    //Direção for anti horaria
+    if(DIRECTION == CCW)
+    {
+        enable_ch1 = 1;
+        in_ch1.write(0);
+        enable_ch2 = 1;
+        in_ch2.write(DUTY);
+    }
+    
+    //Freiado
+    if(DIRECTION == BRAKED)
     {
-        enable_ch3 = 2; //alta impedancia - Z
-        in_ch3.write(0);
-    }  
+        enable_ch1 = 1;
+        in_ch1.write(DUTY);
+        enable_ch2 = 1;
+        in_ch2.write(DUTY);
+    }
+    
+    //Livre
+    if(DIRECTION == HIGH_Z)
+    {
+        enable_ch1 = 2;
+        in_ch1.write(0);
+        enable_ch2 = 2;
+        in_ch2.write(0);
+    }
 }
-