Arduino轮速传感器检测方向

时间:2016-01-21 16:54:16

标签: arduino

我正在尝试开发一个确定旋转速度和旋转方向的轮计数器。

我有两个磁性传感器安装在一起,两个金属块相隔180度。

enter image description here

目前我已经能够测量旋转速度并将其转换为距离,并且显示的距离看起来很好。这是代码:

volatile byte half_revolutions;
volatile byte half_revolutions_b;
volatile unsigned long last_time_a;
volatile unsigned long last_time_b;
volatile byte dir;



unsigned int rpm;
unsigned int rpm_b;
unsigned long timeold;
unsigned long timeold_b;
unsigned long time_print;

double distance = 0.0;
void setup()
{
 Serial.begin(9600);
 attachInterrupt(0, rpm_fun, RISING);
 attachInterrupt(1, rpm_fun_b, RISING);
 half_revolutions = 0;
 half_revolutions_b = 0;
 rpm = 0;
 timeold = 0;
 timeold_b = 0;
 time_print = 0;
 dir = 1;
}

void loop()
{
   int rpm_guess;

   if (half_revolutions >= 2) 
   { 

    rpm = 30*1000/(millis() - timeold)*half_revolutions;
    timeold = millis();
    half_revolutions = 0;
   }
   else if (millis() - timeold > 1000)
   {
      rpm = 0;
   }

   if (half_revolutions_b >= 2) 
   { 
     rpm_b = 30*1000/(millis() - timeold_b)*half_revolutions_b;
     timeold_b = millis();
     half_revolutions_b = 0;  
   }


   else if (millis() - timeold_b > 1000)
   {
      rpm_b = 0;
   }

   if (millis() - time_print > 500)
   {
     rpm_guess = ((int)rpm + (int)rpm_b) / 2.0;
     double rad_per_sec = (6.0*3.14159* rpm_guess)/180.0;
     double metres_per_sec = rad_per_sec*0.038;

     distance += metres_per_sec * 0.5;
     Serial.print((int)last_time_b - (int)last_time_a);
     Serial.print(",");
     Serial.println(distance);
     time_print = millis();
   }


}

void rpm_fun()
{
  half_revolutions++;
  last_time_a = micros();
  //Each rotation, this interrupt function is run twice
}

void rpm_fun_b()
{

  half_revolutions_b++;
  last_time_b = micros();
  //Each rotation, this interrupt function is run twice
}

我希望使用这样的事实:如果旋转是顺时针方向,传感器A应该引导传感器B,反之亦然,反之亦然。然而,我的逻辑似乎没有正常工作,Serial.print((int)last_time_b - (int)last_time_a);似乎在正面和负面之间切换,无论我正在旅行的方向。

我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:1)

我宁愿在中断处理程序中进行方向猜测:

void rpm_fun() {
    if (last_time_a > last_time_b) dir = 0;
    else dir = 1;
    half_revolutions++;
    last_time_a = micros();
}

void rpm_fun_b() {
    if (last_time_a > last_time_b) dir = 0;
    else dir = 1;
    half_revolutions_b++;
    last_time_b = micros();
}

如果您遇到“跳出”,则可以添加一些“debouncing”代码。