Você está na página 1de 22

Programming embedded systems Seminar 6

Multi-state systems
Dr. Tran Thanh Hung Department of Automation Technology, College of Engineering, Can Tho University Email: tthung@ctu.edu.vn

Review
In the seminar 5, we have used EOS to implement periodic functions for one state system.

In this seminar, we will use EOS to implement multi-state systems.

Outline
How to implement for multi-state systems? multi-state systems implement for multi-state systems: type 1 implement for multi-state systems: type 2

Seminar objectives

At the end of this seminar, by referring the lecture notes, students will be able to: understand issue of multi-state systems implement multi-state systems, using EOS

What are multi-state systems?


Example of multi-state system: Traffic light system

3 state system

4 state system

What are multi-state systems?


Example of multi-state system: Washing machine

Types of multi-state systems


Type 1: Multi-state (timed) system: The transition between states will depend only on the passage of time. - Example: Traffic light system Type 2: Multi-state (input/timed) system: The transition between states will depend both on the passage of time and system inputs. - Example: Washing machine Type 3: Multi-state (input) system: The transition between states will depend only on system inputs.

How to implement multi-state systems?


You are going to write a program for a traffic light system, operating on the following states: State 1 (RED): Turn Red light on for 10 seconds and go to State 2

State 2 (GREEN): Turn Green light on for 15 seconds and go to State 3


State 3 (YELLOW): Turn Yellow light on for 3 seconds and go to State 1

EOS: Review
#include "Main.h" #include "EOS.h" void main (void) { EOS_init(50000); //Initialize EOS to interrupt every 50ms while(1) { } } #pragma vector = TIMERA0_VECTOR interrupt void EOS_ISR(void) LPM1; // Enter low power mode to save power

{
// Put your code here }

Implement multi-state systems


Represent the states
typedef enum {RED,GREEN,YELLOW} Light_State; static Light_State current_state = RED; //initial state = RED

Define duration for the states


#define RED_DURATION 10 #define GREEN_DURATION 15 #define YELLOW _DURATION 3

Define global variables to measure time


static unsigned char time_in_state = 0; // time (s) in each state

Implement multi-state systems


Update states
//This function must be called every 1s by EOS_ISR void TRAFFIC_LIGHTS_Update(void) { switch (current_state) { case RED: { turn on Red light; turn off other lights; if (++time_in_state == RED_DURATION) { current_state = GREEN; //change state time_in_state = 0; } break; }

Implement multi-state systems


case GREEN : { turn on Green light; ; turn off other lights; break; } case YELLOW : { turn on Yellow light ; turn off other lights; break; } } }

Exercise 6.1
Write a program to control a complete traffic light system with states and hardware configuration as following:

Type 2 multi-state systems

Type 2 multi-state systems


Operating description of the system: 1. The user selects a wash program on the selector dial. 2. The user presses the Start switch. 3. The door lock is engaged. 4. The water valve is opened to allow water into the wash drum. 5. If the wash program involves detergent, the detergent hatch is opened. When the detergent has been released, the detergent hatch is closed. 6. When the full water level is sensed, the water valve is closed.

Type 2 multi-state systems


7. If the wash program involves warm water, the water heater is switched on. When the water reaches the correct temperature, the water heater is switched off. 8. The washer motor is turned on to rotate the drum. At the end of the wash cycle, the motor is stopped. 9. The pump is switched on to drain the drum. When the drum is empty, the pump is switched off. 10. The water valve is opened to allow water into the wash drum. 11. The washer motor is turned on to rotate the drum (to rinse detergent). 12. The pump is switched on to drain the drum.

Type 2 multi-state systems

Type 2 multi-state systems


typedef enum {INIT, START, FILL1, HEAT,WASH,DRAIN1,FILL2, RINSE, DRAIN2, ERROR} System_state; #define MAX_FILL_DURATION 1000 #define MAX_HEAT_DURATION 1000 #define MAX_DRAIN_DURATION 1000 static System_state current_state = INIT; //initial state static unsigned char Detergent[10] = {1,1,1,0,0,1,0,1,1,0}; static unsigned char Heat[10] = {1,1,1,0,0,1,0,1,1,0}; static unsigned char wash_program = 0; static unsigned char time_in_state = 0; // time (s) in each state

Type 2 multi-state systems


void WASHER_Update(void) // Call once per second by EOS_ISR { switch (current_state) { case INIT: { Display the state on LEDs; turn all motor, valve off wash_program = read Selector Dial (SWs); //0-7 Display the wash_program user chosen on LEDs; if (Start SW pressed) current_state = START; break; }

Type 2 multi-state systems


case START: { Display the state on LEDs; lock the door; open the valve to fill water; if (Detergent[wash_program] ==1) open Detergent Hatch; current_state = FILL1; Time_in_state = 0; break; }

Type 2 multi-state systems


case FILL1 : { Display the state on LEDs; if (++Time_in_state >= MAX_FILL_DURATION) current_state = ERROR; if (Drum is full water) { close the valve; if (Heat[Program] == 1) // Does program require hot water? { Turn on Heater; current_state = HEAT;Time_in_state = 0;} else { current_state = WASH;Time_in_state = 0;} } break; }

Exercise 6.2
Write a program to control a washing machine. You decide the hardware configuration

Você também pode gostar