SlideShare a Scribd company logo
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 hệ thống nhiều trạng thái

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: hệ thống nhiều trang thái

•   implement for multi-state systems: type 1

•   implement for multi-state systems: type 2 viết được chương trình
    thực thi hệ thống nhiều trạng thái
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: hệ thống có nhiều trạng
  thái khác nhau
• Traffic light system




       3 state system
                                        4 state system
What are multi-state systems?

Example of multi-state system:
• Washing machine máy giặc
                    Bắt đầu    Nước nóng
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. Ngoài thời gian còn phụ thuộc 1 số điều kiện ngõ vào- máy giặc
-   Example: Washing machine
•    Type 3: Multi-state (input) system:
     The transition between states will depend only on system inputs.
-    Chuyện trạng thái này sang trạng thái kia phụ thuộc đk ngõ vào
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: bật đèn đỏ trong 10s
− State 2 (GREEN): Turn Green light on for 15 seconds and go to
  State 3 bật đèn xanh trong 15s
− State 3 (YELLOW): Turn Yellow light on for 3 seconds and go to
  State 1 bật đèn vàng trong 3s
EOS: Review
#include "Main.h"
#include "EOS.h"
void main (void)
{   EOS_init(50000); //Initialize EOS to interrupt every 50ms
    while(1)
    {      LPM1; // Enter low power mode to save power ngủ
    }
}
#pragma vector = TIMERA0_VECTOR
interrupt void EOS_ISR(void)
{
    // Put your code here hàm phục vụ ngắt, viết chương trình, hàm riêng, file riêng
    Gọi 1 giây 1 lần , kt 20 lan
    …
}
Implement multi-state systems
•   Represent the states                Tên kiểu
                 0     1        2
typedef enum {RED,GREEN,YELLOW} Light_State; khai báo kiểu liệt kê
static Light_State current_state = RED; //initial state = RED
• Define duration for the states
#define RED_DURATION 10 // time đen đỏ
                                  ̀
                                     ̀
#define GREEN_DURATION 15 // time đen xanh
                                        ̀ ̀
#define YELLOW _DURATION 3 // time đen vang
• Define global variables to measure time
static unsigned int time_in_state = 0; // time (s) in each state

Ghi chú: Đặt trong file TRAFFIC_LIGHTS_Update.c
Implement multi-state systems
•   Update states hàm cập nhật trạng thái và công việc mỗi trạng thái
//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;// bật đèn đỏ, tắt đèn vàng
             if (++time_in_state == RED_DURATION) // đã đến time chuyển
    trạng thái hay chưa, kiểm tra cho đến khi
                      { current_state = GREEN; //change state
                        time_in_state = 0;
                      }
             break;// thoát ra ngủ
           }
Ghi chú: Đặt trong file TRAFFIC_LIGHTS_Update.c
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

Ngõ vào


        Bắt đầu


        Chọn


            Mức nước


            Nhiệt độ
                                      Led hiển thị



                                Chứa bột gặt
Type 2 multi-state systems

Operating description of the washing machine:
1. The user selects a wash program on the selector dial or keypad.// bắm
   nút chọn chế đọ giặc 10 phím
2. The user presses the ‘Start’ switch. Ng sử dụng bấm star
3. The door lock is engaged.// nước chay ra
4. The water valve is opened to allow water into the wash drum.// if cần bột
   giặc sẽ mở ra
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.// mức
   nước đòng
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.// nếu cần nóng, thì mở và tắt đử lớn
8. The washer motor is turned on to rotate the drum. At the end of the wash
    cycle, the motor is stopped.// quay lòng giặc
9. The pump is switched on to drain the drum. When the drum is empty, the
    pump is switched off.// bơm nước sau khi giặc
10. The water valve is opened to allow water into the wash drum.// mở nước
11. The washer motor is turned on to rotate the drum (to rinse detergent).//
    quay giặc
12. The pump is switched on to drain the drum… bơm nươć ra
Type 2 multi-state systems
10 trạng thái


                   Khởi động
