Society of Robots - Robot Forum

Electronics => Electronics => Topic started by: Joker94 on June 05, 2009, 01:52:52 AM

Title: Community project: Encoder wheel
Post by: Joker94 on June 05, 2009, 01:52:52 AM
I am making an encoder wheel for the community project. I'll post and keep people updated with plans ,shematics, and 3D files which will be Sketchup and/or Auto Cad

what do people think of this module idea?
Title: Re: Community project: Encoder wheel
Post by: hazzer123 on June 05, 2009, 02:18:23 AM
Sounds great.

You should talk with the people who want to build motor controller modules and decide on some specifications such as the output signal of the encoder (generally quadrature) and the physical connector.
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 05, 2009, 02:28:33 AM
i think that the connectors should just be a standard 2.54mm molex connector to make
it easier for everyone who uses it. what do you guys think?
Title: Re: Community project: Encoder wheel
Post by: hazzer123 on June 05, 2009, 02:43:34 AM
I like those. But you still have to consider the pinout. For non-polarised connectors, the mirrored opposite of the V+ supply pin should be 'not connected'. E.g. in a 5 pin connector, if V+ is pin2 then pin 4 shouldn't be connected.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 05, 2009, 03:28:03 AM
that sounds good for ease of use as most people probably would have a molex connecter and pins lying around some where
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 05, 2009, 03:34:43 AM
My robot Woody had very good single channel encoders I made from sparkfun photo interrupters.  I just attached a code wheel drilled with 10 holes to the backshaft of my gearmotors.  The gear ratio was 80:1 and I used a change interrupt to achieve a resolution of 1600 pulses per revolution of the motor output shaft.  The little black squares next to the code wheels are the photo interrupters poking through Woody's frame.  The picture is of Woody's underbelly.

Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 05, 2009, 03:50:28 AM
Great idea, not to difficult to achieve at first glance but i imagine there may be more that i am some how missing
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 05, 2009, 04:14:37 AM
Quote
Great idea, not to difficult to achieve at first glance but i imagine there may be more that i am some how missing
yah and you can't beat it at 4 dollars an encoder.  They worked great!

I simply took datasheet dimensions of the photo interrupters and catered my code wheels and interrupter mounting to fit.  I was able to make a 20Hz PID controller using these encoders.  I actually just wrote a paper on the design and implementation.   I'd attached it to this post but it about 10meg.  I have since learned more about PID implementation.  I have developed a pretty nice controller that can run on an atmega168 using an Output compare interrupt and two external interrupts.  My latest build had the controller taking 37 us for the PID loop to complete.  The software is currently running in the arduino environment but The code is pure c so I can easily port it to avr studio and post a copy if there is a need.
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 05, 2009, 04:19:58 AM
can you upload the paper to mega upload or similar?
i would really like to see it.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 05, 2009, 04:40:31 AM
sonictj, about the code it is not my strong point at the moment as i have not had much need to write much, so when i have finished the module would you be able to assist me in writing some code or if you want to write the code for the module.

i don't know how long it will take me to complete the module as i have another project on the go but hopfully it wont take to long.

and it would be great to be able to see the paper that you wrote, can you let me know if you upload it
Title: Re: Community project: Encoder wheel
Post by: Ro-Bot-X on June 05, 2009, 05:25:29 AM
Quote
Great idea, not to difficult to achieve at first glance but i imagine there may be more that i am some how missing
yah and you can't beat it at 4 dollars an encoder.  They worked great!

I simply took datasheet dimensions of the photo interrupters and catered my code wheels and interrupter mounting to fit.  I was able to make a 20Hz PID controller using these encoders.  I actually just wrote a paper on the design and implementation.   I'd attached it to this post but it about 10meg.  I have since learned more about PID implementation.  I have developed a pretty nice controller that can run on an atmega168 using an Output compare interrupt and two external interrupts.  My latest build had the controller taking 37 us for the PID loop to complete.  The software is currently running in the arduino environment but The code is pure c so I can easily port it to avr studio and post a copy if there is a need.

Sonicj, I would like to take a look at your code, perhaps I will also need your help writing it for the DC motor controller I am building. Since I am not a pure C programmer, I was thinking of using Arduino libraries to write the code, but if you allready been there, done that, there is no point reinventing the wheel.

Thank you so much.
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 05, 2009, 11:17:13 AM
do you want me to post the code here or send you a personal message since this is the electronics forum?
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 05, 2009, 11:57:18 AM
Also note even though I said I used the arduino environment I actually broke a great deal of its functions and hacked their source code.

for instance I revved up the pwm on timer0 so that it was at 62khz (above audible)  this in turn killed the delay function so made timer 2 the timer source of the delay command.  I also completely changed the timer1 settings so that it just counted up to some number equivalent to 50ms for a timer interrupt.

