This is firmware for the smart gearbox! Features: Forward Reverse

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
rvasquez6089
Date:
Thu Oct 29 20:56:55 2015 +0000
Parent:
1:12e11042b2e9
Commit message:
Forward and reverse Implemented

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 12e11042b2e9 -r fe723d003d1f main.cpp
--- a/main.cpp	Thu Oct 29 19:26:23 2015 +0000
+++ b/main.cpp	Thu Oct 29 20:56:55 2015 +0000
@@ -3,7 +3,8 @@
 DigitalOut MOT_B(PA_8);
 PwmOut MOT_D(PA_10);
 DigitalOut MOT_A(PA_7);
-InterruptIn event(USER_BUTTON);
+InterruptIn Button_P(PC_13);
+InterruptIn Button_D(PC_14);
 DigitalOut Orange(PB_0);
 DigitalOut myled(LED1);
 Ticker Motor;
@@ -15,6 +16,8 @@
 DigitalOut SEL2(PA_12);
 void run_300();
 Timeout go_sleep;
+bool F = 0;
+bool R = 0;
 
 
 bool battery_status()
@@ -101,7 +104,7 @@
 	return low_bat;
 }
 
-void pressed()
+void pressed_F()
 {
     printf("Button pressed\n");
     //battery_status();
@@ -109,6 +112,7 @@
     if(go_to_sleep == 1)
     {
     	//battery_status();
+    	//F = 0;
     	go_sleep.detach();
     	//Orange = 0.0f;
     }
@@ -116,7 +120,32 @@
     {
     	if(!(battery_status()))
     	{
-    		go_sleep.attach(&pressed, 10.0);
+    		go_sleep.attach(&pressed_F, 10.0);
+    		F = 1;
+    		
+    	}
+	}
+}
+
+void pressed_R()
+{
+    printf("Button pressed\n");
+    //battery_status();
+    go_to_sleep = !go_to_sleep;
+    if(go_to_sleep == 1)
+    {
+    	//battery_status();
+    	//R = 0;
+    	go_sleep.detach();
+    	//Orange = 0.0f;
+    }
+    if(go_to_sleep == 0)
+    {
+    	if(!(battery_status()))
+    	{
+    		go_sleep.attach(&pressed_R, 10.0);
+    		R = 1;
+    		
     	}
 	}
 }
@@ -312,7 +341,38 @@
 	}
 }
 
+void run_4500_F()
+{
 
+	float current;
+	int output;
+
+
+	MOT_D = 0.0;
+	MOT_A = 0;
+	MOT_B = 1;
+	current = 0.0;
+	for(int i = 0; i < 10; i++)
+	{
+		current = current + CSENSE.read();
+		//wait_us(10);
+	}
+	current = current/10;
+	current  = ((current*3.3f)/100.0f)/(0.015f);
+	current = current*1000.0f;
+	output = static_cast<int>(current);
+	//pc.printf("Motor Current = %d mA\r\n",output );
+
+	//wait(0.001);
+	if(output >= 4500)
+	{
+		MOT_C = MOT_C - (0.01f*(((output-4500)/1)));
+	}
+	if(output < 4500)
+	{
+		MOT_C = 1.0f;
+	}
+}
 
 int main()
 {
@@ -322,7 +382,8 @@
     MOT_C = 0.0f;
     MOT_D = 0.0f;
     //Orange = 0.0f;
-    event.fall(&pressed);
+    Button_P.fall(&pressed_F);
+    Button_D.fall(&pressed_R);
     battery_status();
     MOT_C.period(0.00005);
     MOT_D.period(0.00005);
@@ -331,6 +392,8 @@
     while (1) {
         if (go_to_sleep) {
             
+            F = 0;
+            R = 0;
             myled = 0;
             //printf("%d: Entering sleep (press user button to resume)\n", i);
             Motor.detach();
@@ -353,8 +416,14 @@
             //printf("%d: Running\n", i);
             myled = 1;
             Orange = 1;  
-            run_4500_R();
-            
+            if(R && !F)
+            {
+            	run_4500_R();
+            }
+            if(F && !R)
+            {
+            	run_4500_F();
+            }
         }
         i++;
     }