Cấm điện

             Quá time mà chưa được, báo lỗi

                                                   giặc




             Xả
Type 2 multi-state systems

typedef enum {INIT, START, FILL1, HEAT,WASH,DRAIN1,FILL2, RINSE,
   DRAIN2, ERROR} System_state;// khai báo kiểu dl liệt kê

#define MAX_FILL_DURATION 1000// xa nước tôi đa
                                            ́
#define MAX_HEAT_DURATION 1000// đun nươć tôi đa
                                               ́
#define MAX_DRAIN_DURATION 1000// chơ nươc đây   ̀

static System_state current_state = INIT; //initial state khai báo gắn gt ban
    đầu
static unsigned char Detergent [10] = {1,1,1,0,0,1,0,1,1,0};// man chưa số nhị
                                                                  ̃
    phân, tương ưng 10 chế độ giăc
                                   ̣
static unsigned char Heat[10] = {1,1,1,0,0,1,0,1,1,0};// nươc nong
                                                                ́
static unsigned char wash_program = 0;//chương trình giặc chọn
static unsigned char time_in_state = 0; // time (s) in each state// đếm time
    trong trạng thái
Type 2 multi-state systems

void WASHER_Update(void) // Call once per second by EOS_ISR// diều khiển và
     cập nhật trạng tahsi hệ thống// hệ thống loại 3
{
     switch (current_state)
     {
     case INIT:
             { Display the state on LEDs;// hiển thị trang thái hiện tại lên led
             p1out = current_state
               turn all motor, valve off// tắt all motor và valve đi
//kif eyscan 0-9{
               wash_program = read keypad (SWs); //0-9// đọc phím kt chọn chế độ giặc}
               Display the wash_program user chosen on LEDs;//                         if
     (Start SW pressed) current_state = START;// kt start có đc bấm ko, chuyển sang
     trạng thái start, ko phụ thuộc time
             break;
             }
}
Type 2 multi-state systems

case START:
      { Display the state on LEDs;// hiển led có phải 1//Pour =C
                  lock the door;// khóa cửa mấy giậc -> khỏi làm
        open the valve to fill water;// mở valve nước
        if (Detergent[wash_program] ==1) // xét mãn với chế độ
giặc
                open Detergent Hatch;// mở hộp bột giặc
      current_state = FILL1;// đặt trạng thái 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)// kt time danh cho time đổ
nước vô
                   current_state = ERROR;
      if (Drum is full water) // nước đầy hay chưa
        { close the valve;
          if (Heat[wash_program] == 1) // Does program require hot water?// có cần
nướ nóng hay ko có =1 hay ko
                   { Turn on Heater;/ bật bộ đun nước lên
                     current_state = HEAT;Time_in_state = 0
else
                   { current_state = WASH;Time_in_state = 0;} ;}// vào thẳng chế độ
giặc

        }
      break;
      }
ĐỒ ÁN

•  Viết chương trình cho máy giặt, theo mô tả trên
Quy ước: Dùng
- phím SW15 làm nút Start
- SW12 cam bien bao het nuoc, chuyen qua trang thai khac
- phím SW10 làm cảm biến mức nước (bấm = nước đầy), bấm vào là báo hệ thống
   đầy
- phím SW11 làm cảm biến nhiệt độ (bấm = nhiệt độ đạt yêu cầu), bấm thì nhiệt độ
   đã đạt
- LED ở P1.7 làm van nước, mở van nước, đèn tắt van đóng
- LED ở P1.6 làm hộp đựng bột giặt, sáng, hộp đựng bột giạc đã có
- LED ở P1.5 làm bộ đun nước
- LED ở P1.4 làm motor lồng giặt
- LED ở P1.3 làm bơm nước
- Trang thai error p1.0 p1.1 p1.2
- Trang thai giat 20s

More Related Content

Similar to Seminar 6

rtos by mohit
rtos by mohitrtos by mohit
rtos by mohit
Mohit Kumar
 
101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot
Acácio Oliveira
 
Flow of control c++
Flow of control c++Flow of control c++
Flow of control c++
Arpit Meena
 
Systemd cheatsheet
Systemd cheatsheetSystemd cheatsheet
Systemd cheatsheet
Susant Sahani
 
NRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxNRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptx
KRIPABHARDWAJ1
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
Mavoori Soshmitha
 
DISTRIBUTED COMPUTTING (snapshot).pptx
DISTRIBUTED COMPUTTING (snapshot).pptxDISTRIBUTED COMPUTTING (snapshot).pptx
DISTRIBUTED COMPUTTING (snapshot).pptx
950621104027GODWINJ
 
test1_361
test1_361test1_361
Operating System
Operating SystemOperating System
Operating System
GowriLatha1
 
Advanced insrumentation lab manual
Advanced insrumentation lab manualAdvanced insrumentation lab manual
Advanced insrumentation lab manual
Gautam sai teza
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
Master-trol | Electronic Water Management System
Master-trol | Electronic Water Management SystemMaster-trol | Electronic Water Management System
Master-trol | Electronic Water Management System
Morris Group International
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
JavvajiVenkat
 
52bf066dbfcc4d739fa99d255dba287a.pptx
52bf066dbfcc4d739fa99d255dba287a.pptx52bf066dbfcc4d739fa99d255dba287a.pptx
52bf066dbfcc4d739fa99d255dba287a.pptx
11SnehlataGujar
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
ayshasafdarwaada
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
1230 Rtf Final
1230 Rtf Final1230 Rtf Final
1230 Rtf Final
luisotaviomedici
 
Boot prom basics
Boot prom basicsBoot prom basics
Boot prom basics
Ganesh Kumar Veerla
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).ppt
ssuserf67e3a
 
