c-使用原线程通过Arduino控制电机继电器



我正在通过arduino控制两个电机继电器。设置如下:

每个继电器都由一个简单的开关设置控制。当开关闭合时,继电器处于激活状态。当开关断开时,继电器处于非激活状态。

我决定试试protothreads,因为我想用这两个开关来控制paracell中的电机。为了做到这一点,我写了以下代码:

 #include <pt.h>
#define yMaxLimitLeft 8 //these are the limit switches that control the relays
#define yMaxLimitRight 9 //these are the limit switches that control the relays
#define leftRelay 4
#define rightRelay 5
static struct pt pt1, pt2; // each protothread needs one of these

void setup() {
  Serial.begin(9600);
  PT_INIT(&pt1);  // initialise the two
  PT_INIT(&pt2);  // protothread variables
  pinMode(leftRelay, OUTPUT);  //set pins     
  pinMode(rightRelay, OUTPUT);
  pinMode(yMaxLimitLeft,INPUT);
  pinMode(yMaxLimitRight,INPUT);
}
void loop() {
   digitalWrite(leftRelay, HIGH);//enable relays
  digitalWrite(rightRelay, HIGH);//enable relays
   runLeft(&pt1); //protothreads that controls relay number 1
  runRight(&pt2); //protothreads that controls relay number 2
}
/////////////////////////////////////////////////////////////////////////////
// This function controls the left relay
static int runLeft(struct pt *pt) {
  PT_BEGIN(pt);
  while(1){
    PT_WAIT_UNTIL(pt, digitalRead(yMaxLimitLeft)==HIGH); //wait until the switch is closed
     Serial.println("left off"); 
      digitalWrite(leftRelay, LOW);//disable relay
      }   
    PT_END(pt);
}

//this function controls the right relay
static int runRight(struct pt *pt) {
  PT_BEGIN(pt);
   while(1){

    PT_WAIT_UNTIL(pt, digitalRead(yMaxLimitRight)==HIGH); //wait until the switch is closed
     Serial.println("Right off");   
     digitalWrite(rightRelay, LOW);//disable relay
    }

  PT_END(pt);
}

问题如下。只要我只按下一个开关,相应的继电器就会做出响应。然而,当我按下两个开关时,只有一个继电器响应(可能是最早被触发的那个(。

如何修复代码,以便在按住两个开关时,两个继电器同时响应?

非常感谢您的时间和帮助提前

只要PT_WAIT_UNTIL(即digitalRead(yMaxLimitLeft)==HIGH(中的谓词为true,执行就会卡在while循环中。

如果只有loop()负责保持原线程的运行,那么代码可能会变得更容易推理。如果我们将继电器的初始启用移动到setup(),则runLeft/Right可以重写为包含与某个开关+继电器相关的所有逻辑。在这种情况下,可以使用第二个PT_WAIT_UNTIL语句来等待开关再次打开。

#include <pt.h>
#define yMaxLimitLeft 8 //these are the limit switches that control the relays
#define yMaxLimitRight 9 //these are the limit switches that control the relays
#define leftRelay 4
#define rightRelay 5
static struct pt pt1, pt2; // each protothread needs one of these

void setup() {
  Serial.begin(9600);
  PT_INIT(&pt1);  // initialise the two
  PT_INIT(&pt2);  // protothread variables
  pinMode(leftRelay, OUTPUT);  //set pins     
  pinMode(rightRelay, OUTPUT);
  pinMode(yMaxLimitLeft,INPUT);
  pinMode(yMaxLimitRight,INPUT);    
  digitalWrite(leftRelay, HIGH);//enable relays
  digitalWrite(rightRelay, HIGH);//enable relays
}
void loop() {
  runLeft(&pt1); //protothreads that controls relay number 1
  runRight(&pt2); //protothreads that controls relay number 2
}
/////////////////////////////////////////////////////////////////////////////
// This function controls the left relay
static int runLeft(struct pt *pt) {
  PT_BEGIN(pt);
  while(1) {
    PT_WAIT_UNTIL(pt, digitalRead(yMaxLimitLeft)==HIGH); //wait until the switch is closed
    Serial.println("left off"); 
    digitalWrite(leftRelay, LOW);//disable relay
    PT_WAIT_UNTIL(pt, digitalRead(yMaxLimitLeft)==LOW); //wait until the switch opens again
    digitalWrite(leftRelay, HIGH);//enable relay
  }   
  PT_END(pt);
}

//this function controls the right relay
static int runRight(struct pt *pt) {
  PT_BEGIN(pt);
    while(1){
      PT_WAIT_UNTIL(pt, digitalRead(yMaxLimitRight)==HIGH); //wait until the switch is closed
      Serial.println("Right off");   
      digitalWrite(rightRelay, LOW);//disable relay
      PT_WAIT_UNTIL(pt, digitalRead(yMaxLimitRight)==LOW); //wait until the switch opens again
      digitalWrite(rightRelay, HIGH);//enable relay
    }

  PT_END(pt);
}

相关内容

  • 没有找到相关文章

最新更新