a simple code for elevator
Dependencies: PinDetect mbed Servo
event_and_actions.cpp@0:85829f7bbe62, 2012-06-04 (annotated)
- Committer:
- kemken
- Date:
- Mon Jun 04 13:51:43 2012 +0000
- Revision:
- 0:85829f7bbe62
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kemken | 0:85829f7bbe62 | 1 | /* |
kemken | 0:85829f7bbe62 | 2 | * ENEVTS AND ACTIONS FUNCTIONS. |
kemken | 0:85829f7bbe62 | 3 | * ^^^^^^ ^^^ ^^^^^^^ ^^^^^^^^^ |
kemken | 0:85829f7bbe62 | 4 | * |
kemken | 0:85829f7bbe62 | 5 | * FILE NAME: event_and_actins.cpp |
kemken | 0:85829f7bbe62 | 6 | * USAGE: all the actions and some of the events functions are here. |
kemken | 0:85829f7bbe62 | 7 | */ |
kemken | 0:85829f7bbe62 | 8 | |
kemken | 0:85829f7bbe62 | 9 | |
kemken | 0:85829f7bbe62 | 10 | /* |
kemken | 0:85829f7bbe62 | 11 | * including the wanted library files. |
kemken | 0:85829f7bbe62 | 12 | */ |
kemken | 0:85829f7bbe62 | 13 | #include "mbed.h" |
kemken | 0:85829f7bbe62 | 14 | #include "speed.h" |
kemken | 0:85829f7bbe62 | 15 | #include "LED.h" |
kemken | 0:85829f7bbe62 | 16 | #include "beeper.h" |
kemken | 0:85829f7bbe62 | 17 | |
kemken | 0:85829f7bbe62 | 18 | /*--------------------------*\ |
kemken | 0:85829f7bbe62 | 19 | * defineding the variables* |
kemken | 0:85829f7bbe62 | 20 | \*--------------------------*/ |
kemken | 0:85829f7bbe62 | 21 | int upload; |
kemken | 0:85829f7bbe62 | 22 | int loaded; |
kemken | 0:85829f7bbe62 | 23 | int flag_call_1=0; |
kemken | 0:85829f7bbe62 | 24 | int flag_call_2=0; |
kemken | 0:85829f7bbe62 | 25 | |
kemken | 0:85829f7bbe62 | 26 | event event_queue [11]; // variable named current_state; event type |
kemken | 0:85829f7bbe62 | 27 | |
kemken | 0:85829f7bbe62 | 28 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of moving the elevotor to the first floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 29 | void move_to_bottom (void) { |
kemken | 0:85829f7bbe62 | 30 | motor (1.0, 2000); /* calling "motor" function to run the motor from 0 to high speed during 2mS */ |
kemken | 0:85829f7bbe62 | 31 | current_state = going_down; /* putting this function in "going_up" state. */ |
kemken | 0:85829f7bbe62 | 32 | } |
kemken | 0:85829f7bbe62 | 33 | |
kemken | 0:85829f7bbe62 | 34 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of stopping the motor at the top floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 35 | void motor_stop_tp (void) { |
kemken | 0:85829f7bbe62 | 36 | motor (0.5,0); /* calling "motor" function to stop the motor */ |
kemken | 0:85829f7bbe62 | 37 | bing (); /* calling "bing" function that start beeping */ |
kemken | 0:85829f7bbe62 | 38 | current_state = waiting_beeper_tp; /* putting this function in "waiting_beeper_tp" state. */ |
kemken | 0:85829f7bbe62 | 39 | } |
kemken | 0:85829f7bbe62 | 40 | |
kemken | 0:85829f7bbe62 | 41 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of stopping the motor at the bottom floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 42 | void motor_stop_bm (void) { |
kemken | 0:85829f7bbe62 | 43 | motor (0.5,0); /* calling "motor" function to stop the motor*/ |
kemken | 0:85829f7bbe62 | 44 | bing (); /* calling "bing" function that start beeping*/ |
kemken | 0:85829f7bbe62 | 45 | current_state =waiting_beeper_bm; /* putting this function in "waiting_beeper_bm" state. */ |
kemken | 0:85829f7bbe62 | 46 | } |
kemken | 0:85829f7bbe62 | 47 | |
kemken | 0:85829f7bbe62 | 48 | /*>>>>>>>>>>> function responsible of slowing down the motor when the elevator is near the top floor <<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 49 | void slow_down_tp (void) { |
kemken | 0:85829f7bbe62 | 50 | motor (0.35, 1000); /* calling "motor" function to run the motor from high to slow speed (0.2) during 1mS */ |
kemken | 0:85829f7bbe62 | 51 | current_state = wating_top_sw; /* putting this function in "wating_top_sw" state. */ |
kemken | 0:85829f7bbe62 | 52 | } |
kemken | 0:85829f7bbe62 | 53 | |
kemken | 0:85829f7bbe62 | 54 | /*>>>>>>>>>>> function responsible of slowing down the motor when the elevator is near the bottom floor <<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 55 | void slow_down_bm (void) { |
kemken | 0:85829f7bbe62 | 56 | motor (0.6, 1000); /* calling "motor" function to run the motor from high to slow speed (0.8) during 1mS */ |
kemken | 0:85829f7bbe62 | 57 | current_state = waiting_bottom_sw;/* putting this function in "waiting_bottom_sw" state. */ |
kemken | 0:85829f7bbe62 | 58 | } |
kemken | 0:85829f7bbe62 | 59 | |
kemken | 0:85829f7bbe62 | 60 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of stopping the motor at the top floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 61 | void move_to_top (void) { |
kemken | 0:85829f7bbe62 | 62 | motor (0.0, 2000); /* calling "motor" function to run the motor from 0 to high speed during 2mS */ |
kemken | 0:85829f7bbe62 | 63 | current_state = going_up;/* putting this function in "going_up" state. */ |
kemken | 0:85829f7bbe62 | 64 | } |
kemken | 0:85829f7bbe62 | 65 | |
kemken | 0:85829f7bbe62 | 66 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of stopping the motor at the top floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 67 | void door_top (void) { |
kemken | 0:85829f7bbe62 | 68 | open_close_doors ( 1 ); /* calling "open_close_doors" function to open the top door; name of the function(selecting floor)*/ |
kemken | 0:85829f7bbe62 | 69 | current_state = waiting_door_tp;/* putting this function in "waiting_door_tp" state. */ |
kemken | 0:85829f7bbe62 | 70 | } |
kemken | 0:85829f7bbe62 | 71 | |
kemken | 0:85829f7bbe62 | 72 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of stopping the motor at the top floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 73 | void door_bottom (void) { |
kemken | 0:85829f7bbe62 | 74 | open_close_doors ( 0 );/* calling "open_close_doors" function to open the bottom door; name of the function(selecting floor)*/ |
kemken | 0:85829f7bbe62 | 75 | current_state = waiting_door_bm;/* putting this function in "waiting_door_bm" state. */ |
kemken | 0:85829f7bbe62 | 76 | } |
kemken | 0:85829f7bbe62 | 77 | |
kemken | 0:85829f7bbe62 | 78 | |
kemken | 0:85829f7bbe62 | 79 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of stopping the motor at the top floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 80 | /*-----> start the door open close sequence on the led's, when the sequence finishes then a timeout event is generated <-----*/ |
kemken | 0:85829f7bbe62 | 81 | void close_top (void) { |
kemken | 0:85829f7bbe62 | 82 | current_state = top;/* putting this function in "top" state. */ |
kemken | 0:85829f7bbe62 | 83 | /*>>>>>>> flag operation <<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 84 | flag_call_2=0; |
kemken | 0:85829f7bbe62 | 85 | if (flag_call_1) move_to_bottom(); |
kemken | 0:85829f7bbe62 | 86 | } |
kemken | 0:85829f7bbe62 | 87 | |
kemken | 0:85829f7bbe62 | 88 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of stopping the motor at the top floor <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 89 | /*-----> start the door open close sequence on the led's, when the sequence finishes then a timeout event is generated <-----*/ |
kemken | 0:85829f7bbe62 | 90 | void close_bottom (void) { |
kemken | 0:85829f7bbe62 | 91 | current_state = bottom;/* putting this function in "bottom" state. */ |
kemken | 0:85829f7bbe62 | 92 | /*>>>>>>> flag operation <<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 93 | flag_call_1=0; |
kemken | 0:85829f7bbe62 | 94 | if (flag_call_2) move_to_top(); |
kemken | 0:85829f7bbe62 | 95 | } |
kemken | 0:85829f7bbe62 | 96 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of do nothing <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 97 | void ND (void) { |
kemken | 0:85829f7bbe62 | 98 | } |
kemken | 0:85829f7bbe62 | 99 | |
kemken | 0:85829f7bbe62 | 100 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of getting new event <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 101 | void new_event (event next_event) { |
kemken | 0:85829f7bbe62 | 102 | event_queue [upload]=next_event; |
kemken | 0:85829f7bbe62 | 103 | upload++; |
kemken | 0:85829f7bbe62 | 104 | if (upload==11) |
kemken | 0:85829f7bbe62 | 105 | upload=0; |
kemken | 0:85829f7bbe62 | 106 | } |
kemken | 0:85829f7bbe62 | 107 | |
kemken | 0:85829f7bbe62 | 108 | /*>>>>>>>>>>>>>>>>>>>>>>> function responsible of getting event <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 109 | event get_event (void) { |
kemken | 0:85829f7bbe62 | 110 | do { |
kemken | 0:85829f7bbe62 | 111 | printf(""); |
kemken | 0:85829f7bbe62 | 112 | } while (upload==loaded); |
kemken | 0:85829f7bbe62 | 113 | event ev; |
kemken | 0:85829f7bbe62 | 114 | ev = event_queue [loaded++]; |
kemken | 0:85829f7bbe62 | 115 | if (loaded ==11) |
kemken | 0:85829f7bbe62 | 116 | loaded =0; |
kemken | 0:85829f7bbe62 | 117 | return (ev); |
kemken | 0:85829f7bbe62 | 118 | } |
kemken | 0:85829f7bbe62 | 119 | |
kemken | 0:85829f7bbe62 | 120 | /*>>>>>>>>>> function responsible of running the motor backward when the elevotor hit the safety switch <<<<<<<<<<<*/ |
kemken | 0:85829f7bbe62 | 121 | void slow_back (void) { |
kemken | 0:85829f7bbe62 | 122 | motor(0.6,0); /* calling "motor" function to run the motor at slow speed*/ |
kemken | 0:85829f7bbe62 | 123 | current_state=waiting_for_Bfloor; /* putting this function in "waiting_for_Bfloor" state. */ |
kemken | 0:85829f7bbe62 | 124 | /*---- Print message: shows when this function is called ---------------------------------*/ |
kemken | 0:85829f7bbe62 | 125 | printf("slow back running "); |
kemken | 0:85829f7bbe62 | 126 | } |