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@0:32827ba075c9, 2016-01-07 (annotated)
- Committer:
- eencae
- Date:
- Thu Jan 07 12:01:55 2016 +0000
- Revision:
- 0:32827ba075c9
- Child:
- 1:b6df1b5309e7
Initial commit.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| eencae | 0:32827ba075c9 | 1 | /* |
| eencae | 0:32827ba075c9 | 2 | |
| eencae | 0:32827ba075c9 | 3 | 2645_FSM_Puffin |
| eencae | 0:32827ba075c9 | 4 | |
| eencae | 0:32827ba075c9 | 5 | Sample code from ELEC2645 Week 16 Lab |
| eencae | 0:32827ba075c9 | 6 | |
| eencae | 0:32827ba075c9 | 7 | Demonstrates how to implement a puffin crossing using a FSM |
| eencae | 0:32827ba075c9 | 8 | |
| eencae | 0:32827ba075c9 | 9 | (c) Craig A. Evans, University of Leeds, Jan 2016 |
| eencae | 0:32827ba075c9 | 10 | |
| eencae | 0:32827ba075c9 | 11 | */ |
| eencae | 0:32827ba075c9 | 12 | |
| eencae | 0:32827ba075c9 | 13 | #include "mbed.h" |
| eencae | 0:32827ba075c9 | 14 | |
| eencae | 0:32827ba075c9 | 15 | // K64F on-board LEDs |
| eencae | 0:32827ba075c9 | 16 | DigitalOut r_led(LED_RED); |
| eencae | 0:32827ba075c9 | 17 | DigitalOut g_led(LED_GREEN); |
| eencae | 0:32827ba075c9 | 18 | DigitalOut b_led(LED_BLUE); |
| eencae | 0:32827ba075c9 | 19 | |
| eencae | 0:32827ba075c9 | 20 | // LEDs to display traffic light output |
| eencae | 0:32827ba075c9 | 21 | // connect up external LEDs to these pins with appropriate current-limiting resistor |
| eencae | 0:32827ba075c9 | 22 | // LSB MSB |
| eencae | 0:32827ba075c9 | 23 | // car - green,amber, red pedestrian - green, red |
| eencae | 0:32827ba075c9 | 24 | BusOut output(PTB2,PTB3,PTB10,PTB11,PTC11); |
| eencae | 0:32827ba075c9 | 25 | // BusIn to read inputs simutaneously |
| eencae | 0:32827ba075c9 | 26 | // camera , pedestrian |
| eencae | 0:32827ba075c9 | 27 | BusIn input(SW3,SW2); |
| eencae | 0:32827ba075c9 | 28 | |
| eencae | 0:32827ba075c9 | 29 | // struct for state |
| eencae | 0:32827ba075c9 | 30 | struct State { |
| eencae | 0:32827ba075c9 | 31 | int output; // output value |
| eencae | 0:32827ba075c9 | 32 | float time; // time in state |
| eencae | 0:32827ba075c9 | 33 | int nextState[4]; // array of next states |
| eencae | 0:32827ba075c9 | 34 | }; |
| eencae | 0:32827ba075c9 | 35 | typedef const struct State STyp; |
| eencae | 0:32827ba075c9 | 36 | |
| eencae | 0:32827ba075c9 | 37 | STyp fsm[4] = { |
| eencae | 0:32827ba075c9 | 38 | {0x0C,5.0,{0,1,0,1}}, // 0 - cars stop, pedestrian walk 5 seconds |
| eencae | 0:32827ba075c9 | 39 | {0x16,2.0,{2,2,2,2}}, // 1 - cars get ready to go, pedestrian stop 2 seconds |
| eencae | 0:32827ba075c9 | 40 | {0x11,10.0,{3,2,2,2}}, // 2 - cars go, pedestrian stop 10 seconds |
| eencae | 0:32827ba075c9 | 41 | {0x12,2.0,{0,0,0,0}} // 3 - cars get ready to stop, pedestrian stop 2 seconds |
| eencae | 0:32827ba075c9 | 42 | }; |
| eencae | 0:32827ba075c9 | 43 | |
| eencae | 0:32827ba075c9 | 44 | int main() |
| eencae | 0:32827ba075c9 | 45 | { |
| eencae | 0:32827ba075c9 | 46 | // on-board LEDs are active-low, so set pin high to turn them off. |
| eencae | 0:32827ba075c9 | 47 | r_led = 1; |
| eencae | 0:32827ba075c9 | 48 | g_led = 1; |
| eencae | 0:32827ba075c9 | 49 | b_led = 1; |
| eencae | 0:32827ba075c9 | 50 | |
| eencae | 0:32827ba075c9 | 51 | // on-board switches have external pull-ups, so turn off default internal pull-downs |
| eencae | 0:32827ba075c9 | 52 | input.mode(PullNone); |
| eencae | 0:32827ba075c9 | 53 | |
| eencae | 0:32827ba075c9 | 54 | int state = 2; // start with cars green |
| eencae | 0:32827ba075c9 | 55 | |
| eencae | 0:32827ba075c9 | 56 | while(1) { |
| eencae | 0:32827ba075c9 | 57 | output = fsm[state].output; // set ouput depending on current state |
| eencae | 0:32827ba075c9 | 58 | wait(fsm[state].time); // wait in that state for desired time |
| eencae | 0:32827ba075c9 | 59 | state = fsm[state].nextState[input]; // read input (BusIn) and update curent state |
| eencae | 0:32827ba075c9 | 60 | } |
| eencae | 0:32827ba075c9 | 61 | } |