The easiest way to do all this is to change the initializations in wiring.c.  If your not comfortable with this I can develop a project in avr studio.
Title: Re: Community project: Encoder wheel
Post by: Ro-Bot-X on June 05, 2009, 08:07:29 PM
Sonictj,

I am not such a good programmer as obviously you are, that's why I use the libraries in Arduino. I know there are some libraries allready written for GCC by Webbot and by Admin, but I don't feel comfortable writing code in GCC yet. So, for my personal use, I will stay with Arduino for now.

That been said, here is my setup:
- tiny2313 microcontroller, with UART and I2C connections
- SN754410 dual H-Bridge, all pins connected to micro, 2 pins to OSC1A and OSC1B for PWM control, that means it uses Timer1
- 2 quadrature encoders, channel A of each tied to INT0 and INT1 (external interrupts) and channel B of each tied to regular pins that have Pin Change Interrupt

What I need to do is:
- have PID control with trapezoidal profile for smooth acceleration-topspeed-deceleration (for both motors)
- have PID control to keep left-right error to zero
- have the following commands:
          - move(distance, speed); positive goes forward, negative goes bakward
          - turn(degrees, speed); positive turns counterclockwise, negative turn clockwise
          - drive(direction, speed); no distance target, drive until other command
          - stop
          - turn(direction, speed)
- should return 1 after command completed
- should return total traveled distance since started (optional)

If I can remember anything else... I'll add up.

So far I have the ISR for the encoders and some of the PID control, using the PID library. I still need to work on the trapezoidal profile and the commands.

The code you have is just as reference, so it will help if you attach a zip file.

Thanks.
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 05, 2009, 08:41:30 PM
I'll work on writing the code in a way that will not get too complicated (lots of comments and such).  I'll probably post it in a day or two ok?
Title: Re: Community project: Encoder wheel
Post by: Soeren on June 05, 2009, 08:54:36 PM
Hi,

I am making an encoder wheel for the community project. I'll post and keep people updated with plans ,shematics, and 3D files which will be Sketchup and/or Auto Cad

what do people think of this module idea?
I think it's kind of reinventing the wheel (pun not intended;))
A CAD drawing will be one resolution//rev and if people needs AutoCAD, it will barr many from using it, since not many of the projected target audience can afford AutoCAD and nobody here would use "BlackWare" i assume(?).

For arbitrary resolutions, free and legal discs can be made with the Post Script found at http://research.meei.harvard.edu/epl/Engineering/encoderdisk.html (http://research.meei.harvard.edu/epl/Engineering/encoderdisk.html)

Another possibility is http://www.sxlist.com/techref/io/sensor/pos/enc/enc_ps.htm (http://www.sxlist.com/techref/io/sensor/pos/enc/enc_ps.htm)

There exists a program running stand alone as well, but I don't recall where I got it until I fire up an old PC of mine where the link and the software is, but the .ps versions are fine and very customizeable.


P.S. "shematics"! Is that diagrams for women?  ;D
Title: Re: Community project: Encoder wheel
Post by: Ro-Bot-X on June 05, 2009, 09:02:38 PM
I'll work on writing the code in a way that will not get too complicated (lots of comments and such).  I'll probably post it in a day or two ok?

Sure, no rush! I still need a bit of time to get the motors with built in encoders so I can test out the code. My first test rig will use a mega168 instead of the tiny2313. I don't think there will be issues on porting the code afterwards. It will be just resetting the hardware bits.

Thanks.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 06, 2009, 02:34:56 AM
sonictj, it would be great if you would be able to PM me the code just to see what your code entails

Soeren, when i post files iw il post a sip file or equivelant so every body can view them

 it may be a few days before i can post any plans, ihave a school assignment due on tuseday and it accounts for 55 % of my grade

thanks for the tips and keep posting
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 06, 2009, 06:42:10 AM
All i have done so far is design the way i am going to collect the speed data

i am not sure about the encoders wheels final design or whether i am going to us an LED and a light dependent resistor or infa red

i have attached a jpg of my very basic design, not alot of detail as it is  only early on  ;D
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 06, 2009, 07:25:30 AM
i would say go with infrared as there is less chance of other light sources interfering with the sensor.
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 06, 2009, 10:29:59 AM
I agree with smash infrared is the best for this sort of thing.  Also a photoresistor is not fast enough.  photo resistors have a response time of about 100 ms, way too slow for encoders.  My motors were recording 200+ ticks per 50 ms.  you need a device with a response of a few microseconds.  The photo interrupter sparkfun sells have a response of ~ 6 us.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 06, 2009, 06:27:07 PM
where would be the best place to purchase som infa red sensors

