Shop OBEX P1 Docs P2 Docs Learn Events
Help with code to simulate motor over run — Parallax Forums

Help with code to simulate motor over run

Don MDon M Posts: 1,653
edited 2011-08-14 19:23 in Propeller 1
I have some code I wrote that outputs a specific pulse train in gray code when a button is pressed. I use it to simulate output from sensors on a rotating shaft to test another device I made. The timing matches exactly with the speed at which a rotating shaft turns.

Here is my bit of code:
  repeat

    if ina[tbl_up] == 0
  
      repeat until ina[tbl_up] == 1
        outa[sen_a] := 1
        pause_ms(3)
        outa[sen_b] := 1
        pause_ms(7)
        outa[sen_a] := 0
        pause_ms(3)
        outa[sen_b] := 0
        pause_ms(32)

    if ina[tbl_dn] == 0

      repeat until ina[tbl_dn] == 1
        outa[sen_b] := 1
        pause_ms(3)
        outa[sen_a] := 1
        pause_ms(7)
        outa[sen_b] := 0
        pause_ms(3)
        outa[sen_a] := 0
        pause_ms(32)

What I would like to add to this is an amount over "over run" when I let up on the button to simulate how the machine sensor output actually looks like when the motor slows down to a stop after power has been removed from the motor. So after I release the button I would like the loop to add an additional 10 pulses then stop.

Any idea how to do this? Maybe another nested loop inside a loop?

Thanks.

Don

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-08-14 10:10
    Extend your condition in the existing inner loop.

    repeat until ina[tbl_dn] == 1 and counter == 0

    In the outer loop you can set counter to 10.

    And inside of the inner loop you would decrease counter if ina[tbl_dn]==1.

    In this if statement you could also add some instructions which extend the wait-time, as I'd expect a slower motor to give other impulses than a motor running at a constant speed.
  • Don MDon M Posts: 1,653
    edited 2011-08-14 18:32
    MagIO2- Thanks for your help. Here's what I tried and it works so far.
      repeat
      
        counter := 10
        if ina[tbl_up] == 0
      
          repeat until ina[tbl_up] == 1 and counter == 0
            outa[sen_a] := 1
            pause_ms(3)
            outa[sen_b] := 1
            pause_ms(7)
            outa[sen_a] := 0
            pause_ms(3)
            outa[sen_b] := 0
            pause_ms(32)
            counter --
    
        if ina[tbl_dn] == 0
    
          repeat until ina[tbl_dn] == 1 and counter == 0
            outa[sen_b] := 1
            pause_ms(3)
            outa[sen_a] := 1
            pause_ms(7)
            outa[sen_b] := 0
            pause_ms(3)
            outa[sen_a] := 0
            pause_ms(32)
            counter --
    
  • Don MDon M Posts: 1,653
    edited 2011-08-14 18:49
    OK I spoke too soon. If I hold either input low long enough the counter over runs past 0 so the loop keeps going until it reaches 0 again. So how do I get counter to stop at 0 and not count down past 0?
  • kuronekokuroneko Posts: 3,623
    edited 2011-08-14 18:58
    Don M wrote: »
    So how do I get counter to stop at 0 and not count down past 0?
    Only decrement when it's not null?
    if counter
        counter--
    
  • Don MDon M Posts: 1,653
    edited 2011-08-14 19:06
    I tried this and at least it will stop running but depending on when you let go of the button with relationship of the counter value it may run 10 more loops or it might run only 3 loops. So this still isn't the answer.

    Somehow I need to detect when the input goes high and then implement counter...
      repeat
      
        counter := 10
        if ina[tbl_up] == 0
      
          repeat until ina[tbl_up] == 1 and counter == 0
            if counter == 0
              quit
            outa[sen_a] := 1
            pause_ms(3)
            outa[sen_b] := 1
            pause_ms(7)
            outa[sen_a] := 0
            pause_ms(3)
            outa[sen_b] := 0
            pause_ms(32)
            counter --
    
        if ina[tbl_dn] == 0
    
          repeat until ina[tbl_dn] == 1 and counter == 0
            if counter == 0
              quit
            outa[sen_b] := 1
            pause_ms(3)
            outa[sen_a] := 1
            pause_ms(7)
            outa[sen_b] := 0
            pause_ms(3)
            outa[sen_a] := 0
            pause_ms(32)
            counter --
    
  • Don MDon M Posts: 1,653
    edited 2011-08-14 19:09
    kuroneko- I only want to decrement the counter after the input has gone high. I'm not sure what you mentioned will work. But I'm game for anything.
  • Don MDon M Posts: 1,653
    edited 2011-08-14 19:16
    Ok- here is what I have come up with and it seems to work.

    Does this look like the correct way to do this?
      repeat
      
        counter := 10
        if ina[tbl_up] == 0
      
          repeat until ina[tbl_up] == 1 and counter == 0
            if ina[tbl_up] == 1
              counter --
            outa[sen_a] := 1
            pause_ms(3)
            outa[sen_b] := 1
            pause_ms(7)
            outa[sen_a] := 0
            pause_ms(3)
            outa[sen_b] := 0
            pause_ms(32)
            if counter == 0
              quit
    
        if ina[tbl_dn] == 0
    
          repeat until ina[tbl_dn] == 1 and counter == 0
            if ina[tbl_dn] == 1
              counter --
            outa[sen_b] := 1
            pause_ms(3)
            outa[sen_a] := 1
            pause_ms(7)
            outa[sen_b] := 0
            pause_ms(3)
            outa[sen_a] := 0
            pause_ms(32)
            if counter == 0
              quit
    

    Edit: Actually it works the same if I delete the "if counter == 0 then quit" statement at the end of each loop. Doesn't look like I need them.
  • kuronekokuroneko Posts: 3,623
    edited 2011-08-14 19:19
    Don M wrote: »
    kuroneko- I only want to decrement the counter after the input has gone high. I'm not sure what you mentioned will work. But I'm game for anything.
    I just re-read your initial post and figured that one out :) In that case I'd use something like this:
    repeat until ina[tbl_up] == 1 and counter == 0
            outa[sen_a] := 1
            pause_ms(3)
            outa[sen_b] := 1
            pause_ms(7)
            outa[sen_a] := 0
            pause_ms(3)
            outa[sen_b] := 0
            pause_ms(32)
            if ina[tbl_up]
              counter --
    
    counter stays untouched and is only decremented once the input reads high. Provided you don't press the button again during that fade-out time you'll get 10 extra loop cycles.

    Update: You figured it out already. And yes, the quit is redundant. The next repeat evaluation has the same effect.
  • Don MDon M Posts: 1,653
    edited 2011-08-14 19:23
    Thanks for your help.
Sign In or Register to comment.