pbasic question
Annoying
Posts: 50
this problem seems really simple but I can't find the answer in pbasic tutorials:
I want a sequence of actions to take place once a condition is met. this code sequence is not valid: IF (condition)THEN DO UNTIL ...THEN DO UNTIL ...THEN DO UNTIL... etc.· do you know what code would accomplish this? Thanks.
I want a sequence of actions to take place once a condition is met. this code sequence is not valid: IF (condition)THEN DO UNTIL ...THEN DO UNTIL ...THEN DO UNTIL... etc.· do you know what code would accomplish this? Thanks.
Comments
IF {condition} THEN DO UNTIL {condition} DO UNTIL {condition} DO UNTIL {condition} ENDIF
Remember that this is PBasic 2.5 and you'll need the proper directive at the beginning of your program.
the motor on one robot turns as it should in the first loop, and I want it to immediately turn off once y2 < 1930. for some reason the PULSOUT 3, 750
following this loop does not cause the motor to stop turning. it stops turning after like 30 seconds...
IF y2 >3120 THEN
DO UNTIL y2 <1930
PULSOUT 3, 1000
PAUSE 20
PULSOUT 2, 1200
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), ' Wait for start character
y2.HIGHBYTE, y2.LOWBYTE] ' Get y-axis word
LOOP
PAUSE 5
PULSOUT 3, 750
PAUSE 1
DO UNTIL y1 > 3116
PULSOUT 3, 750
etc.
Is the SERIN all on one line? You've put a comment after part of the SERIN and continued the SERIN on a 2nd line.
In any event, putting a SERIN like this in a movement loop won't work since a servo motor expects a control pulse every 20ms. If it doesn't get a control pulse, it shuts down until it sees another pulse. That causes jerky movement at best.
The single PULSOUT 3,750 won't stop the motor. The motor needs to see a series of 1.5ms (PULSOUT 3,750) pulses roughly every 20ms. It shouldn't take 30 seconds to stop the motor if it's getting a series of 1.5ms pulses.
If the 2nd DO loop is like the 1st and the Stamp doesn't see serial data for periods of time, the servo will shut down which may allow it to coast for a while. Still, 30 seconds seems like a lot. Can't tell without more of the program.
'{$STAMP BS2}
'{$PBASIC 2.5}
y1 VAR Word
y2 VAR Word
Main:
PULSOUT 2, 1200
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
IF y2 >3120 THEN
DO UNTIL y2 <1930
PULSOUT 3, 1000
PAUSE 20
PULSOUT 2, 1200
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
LOOP
DO UNTIL y1 > 3116
PULSOUT 3, 750
PULSOUT 15, 1200
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
PULSIN 5, 1, y1
PAUSE 10
LOOP
DO UNTIL y2 > 3120
PULSOUT 3, 1000
PAUSE 20
PULSOUT 2, 1200
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
LOOP
DO UNTIL y1 < 1880
PULSOUT 3, 750
PULSOUT 15, 1200
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
PULSIN 5,1, y1
PAUSE 10
LOOP
ENDIF
GOTO Main
It works at first and then gets weird...
thanks.
What's hooked to pin 5?
I'd shorten the PAUSE 20. Try PAUSE 10. 20ms is about the limit between servo control pulses and the SERIN will take some time.
The transmitting robot is transmitting more often than the receiving robot can receive.· Maybe the delay in the transmit loop should be larger than that in the receive loop.· The receiver will wait for the data to be received.
·
'{$STAMP BS2}
'{$PBASIC 2.5}
y1 VAR Word
y2 VAR Word
Main:
PULSOUT 2, 1200
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
IF y2 >3120 THEN
DO UNTIL y2 <1930
PULSOUT 3, 1000
PAUSE 10
PULSOUT 2, 1200
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
DEBUG "loop one"
LOOP
PULSIN 5, 1, y1
DO UNTIL y1 > 3100
DEBUG "loop two"
PULSOUT 3, 750
PULSOUT 15, 1200
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
PULSIN 5, 1, y1
PAUSE 30
LOOP
DO UNTIL y2 > 3120
PULSOUT 3, 1000
PAUSE 10
PULSOUT 2, 1200
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
LOOP
DO UNTIL y1 < 1880
PULSOUT 3, 750
PULSOUT 15, 1200
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
PULSIN 5,1, y1
PAUSE 30
LOOP
ENDIF
GOTO Main
What you're trying to do is, unfortunately, very problematic with an unassisted Stamp because, when the Stamp is waiting for input data, it's not keeping track of the time and the servo control pulses have to be produced roughly every 20ms. The only good timekeeping mechanism available here is the PAUSE statement and, when the Stamp is paused, it can't listen to the serial input line and will miss anything that comes in. You have some advantage in that the information is repeated so, if you miss one value, you will catch the next one.
You might consider using a ServoPAL which would allow the Stamp to only have to do something with the servo when the setting changes. See: www.parallax.com/tabid/768/txtSearch/servopal/List/0/SortField/4/Default.aspx
maybe this is obvious but I'm actually using motors controlled through the HB-25 motor controller, so would the ServoPAL be able to be used with it?
thanks again!
IF ... PULSOUT 3,1000 DO UNTIL PULSOUT 3,750 DO UNTIL PULSOUT 3,1000 DO UNTIL PULSOUT 3,750 DO UNTIL ENDIF
By the way, why are you doing a PULSOUT to the receiver? That can damage the receiver's output pin.
'{$STAMP BS2}
'{$PBASIC 2.5}
y1 VAR Word
y2 VAR Word
Main:
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
IF y2 >3120 THEN
PULSOUT 3, 1000
DO UNTIL y2 <1930
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
LOOP
PULSIN 5, 1, y1
PULSOUT 3, 750
DO UNTIL y1 > 3100
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
PULSIN 5, 1, y1
LOOP
PULSOUT 3, 1000
DO UNTIL y2 > 3120
SERIN 2, 16468, [noparse][[/noparse]WAIT("!"), y2.HIGHBYTE, y2.LOWBYTE]
LOOP
PULSOUT 3, 750
DO UNTIL y1 < 1880
SEROUT 15, 16468, [noparse][[/noparse] "!", y1.HIGHBYTE, y1.LOWBYTE]
PULSIN 5,1, y1
LOOP
ENDIF
GOTO Main