Shop OBEX P1 Docs P2 Docs Learn Events
Is it possible to simplify this code — Parallax Forums

Is it possible to simplify this code

agfaagfa Posts: 295
edited 2007-06-04 22:50 in BASIC Stamp
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

Comments

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-06-01 16:51
    I think this might be a little faster:

    x = distance
    DO
      new = speedin ' monitor for 0 to 1 transistion
      x = x - (new ^ prev & new)  ' computed increment
      prev=new
    LOOP while x  ' loops until x=0
    GOTO quit    '....
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • metron9metron9 Posts: 1,100
    edited 2007-06-02 01:01
    I have not set up a pulse generator to output 10 pulses but if you did you could test Tracy's code against this code, output 10 transitions and see if it picks up all of them as you increase the speed, but this code saves two variables and may allow faster transitions but since it's not assembler I don't know.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' {$PORT COM2}
    
    
    distance CON 10
    speedin PIN 2
    x VAR Word
    
    
    ENTER:
    x = distance+speedin  'allow for pin state
    DEBUG "Distance=",DEC x,CR,"------------------",CR
    DO
    LP:
    IF speedin=0 THEN LP
    x=x-1
    DEBUG " x=",DEC X,CR
    IF x=0 THEN finished
    LP1:
    IF speedin=1 THEN LP1
    x=x-1
    DEBUG " x=",DEC X,CR
    LOOP WHILE x  ' loops until x=0
    finished:
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • agfaagfa Posts: 295
    edited 2007-06-02 14:28
    Thanks Tracy, Thanks metron9.

    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
  • metron9metron9 Posts: 1,100
    edited 2007-06-02 17:20
    The thing I look at in my code vs the other is the transition of the pulse. Remember there is more time to do calculations if you can catch the start of the pulse as it transitions. So my code puts all of the processing power up front looking for the transition, once the transition is made it then processes the data. To save even more time more code was used instead of changing a variable to compare 1 and 0 I used a constant. The compiled Pbasic code should be faster for the compare as well as it does not have to find the address and get the compare bit. So finding the transition gives you the amount of time to process data until the next transition. The other code spends time moving memory pointers and data without regard to the transition points. The only better way would be to use a pin change interrupt (not available on BS2) that would within x amount of clock cycles execute your processing code. In an interrupt situation you could also buffer the input by simply storing the transition count and processing the data in the main program. Instead of losing possible transitions it would just fall behind in it's data update process.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • agfaagfa Posts: 295
    edited 2007-06-02 18:00
    Thanks metron9 for the code and the·explanation.· I am impressed at both your,s and Tracy's code.· I never would have thought of them.· Hope to test tommorrow.· I can post what my results are if you want.

    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
  • AA Posts: 22
    edited 2007-06-04 18:28
    I have tried a similiar methods to count pulses.· I have the same problem with missed counts.· Even the shortest fastest code·
    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
  • metron9metron9 Posts: 1,100
    edited 2007-06-04 18:50
    Hmmm sounds like I should try making one with a tiny13, I could use the shiftin and shiftout commands to read and set the counters.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • AA Posts: 22
    edited 2007-06-04 19:07
    ·I forgot to mention that yes,· the counters are presetable to any count.· It requires extra pins or other hardware connections.
    A serial in parallel out could be used to set the initial count with less pins.

    aaron·
  • agfaagfa Posts: 295
    edited 2007-06-04 22:50
    A,

    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
Sign In or Register to comment.