1
votes

I'm trying to develop a wheel counter that determines both rotation rate, and the direction of rotation.

I have two magnetic pickup sensors mounted close together, and two metal chunks on the wheel 180 degrees apart.

enter image description here

Currently I have been able to measure rotation rate and convert this to distance, and the distance shown looks good. Here is the code for that:

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
}

I was hoping to use the fact that sensor A should lead sensor B if rotation is clockwise, and vice-versa if anti-clockwise. However my logic doesn't seem to be working properly, Serial.print((int)last_time_b - (int)last_time_a); seems to switch between positive and negative no matter the direction I'm travelling.

I'd really appreciated any help with this.

1

1 Answers

1
votes

I would rather do the direction guess in the interrupt handler:

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

If you experience "bounces", you could add some "debouncing" code.