### Author Topic: help me with encoders!!!  (Read 1094 times)

0 Members and 1 Guest are viewing this topic.

#### prasanga17

• Beginner
• Posts: 6
##### help me with encoders!!!
« 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.

#### newInRobotics

• Supreme Robot
• Posts: 1,015
• N.I.R.
##### Re: help me with encoders!!!
« Reply #1 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.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

#### prasanga17

• Beginner
• Posts: 6
##### Re: help me with encoders!!!
« Reply #2 on: June 26, 2012, 11:12:04 PM »

instead of using interrupts can't i use a counter ic to count encoder reading?

#### newInRobotics

• Supreme Robot
• Posts: 1,015
• N.I.R.
##### Re: help me with encoders!!!
« Reply #3 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.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

#### prasanga17

• Beginner
• Posts: 6
##### Re: help me with encoders!!!
« Reply #4 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?

#### mstacho

• Supreme Robot
• Posts: 370
##### Re: help me with encoders!!!
« Reply #5 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
Current project: tactile sensing systems for multifingered robot hands

#### newInRobotics

• Supreme Robot
• Posts: 1,015
• N.I.R.
##### Re: help me with encoders!!!
« Reply #6 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.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W