Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ROCO104_Buggy BuggyDesign
Movement.cpp@2:878b6fd215d7, 2020-03-17 (annotated)
- Committer:
- Mikebob
- Date:
- Tue Mar 17 15:22:46 2020 +0000
- Revision:
- 2:878b6fd215d7
- Parent:
- 1:401bcbb58715
- Child:
- 3:bbb2ef5e0f68
Final
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jakesmart | 0:0285394cacfa | 1 | #include "mbed.h" |
| jakesmart | 0:0285394cacfa | 2 | #include "motor.h" |
| jakesmart | 0:0285394cacfa | 3 | #include "UltraSonic.h" |
| jakesmart | 0:0285394cacfa | 4 | #include "clickers.h" |
| Mikebob | 2:878b6fd215d7 | 5 | //These externs allow variables and functiones to be used that in another folder/library |
| Mikebob | 1:401bcbb58715 | 6 | extern bool rStopped, fStopped; |
| Mikebob | 1:401bcbb58715 | 7 | extern Motor Wheel; |
| Mikebob | 1:401bcbb58715 | 8 | extern int HallState; |
| Mikebob | 1:401bcbb58715 | 9 | extern void dist(); |
| Mikebob | 1:401bcbb58715 | 10 | extern Serial pc; |
| Mikebob | 2:878b6fd215d7 | 11 | int i = 0;//This variable is used to control an if statement |
| Mikebob | 2:878b6fd215d7 | 12 | int HallState = 0;//This is used to select which case the switch case will fall in to |
| Mikebob | 2:878b6fd215d7 | 13 | void Direction (){//This function will control the movement of the buggy |
| Mikebob | 1:401bcbb58715 | 14 | switch(HallState) |
| Mikebob | 1:401bcbb58715 | 15 | { |
| Mikebob | 1:401bcbb58715 | 16 | case 0: |
| Mikebob | 1:401bcbb58715 | 17 | if(fStopped == false){ |
| Mikebob | 2:878b6fd215d7 | 18 | dist();//Get the distance |
| Mikebob | 2:878b6fd215d7 | 19 | if (i<1)//Set the wheel speed only once to save processing |
| Mikebob | 1:401bcbb58715 | 20 | { |
| Mikebob | 2:878b6fd215d7 | 21 | Wheel.Speed(0.65,0.65);//forward 65% |
| Mikebob | 1:401bcbb58715 | 22 | pc.printf("Do this once\n\r"); |
| Mikebob | 1:401bcbb58715 | 23 | i++; |
| Mikebob | 1:401bcbb58715 | 24 | } |
| Mikebob | 2:878b6fd215d7 | 25 | FwdStop();//Check if the buggy has been told to stop |
| Mikebob | 1:401bcbb58715 | 26 | } |
| Mikebob | 2:878b6fd215d7 | 27 | else if (fStopped == true)//If the buggy has been stopped,change state |
| Mikebob | 2:878b6fd215d7 | 28 | HallState = 1;//Cange state |
| Mikebob | 1:401bcbb58715 | 29 | break; |
| Mikebob | 1:401bcbb58715 | 30 | case 1: |
| Mikebob | 2:878b6fd215d7 | 31 | if(fStopped == true && rStopped == false){//Make the go backwards |
| Mikebob | 2:878b6fd215d7 | 32 | Wheel.Speed(-0.65,-0.65);//backward 65% |
| Mikebob | 2:878b6fd215d7 | 33 | RevStop();//Check if the buggy has benn told to stop going backwards |
| Mikebob | 2:878b6fd215d7 | 34 | dist();//Check distance |
| Mikebob | 1:401bcbb58715 | 35 | } |
| Mikebob | 2:878b6fd215d7 | 36 | else if(fStopped == true && rStopped == true)//If the buggy has been stopped,change state |
| Mikebob | 2:878b6fd215d7 | 37 | HallState = 2;//Cange state |
| Mikebob | 1:401bcbb58715 | 38 | break; |
| Mikebob | 1:401bcbb58715 | 39 | case 2: |
| Mikebob | 2:878b6fd215d7 | 40 | Wheel.Speed(0.65,0.65);//forward 65% |
| Mikebob | 2:878b6fd215d7 | 41 | wait_ms(250); |
| Mikebob | 2:878b6fd215d7 | 42 | Wheel.Speed(0.65,-0.65);//Right 65% |
| Mikebob | 1:401bcbb58715 | 43 | wait_ms(500); |
| Mikebob | 2:878b6fd215d7 | 44 | Wheel.Speed(0.65,0.65);//forward 65% |
| Mikebob | 1:401bcbb58715 | 45 | wait_ms(500); |
| Mikebob | 2:878b6fd215d7 | 46 | Wheel.Speed(-0.65,0.65);//Left 65% |
| Mikebob | 2:878b6fd215d7 | 47 | wait_ms(500); |
| Mikebob | 2:878b6fd215d7 | 48 | Wheel.Stop();//Stop wheels |
| Mikebob | 2:878b6fd215d7 | 49 | HallState = 3;//Change state |
| Mikebob | 1:401bcbb58715 | 50 | break; |
| Mikebob | 1:401bcbb58715 | 51 | case 3: |
| Mikebob | 1:401bcbb58715 | 52 | { |
| Mikebob | 2:878b6fd215d7 | 53 | Wheel.Speed(-0.65,-0.65);//backward 65% |
| Mikebob | 2:878b6fd215d7 | 54 | RevStop();//Check if the buggy has benn told to stop going backwards |
| Mikebob | 2:878b6fd215d7 | 55 | dist();//Check distance |
| Mikebob | 2:878b6fd215d7 | 56 | HallState = 4;//Change state |
| Mikebob | 1:401bcbb58715 | 57 | } |
| Mikebob | 2:878b6fd215d7 | 58 | case 4: |
| Mikebob | 2:878b6fd215d7 | 59 | //Reset all variables back to what they were at the beginnig so the code can restart |
| Mikebob | 2:878b6fd215d7 | 60 | i = 0; |
| Mikebob | 2:878b6fd215d7 | 61 | rStopped = false; |
| Mikebob | 2:878b6fd215d7 | 62 | fStopped = false; |
| Mikebob | 2:878b6fd215d7 | 63 | HallState = 0; |
| Mikebob | 1:401bcbb58715 | 64 | } |
| jakesmart | 0:0285394cacfa | 65 | } |