Spin Code Help
p00ndawg
Posts: 70
[b]pub forward ' forward movement PulsOut(0, 1_600) PulsOut(23, 1_600) [/b] pub right_turn PulsOut(0, 1_300) PulsOut(23, 1_620) pub left_turn PulsOut(0, 1_620) PulsOut(23, 1_300) pub stop PulsOut(23, 1_475) PulsOut(0, 1_500) pri Pause(duration) 'This function pauses program execution for approximately [noparse][[/noparse]duration] micro seconds 'so 1_000_000 would be approximately one second. Doesnt account for instruction 'execution time overhead. if duration < 381 duration := 381 'lower bound limit. anything lower than this, we 'have to wait until the counter comes back around, 'which is MUCH longer than [noparse][[/noparse]duration]. waitcnt(((clkfreq / 1_000_000) * duration) + cnt) [b]pri PulsOut(pin, duration) 'similar to the BS2 command. This function pulses the [noparse][[/noparse]pin] for [noparse][[/noparse]duration]. 'This is extremely useful for controlling servos and speed controllers. dira[noparse][[/noparse]pin]~~ 'make pin an output outa[noparse][[/noparse]pin]~~ 'make pin go high waitcnt(((clkfreq / 1_000_000) * duration) + cnt) 'pause execution outa[noparse][[/noparse]pin]~ 'make pin go low [/b] pri pulsin(pin) : time | cnt1, cnt2 dira[noparse][[/noparse]Pin]~ ' Make I/O Pin Input waitpne(0, |< Pin, 0) ' Wait For Pin To Go HIGH cnt1 := cnt ' Store Current Counter Value waitpeq(0, |< Pin, 0) ' Wait For Pin To Go LOW cnt2 := cnt time := (||(cnt1 - cnt2) / (clkfreq / 1_000_000))
im using this for movement using the msr1 and the 2 hb 25's connected to two motors, although as another user mentioned to me my pulsout method gets stuck in a counter rollover.
I have used these variations of the pulsout method,
pub pulsout(pin, duration) duration := (duration * (clkfreq / 1_000_000)) #> 381 dira[noparse][[/noparse]pin]~~ !outa[noparse][[/noparse]pin] waitcnt(duration) !outa[noparse][[/noparse]pin]
pub pulsout(pin, us) if (pin => 0) and (pin =< 31) and (us > 0) us := (clkfreq / 1_000_000 * us) #> 381 outa[noparse][[/noparse]pin]~~ !dira[noparse][[/noparse]pin] waitcnt(us + cnt) !dira[noparse][[/noparse]pin]
with little success. The 2nd pulsout however, executes all the code I have set up it just doesnt actually move the wheels. The other versions tend to get stuck the first time I call forward and never continue.
Post Edited (p00ndawg) : 6/21/2010 5:21:27 PM GMT
Comments
In the first, you've got WAITCNT(duration). That can't work because WAITCNT doesn't use a duration, it uses an absolute time (system clock value). You'd have to use WAITCNT(cnt + duration).
In the second, you're toggling the I/O pin between high (1) and input. That would work if the I/O pin had a pulldown resistor, but I don't think that's the way things are set up.
same result as others.
Post Edited (p00ndawg) : 6/21/2010 5:09:54 PM GMT
tried this as well, was sure it would work and it didnt. Still getting a massive stall in execution when waiting for the system counter.
best regards
Stefan
I just pulled this quote from the propellor manual. is the manual wrong?
ok now im getting a little annoyed.
Here is my full code. Another one of my group members here came and loaded up his code which provided similar functionality to mine, using the pulsout and pause methods provided, and he got no code stalling.
is there something that anyone can quickly see that I am not?
Post Edited (p00ndawg) : 6/21/2010 10:25:29 PM GMT
this is all of it sorry
Sometimes those other objects are changed by mistake, and render your program unusable. It would help to diagnose the problem.
Also, when posting code on the forum page, the forum software will sometimes corrupt the code when using the· "code tags".
Jim
Post Edited (hover1) : 6/22/2010 12:54:16 AM GMT
Thanks for the tip.
There is a hold-off time of 5 ms where the HB-25 will ignore incoming pulses. As a result, the unit should
not be refreshed more frequently than about 5.25 ms + pulse time. Pulse time can be anywhere from
0.8 ms to 2.2 ms. If the HB-25 receives a pulse outside of this range, it will temporarily shut off the
motor until it receives a valid pulse.
We still don't have all of you code. distance_back is not defined. If all the ping distances are greater than distance/distance_back you will send pulses too fast to the HB-25s. Instead of the PulsOuts in the else sections, call Forward I would change Forward to
pub forward
'pin 0 = left wheel pin 23 = right wheel
PulsOut(0, 1_600)
PulsOut(23, 1_600)
pause(10_000)
John Abshier
Make the minimum pulse width or minimum pause = 400. Servos won't respond to anything less than 500us anyway.
In what you've posted you have two declarations for pulsout since Spin ignores case. I don't know why the compiler didn't flag it.
Jim
Darn Mike, I was going to mention the stack, but I got distracted.
Post Edited (hover1) : 6/22/2010 1:13:26 AM GMT
I also replaced all the pulses with proper functions for right, left, and forward.
repeat
I couldn't find in in the manual but I found in a PE Lab that the other cogs continue running.
Making people finde part of the code in the zip file and another part of the code in the post is not conductive to getting help.
However, the logic of you program probably doesn't work as you thing. Assume that inch1 is > distance and inch2 is < distance. I think you want to turn right. But what you will do is go forward then turn right. I think what you want is (summarized pseudo code)
if inches1 < distance
right_turn
elseif inches2 < distance
right_turn
...
elseif inches10 < distance_back
right_turn
else
forward
John Abshier
Post Edited (John Abshier) : 6/22/2010 7:55:34 PM GMT
Ive reset it a few times and it seems to always get stuck in forward motion and all the ping sensors turn off.
Post Edited (p00ndawg) : 6/22/2010 8:27:46 PM GMT
John Abshier
It would seem to me however that the sensor_scan cog is stopping, because they all go blank and it just goes forward.
Im not sure if this is helpful because I dont have any 3 prong led's for the msr1 so I used the led's on the tsl230 color sensor lol, but it got stuck in forward motion and the led's turned off.
Next I ran this for the movement class:
and when it got stuck the led kept blinking.
Post Edited (p00ndawg) : 6/23/2010 5:26:41 PM GMT
I found the bug!
Thanks for all the help guys. Really appreciated!
John Abshier
you live and you learn I guess.