unexplained gosub delay
KidE
Posts: 29
Hi All,
As a good soldier iḿ following the basics book and while doing some experimenting of my own i ran into some strange behaviour while working with gosub routines which i cannot overcome.
My 1st code runs pefectly but if i turn the pot the servo takes small steps to get to its new position.
If i remove all the gosub stuff it runs perfectly.
Why its that? i looked in my books but i just cant find a explanation for this
As a good soldier iḿ following the basics book and while doing some experimenting of my own i ran into some strange behaviour while working with gosub routines which i cannot overcome.
My 1st code runs pefectly but if i turn the pot the servo takes small steps to get to its new position.
' {$STAMP BS2} ' {$PBASIC 2.5} rcLR PIN 1 rcUD PIN 2 serLR PIN 14 serUD PIN 15 scaleFactor CON 143 offset CON 400 delay CON 10 timeLR VAR Word accuLR VAR Word timeUD VAR Word accuUD VAR Word 'Main PAUSE 1000 DO GOSUB getrc GOSUB calcPulse GOSUB dispPulse LOOP getrc: HIGH rcLR HIGH rcUD ' charge the cap PAUSE 1 ' for 1 ms RCTIME rcLR, 1, timeLR ' measure RC discharge time RETURN calcPulse: timeLR = timeLR */ scaleFactor timeLR = timeLR + offset accuLR = (accuLR + timeLR) /2 RETURN dispPulse: PULSOUT serLR, accuLR RETURN
If i remove all the gosub stuff it runs perfectly.
' {$STAMP BS2} ' {$PBASIC 2.5} rcLR PIN 1 rcUD PIN 2 serLR PIN 14 serUD PIN 15 scaleFactor CON 143 offset CON 400 delay CON 10 timeLR VAR Word accuLR VAR Word timeUD VAR Word accuUD VAR Word 'Main PAUSE 1000 DO HIGH rcLR HIGH rcUD ' charge the cap PAUSE 1 ' for 1 ms RCTIME rcLR, 1, timeLR ' measure RC discharge time timeLR = timeLR */ scaleFactor timeLR = timeLR + offset accuLR = (accuLR + timeLR) /2 PULSOUT serLR, accuLR PAUSE 10 LOOP
Why its that? i looked in my books but i just cant find a explanation for this
Comments
Maybe PhiPi or Tracy Allen can weigh in here on gosub execution speeds. Tracy has a website at http://www.emesystems.com/BS2speed.htm with lots of juicy timing info and more.
Can you "attach" both versions as actual .bs2 files? Something seems odd here. A BS2 gosub might take 750us, so say 2.25ms extra for the gosubs. Still that would be like having a PAUSE 2 or PAUSE 3 in the second program, not a PAUSE 10 or PAUSE 100.
Edit: Zoot beat me to it!
Try a different servo, at any rate.
In the program with the GOSUBs, you need to do the same thing. You need to add a variable PAUSE based on the RCTIME value. It's just that the PAUSE time would be slightly different because of the difference in the overall execution time of the main loop.
DO
GOSUB getrc
GOSUB dispPulse
GOSUB calcPulse
GOSUB dispPulse
LOOP
BTW, did you leave any DEBUG statements in your first program? Does "dispPulse" mean "display Pulse" on a debug screen? That would clearly slow down a program and make your servo stutter, my good soldier.
I stripped and added a debug and this cleared the issue. With DEBUG ON you have a servo with huge jitter. with DEBUG OFF it runs very smooth as desired.
And up went the learning curve again ;-)
Thanks all for the good feedback (in which i find this forum really excels)