The Gray Code Project - Building a Better Encoder
TechnoRobbo
Posts: 323
The Gray Code Project - Building a Better Encoder
The origins of this project lies in some posts that I've read where people wanted to find the position of a rotating device.· The reply usually was to create a radial encoder or a quadrature encoder and count the bars. I know these methods are tried and true but they seemed very time consuming for the processor and better suited for speed indication than finding a position on a wheel. They also don't retain their position when the power's off.·
Solution
What about an absolute encoder, I thought, that must be easy to build.· Eight sensors - eight tracks - 256 positions on a wheel. I created several binary encoder discs from different medias but all seem to have calibration problems. Not all sensors aligned - not only with each other but with their corresponding bars on the disc. Then I stumbled upon the Gray Code (Ta Da)!· Reflected Binary Gray Code, to be exact.· All my problems were solved - it worked like magic - no calibration errors!
What makes the Gray Code so good?
Here is an example of a binary code. Note that several bits can change simultaneously.
Now here is an example of Gray Code . Note that only one bit can change at a time. Therefore only 1 sensor and 1 code bar must agree for a bit to be "ON".
The Software
To make things easy I wrote a program to create the discs. I based it on CD Labels by Neato and provided a "Draft" Printing for alignment.· The program is Freeware and will allow you to customize the disc dimensions and attributes from 1 to 16 bits. Here's examples of the Discs:
Here's a link to download it:
http://home.comcast.net/~technorobbo/Encoder.zip
The Hardware
I created a small carnival wheel pictured below, using the electronics in the Schematics attached.
The BS2 Code generates a string that includes Absolute Position, Wheel RPM and Wheel Direction. The comments in the code explain how to customize it for other Stamps.
I've also included a link to an Excel Spreadsheet used to receive and display the data from the BS2.
http://home.comcast.net/~technorobbo/Encoder_Demo.xls
And here's a spreadsheet that that tells you (via voice)the wheel position:
http://home.comcast.net/~technorobbo/Encoder Demo with voice.xls·
The Math
Here are the important algorithms:·
·Binary to Gray Code: (in VB , x is the binary)
···gray = x Xor Int(x / 2)
·Gray Code to Binary: (in BS2 encode is the gray code)
····GCode8· CON 7················· 'Graycode Constant use for bytes
··· GCode16 CON 15················ 'Graycode Constant use for words
· ··FOR BitCount= 1 TO GCode8·········· 'Binary to Gray Code 8 bit
··· ···Encode=Encode^(Encode>>1)
· ··NEXT
The Parts
1 - 74HC165 Shift Register Available at· http://www.parallax.com/detail.asp?product_id=602-00010
5·to 8·- Infrared Emitter LED
5· to 8··-· Phototransistor Infrared Detector
Radio Shack sells a matched pair of Detectors and Emitters for $3.95USD·Model: 276-142
Errata & Updates:
Uploaded New BS2 Code·for 8 bit modification
Uploaded·updated Excel.·I printed Carnival wheel's numbers backwards and·I was to lazy to turn the code disc over so I changed it in the Excel code. - This has been corrected - My Bad.·
Added talking spreadsheet Demo!
Added Parts List
Reposted Corrected Schematic - added missing resistor
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 8/23/2007 11:56:57 AM GMT
The origins of this project lies in some posts that I've read where people wanted to find the position of a rotating device.· The reply usually was to create a radial encoder or a quadrature encoder and count the bars. I know these methods are tried and true but they seemed very time consuming for the processor and better suited for speed indication than finding a position on a wheel. They also don't retain their position when the power's off.·
Solution
What about an absolute encoder, I thought, that must be easy to build.· Eight sensors - eight tracks - 256 positions on a wheel. I created several binary encoder discs from different medias but all seem to have calibration problems. Not all sensors aligned - not only with each other but with their corresponding bars on the disc. Then I stumbled upon the Gray Code (Ta Da)!· Reflected Binary Gray Code, to be exact.· All my problems were solved - it worked like magic - no calibration errors!
What makes the Gray Code so good?
Here is an example of a binary code. Note that several bits can change simultaneously.
Now here is an example of Gray Code . Note that only one bit can change at a time. Therefore only 1 sensor and 1 code bar must agree for a bit to be "ON".
The Software
To make things easy I wrote a program to create the discs. I based it on CD Labels by Neato and provided a "Draft" Printing for alignment.· The program is Freeware and will allow you to customize the disc dimensions and attributes from 1 to 16 bits. Here's examples of the Discs:
Here's a link to download it:
http://home.comcast.net/~technorobbo/Encoder.zip
The Hardware
I created a small carnival wheel pictured below, using the electronics in the Schematics attached.
The BS2 Code generates a string that includes Absolute Position, Wheel RPM and Wheel Direction. The comments in the code explain how to customize it for other Stamps.
I've also included a link to an Excel Spreadsheet used to receive and display the data from the BS2.
http://home.comcast.net/~technorobbo/Encoder_Demo.xls
And here's a spreadsheet that that tells you (via voice)the wheel position:
http://home.comcast.net/~technorobbo/Encoder Demo with voice.xls·
The Math
Here are the important algorithms:·
·Binary to Gray Code: (in VB , x is the binary)
···gray = x Xor Int(x / 2)
·Gray Code to Binary: (in BS2 encode is the gray code)
····GCode8· CON 7················· 'Graycode Constant use for bytes
··· GCode16 CON 15················ 'Graycode Constant use for words
· ··FOR BitCount= 1 TO GCode8·········· 'Binary to Gray Code 8 bit
··· ···Encode=Encode^(Encode>>1)
· ··NEXT
The Parts
1 - 74HC165 Shift Register Available at· http://www.parallax.com/detail.asp?product_id=602-00010
5·to 8·- Infrared Emitter LED
5· to 8··-· Phototransistor Infrared Detector
Radio Shack sells a matched pair of Detectors and Emitters for $3.95USD·Model: 276-142
Errata & Updates:
Uploaded New BS2 Code·for 8 bit modification
Uploaded·updated Excel.·I printed Carnival wheel's numbers backwards and·I was to lazy to turn the code disc over so I changed it in the Excel code. - This has been corrected - My Bad.·
Added talking spreadsheet Demo!
Added Parts List
Reposted Corrected Schematic - added missing resistor
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 8/23/2007 11:56:57 AM GMT
bs2
2K
Comments
An LED shines through a couple of optical masks....the combination of the light shining through indicates the sensors direction.
Very cool!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<FONT>Steve
What's the best thing to do in a lightning storm? "take a one iron out the bag and hold it straight up above your head, even God cant hit a one iron!"
Lee Travino after the second time being hit by lightning!
Steve b,
Thanks - the point of the project is the software that makes this rather complicated disc setup a snap - It's size is only limited by your printer. It's designed for 16 bits max for a disc resolution of 65536. Daisy chain 2 shift registers to achieve it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 8/23/2007 11:19:44 AM GMT
This is really neat. Thanks for documenting this project so others can study it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Whit+
"We keep moving forward, opening new doors, and doing new things, because we're curious and curiosity keeps leading us down new paths." - Walt Disney
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never give up when things go wrong.