would it be possible just to use a infared tranciever and reciver and use the encoder wheel to block the signalor is that going to be to slow
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 06, 2009, 06:55:18 PM
digikey sells photo interrupters of many sizes.
Title: Re: Community project: Encoder wheel
Post by: Webbot on June 06, 2009, 07:08:27 PM
If you are starting from scratch then this product is pretty good: http://www.solarbotics.com/products/gmww02/ (http://www.solarbotics.com/products/gmww02/)
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 06, 2009, 07:15:24 PM
willa infared emitter and detector work for my purpose, or will i need a more powerful infared system
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 06, 2009, 07:42:49 PM
yes, an infrared emitter/detector pair will work for this.
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 06, 2009, 09:41:34 PM
if you want a good pid controller you need the appropiate resolution per your design.  Here are some equations I have developed for determining the best resolution, sampling frequency etc. .  I prefer a sampling rate of 20HZ or more.  I also shoot to have the maximum ticks per PID cycle to be greater than my pwm resolution (0-255 for an 8 bit timer ).

for encoder placed on backshaft for non backshaft placement gear ratio = 1

(max motor rpm / 60 )*(encoder resolution*gear ratio)/PID frequency = max ticks per PID cycle

(max motor rpm / max ticks per PID cycle) = tolerence in speed in rpm t


example:

200 rpm motor
gear ratio 80:1
encoder res 10
PID frequency 20Hz

(200/60)*(10*80)/20 =133 ticks max

200/133 = 1.5 rpm max error

These calculations are for rpm.  wheel diameter will be a factor when distance measurement is required.  I recommend only using encoders for rpm management, there is too much slip for accurate distance measurement.



Title: Re: Community project: Encoder wheel
Post by: Ro-Bot-X on June 06, 2009, 10:22:24 PM
I also shoot to have the maximum ticks per PID cycle to be greater than my pwm resolution (0-255 for an 8 bit timer ).

What do you mean by ticks per PID cycle? Encoder clicks? The motors I will use have 141 high pulses per output rotation or 1 high pulse per motor rotation (per channel), 80RPM at the voltage I'm going to use them.
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 07, 2009, 12:38:02 AM
when I say per PID cycle I'm talking about how I have my PID algorithm refreshing every 50 ms (20Hz).  During the 50ms period I have the controller collect the number of pulses of the encoders.  The number of pulses is proportional to the rpm of the motors.  I like for the max speed of the motors to have greater than 255 pulses collected over the 50ms period.
Title: Re: Community project: Encoder wheel
Post by: Ro-Bot-X on June 07, 2009, 06:06:32 AM
Ok, so I have 80 RPM max speed and 141 clicks per rotation.

At 80 rotations I have 141x80=11280 clicks per minute, that means 188 clicks per second, that means 9.4 clicks per 50 ms, per channel. So for a quadrature that counts both low-high and high-low transitions for both channels, there will be 37.6 clicks per 50 ms. A bit low, but what can I do, that's the motor specs. Hard to find a motor with built in encoders.
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 09, 2009, 02:56:18 AM
you have the option of going to 10Hz which may help (100ms sampling time)

I just clean installed windows on this laptop so I haven't had time to write the PID code yet.  I'll try and get to it soon.

essentially though you need to create a timer interrupt that triggers the pid algorithm.

//this function and another for the other wheel will be called via a timer interrupt.  The period of time is up to you, but an interrupt is nessicary to keep the time //consistent.

PID_leftMotor()
{

//might need float values
// you will have to determine kp, ki and kd
int kp = 1, ki = .01, kd = 3;

P_error   = desired_num_of_ticks - acctual_num_of_ticks;
I_error += P_error;
D_error  = P_error - prevP_error;

prevP_error = P_error

int duty = P_error*kp + Ithe _error*KI + D_error*kd;

analogWrite(leftMotor,duty);
}
end pseudo code

The way to tune the controller is to copy the encoder clicks per each sample in a array say 200 samples per motor.  This means two byte arrays of 200.  make a test function that allows for 200 PID cycles then it should (not required) stop the motors.  Then you can send out all of the data stored via uart to Hyper terminal on your PC.  If you use println() for each piece of data you can simple copy the data and paste it in excel to make a graph of the motor speed.  All you have to do in excel is select the first 200 lines of data and compare them to their respective time.  The time that each sample is know because you chose the frequency of the PID controller.

example of what data should look like in excel assuming 50ms sampling (.05s)

time s   encoder clicks
.05       0
.10      20
.15      30
.20      35
......

Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 10, 2009, 04:48:36 AM
what would a suitable mcu be for this project, are we using a universal mcu or can we chose our own
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 10, 2009, 05:30:08 AM
well, from what ive heard most people involved in building modules are using the attiny2313..
but it is completely up to you, the microcontroller can be changed later with not much effort.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 10, 2009, 05:33:17 AM
great thanks for the clarification smash
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 10, 2009, 12:30:25 PM
I would recommend one of the atmega48/88/168 because they have more flash and sram.  my encoder code was too big for an attiny2313 even with full optimization.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 15, 2009, 05:46:49 AM
is this shematic correct

