c - 将开关设置为打开,直到达到标志,然后将开关设置为关闭

标签 c

执行此操作的最佳和最简单(最简单)的方法是什么......?

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

我该怎么做?

我想做这样的事

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

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

最佳答案

我对您正在使用的系统了解不多,所以这将是一些简单的代码。不管怎样,让我们​​一步一步地完成这个过程。

假设:

  • 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;
    }
}

由于缺乏关于您的代码的具体信息,因此很难确切地弄清楚您可能遇到的困难是什么,但希望上面的算法将是一个很好的起点。

关于c - 将开关设置为打开,直到达到标志,然后将开关设置为关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16970791/

相关文章:

c - 由两个 3 位数字的乘积组成的最大回文数。使用 c 。我的代码有什么问题吗?

c - 为什么我似乎无法向工具栏添加按钮? [WINAPI]

c++ - CMake "undefined reference to function"

php - 从 php 加载 c 库

c - 双链表显示函数中的段错误

c - 函数调用时参数不足

c - eclipse:C - 控制台窗口输入

javah -jni <classname> 不起作用?

c - sizeof((int)(float)(char)i)) 当 int 被定义时 (int)(float)(char) i;

c - C 编程作业简介