Society of Robots - Robot Forum

Software => Software => Topic started by: prasanga17 on June 26, 2012, 08:42:10 AM

Title: help me with encoders!!!
Post by: prasanga17 on June 26, 2012, 08:42:10 AM
I am building a obstacle avoiding robot and i was advised to use motors with encoders . but i have never used encoders before and need some help to program my code...
how should i use encoder readings to turn my robot? where can i find sample code with encoders used ...
i am using mikroc and using pic 16f877a microcontroller.
Title: Re: help me with encoders!!!
Post by: newInRobotics on June 26, 2012, 11:52:19 AM
I cannot tell You exact code, as I've never used PIC, however I can explain how it all works.

You have to have 2 interrupts to monitor when 2 pins go HIGH or LOW. Each transition HIGH --> LOW and LOW --> HIGH should increment variable (say int rightWheelTicks). You should also have 1 timer running and calling function every 1 second (or every 100 milliseconds if Your robot is fast); the function called by timer should check rightWheelTicks and leftWheelTicks and workout RPM or distance travelled. Lets look at the example:

Motor RPM = 1000;
Gearbox gear ratio = 1:50;
Gearbox output RPM = 20;
Stripes on encoder disk = 10;
Wheel diameter = 10cm;
Wheel circumference = ~31.42cm (in one rotation wheel will travel ~31.42cm);

When timer ends counting to 1 second it checks wheel monitor variables, lets say they are as follows:

rightWheelTicks = 1200;
leftWheelTicks = 1800;

That means that right motor made 1200/10=120 rotations and left motor made 1800/10=180 rotations, hence right wheel made 120/50=2.4 rotation and left wheel made 180/50=3.6 rotation, hence right wheel travelled 2.4*10cm=24cm and left wheel travelled 3.6*10cm=36cm, hence right wheel speed is 24cm/s and left wheel speed is 36cm/s. By knowing speed of each wheel You can adjust them individually (normally using PID) to make robot go in straight line or perform controlled turns;

To make 90° turn You have to tun each wheel to opposite direction exactly by 1/4 of total wheel circumference. Now we not that with each wheel rotation encoder disk spins 50 times and produces 50 clicks, so if we need only 1/4 of wheel rotation - encoder disk will spin only 50/4=12.5 times producing 125 clicks. Knowing all that what You have to do is on every single pin state change interrupt click value has to be checked and when rightWheelTicks = 125 stop the right wheel, when leftWheelTicks = 125 stop the left wheel. To get smooth motion You probably want to consider PID or Fuzzy Logic.
Title: Re: help me with encoders!!!
Post by: prasanga17 on June 26, 2012, 11:12:04 PM
thanks for your quick reply....

instead of using interrupts can't i use a counter ic to count encoder reading?
Title: Re: help me with encoders!!!
Post by: newInRobotics on June 27, 2012, 01:04:46 AM
Why would you want extra hardware when all can be done with one chip in software?

Anyway, counter IC increments on transition from LOW to HIGH only (there might be counters which increment on both transitions), what You need is to increment on both transitions. Does that make sense? To do counting You probably want to use integer variable (32 bit), to achieve that with counter IC You would need 32 bit counter; to read data from 32 bit counter You would need 32 I/O pins on microcontroller per wheel, so 64 in total, or 2 serial/I2C connections, which still uses interrupts and is much slower than directly feeding encoder signal to microcontroller and incrementing counter in software.
Title: Re: help me with encoders!!!
Post by: prasanga17 on June 27, 2012, 09:51:48 AM
thanks!!

one more question , can you give me a hint on how to use encoder values to turn my robot? lets say for an example i want to make 90 degree turn. how do i combine encoder reading to my normal pwm code?
Title: Re: help me with encoders!!!
Post by: mstacho on June 27, 2012, 01:32:55 PM
I don't mean to hijack the thread, but I ALSO have a question, since I've been writing my own encoder code for a while now and have always wondered why my tick count is 1/2 that of everyone elses:

Why do you count rising and falling edges?  If the edge rises, it has to fall again, so aren't you counting that tick twice?  Or, to put another way, why does that provide more information (in terms of resolution)?

MIKE
Title: Re: help me with encoders!!!
Post by: newInRobotics on June 27, 2012, 04:16:49 PM
one more question , can you give me a hint on how to use encoder values to turn my robot? lets say for an example i want to make 90 degree turn. how do i combine encoder reading to my normal pwm code?
But I did in my previous post. You enable PWM and keep it on until tickCounter registers 1/4 of wheel rotation. To have smooth motion You will have to use PID or Fuzzy Logic.

Why do you count rising and falling edges?  If the edge rises, it has to fall again, so aren't you counting that tick twice?  Or, to put another way, why does that provide more information (in terms of resolution)?
You use both edges to get full resolution available from encoder. If edge rises it does not necessarily mean it has to fall, lets take very simple example and use encoder disk only with 2 stripes (half of the disk is white and half is black. Let's also say that You want to turn wheel only 180°, You can imagine that encoder disk will transition from black to white (or vice versa) once, because 2 ticks would mean full 360° revolution. Now, if You use only one edge to do calculations, it means that encoder disk with two stripes will only produce one tick per full revolution, so You no longer can have wheel turning 180°, as after each tick microcontroller will think that one revolution was complete.