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.
main.cpp@3:c3d716d6c6a7, 2015-03-26 (annotated)
- Committer:
- avinashr
- Date:
- Thu Mar 26 23:41:44 2015 +0000
- Revision:
- 3:c3d716d6c6a7
- Parent:
- 2:2cc70773996b
- Child:
- 4:95fe80130442
Logic completely implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
avinashr | 0:27111a353928 | 1 | #include "mbed.h" |
avinashr | 0:27111a353928 | 2 | #include "DCMotor.h" |
avinashr | 1:4ec28912c9e7 | 3 | #include "LeftServoMotor.h" |
avinashr | 1:4ec28912c9e7 | 4 | #include "RightServoMotor.h" |
avinashr | 1:4ec28912c9e7 | 5 | #include "Keypad.h" |
avinashr | 2:2cc70773996b | 6 | #include "GlobalObjects.h" |
avinashr | 2:2cc70773996b | 7 | #include "RequestQueue.h" |
avinashr | 1:4ec28912c9e7 | 8 | |
avinashr | 0:27111a353928 | 9 | DigitalOut myled(LED1); |
avinashr | 2:2cc70773996b | 10 | |
avinashr | 3:c3d716d6c6a7 | 11 | void moveForward (){ |
avinashr | 3:c3d716d6c6a7 | 12 | dcMotor.forward(); |
avinashr | 3:c3d716d6c6a7 | 13 | movingDirection = 1; |
avinashr | 3:c3d716d6c6a7 | 14 | } |
avinashr | 3:c3d716d6c6a7 | 15 | |
avinashr | 3:c3d716d6c6a7 | 16 | void moveReverse(){ |
avinashr | 3:c3d716d6c6a7 | 17 | dcMotor.reverse(); |
avinashr | 3:c3d716d6c6a7 | 18 | movingDirection = -1; |
avinashr | 3:c3d716d6c6a7 | 19 | } |
avinashr | 3:c3d716d6c6a7 | 20 | |
avinashr | 2:2cc70773996b | 21 | void floorChosen (int i){ |
avinashr | 2:2cc70773996b | 22 | queue.add(i); |
avinashr | 2:2cc70773996b | 23 | if(!dcMotor.isMoving()){ |
avinashr | 3:c3d716d6c6a7 | 24 | if(i == currentFloor){ |
avinashr | 2:2cc70773996b | 25 | floorReached(i); |
avinashr | 2:2cc70773996b | 26 | }else if(i>currentFloor){ |
avinashr | 3:c3d716d6c6a7 | 27 | moveForward(); |
avinashr | 2:2cc70773996b | 28 | }else{ |
avinashr | 3:c3d716d6c6a7 | 29 | moveReverse(); |
avinashr | 2:2cc70773996b | 30 | } |
avinashr | 2:2cc70773996b | 31 | } |
avinashr | 2:2cc70773996b | 32 | } |
avinashr | 2:2cc70773996b | 33 | |
avinashr | 2:2cc70773996b | 34 | void doStopOperation (){ |
avinashr | 2:2cc70773996b | 35 | dcMotor.stop(); |
avinashr | 2:2cc70773996b | 36 | leftDoor.open(); |
avinashr | 2:2cc70773996b | 37 | rightDoor.open(); |
avinashr | 2:2cc70773996b | 38 | wait(1); |
avinashr | 2:2cc70773996b | 39 | leftDoor.close(); |
avinashr | 2:2cc70773996b | 40 | rightDoor.close(); |
avinashr | 2:2cc70773996b | 41 | } |
avinashr | 1:4ec28912c9e7 | 42 | |
avinashr | 2:2cc70773996b | 43 | void floorReached (int i){ |
avinashr | 2:2cc70773996b | 44 | doStopOperation(); |
avinashr | 2:2cc70773996b | 45 | queue.remove(i); |
avinashr | 3:c3d716d6c6a7 | 46 | if(movingDirection == 1){ |
avinashr | 3:c3d716d6c6a7 | 47 | if(queue.isForwardRequested(i)){ |
avinashr | 3:c3d716d6c6a7 | 48 | moveForward(); |
avinashr | 3:c3d716d6c6a7 | 49 | }else if(queue.isReverseRequested(i)){ |
avinashr | 3:c3d716d6c6a7 | 50 | moveReverse(); |
avinashr | 3:c3d716d6c6a7 | 51 | }else{ |
avinashr | 3:c3d716d6c6a7 | 52 | movingDirection = 0; |
avinashr | 3:c3d716d6c6a7 | 53 | } |
avinashr | 3:c3d716d6c6a7 | 54 | }else{ |
avinashr | 3:c3d716d6c6a7 | 55 | if(queue.isReverseRequested(i)){ |
avinashr | 3:c3d716d6c6a7 | 56 | moveReverse(); |
avinashr | 3:c3d716d6c6a7 | 57 | }else if(queue.isForwardRequested(i)){ |
avinashr | 3:c3d716d6c6a7 | 58 | moveForward(); |
avinashr | 3:c3d716d6c6a7 | 59 | }else{ |
avinashr | 3:c3d716d6c6a7 | 60 | movingDirection = 0; |
avinashr | 3:c3d716d6c6a7 | 61 | } |
avinashr | 3:c3d716d6c6a7 | 62 | } |
avinashr | 2:2cc70773996b | 63 | } |
avinashr | 2:2cc70773996b | 64 | |
avinashr | 2:2cc70773996b | 65 | void floorDetected (int i){ |
avinashr | 2:2cc70773996b | 66 | currentFloor = i; |
avinashr | 2:2cc70773996b | 67 | if(queue.isRequested(i)) |
avinashr | 2:2cc70773996b | 68 | floorReached(i); |
avinashr | 2:2cc70773996b | 69 | } |
avinashr | 2:2cc70773996b | 70 | |
avinashr | 0:27111a353928 | 71 | int main() { |
avinashr | 1:4ec28912c9e7 | 72 | initializeKeypad(); |
avinashr | 0:27111a353928 | 73 | while(1) { |
avinashr | 1:4ec28912c9e7 | 74 | //dcMotor.forward(); |
avinashr | 1:4ec28912c9e7 | 75 | leftDoor.open(); |
avinashr | 1:4ec28912c9e7 | 76 | rightDoor.open(); |
avinashr | 0:27111a353928 | 77 | myled = 1; |
avinashr | 1:4ec28912c9e7 | 78 | wait(3); |
avinashr | 1:4ec28912c9e7 | 79 | //dcMotor.stop(); |
avinashr | 1:4ec28912c9e7 | 80 | leftDoor.close(); |
avinashr | 1:4ec28912c9e7 | 81 | rightDoor.close(); |
avinashr | 0:27111a353928 | 82 | myled = 0; |
avinashr | 1:4ec28912c9e7 | 83 | wait(3); |
avinashr | 0:27111a353928 | 84 | } |
avinashr | 1:4ec28912c9e7 | 85 | } |