停车场门禁系统状态机

 

一辆汽车的通过流程为:

  • 起落杆处于落下状态,通行灯为红灯。
  • 汽车进入门禁系统,入闸传感器值变为True。
  • 控制起落杆上升,直到起落杆位置传感器到达升起位置。
  • 通行灯为绿灯。
  • 汽车离开门禁,触发汽车出闸传感器值为True。
  • 控制起落杆下降,直到起落杆位置传感器到达落下位置。
  • 通行灯变为红灯。

所描述的控制系统的状态机包括:

  • 状态机的所有状态
  • 状态机所接收到的外部事件
  • 状态机所产生的动作
  • 状态机的所有状态跃迁:(原状态、新状态、触发条件、产生动作)

状态机的状态分析:

  根据工作条件和形式,状态机总共有四种形态:杆停在下方,杆由下往上,杆停在上方,杆由上往下。这四种工作状态循环进行。  

(1=true,0=false

状态

上传感器

下传感器

入闸

出闸

停在下面

0

1

0

0

由下往上

0

0

1

0

停在上面

1

0

0

0

由下往上

0

0

0

1

 

 

 

 

 

 

 

 

状态机接受到的外部事件有:  

 1.入闸传感器

 2.出闸传感器

 3. 起落杆上传感器

 4. 起落杆下传感器

状态机的动作:

  1. 红绿灯的切换

  2. 起落杆的升降

所有状态的越迁:

   

 

C++代码模拟:

#include <iostream>

using namespace std;

string state;
bool car_in, car_out;
bool light_red, light_green;
bool sensor_up, sensor_down;

/**
 *
 * state:           car_in  car_out sensor_up   sensor_down light_red   light_green
 * down                 0       0       0           1           1               0
 * down2up              1       0       0           0           1               0
 * up                   0       0       1           0           0               1
 * up2down              0       1       0           0           1               0
 *
 * ps:the original state is set to down
 */


/*
 * recover to the original state, namely the down state
 */
void ncr() {
    state = "down";
    car_in = 0;
    car_out = 0;
    sensor_down = 1;
    sensor_up = 0;
    light_green = 0;
    light_red = 1;
}

void read(string s) {
    if (s == "in") {
        car_in = 1;
        car_out = 0;
    }

    if (s == "out") {
        car_in = 0;
        car_out = 1;
    }
}

void change_state() {
    if (car_in) {
        if (sensor_down) {
            //current state is down and should change to state down2up
            cout << "The Gan is going up, wait for a second " << endl;
            cout << "Current state is down2up" << endl;
            cout << "Current state is up" << endl;
            state = "up";
        }

    }

    if (car_out) {
        if (sensor_up) {
            cout << "The Gan is going down, wait for a secong " << endl;
            cout << "Current state is up2down" << endl;
            cout << "Current state is down" << endl;
            state = "down";
        }

    }
}



int main() {
    string sensor_signal;
    ncr();
    while (1) {
        cout << "Current state is " << state << endl;
        cout << "******Waiting for the sensor signal (in/out?)" << endl;
        cin >> sensor_signal;
        read(sensor_signal);
        change_state();
    }

}

运行截图:

PS:这个程序只是简单的模拟系统的运行,在内部采取了简化过程,并且只是考虑一次通过一量车的情况,对于多辆车连续通过的情况没有考虑,接下俩会进一步完善逻辑控制。

 

posted on 2016-12-07 11:41  QingLiu2016  阅读(813)  评论(1)    收藏  举报

导航