State analysis
Described by the topic, there are only two different events, one is vehicle entry, one is vehicle departure, and the corresponding sensor signal, landing bar signal, the signal is changed together, so can be simplified into two composite states.
Code implementation
Sensor header File
////Created by Zhuhaihao on 2016/12/4.//#ifndef Statemachine_statemachine_h#defineStatemachine_statemachine_hclassSensor { Public: Sensor (); Sensor (BOOLState ); BOOLGetState ()Const; voidSetState (BOOLnewstate); voidrestore ();Private: //here, a state is used to indicate that the vehicle enters the sensor signal and the vehicle leaves the sensor signal.//If True indicates vehicle entry false indicates the vehicle is leaving BOOLState ;};#endif //Statemachine_statemachine_h
Sensor implementation
////Created by Zhuhaihao on 2016/12/4.//#include".. /header/sensor.h"#include<iostream>using namespacestd; Sensor::sensor () { state=false;} Sensor::sensor (BOOLState ) { This->state =State ;}BOOLSensor::getstate ()Const { return This-State ;}voidSensor::setstate (BOOLnewstate) { This->state =newstate;}voidSensor::restore () { This->state =false;}
The code for the remaining parts is similar to the sensor. All the code here https://github.com/hacktw/RTCSD2016
////Created by Zhuhaihao on 2016/12/4.//#include".. /header/gurdsystem.h"#include<iostream>using namespacestd;voidGurdsystem::carin () { This->sensor.setstate (true); cout<<"Now the car is comming in ..."<<Endl; if( ! This-lifter.getstate ()) { This-lifter.action (); cout<<"Lifter up ..."<<Endl; } if( ! This-light.getstate ()) { This-light.action (); cout<<"Light Green ..."<<Endl; }}voidgurdsystem::carout () { This->sensor.setstate (false); cout<<"Now the car is comming out ..."<<Endl; if( This-lifter.getstate ()) { This-lifter.action (); cout<<"Lifter down ..."<<Endl; } if( This-light.getstate ()) { This-light.action (); cout<<"Light Red ..."<<Endl; }}
Test
Real-time control software design the second week of work