a simple code for elevator
Dependencies: PinDetect mbed Servo
main.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 | * state machine assignment - elevator. * |
kemken | 0:85829f7bbe62 | 3 | * a software for two level elevator * |
kemken | 0:85829f7bbe62 | 4 | * * |
kemken | 0:85829f7bbe62 | 5 | * Author: Fatema A.Razaq Mohammed * |
kemken | 0:85829f7bbe62 | 6 | * Purpose: improve the abilty of using state machine * |
kemken | 0:85829f7bbe62 | 7 | * *******************************************************/ |
kemken | 0:85829f7bbe62 | 8 | |
kemken | 0:85829f7bbe62 | 9 | /* |
kemken | 0:85829f7bbe62 | 10 | * THE MAIN FILE OF THIS PROGRAM. |
kemken | 0:85829f7bbe62 | 11 | * ^^^ ^^^^ ^^^^ ^^ ^^^^ ^^^^^^^ |
kemken | 0:85829f7bbe62 | 12 | * |
kemken | 0:85829f7bbe62 | 13 | * FILE NAME: main.cpp |
kemken | 0:85829f7bbe62 | 14 | * USAGE: gathering the programs funtcions. |
kemken | 0:85829f7bbe62 | 15 | */ |
kemken | 0:85829f7bbe62 | 16 | |
kemken | 0:85829f7bbe62 | 17 | |
kemken | 0:85829f7bbe62 | 18 | |
kemken | 0:85829f7bbe62 | 19 | /* |
kemken | 0:85829f7bbe62 | 20 | * including all the created library files of the elevator program. |
kemken | 0:85829f7bbe62 | 21 | */ |
kemken | 0:85829f7bbe62 | 22 | #include "mbed.h" |
kemken | 0:85829f7bbe62 | 23 | #include "main.h" |
kemken | 0:85829f7bbe62 | 24 | #include "PIN_DETECT.h" |
kemken | 0:85829f7bbe62 | 25 | #include "beeper.h" |
kemken | 0:85829f7bbe62 | 26 | #include "LED.h" |
kemken | 0:85829f7bbe62 | 27 | #include "speed.h" |
kemken | 0:85829f7bbe62 | 28 | #include "event_and_actions.h" |
kemken | 0:85829f7bbe62 | 29 | |
kemken | 0:85829f7bbe62 | 30 | |
kemken | 0:85829f7bbe62 | 31 | event get_event (void); // a function that has "event" as return type. |
kemken | 0:85829f7bbe62 | 32 | state current_state; // variable named current_state |
kemken | 0:85829f7bbe62 | 33 | event current_event; // variable named current_event |
kemken | 0:85829f7bbe62 | 34 | |
kemken | 0:85829f7bbe62 | 35 | /*---- for testing the program, all the events and states were rewrite to be use in Printf -------------------*/ |
kemken | 0:85829f7bbe62 | 36 | const char event_lookup[MAX_EVENT][20] ={"B_level_1", "B_level_2", "call_1", "call_2", "NR_top", "NR_bottom", "at_top", "at_bottom", "timeout", "safety"}; |
kemken | 0:85829f7bbe62 | 37 | const char state_lookup[MAX_STATE][20] ={"top", "waiting_beeper_tp", "waiting_door_tp", "wating_top_sw", "going_down","bottom", "waiting_beeper_bm","waiting_door_bm", "waiting_bottom_sw", "going_up", "waiting_for_safety", "waiting_for_Bfloor"}; |
kemken | 0:85829f7bbe62 | 38 | |
kemken | 0:85829f7bbe62 | 39 | /* state machine table (events VS states) */ |
kemken | 0:85829f7bbe62 | 40 | void (*const state_table [MAX_STATE][MAX_EVENT]) (void) = { |
kemken | 0:85829f7bbe62 | 41 | /*B_level_1 B_level_2 call_1 call_2 NR_top NR_bottom at_top at_bottom timeout safety */ |
kemken | 0:85829f7bbe62 | 42 | {move_to_bottom, door_top, move_to_bottom, door_top, ND, ND, motor_stop_tp, ND, ND, slow_back }, /*top*/ |
kemken | 0:85829f7bbe62 | 43 | {ND, ND, ND, ND, ND, ND, ND, ND, door_top, ND }, /*waiting_beeper_tp*/ |
kemken | 0:85829f7bbe62 | 44 | {ND, ND, ND, ND, ND, ND, ND, ND, close_top, ND }, /*waiting_door_tp*/ |
kemken | 0:85829f7bbe62 | 45 | {ND, ND, ND, ND, slow_down_tp, ND, motor_stop_tp, ND, ND, slow_back }, /*wating_top_sw*/ |
kemken | 0:85829f7bbe62 | 46 | {ND, ND, ND, ND, ND, slow_down_bm, ND, ND, ND, ND }, /*going_down (when it's hite the near bottom switch)*/ |
kemken | 0:85829f7bbe62 | 47 | {door_bottom, move_to_top, door_bottom, move_to_top, ND, ND, ND, motor_stop_bm, ND, ND }, /*bottom*/ |
kemken | 0:85829f7bbe62 | 48 | {ND, ND, ND, ND, ND, ND, ND, ND, door_bottom, ND }, /*waiting_beeper_bm*/ |
kemken | 0:85829f7bbe62 | 49 | {ND, ND, ND, ND, ND, ND, ND, ND, close_bottom, ND }, /*waiting_door_bm*/ |
kemken | 0:85829f7bbe62 | 50 | {ND, ND, ND, ND, ND, slow_down_bm, ND, motor_stop_bm, ND, ND }, /*waiting_bottom_sw*/ |
kemken | 0:85829f7bbe62 | 51 | {ND, ND, ND, ND, slow_down_tp, ND, ND, ND, ND, slow_back }, /*going_up*/ |
kemken | 0:85829f7bbe62 | 52 | {ND, ND, ND, ND, ND, ND, ND, ND, ND, slow_back }, /*waiting_for_safety*/ |
kemken | 0:85829f7bbe62 | 53 | {ND, ND, ND, ND, ND , ND, ND, motor_stop_bm, ND, ND } /*waiting_for_Bfloor*/ |
kemken | 0:85829f7bbe62 | 54 | }; |
kemken | 0:85829f7bbe62 | 55 | |
kemken | 0:85829f7bbe62 | 56 | |
kemken | 0:85829f7bbe62 | 57 | int main (void) { |
kemken | 0:85829f7bbe62 | 58 | |
kemken | 0:85829f7bbe62 | 59 | /*-------------------------------------------------------------------*\ |
kemken | 0:85829f7bbe62 | 60 | * first calling the ticker and switche depancing created finctions * |
kemken | 0:85829f7bbe62 | 61 | \*-------------------------------------------------------------------*/ |
kemken | 0:85829f7bbe62 | 62 | switches(); /* call switche depancing function */ |
kemken | 0:85829f7bbe62 | 63 | bing_start(); /* call the created ticker function of the beeper */ |
kemken | 0:85829f7bbe62 | 64 | doors_start();/* call the created ticker function of the doors */ |
kemken | 0:85829f7bbe62 | 65 | motor_start();/* call the created ticker function of the motor */ |
kemken | 0:85829f7bbe62 | 66 | |
kemken | 0:85829f7bbe62 | 67 | current_state = waiting_for_safety; /* sat the first state to begain with */ |
kemken | 0:85829f7bbe62 | 68 | |
kemken | 0:85829f7bbe62 | 69 | /*---- Print message: shows the starting states which program at ------------------------------------*/ |
kemken | 0:85829f7bbe62 | 70 | printf("Statrting State: %s\n",state_lookup[current_state]); |
kemken | 0:85829f7bbe62 | 71 | |
kemken | 0:85829f7bbe62 | 72 | motor(0.4,0); /* calling a function to run the motor at slow speed*/ |
kemken | 0:85829f7bbe62 | 73 | |
kemken | 0:85829f7bbe62 | 74 | while (1) { |
kemken | 0:85829f7bbe62 | 75 | current_event=get_event(); /* changing the event according to what happen next */ |
kemken | 0:85829f7bbe62 | 76 | /*---- a print message: shows the event' program at ------------------------------------*/ |
kemken | 0:85829f7bbe62 | 77 | printf("Event: %s ",event_lookup[current_event]); |
kemken | 0:85829f7bbe62 | 78 | state_table[current_state][current_event](); /* changing the state according to the events (depending on the state machine table)*/ |
kemken | 0:85829f7bbe62 | 79 | /*---- a print message: shows the state' program at ------------------------------------*/ |
kemken | 0:85829f7bbe62 | 80 | printf("State: %s\n",state_lookup[current_state]); |
kemken | 0:85829f7bbe62 | 81 | } |
kemken | 0:85829f7bbe62 | 82 | |
kemken | 0:85829f7bbe62 | 83 | } |