Form 1 Term 1 Week 8.1
Form 1   Term 1  Week 8.1Form 1   Term 1  Week 8.1
Form 1 Term 1 Week 8.1
Harley Greenberg
 

Similar to Seminar 6 (20)

rtos by mohit
rtos by mohitrtos by mohit
rtos by mohit
 
101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot101 1.3 runlevels , shutdown, and reboot
101 1.3 runlevels , shutdown, and reboot
 
Flow of control c++
Flow of control c++Flow of control c++
Flow of control c++
 
Systemd cheatsheet
Systemd cheatsheetSystemd cheatsheet
Systemd cheatsheet
 
NRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxNRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptx
 
Ppt on java basics1
Ppt on java basics1Ppt on java basics1
Ppt on java basics1
 
DISTRIBUTED COMPUTTING (snapshot).pptx
DISTRIBUTED COMPUTTING (snapshot).pptxDISTRIBUTED COMPUTTING (snapshot).pptx
DISTRIBUTED COMPUTTING (snapshot).pptx
 
test1_361
test1_361test1_361
test1_361
 
Operating System
Operating SystemOperating System
Operating System
 
Advanced insrumentation lab manual
Advanced insrumentation lab manualAdvanced insrumentation lab manual
Advanced insrumentation lab manual
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
Master-trol | Electronic Water Management System
Master-trol | Electronic Water Management SystemMaster-trol | Electronic Water Management System
Master-trol | Electronic Water Management System
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
52bf066dbfcc4d739fa99d255dba287a.pptx
52bf066dbfcc4d739fa99d255dba287a.pptx52bf066dbfcc4d739fa99d255dba287a.pptx
52bf066dbfcc4d739fa99d255dba287a.pptx
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
1230 Rtf Final
1230 Rtf Final1230 Rtf Final
1230 Rtf Final
 
Boot prom basics
Boot prom basicsBoot prom basics
Boot prom basics
 
Lecture18-19 (1).ppt
Lecture18-19 (1).pptLecture18-19 (1).ppt
Lecture18-19 (1).ppt
 
Form 1 Term 1 Week 8.1
Form 1   Term 1  Week 8.1Form 1   Term 1  Week 8.1
Form 1 Term 1 Week 8.1
 

