MRJJ / Movement

Dependents:   ROCO104_Buggy BuggyDesign

Revision:
2:878b6fd215d7
Parent:
1:401bcbb58715
Child:
3:bbb2ef5e0f68
--- a/Movement.cpp	Tue Mar 17 13:13:52 2020 +0000
+++ b/Movement.cpp	Tue Mar 17 15:22:46 2020 +0000
@@ -2,53 +2,64 @@
 #include "motor.h"
 #include "UltraSonic.h"
 #include "clickers.h"
-
+//These externs allow variables and functiones to be used that in another folder/library 
 extern bool rStopped, fStopped;
 extern Motor Wheel;
 extern int HallState;
 extern void dist();
 extern Serial pc;
-int i = 0;
-void Direction (){
+int i = 0;//This variable is used to control an if statement
+int HallState = 0;//This is used to select which case the switch case will fall in to
+void Direction (){//This function will control the movement of the buggy
     switch(HallState)
             {
             case 0:
                 if(fStopped == false){
-                    dist();
-                    if (i<1)
+                    dist();//Get the distance
+                    if (i<1)//Set the wheel speed only once to save processing
                     {
-                        Wheel.Speed(1.0,1.0);//forward 100%
+                        Wheel.Speed(0.65,0.65);//forward 65%
                         pc.printf("Do this once\n\r");
                         i++;
                     }
-                    
-                    //FwdStop();
+                    FwdStop();//Check if the buggy has been told to stop
                 }
-                else if (fStopped == true)
-                    HallState = 1;
+                else if (fStopped == true)//If the buggy has been stopped,change state
+                    HallState = 1;//Cange state
                 break;
             case 1:
-                if(fStopped == true && rStopped == false){
-                    Wheel.Speed(-1.0,-1.0);//backward 100%
-                    RevStop();
-                    dist();
+                if(fStopped == true && rStopped == false){//Make the go backwards 
+                    Wheel.Speed(-0.65,-0.65);//backward 65%
+                    RevStop();//Check if the buggy has benn told to stop going backwards
+                    dist();//Check distance
                 }
-                else if(fStopped == true && rStopped == true)
-                    HallState = 2;
+                else if(fStopped == true && rStopped == true)//If the buggy has been stopped,change state
+                    HallState = 2;//Cange state
                 break;
             case 2:
-                Wheel.Speed(1.0,-1.0);//Right 100%
-                wait_ms(500);
-                Wheel.Speed(1.0,1.0);//forward 100%
+                Wheel.Speed(0.65,0.65);//forward 65%
+                wait_ms(250);
+                Wheel.Speed(0.65,-0.65);//Right 65%
                 wait_ms(500);
-                Wheel.Speed(-1.0,1.0);//Left 100%
+                Wheel.Speed(0.65,0.65);//forward 65%
                 wait_ms(500);
-                Wheel.Stop();
-                HallState = 3;
+                Wheel.Speed(-0.65,0.65);//Left 65%
+                wait_ms(500);
+                Wheel.Stop();//Stop wheels
+                HallState = 3;//Change state
                 break;
             case 3:
             {
-                
+                Wheel.Speed(-0.65,-0.65);//backward 65%
+                RevStop();//Check if the buggy has benn told to stop going backwards
+                dist();//Check distance
+                HallState = 4;//Change state
             }
+            case 4:
+                //Reset all variables back to what they were at the beginnig so the code can restart
+                i = 0;
+                rStopped = false;
+                fStopped = false;
+                HallState = 0;
             }    
 }
\ No newline at end of file