Movement functions (Forward, Reverse etc).
Dependents: ROCO104_Buggy BuggyDesign
Movement.cpp
00001 #include "mbed.h" 00002 #include "motor.h" 00003 #include "UltraSonic.h" 00004 #include "clickers.h" 00005 //These externs allow variables and functiones to be used that in another folder/library 00006 extern bool rStopped, fStopped; 00007 extern Motor Wheel; 00008 extern int HallState; 00009 extern void dist(); 00010 extern Serial pc; 00011 int i = 0;//This variable is used to control an if statement 00012 int HallState = 0;//This is used to select which case the switch case will fall in to 00013 void Direction (){//This function will control the movement of the buggy 00014 switch(HallState) 00015 { 00016 case 0: 00017 if(fStopped == false){ 00018 dist();//Get the distance 00019 if (i<1)//Set the wheel speed only once to save processing 00020 { 00021 Wheel.Speed(0.65,0.65);//forward 65% 00022 pc.printf("Do this once\n\r"); 00023 i++; 00024 } 00025 FwdStop();//Check if the buggy has been told to stop 00026 } 00027 else if (fStopped == true)//If the buggy has been stopped,change state 00028 HallState = 1;//Cange state 00029 break; 00030 case 1: 00031 if(fStopped == true && rStopped == false){//Make the buggy go backwards 00032 Wheel.Speed(-0.65,-0.65);//backward 65% 00033 RevStop();//Check if the buggy has benn told to stop going backwards 00034 dist();//Check distance 00035 } 00036 else if(fStopped == true && rStopped == true)//If the buggy has been stopped,change state 00037 HallState = 2;//Cange state 00038 break; 00039 case 2: 00040 Wheel.Speed(0.65,0.65);//forward 65% 00041 wait_ms(250); 00042 Wheel.Speed(0.65,-0.65);//Right 65% 00043 wait_ms(500); 00044 Wheel.Speed(0.65,0.65);//forward 65% 00045 wait_ms(500); 00046 Wheel.Speed(-0.65,0.65);//Left 65% 00047 wait_ms(500); 00048 Wheel.Stop();//Stop wheels 00049 HallState = 3;//Change state 00050 break; 00051 case 3: 00052 { 00053 Wheel.Speed(-0.65,-0.65);//backward 65% 00054 RevStop();//Check if the buggy has benn told to stop going backwards 00055 dist();//Check distance 00056 HallState = 4;//Change state 00057 } 00058 case 4: 00059 //Reset all variables back to what they were at the beginnig so the code can restart 00060 i = 0; 00061 rStopped = false; 00062 fStopped = false; 00063 HallState = 0; 00064 } 00065 }
Generated on Fri Aug 19 2022 10:17:49 by
1.7.2