Help with code to simulate motor over run
Don M
Posts: 1,653
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:
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
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
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.
Somehow I need to detect when the input goes high and then implement counter...
Does this look like the correct way to do this?
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.
Update: You figured it out already. And yes, the quit is redundant. The next repeat evaluation has the same effect.