E = infared emitter

D= infared detector
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 15, 2009, 05:51:33 AM
mate, get eagle! ;D i cant tell a thing by looking at that (no offense) although i don't know too much about encoders, do "E" and "D" stand for "emitter" and "detector? and by "atmega88" do you mean "atmega8"?
when you say "pin out to mother board" you mean I2C correct?
sorry for all the questions but im new to encoders too, and its hard to tell from the non-eagle file :D
if you want i could knock up a quick schematic of what you are saying?
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 15, 2009, 06:06:43 AM
i have eagle but i can not get a 28 pin chip on to my shematic.

the atmega88 is the chip sonictj recommendedand i did mention that e stands for emitter and d stands for detector

and if you were able to knock up a shematic and email me the jpg and eagle file that would be great

if you have any other questions of info i have left out just PM me
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 15, 2009, 06:16:30 AM
the atmega88 is the chip sonictj recommendedand i did mention that e stands for emitter and d stands for detector
lol at your spelling ;D i take a look at the mega88, have you considered the 168?
Quote
i have eagle but i can not get a 28 pin chip on to my shematic.
oh ill find it, don't you worry about that *gets evil look in eyes*
Quote
and if you were able to knock up a shematic and email me the jpg and eagle file that would be great
ill get on that now... should be done in 5-10 mins ;D
Title: Re: Community project: Encoder wheel
Post by: sonictj on June 15, 2009, 08:59:21 AM
the atmega88 and atmega168 are of the same family which is why I recommended them.  I believe that they have the same features with the exception of flash memory.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 16, 2009, 05:34:08 AM
Thanks for the shematic smash, and now i know how to work eagle ;D
Title: Re: Community project: Encoder wheel
Post by: SmAsH on June 16, 2009, 06:47:54 AM
any time mate :) if you need any more help give me a whistle ;)
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 19, 2009, 07:10:25 PM
1)Do i have to hook up the emitters and detectors to different  ports, eg. emitters to D pins and detectors to the B pins

2)will i have to be able to program the encoder through my mcu or can are you able to program it from the mother board.
Title: Re: Community project: Encoder wheel
Post by: Ro-Bot-X on June 19, 2009, 07:34:02 PM
1. No, you tie the emitters to GND and through a resistor to Vcc. The detectors are tied to GND and to the microcontroller pin. You eighter need a pull up resistor there or turn ON the internal pull up resistors. I usually use external ones.

2. Yes, you need to program the microcontroller to read the encoders, eighter by polling or by interrupts.

Chat?
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 25, 2009, 11:21:37 PM
sorry this is taking a while, this project has hit the back burner for the moment

in may be a week or a month untill i can get back into it
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 26, 2009, 02:49:31 AM
what is an average robot velocity so i  can make some quick caulculations before i got any further with the mechanics

oh and wheel size
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 26, 2009, 03:04:27 AM
Attached is the wheel design

Diameter =35 mm / 1.378"
Clicks=16
Space between clicks 6.78 mm
expected slip =5%

in my calculations i will use square numbers. 1 m/s robot velocity/, 100 mm /4" diameter wheel
Title: Re: Community project: Encoder wheel
Post by: Ro-Bot-X on June 26, 2009, 05:23:39 AM
Velocity(m/s) = RPS * Wheel circumference(m)
Wheel circumference(m) = Pi(3.14) * Diameter(mm) / 1000
RPS = RPM / 60


If targeted Velocity is 0.5 m/s, RPM is 60 (standard servo) results a Wheel circumference of 0.5 m, or 500 mm. This is way too big for a servo. Let's calculate the other way around. The wheels that I have are 66.88 mm in diameter, that means the circumference is 210 mm or 0.21 m. For the same targeted velocity, the RPM should be Velocity * 60 / Wheel circumference, that is 0.5 m/s * 60 / 0.21 m = 142.857.

Now to calculate the encoder pulses. For the 210 mm wheel circumference, to have a traveled distance of 5 mm per click, I need 42 clicks per rotation. That means 21 white and 21 black stripes, or 21 holes in the wheel. I would like to have more precision than that, so I will try to mount the encoders directly on the back shaft of the motor that spins 51 times faster (that's the gear ratio) than the output shaft.
Title: Re: Community project: Encoder wheel
Post by: Joker94 on June 26, 2009, 05:36:09 AM
brilliant ;D

thanks Ro-Bot-X
Title: Re: Community project: Encoder wheel
Post by: sanketjog on July 01, 2009, 02:36:34 AM
Can anyone please share the  encoder wheel feedback diagram using PID.....