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@4:95fe80130442, 2015-03-27 (annotated)
- Committer:
- avinashr
- Date:
- Fri Mar 27 03:53:57 2015 +0000
- Revision:
- 4:95fe80130442
- Parent:
- 3:c3d716d6c6a7
Elevator code, working on test bench
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 | 4:95fe80130442 | 10 | InterruptIn irInput(p14); |
| avinashr | 4:95fe80130442 | 11 | Timer irTimer; |
| avinashr | 2:2cc70773996b | 12 | |
| avinashr | 3:c3d716d6c6a7 | 13 | void moveForward (){ |
| avinashr | 3:c3d716d6c6a7 | 14 | dcMotor.forward(); |
| avinashr | 3:c3d716d6c6a7 | 15 | movingDirection = 1; |
| avinashr | 4:95fe80130442 | 16 | irFlag = 1; |
| avinashr | 3:c3d716d6c6a7 | 17 | } |
| avinashr | 3:c3d716d6c6a7 | 18 | |
| avinashr | 3:c3d716d6c6a7 | 19 | void moveReverse(){ |
| avinashr | 3:c3d716d6c6a7 | 20 | dcMotor.reverse(); |
| avinashr | 3:c3d716d6c6a7 | 21 | movingDirection = -1; |
| avinashr | 4:95fe80130442 | 22 | irFlag = 1; |
| avinashr | 3:c3d716d6c6a7 | 23 | } |
| avinashr | 3:c3d716d6c6a7 | 24 | |
| avinashr | 2:2cc70773996b | 25 | void floorChosen (int i){ |
| avinashr | 4:95fe80130442 | 26 | pc.printf(": %d floor is chosen :",i); |
| avinashr | 2:2cc70773996b | 27 | queue.add(i); |
| avinashr | 2:2cc70773996b | 28 | if(!dcMotor.isMoving()){ |
| avinashr | 3:c3d716d6c6a7 | 29 | if(i == currentFloor){ |
| avinashr | 2:2cc70773996b | 30 | floorReached(i); |
| avinashr | 2:2cc70773996b | 31 | }else if(i>currentFloor){ |
| avinashr | 3:c3d716d6c6a7 | 32 | moveForward(); |
| avinashr | 2:2cc70773996b | 33 | }else{ |
| avinashr | 3:c3d716d6c6a7 | 34 | moveReverse(); |
| avinashr | 2:2cc70773996b | 35 | } |
| avinashr | 2:2cc70773996b | 36 | } |
| avinashr | 2:2cc70773996b | 37 | } |
| avinashr | 2:2cc70773996b | 38 | |
| avinashr | 2:2cc70773996b | 39 | void doStopOperation (){ |
| avinashr | 2:2cc70773996b | 40 | dcMotor.stop(); |
| avinashr | 2:2cc70773996b | 41 | leftDoor.open(); |
| avinashr | 2:2cc70773996b | 42 | rightDoor.open(); |
| avinashr | 2:2cc70773996b | 43 | wait(1); |
| avinashr | 2:2cc70773996b | 44 | leftDoor.close(); |
| avinashr | 2:2cc70773996b | 45 | rightDoor.close(); |
| avinashr | 2:2cc70773996b | 46 | } |
| avinashr | 1:4ec28912c9e7 | 47 | |
| avinashr | 2:2cc70773996b | 48 | void floorReached (int i){ |
| avinashr | 2:2cc70773996b | 49 | doStopOperation(); |
| avinashr | 2:2cc70773996b | 50 | queue.remove(i); |
| avinashr | 3:c3d716d6c6a7 | 51 | if(movingDirection == 1){ |
| avinashr | 3:c3d716d6c6a7 | 52 | if(queue.isForwardRequested(i)){ |
| avinashr | 3:c3d716d6c6a7 | 53 | moveForward(); |
| avinashr | 3:c3d716d6c6a7 | 54 | }else if(queue.isReverseRequested(i)){ |
| avinashr | 3:c3d716d6c6a7 | 55 | moveReverse(); |
| avinashr | 3:c3d716d6c6a7 | 56 | }else{ |
| avinashr | 3:c3d716d6c6a7 | 57 | movingDirection = 0; |
| avinashr | 3:c3d716d6c6a7 | 58 | } |
| avinashr | 3:c3d716d6c6a7 | 59 | }else{ |
| avinashr | 3:c3d716d6c6a7 | 60 | if(queue.isReverseRequested(i)){ |
| avinashr | 3:c3d716d6c6a7 | 61 | moveReverse(); |
| avinashr | 3:c3d716d6c6a7 | 62 | }else if(queue.isForwardRequested(i)){ |
| avinashr | 3:c3d716d6c6a7 | 63 | moveForward(); |
| avinashr | 3:c3d716d6c6a7 | 64 | }else{ |
| avinashr | 3:c3d716d6c6a7 | 65 | movingDirection = 0; |
| avinashr | 3:c3d716d6c6a7 | 66 | } |
| avinashr | 3:c3d716d6c6a7 | 67 | } |
| avinashr | 2:2cc70773996b | 68 | } |
| avinashr | 2:2cc70773996b | 69 | |
| avinashr | 2:2cc70773996b | 70 | void floorDetected (int i){ |
| avinashr | 2:2cc70773996b | 71 | currentFloor = i; |
| avinashr | 2:2cc70773996b | 72 | if(queue.isRequested(i)) |
| avinashr | 2:2cc70773996b | 73 | floorReached(i); |
| avinashr | 4:95fe80130442 | 74 | else{ |
| avinashr | 4:95fe80130442 | 75 | irFlag = 1; |
| avinashr | 4:95fe80130442 | 76 | if(i == 1 && movingDirection == -1){ |
| avinashr | 4:95fe80130442 | 77 | dcMotor.stop(); |
| avinashr | 4:95fe80130442 | 78 | if(queue.isForwardRequested(i)){ |
| avinashr | 4:95fe80130442 | 79 | dcMotor.forward(); |
| avinashr | 4:95fe80130442 | 80 | } |
| avinashr | 4:95fe80130442 | 81 | }else if(i==5 && movingDirection == 1){ |
| avinashr | 4:95fe80130442 | 82 | dcMotor.stop(); |
| avinashr | 4:95fe80130442 | 83 | if(queue.isReverseRequested(i)){ |
| avinashr | 4:95fe80130442 | 84 | dcMotor.reverse(); |
| avinashr | 4:95fe80130442 | 85 | } |
| avinashr | 4:95fe80130442 | 86 | } |
| avinashr | 4:95fe80130442 | 87 | } |
| avinashr | 4:95fe80130442 | 88 | } |
| avinashr | 4:95fe80130442 | 89 | |
| avinashr | 4:95fe80130442 | 90 | int calculateFloor (float timePeriod){ |
| avinashr | 4:95fe80130442 | 91 | float f = 1/timePeriod; |
| avinashr | 4:95fe80130442 | 92 | //pc.printf("In calculateFloor %f",f); |
| avinashr | 4:95fe80130442 | 93 | if (f <120.0 && f >80.0){ |
| avinashr | 4:95fe80130442 | 94 | return 1; |
| avinashr | 4:95fe80130442 | 95 | }else if (f <220.0 && f >180.0){ |
| avinashr | 4:95fe80130442 | 96 | return 2; |
| avinashr | 4:95fe80130442 | 97 | }else if (f <320.0 && f >280.0){ |
| avinashr | 4:95fe80130442 | 98 | return 3; |
| avinashr | 4:95fe80130442 | 99 | }else if (f <420.0 && f >380.0){ |
| avinashr | 4:95fe80130442 | 100 | return 4; |
| avinashr | 4:95fe80130442 | 101 | }else if (f <520.0 && f >480.0){ |
| avinashr | 4:95fe80130442 | 102 | return 5; |
| avinashr | 4:95fe80130442 | 103 | }else |
| avinashr | 4:95fe80130442 | 104 | return 0; |
| avinashr | 4:95fe80130442 | 105 | } |
| avinashr | 4:95fe80130442 | 106 | |
| avinashr | 4:95fe80130442 | 107 | |
| avinashr | 4:95fe80130442 | 108 | int isFirstRise = 1; |
| avinashr | 4:95fe80130442 | 109 | void irRise(){ |
| avinashr | 4:95fe80130442 | 110 | //pc.printf("Interrupt detected: %d",irFlag); |
| avinashr | 4:95fe80130442 | 111 | if(irFlag){ |
| avinashr | 4:95fe80130442 | 112 | if(isFirstRise){ |
| avinashr | 4:95fe80130442 | 113 | irTimer.start(); |
| avinashr | 4:95fe80130442 | 114 | isFirstRise = 0; |
| avinashr | 4:95fe80130442 | 115 | }else{ |
| avinashr | 4:95fe80130442 | 116 | irTimer.stop(); |
| avinashr | 4:95fe80130442 | 117 | currentFloor = calculateFloor(irTimer.read()); |
| avinashr | 4:95fe80130442 | 118 | if(currentFloor){ |
| avinashr | 4:95fe80130442 | 119 | irFlag = 0; |
| avinashr | 4:95fe80130442 | 120 | floorDetected(currentFloor); |
| avinashr | 4:95fe80130442 | 121 | } |
| avinashr | 4:95fe80130442 | 122 | irTimer.reset(); |
| avinashr | 4:95fe80130442 | 123 | isFirstRise = 1; |
| avinashr | 4:95fe80130442 | 124 | } |
| avinashr | 4:95fe80130442 | 125 | } |
| avinashr | 2:2cc70773996b | 126 | } |
| avinashr | 2:2cc70773996b | 127 | |
| avinashr | 0:27111a353928 | 128 | int main() { |
| avinashr | 1:4ec28912c9e7 | 129 | initializeKeypad(); |
| avinashr | 4:95fe80130442 | 130 | irInput.rise(&irRise); |
| avinashr | 0:27111a353928 | 131 | while(1) { |
| avinashr | 4:95fe80130442 | 132 | myled = 1; |
| avinashr | 4:95fe80130442 | 133 | wait(1); |
| avinashr | 4:95fe80130442 | 134 | myled = 0; |
| avinashr | 4:95fe80130442 | 135 | wait(1); |
| avinashr | 0:27111a353928 | 136 | } |
| avinashr | 1:4ec28912c9e7 | 137 | } |