Is it possible to simplify this code
agfa
Posts: 295
I am using a bs2 to control a bot.· I am reading an encoder to determine distance traveled.· I originally tried using the COUNT command but found it to be inconsistant.
My question is.· Is there a better, more efficient, or faster way to count the encoder pulses?· I want to count either the 0 to 1 transition or the 1 to 0 transition.
here is·a piece of the code I am using now.
distance con 100
speedin PIN 2
new·VAR Bit
prev·VAR Bit
x VAR Word
main:
new = speedin ' monitor for 0 to 1 transistion
IF·new=1 AND prev=0 THEN x=x+1
prev=new
IF x=distance then goto quit
GOTO main
My question is.· Is there a better, more efficient, or faster way to count the encoder pulses?· I want to count either the 0 to 1 transition or the 1 to 0 transition.
here is·a piece of the code I am using now.
distance con 100
speedin PIN 2
new·VAR Bit
prev·VAR Bit
x VAR Word
main:
new = speedin ' monitor for 0 to 1 transistion
IF·new=1 AND prev=0 THEN x=x+1
prev=new
IF x=distance then goto quit
GOTO main
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
I will try them both with the bot as soon as I can.· I am in the process of rewiring, I need to elliminate the breadboard and the long lengths of wire.
Also thanks Tracy, I was unaware of the bitwise logic, a good learning experience.
agfa
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
The problem I am having is, the faster I run the bot the more transitions I miss.· I'm sure some of the problem is other code overhead I am using, which will·be addressed after testing the new advise.· I am anxious to try both methods and also plan to incorporate in my future programming.
Thanks again guys.
agfa
is not fast enough.· Also·counting is pretty much the only thing the stamp can do if you need accuracy at high speeds.
The solution I found uses·two 4029 Binary up counters fed into a·parallel in serial out shift register.· It can handle clock frequencies up to 4Mhz. It counts complete pulses so no worry about looking for the transitions(high to low or vice vs)
Two counters and one shift register gives counts up to 255 before rolling over.·The circuit is easly expanded to include more counters for larger numbers.·(4 counters + 2 shifters·= 16 bits)·It takes 3 stamp pins to operate and can be sampled and reset as often as your program and stamp allow.
If anyone is interested I will post a schematic and sample code
Aaron
Post Edited (A) : 6/4/2007 6:32:47 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
A serial in parallel out could be used to set the initial count with less pins.
aaron·
Thanks for the input,· I thought it may require extra hardware but I wanted to avoid it if possible.· I thought the stamp could handle what I was trying to do, even·a small margin of error wouldn't hurt.· The pulses are low freq.
metron9,
I am intrigued with your posts, not just this thread, but many others.· I want to explore pics and their programing.
Once again thanks to all.· I think I finally have my hardware in working order.· Now to the next step. It's time for the propeller.· Thanks again Paul.
agfa