根据本周课堂内容,设计一个停车场门禁控制系统的状态机。ios
门禁控制系统的输入信号包括:c++
门禁控制系统的输出信号包括:函数
一辆汽车的经过流程为:测试
所描述的控制系统的状态机包括:spa
module carlight( car_in , //入闸传感器信号 car_out , //出闸传感器信号 gan_up , //起落杆上方传感器信号 gan_down , //起落杆下方传感器信号 motion_up , //起落杆电机上升控制信号 motion_down , //起落杆电机降低控制信号 light_red , //红灯信号 light_green , //绿灯信号 clk , //时钟信号 ncr ); //复位信号 //输入信号 input clk , ncr ; input car_in , car_out ; input gan_up , gan_down ; //输出信号 output motion_up , motion_down ; output light_red , light_green ; //寄存器 reg [1:0] state , next_state ; reg motion_up , motion_down ; reg light_red , light_green ; //状态机四种状态 parameter [1:0] down = 2'b00 , //杆处于下方状态 down2up = 2'b01 , //杆处于上升状态 up = 2'b11 , //杆处于上方状态 up2down = 2'b10 ; //杆处于降低状态 always@(posedge clk or negedge ncr) begin if(~ncr) state <= down ; else state <= next_state ; //状态转移 end always@(*) begin case(state) down: begin //down状态时,有car_in信号,转换到down2up状态,不然,状态不变 if(car_in==1) next_state = down2up ; else next_state = down ; end down2up: begin //down2up状态时,有gan_up信号,转换到up状态,不然,状态不变 if(gan_up==1) next_state = up ; else next_state = down2up ; end up: begin //up状态时,有car_out信号,转换到up2down状态,不然,状态不变 if(car_out==1) next_state = up2down ; else next_state = up ; end up2down: begin //up2down状态时,有gan_down信号,转换到down状态,不然,状态不变 if(gan_down==1) next_state = down ; else next_state = up2down ; end default: next_state = down ; endcase end always@(posedge clk or negedge ncr) begin if(~ncr) begin motion_up = 0 ; motion_down = 0 ; light_red = 1 ; light_green = 0 ; end else begin case(state) down: begin //down状态时对应的输出 motion_up = 0 ; motion_down = 0 ; light_red = 1 ; light_green = 0 ; end down2up: begin //down2up状态时对应的输出 motion_up = 1 ; motion_down = 0 ; light_red = 1 ; light_green = 0 ; end up: begin //up状态时对应的输出 motion_up = 0 ; motion_down = 0 ; light_red = 0 ; light_green = 1 ; end up2down: begin //up2down状态时对应的输出 motion_up = 0 ; motion_down = 1 ; light_red = 0 ; light_green = 1 ; end default: begin motion_up = 0 ; motion_down = 0 ; light_red = 1 ; light_green = 0 ; end endcase end end endmodule
#include <iostream> #include <string> using namespace std; string state; bool car_in,car_out; //出入闸信号 bool gan_up,gan_down; //杆上下传感器信号 bool light_red,light_green; //红绿灯控制信号 bool motion_up,motion_down; //杆上下动做控制信号 void change_state() //状态迁移函数 { if (state=="down") { if (car_in == true) state = "down2up"; else state = "down"; } else if (state == "down2up") { if (gan_up == true) state = "up"; else state = "down2up"; } else if (state == "up") { if (car_out == true) state = "up2down"; else state = "up"; } else if (state == "up2down") { if (gan_down == true) state = "down"; else state = "up2down"; } } void action() //状态动做函数 { if (state == "down") { light_red = true; light_green = false; motion_up = false; motion_down = false; } else if (state == "down2up") { light_red = true; light_green = false; motion_up = true; motion_down = false; } else if (state == "up") { light_red = false; light_green = true; motion_up = false; motion_down = false; } else if (state == "up2down") { light_red = false; light_green = true; motion_up = false; motion_down = true; } } void ncr() //复位函数 { car_in = false; car_out = false; gan_down = false; gan_up = false; } void read(string m) //测试信号函数 { if (m=="1y") car_in = true; //汽车入闸传感器为true else if (m == "1n") car_in = false; //汽车入闸传感器为false else if (m == "2y") car_out = true; //汽车出闸传感器为true else if (m == "2n") car_out = false; //汽车出闸传感器为false else if (m == "3y") gan_up = true; //杆上方传感器为true else if (m == "3n") gan_up = false; //杆上方传感器为false else if (m == "4y") gan_down = true; //杆下方传感器为true else if (m == "4n") gan_down = false; //杆下方传感器为false } void main() //主函数 { state = "down"; //默认杆放下 string sensor; cout << "wait for car coming in..." << endl; cout << "current state is " << "down" << endl; while (1) { action(); ncr(); cout << "please input the signal" << endl; cin >> sensor; read(sensor); change_state(); cout << "current state is " << state << endl; } }
本c++代码程序是为了测试验证逻辑,因此一些信号是手动给出。首先默认当前状态为杆放下,即down状态,当有car_in信号输入时,即1y,状态变为down2up,其余信号发生改变时,状态不变。同理其余三个状态类似。测试结果符合预期要求。设计