0

最好和最简单(最简单)的方法是什么..?

我正在用 C 或 Capel(Vector CANoe)编写代码 我在 Vector CANoe 中设计的面板上有一个开关,可以打开和关闭电机(0 == 关闭和 1 == 打开),我想打开电机达到极限时关闭..以防止电机烧毁!(我们知道当 a 传感器表示已达到限制时它已达到其限制(传感器 = 1 表示已达到限制,0 表示未达到)

我该怎么做?

我想做这样的事情

WHILE (clutchMotorPower==1 & sensorClutchDisengaged !=1) 
break; 
else clutchMotorPower==0 

WHILE (clutchMotorPower==1 & sensorClutchEngaged !=1) 
break;     
else clutchMotorPower==0
4

1 回答 1

2

我对您正在使用的系统了解不多,所以这将是一些基本代码。无论如何,让我们一步一步来。

假设:

  • button是一个变量,如果当前按下按钮,则为 1,否则为 0。
  • motor是一个变量,我们将其设置为 1 以打开电机,或设置为 0 以将其关闭。
  • sensor是一个变量,当传感器被激活时为 1,否则为 0

首先,我们需要在按下按钮时切换电机状态的代码。(假设所有代码示例都在从主循环调用的函数中)

//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
    //(to differentiate between button being held and it being released and pressed again)
if(!button) {
    bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
    if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
        bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
        bActivateMotor = 1; //we should be running the motor
    }
    else {
        motor = 0;
    }
}
if(bActivateMotor) { //not else because could be activated this loop
    if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
        bButtonHeld = 1;
        bActivateMotor = 0;
    }
    else {
        motor = 1;
    }
}

现在,下一部分是在传感器激活时停止电机:

//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
    //(to differentiate between button being held and it being released and pressed again)
if(!button) {
    bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
    if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
        bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
        bActivateMotor = 1; //we should be running the motor
    }
    else {
        motor = 0;
    }
}
if(bActivateMotor) { //not else because could be activated this loop
    if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
        bButtonHeld = 1;
        bActivateMotor = 0;
    }
    /////////////////////
    // Added Code Here //
    /////////////////////
    else if(sensor) {
        bActivateMotor = 0; //motor will shut off next loop
    }
    else {
        motor = 1;
    }
}

由于缺乏关于您的代码的细节,很难准确地弄清楚您可能遇到的困难,但希望上面的算法将是一个很好的起点。

于 2013-06-06T20:15:34.927 回答