SX/B Subroutine Parameters
Brian Riley
Posts: 626
I wrote some code for my SX48 Robot that looks like
the remaining subs are essentially the same just the numbers to the servos are different the idea being if the routine is called with no paremeter it just starts the Bot moving Forward or backward or stops it while it awaits some senoer input to cause it to decide to do something different. If it gets called with a parameter that number is multiplied by 250 (1/4 second) and the pause command lets it move in that direction for that amount of time before being free to read sensors.
If I declare the SUB with 1 parameter I get an "invalid number of parameters errors" for the parameterless call if I declare it
I get the same error against a call to the sub with parameters.
The simple workaround is to make semi-duplicate SUBS, but that is wasteful. The documentation is fairly clear on 1 or 2 paremeters, but I just cannot figure out how to appease the compiler for selectin between 0 and 1 parameter.
Is this a bug, if not how do I make it work?
TNX
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
cheers ... brian riley, n1bq, underhill center, vermont
GoForward SUB 1 GoBack SUB 1 StopBot SUB 1 GoForward: s1Pos = 75 s2Pos = 200 IF __PARAMCNT = 1 THEN temp1 = __PARAM1 pause temp1*250 ENDIF RETURN
the remaining subs are essentially the same just the numbers to the servos are different the idea being if the routine is called with no paremeter it just starts the Bot moving Forward or backward or stops it while it awaits some senoer input to cause it to decide to do something different. If it gets called with a parameter that number is multiplied by 250 (1/4 second) and the pause command lets it move in that direction for that amount of time before being free to read sensors.
If I declare the SUB with 1 parameter I get an "invalid number of parameters errors" for the parameterless call if I declare it
GoForward SUB 0 1
I get the same error against a call to the sub with parameters.
The simple workaround is to make semi-duplicate SUBS, but that is wasteful. The documentation is fairly clear on 1 or 2 paremeters, but I just cannot figure out how to appease the compiler for selectin between 0 and 1 parameter.
Is this a bug, if not how do I make it work?
TNX
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
cheers ... brian riley, n1bq, underhill center, vermont
Comments
GO_FORWARD····· SUB··· 0, 1
Then check and pick-up any parameters before doing anything else in your sub -- otherwise the internal variables that carry parameters (__PARAMx) could get clobbered.
GO_FORWARD:
· IF __PARAMCNT = 1 THEN
··· temp1 = __PARAM1
· ELSE
··· temp1 = 1
· ENDIF
· servo1 = 75
· servo2 = 200
· IF temp1 > 0 THEN
··· PAUSE temp1 * 250
··ENDIF
· RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 9/22/2005 9:17:23 PM GMT
The declarations:
WAIT_US··SUB·1, 2·········· ··' delay in microseconds
WAIT_MS··SUB·1, 2·············' delay in milliseconds
And the code:
' Use: WAIT_US microseconds {, multiplier}
' -- multiplier is optional
WAIT_US:
· temp1 = __PARAM1·········· ·' get microseconds
· IF __PARAMCNT = 1 THEN···· ·' if no multiplier
··· temp2 = 1··········· ··· ·'·· set to 1
· ELSE························' else
··· temp2 = __PARAM2······· ··'·· get multiplier
· ENDIF
· IF temp1 > 0 THEN···········' no delay if either 0
··· IF temp2 > 0 THEN
····· PAUSEUS temp1 * temp2···' do the delay
··· ENDIF
· ENDIF
· RETURN
' Use: WAIT_MS milliseconds {, multiplier}
' -- multiplier is optional
WAIT_MS:
· temp1 = __PARAM1········· ·' get milliseconds
· IF __PARAMCNT = 1 THEN··· ·' if no multiplier
··· temp2 = 1············ · ·'·· set to 1
· ELSE····················· ·' else
··· temp2 = __PARAM2······· ·'·· get multiplier
· ENDIF
· IF temp1 > 0 THEN········ ·' no delay if either 0
··· IF temp2 > 0 THEN
····· PAUSE temp1 * temp2··· ' do the delay
··· ENDIF
· ENDIF
· RETURN
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Thanks Jon. This helps a lot. I am on the road in NJ with just my PowerBook. But to my recollection the example code shows some subroutines fpr DELAY and DELAYUS that shows
without the comma. (or I misread it! ... mea culpa!)
Also thanks for the routines you posted I am going to add them to the serial string handling routines as part of a 'standard include' file I am building.
When I get back early next week (my #1 son is gettin married tomorrow) look for some SX48 Robot code coming forth with IR sensing collison avoidance.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
cheers ... brian riley, n1bq, underhill center, vermont