【问题标题】:PLC function block library in C++C++ 中的 PLC 功能块库
【发布时间】:2019-12-02 06:10:36
【问题描述】:

我一直在用 C++ 开发一个 PLC 库。该库应包含 其他多路复用器块。多路复用器从四个输入中选择一个输入 基于两个控制逻辑信号的值:

control_01 | control_02 | output
----------------------------------
    0      |     0      | input_01
    1      |     0      | input_02
    0      |     1      | input_03
    1      |     1      | input_04

该库将包含比多路复用器块更多的块,例如pid控制器, 滤波器、信号发生器等。我决定为所有这些块声明统一接口:

namespace ControlBlocks
{

class ControlBlk{
public:

    virtual void Update(void) = 0;

private:

};

}

多路复用器通过以下方式实现该接口:

界面:

namespace ControlBlocks
{

class Mux : public ControlBlk{
public:
    Mux(uint32_t* const bitsArray, const uint32_t control_01, const uint32_t control_02,
        float* const input_01, float* const input_02, float* const input_03, float* const input_04,
        float* const output);
    virtual ~Mux();

    void Update(void);

private:

    uint32_t* m_BitsArray;
    uint32_t  m_Control01;
    uint32_t  m_Control02;
    float*    m_Input01;
    float*    m_Input02;
    float*    m_Input03;
    float*    m_Input04;
    float*    m_Output;

    int8_t GetControlValue(uint32_t* const bitsArray, const uint32_t control_01, const uint32_t control_02);

};

}

实施:

ControlBlocks::Mux::Mux(uint32_t* const bitsArray,
                        const uint32_t control_01, const uint32_t control_02,
                        float* const input_01, float* const input_02, float* const input_03, float* const input_04,
                        float* const output):
                        m_BitsArray{bitsArray},
                        m_Control01{control_01},
                        m_Control02{control_02},
                        m_Input01{input_01},
                        m_Input02{input_02},
                        m_Input03{input_03},
                        m_Input04{input_04},
                        m_Output{output}{

}

ControlBlocks::Mux::~Mux() {
    // TODO Auto-generated destructor stub
}

void ControlBlocks::Mux::Update(void){

    uint8_t controlValue = GetControlValue(m_BitsArray, m_Control01, m_Control02);

    switch(controlValue){

        case 0:
            *m_Output = *m_Input01;
        break;

        case 1:
            *m_Output = *m_Input02;
        break;

        case 2:
            *m_Output = *m_Input03;
        break;

        case 3:
            *m_Output = *m_Input04;
        break;

    }

}

float ControlBlocks::Mux::GetOutput(void){
    return *m_Output;
}

int8_t ControlBlocks::Mux::GetControlValue(uint32_t* const bitsArray, const uint32_t control_01, const uint32_t control_02){

    uint8_t controlValue = 0;

    if(Utils::TestBitSet(bitsArray, control_01)){
        controlValue += 1;
    }

    if(Utils::TestBitSet(bitsArray, control_02)){
        controlValue += 2;
    }

    return controlValue;

}

为了完整性:

bool Utils::TestBitSet(uint32_t *bitsArray, uint32_t bit){
    uint32_t wordValue    = *(bitsArray + (bit >> 5));
    uint32_t bitPosInWord = (bit - ((bit >> 5) << 5));

    return ((wordValue & ((uint32_t)1 << bitPosInWord)) >> bitPosInWord) ? true : false;
}

问题是多路复用器接收到指向输入的指针 可与其他块互连。但在某些情况下,我需要 将常量(即编译时已知的常量值)传递给多路复用器。 不幸的是,我不知道如何解决这个问题。有谁知道如何 传递指向变量的指针以及编译时常量?

【问题讨论】:

    标签: c++ embedded plc


    【解决方案1】:

    在函数中使用 const 引用,并在调用函数时传递延迟指针或 const 值:

    #include <iostream>
    
    void f(const float& value) {
        std::cout << value << std::endl;
    }
    
    int main() {
        float a = 5.2;
        float* p = new float(4.3);
        const float c = 2.1;
        constexpr float cc = 1.2;
        f(a);
        f(*p);
        f(c);
        f(cc);
        f(5.1);
        delete p;
    }
    

    【讨论】:

    • 感谢您的反应。是否有可能以某种方式传递浮点文字值?
    • 是的,当然。只需将int 替换为float
    • 对于误导性问题,我深表歉意。假设我想调用 f(5.0),即没有定义变量。
    • 在这种情况下,您可以使用 const 引用。 void f(const float&amp; ref)
    猜你喜欢
    • 2016-03-28
    • 2021-09-24
    • 2014-12-18
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多