• 实时控制软件第二周 停车场门禁控制系统的状态机的设计


    控制系统的状态机设计

    控制系统的状态

    • 等待车辆(waiting)
    • 起落杆上升(opening)
    • 等待车辆通过(passing)
    • 起落杆下降(closing)

    外部事件 输入信号

    • 入闸传感器:汽车进入 signal_in
    • 出闸传感器:汽车离开 signal_out
    • 起落杆上传感器,起落杆升到顶部 signal_up
    • 起落杆下传感器,起落杆降到底部 signal_down

    动作

    • 起落杆的生降
    • 红绿灯的变换

    状态跃迁及触发信号

    • waiting 》opening 入闸传感器信号
    • opening 》passing 起落杆上传感器 灯变位绿
    • passing 》closing 出闸传感器
    • closing 》 waiting 起落杆下传感器 灯变位红

    状态机的C++实现

    #include <iostream>
    using namespace std;
    
    enum state { waiting, opening, passing, closing };
    enum light { red,green};
    enum signal { signal_in, signal_out, signal_up, signal_down};
    
    void state_change(enum signal signal);
    
    int main()
    {
    	char c=0x00;
    	while (1)
    	{
    		c = getchar();
    		switch (c)
    		{
    		case 'i':state_change(signal_in);
    			break;
    		case 'u':state_change(signal_up);
    			break;
    		case 'o':state_change(signal_out);
    			break;
    		case 'd':state_change(signal_down);
    			break;
    		default:
    			break;
    		}
    
    	}
    
    }
    
    void state_change(enum signal signal)
    {
    	static enum state currentState = waiting;
    	static enum light l = red;
    	static string str_state="";
    	static string str_light="";
    	switch (currentState)
    	{
    	case waiting:
    		switch (signal)
    		{
    		case signal_in:
    			currentState = opening;
    			break;
    		default:
    			break;
    		}
    	case opening:
    		switch (signal)
    		{
    		case signal_up:
    			currentState = passing;
    			l = green;
    		default:
    			break;
    		}
    	case passing:
    		switch (signal)
    		{
    		case signal_out:
    			currentState = closing;
    
    		default:
    			break;
    		}
    	case closing:
    		switch (signal)
    		{
    		case signal_down:
    			currentState = waiting;
    			l = red;
    		default:
    			break;
    		}
    	default:
    		break;
    	}
    
    
    	//输出状态
    	switch (currentState)
    	{
    	case waiting:
    		str_state = "waiting";
    		break;
    	case opening:
    		str_state = "opening";
    		break;
    	case passing:
    		str_state = "passing";
    		break;
    	case closing:
    		str_state = "closing";
    		break;
    	default:
    		break;
    	}
    	switch (l)
    	{
    	case red:
    		str_light = "red";
    		break;
    	case green:
    		str_light = "green";
    		break;
    	default:
    		break;
    	}
    
    	cout << "Current state:"<< str_state.c_str() << endl;
    	cout << "Light color:" <<str_light.c_str() << endl;
    
    		
    }
    

    Written with StackEdit.

  • 相关阅读:
    window 操作
    idea使用
    安装zookeeper
    resource和autowired
    python浅见 (Python 3000)
    Tomcat服务器
    servlet
    事件是一种委托吗?什么是委托?什么是事件?
    int值类型的ToString()方法是否装箱
    抽象类,虚方法与普通类的区别
  • 原文地址:https://www.cnblogs.com/lvjw/p/6136802.html
Copyright © 2020-2023  润新知