首页 > 代码库 > 《实时控制软件设计》第二周作业

《实时控制软件设计》第二周作业

 状态分析

由题目描述,只有两种不同事件,一个是车辆进入,一个是车辆离开,与之对应的传感器信号,起落杆信号,通行灯信号都是一同变化,因此可以简化成两种复合状态。

 

代码实现

 

传感器头文件

//
// Created by zhuhaihao on 2016/12/4.
//

#ifndef STATEMACHINE_STATEMACHINE_H
#define STATEMACHINE_STATEMACHINE_H

class Sensor {
public:
    Sensor();

    Sensor(bool state);

    bool getState() const;

    void setState(bool newState) ;

    void restore() ;

private:

    // 这里用一个状态表示车辆进入传感器信号和车辆离开传感器信号的状态
    // 如果为true表示车辆进入 false表示车辆离开
    bool state;
};
#endif //STATEMACHINE_STATEMACHINE_H

 

传感器实现

//
// Created by zhuhaihao on 2016/12/4.
//

#include "../Header/Sensor.h"
#include <iostream>

using  namespace std;

Sensor::Sensor() {
    state = false;
}

Sensor::Sensor(bool state) {
    this->state = state;
}

bool Sensor::getState() const {
    return this->state;
}

void Sensor::setState(bool newState) {
    this->state = newState;
}

void Sensor::restore() {
    this->state = false;
}

 

其余部件的代码类似于传感器。所有代码在这里  https://github.com/hacktw/RTCSD2016

 

//
// Created by zhuhaihao on 2016/12/4.
//

#include "../Header/GurdSystem.h"
#include <iostream>

using namespace std;

void GurdSystem::carIn() {
    this->sensor.setState(true);
    cout << "now the car are 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;
    }
}

void GurdSystem::carOut() {
    this->sensor.setState(false);
    cout << "now the car are 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;
    }
}

 

测试截图

 

技术分享

《实时控制软件设计》第二周作业