Sunday 19 July 2015

Counting Real Time RPM with IR sensors

RPM of robot may change due to:
1) Change in the texture of the floor
2) After long time, current from battery decreases, resulting in decreased speed.

And we may need to get the accurate real time rpm for following applications:
1) We want the robot to cover a fixed distance every time.
2) Fixed degree of turn to be taken every time.

This can be solved in a very simple way:

Paste a paper painted like this on one side of the wheel:


Place an IR sensor module in front of this.. 



The output of IR sensor will go to a interrupt. Now, whenever the color changes from black to white, output will go from LOW to HIGH. Hence, "RISING" state should be chosen for interrupt.

I have used this with Arduino, so the code I have written is:
 
#include "EEPROM.h"
int addr = 0;

#define IR_in 21        //Output from IR sensor

//Pins defined for Motor Driver
#define mota1 4
#define mota2 A5
#define motb1 7
#define motb2 6
#define en1 3
#define en2 5

int countrpm;
void inc_count(void);
void forward(void);
void pause(void);

void setup()
{
  Serial.begin(9600);
  pinMode(IR_in,INPUT);
  pinMode(mota1,OUTPUT);
  pinMode(mota2,OUTPUT);
  pinMode(motb1,OUTPUT);
  pinMode(motb2,OUTPUT);
  pinMode(en1,OUTPUT);
  pinMode(en2,OUTPUT);
  
}

void loop()
{

  forward();
  countrpm = 0;
  attachInterrupt(0,inc_count,RISING);
  delay(10000);
  detachInterrupt(0);
  pause();
  EEPROM.write(addr,countrpm);
  while(1);
  
}

void inc_count()
{
  countrpm = countrpm + 1;
}

void forward(void)
{
  analogWrite(en1,255);
  analogWrite(en2,255);
  digitalWrite(mota1,HIGH);
  analogWrite(mota2,LOW);
  digitalWrite(motb1,HIGH);
  digitalWrite(motb2,LOW);
}
void pause(void)
{
  analogWrite(en1,0);
  analogWrite(en2,0);
  digitalWrite(mota1,LOW);
  analogWrite(mota2,LOW);
  digitalWrite(motb1,LOW);
  digitalWrite(motb2,LOW);
}


RPM value is now stored in EEPROM and you can check it..

Important point to note here is the distance between sensor and the wheel, and also the width of the white strip. Adjust them till you get perfect value.

No comments:

Post a Comment

Powered by Blogger.