使用protothreads通过Arduino控制电机继电器

时间:2015-07-03 10:47:48

标签: c multithreading arduino sensor protothread

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

每个继电器由简单的开关设置控制。当开关闭合时,继电器有效。当开关打开时,继电器无效。

我决定给protothreads一个去,因为我希望两个开关控制并联电机。为此,我编写了以下代码:

 #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);
}

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

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

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

1 个答案:

答案 0 :(得分:0)

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

如果仅loop() 负责保持protothreads运行,则代码可能变得更容易推理。如果我们将继电器的初始启用移动到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);
}