Seminar 6

  • 1. 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
  • 2. Review hệ thống nhiều trạng thái 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.
  • 3. Outline How to implement for multi-state systems? • multi-state systems: hệ thống nhiều trang thái • implement for multi-state systems: type 1 • implement for multi-state systems: type 2 viết được chương trình thực thi hệ thống nhiều trạng thái
  • 4. 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
  • 5. What are multi-state systems? Example of multi-state system: hệ thống có nhiều trạng thái khác nhau • Traffic light system 3 state system 4 state system
  • 6. What are multi-state systems? Example of multi-state system: • Washing machine máy giặc Bắt đầu Nước nóng
  • 7. 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. Ngoài thời gian còn phụ thuộc 1 số điều kiện ngõ vào- máy giặc - Example: Washing machine • Type 3: Multi-state (input) system: The transition between states will depend only on system inputs. - Chuyện trạng thái này sang trạng thái kia phụ thuộc đk ngõ vào
  • 8. 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: bật đèn đỏ trong 10s − State 2 (GREEN): Turn Green light on for 15 seconds and go to State 3 bật đèn xanh trong 15s − State 3 (YELLOW): Turn Yellow light on for 3 seconds and go to State 1 bật đèn vàng trong 3s
  • 9. EOS: Review #include "Main.h" #include "EOS.h" void main (void) { EOS_init(50000); //Initialize EOS to interrupt every 50ms while(1) { LPM1; // Enter low power mode to save power ngủ } } #pragma vector = TIMERA0_VECTOR interrupt void EOS_ISR(void) { // Put your code here hàm phục vụ ngắt, viết chương trình, hàm riêng, file riêng Gọi 1 giây 1 lần , kt 20 lan … }
  • 10. Implement multi-state systems • Represent the states Tên kiểu 0 1 2 typedef enum {RED,GREEN,YELLOW} Light_State; khai báo kiểu liệt kê static Light_State current_state = RED; //initial state = RED • Define duration for the states #define RED_DURATION 10 // time đen đỏ ̀ ̀ #define GREEN_DURATION 15 // time đen xanh ̀ ̀ #define YELLOW _DURATION 3 // time đen vang • Define global variables to measure time static unsigned int time_in_state = 0; // time (s) in each state Ghi chú: Đặt trong file TRAFFIC_LIGHTS_Update.c
  • 11. Implement multi-state systems • Update states hàm cập nhật trạng thái và công việc mỗi trạng thái //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;// bật đèn đỏ, tắt đèn vàng if (++time_in_state == RED_DURATION) // đã đến time chuyển trạng thái hay chưa, kiểm tra cho đến khi { current_state = GREEN; //change state time_in_state = 0; } break;// thoát ra ngủ } Ghi chú: Đặt trong file TRAFFIC_LIGHTS_Update.c
  • 12. 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; } } }
  • 13. Exercise 6.1 • Write a program to control a complete traffic light system with states and hardware configuration as following:
  • 14. Type 2 multi-state systems Ngõ vào Bắt đầu Chọn Mức nước Nhiệt độ Led hiển thị Chứa bột gặt
  • 15. Type 2 multi-state systems Operating description of the washing machine: 1. The user selects a wash program on the selector dial or keypad.// bắm nút chọn chế đọ giặc 10 phím 2. The user presses the ‘Start’ switch. Ng sử dụng bấm star 3. The door lock is engaged.// nước chay ra 4. The water valve is opened to allow water into the wash drum.// if cần bột giặc sẽ mở ra 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.// mức nước đòng
  • 16. 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.// nếu cần nóng, thì mở và tắt đử lớn 8. The washer motor is turned on to rotate the drum. At the end of the wash cycle, the motor is stopped.// quay lòng giặc 9. The pump is switched on to drain the drum. When the drum is empty, the pump is switched off.// bơm nước sau khi giặc 10. The water valve is opened to allow water into the wash drum.// mở nước 11. The washer motor is turned on to rotate the drum (to rinse detergent).// quay giặc 12. The pump is switched on to drain the drum… bơm nươć ra
  • 17. Type 2 multi-state systems 10 trạng thái Khởi động Cấm điện Quá time mà chưa được, báo lỗi giặc Xả
  • 18. Type 2 multi-state systems typedef enum {INIT, START, FILL1, HEAT,WASH,DRAIN1,FILL2, RINSE, DRAIN2, ERROR} System_state;// khai báo kiểu dl liệt kê #define MAX_FILL_DURATION 1000// xa nước tôi đa ́ #define MAX_HEAT_DURATION 1000// đun nươć tôi đa ́ #define MAX_DRAIN_DURATION 1000// chơ nươc đây ̀ static System_state current_state = INIT; //initial state khai báo gắn gt ban đầu static unsigned char Detergent [10] = {1,1,1,0,0,1,0,1,1,0};// man chưa số nhị ̃ phân, tương ưng 10 chế độ giăc ̣ static unsigned char Heat[10] = {1,1,1,0,0,1,0,1,1,0};// nươc nong ́ static unsigned char wash_program = 0;//chương trình giặc chọn static unsigned char time_in_state = 0; // time (s) in each state// đếm time trong trạng thái
  • 19. Type 2 multi-state systems void WASHER_Update(void) // Call once per second by EOS_ISR// diều khiển và cập nhật trạng tahsi hệ thống// hệ thống loại 3 { switch (current_state) { case INIT: { Display the state on LEDs;// hiển thị trang thái hiện tại lên led p1out = current_state turn all motor, valve off// tắt all motor và valve đi //kif eyscan 0-9{ wash_program = read keypad (SWs); //0-9// đọc phím kt chọn chế độ giặc} Display the wash_program user chosen on LEDs;// if (Start SW pressed) current_state = START;// kt start có đc bấm ko, chuyển sang trạng thái start, ko phụ thuộc time break; } }
  • 20. Type 2 multi-state systems case START: { Display the state on LEDs;// hiển led có phải 1//Pour =C lock the door;// khóa cửa mấy giậc -> khỏi làm open the valve to fill water;// mở valve nước if (Detergent[wash_program] ==1) // xét mãn với chế độ giặc open Detergent Hatch;// mở hộp bột giặc current_state = FILL1;// đặt trạng thái fill1 Time_in_state = 0;// break; }
  • 21. Type 2 multi-state systems case FILL1 : { Display the state on LEDs;// if (++Time_in_state >= MAX_FILL_DURATION)// kt time danh cho time đổ nước vô current_state = ERROR; if (Drum is full water) // nước đầy hay chưa { close the valve; if (Heat[wash_program] == 1) // Does program require hot water?// có cần nướ nóng hay ko có =1 hay ko { Turn on Heater;/ bật bộ đun nước lên current_state = HEAT;Time_in_state = 0 else { current_state = WASH;Time_in_state = 0;} ;}// vào thẳng chế độ giặc } break; }
  • 22. ĐỒ ÁN • Viết chương trình cho máy giặt, theo mô tả trên Quy ước: Dùng - phím SW15 làm nút Start - SW12 cam bien bao het nuoc, chuyen qua trang thai khac - phím SW10 làm cảm biến mức nước (bấm = nước đầy), bấm vào là báo hệ thống đầy - phím SW11 làm cảm biến nhiệt độ (bấm = nhiệt độ đạt yêu cầu), bấm thì nhiệt độ đã đạt - LED ở P1.7 làm van nước, mở van nước, đèn tắt van đóng - LED ở P1.6 làm hộp đựng bột giặt, sáng, hộp đựng bột giạc đã có - LED ở P1.5 làm bộ đun nước - LED ở P1.4 làm motor lồng giặt - LED ở P1.3 làm bơm nước - Trang thai error p1.0 p1.1 p1.2 - Trang thai giat 20s

Editor's Notes

  1. Đặt trong file washer.c