Shop OBEX P1 Docs P2 Docs Learn Events
Waitpeq to monitor a change in 2 pins? Possible???? — Parallax Forums

Waitpeq to monitor a change in 2 pins? Possible????

TJHJTJHJ Posts: 243
edited 2009-10-14 19:12 in Propeller 1
Hi all,
So I am trying to pick up a simple optical encoder. I have a cased phototransistor set, and a disc that is cut with missing slots. Each high low transition on a pin is a given distance movement.
My issue is I don’t want to give up 2 cogs to watch the encoders, I don’t mind giving up one…
To monitor just 1 encoder I dedicate off a cog and have it doing the following, I am doing it like this so I don’t miss a transition.
·
Repeat 
  waitpeq(|<XEncoder, |<XEncoder,0) &#8216;wait for pin high 
  waitpne(|<XEncoder, |<XEncoder,0) &#8216; wait for low 
  XPosition ++&#8217;add to position 


·
But can I do something to watch for a change in either pin?
Repeat 
  waitpeq((|< XEncoder & |<Yencoder), ((|< XEncoder & |<Yencoder),0)  &#8216;does it compare the whole mask or just the pins set in state component? 
  waitpne(|< XEncoder & |<Yencoder), ((|< XEncoder & |<Yencoder),0)  &#8216;does it compare the whole mask or just the pins set in state component?   HorCurrentPosition ++
if ina[noparse][[/noparse]xEnxoder] == 0 
                XPosition ++ 
Elseif ina[noparse][[/noparse]yEncoder] == 0 
                YPosition ++
 


·
I think this will cause it to wait for a matched transition set. Not one or the other.....
I am thinking there is a way to do this alot simpler, but I am getting hung up on my bit math……
Any ideas or help is appreciated.
·
TJ

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I owe everyone here a bunch, So thanks again for answering my dumb questions.
Projects. RG500 ECU system. PropCopter. Prop CanSat. Prop Paste Gun.
Suzuki RG500 in a RGV 250 frame.
Bimota V-Due (Running on the fuel injection system)
Aprilia RS250

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-10-13 18:59
    Something like this ought to work:

    [b]CON[/b]
    
      enca          = 0             'Pin number for encoder A.
      encb          = 1             'Pin number for encoder B.
    
      encamask      = 1 << enca
      encbmask      = 1 << encb
      mask          = encamask | encbmask
    
    [b]PUB[/b] encoder | prevstate, change
    
      prevstate := [b]ina[/b] & mask
      [b]repeat[/b]
        [b]waitpne[/b](prevstate, mask, 0)
        change := ([b]ina[/b] & mask) ^ prevstate
        [b]if[/b] (change & prevstate & encamask)
          'Do encoder A stuff on falling edge.
        [b]if[/b] (change & prevstate & encbmask)
          'Do encoder B stuff on falling edge.
        prevstate ^= change    
    
    
    


    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 10/13/2009 7:05:19 PM GMT
  • TJHJTJHJ Posts: 243
    edited 2009-10-14 18:26
    Wait will this wait for the whole transition? Don't I need 2 wait pin's to prevent it from looping around again if the pin didn't change states, while it was doing the update?

    Thanks a bunch Phil, I was thinking way to hard here..
    TJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I owe everyone here a bunch, So thanks again for answering my dumb questions.
    Projects. RG500 ECU system. PropCopter. Prop CanSat. Prop Paste Gun.
    Suzuki RG500 in a RGV 250 frame.
    Bimota V-Due (Running on the fuel injection system)
    Aprilia RS250

    Post Edited (TJHJ) : 10/14/2009 6:31:34 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-10-14 19:12
    The waitpne waits for any change from the previous state (low->high, high->low, on either or both pins). If if statement conditions are true only if the transition is high->low for the selected pin. If neither condition is true, it just goes back to the waitpne without doing anything and waits for the next change.

    NOTE: It's vitally important that ina be read only once in the loop. If your encoder logic needs the value of ina for anything, it will take two lines of code to compute a value for change, viz:

        ...
        [b]waitpne[/b](prevstate, mask, 0)
        inaval := [b]ina[/b] & mask
        change := inaval ^ prevstate
        ...
    
    
    


    Then use inaval in your code instead of ina. This will prevent weirdness should ina change between reads.

    -Phil
Sign In or Register to comment.