Resolved by DJ Sures!

## Help Using A Returned Position Value From Kangaroo In Ez-Script

I'm needing some help figuring out a way to use a returned position value from a Kangaroo X2 in a EZ-Script. What I want to do is take that value and use it in an "If"/"ElseIF" statement, do a couple "Less Than, Greater Than" comparisons and send the script to one of two sub sections. The problem I think I'm having is the returned value is not a clean number that the computer can do math with. For example; I'll send a position value to the Kangaroo of 1800 to move the motor into one position. After the move and I send a request for a position it will return a value of 1,p1800. All's well because it looks like the motor is in the proper spot. But I want to use that value to compare it to the center point of 1290 so I can move back to center.

Any idea how I can use that returned 1,p1800 in an ez script to compare it to a number like 1290 or chance it into a number I can use? I'm sending these commands through the Uart port using SimpleSerial Commands.

Here's the script I'm trying to get to work. assuming the motor has already moved to the endpoint of 1800 (or even the other side of center at 900) and I want to get it back to 1290 (center). It will run and in the script window I can see it's returning the proper position reading the script is asking for. However it will always go to the same subsection weather that reading is above 1290 or below. I have a feeling that the script is not recognizing the way the returned position value is formatted and cannot do the math(1,p1800). However my logic in the script may also be screwed up :

``````
\$Arm_Speed = 600 # Speed that arm runs at (will Stay in this script)

\$Positon = 1290 # Arm Center position sent to Roo in serial command

\$Getp = &quot;Getp&quot; #Command to get position from Roo

#UARTWrite(2, 0, &quot;1, P&quot;+\$Positon, &quot;S&quot;+\$Arm_Speed, 0x0d) #Send serial command w/wanted variables

sleep(200)

uartWrite(2, 0, &quot;1, Getp&quot;, 0x0d)
Sleep(100)
\$x = UartAvailable(2, 0)
print(&quot;Bytes in buffer: &quot; + \$x)

Sleep(200)

Sleep( 1000 )

if(\$Getp &gt;(\$getp + (\$getp + 19/100)))
goto(Center_Down)
ELSEif (\$Getp &lt; (\$getp + (\$getp + 19/100)))
goto(Center_up)
endif

:Center_Down
uartWrite(2, 0, &quot;1, P1290 s600&quot;, 0x0d)
Sleep(4000)
uartWrite(2, 0, &quot;1, Powerdown&quot;, 0x0d)
Halt()

:Center_up
uartWrite(2, 0, &quot;1, P1256 s600&quot;, 0x0d)
Sleep(4000)
uartWrite(2, 0, &quot;1, Powerdown&quot;, 0x0d)
Halt()

``````

ARC Pro is your passport to a world of endless possibilities in robot programming, waiting for you to explore.

PRO
Synthiam
#1
``````
# Speed that arm runs at (will Stay in this script)
\$Arm_Speed = 600

# Arm Center position sent to Roo in serial command
\$Positon = 1290

# Command to get position from Roo
\$Getp = &quot;Getp&quot;

UARTWrite(2, 0, &quot;1, P&quot;+\$Positon, &quot;S&quot;+\$Arm_Speed, 0x0d) #Send serial command w/wanted variables

sleep(200)

uartWrite(2, 0, &quot;1, Getp&quot;, 0x0d)

:waitForData

Sleep(100)

\$x = UartAvailable(2, 0)
print(&quot;Bytes in buffer: &quot; + \$x)

IF (\$x &lt; 4)

# Go back and wait for data again because we did not receive the least number of expected bytes
goto(waitForData)

ENDIF

Sleep( 1000 )

# 1,pXXXX is returned. Get everything after the p
\$position = Split(\$GetP, &quot;p&quot;, 1)

# I have no idea what dave is doing here...
IF (\$position &gt; (\$position + (\$position + 19/100)))
goto(Center_Down)
ELSEIF (\$position &lt; (\$position + (\$position + 19/100)))
goto(Center_up)
ENDIF

:Center_Down
uartWrite(2, 0, &quot;1, P1290 s600&quot;, 0x0d)
Sleep(4000)
uartWrite(2, 0, &quot;1, Powerdown&quot;, 0x0d)
Halt()

:Center_up
uartWrite(2, 0, &quot;1, P1256 s600&quot;, 0x0d)
Sleep(4000)
uartWrite(2, 0, &quot;1, Powerdown&quot;, 0x0d)
Halt()

``````
#2

Wow, thanks for your quick help with the new script. Love your remark about not having any idea what I'm doing with that one command. I guess I don't really know either. I was trying different math formulas and different approaches that I half know how to code. This was the last try before I gave up and I guess that one was wrong. Looks like I was trying to compare the returned position to itself and add a few position points through division. Looks more like desperation. I'll have a look tomorrow as it's bed time and Monday morning comes too soon. Thank again!

EDIT: COOL! I see the main clue (among others). You showed me a way to split up the returned position value and just use the numbers, changed them to a \$Position variable and changed my stupid GetP comparison to the \$Position variable. I'm truly grateful! I'll give it a try tomorrow after work if I don't have to stay late.

PRO
Synthiam
#3

Lol no prob

I used the split() command to receive only the data after the letter p.

#4

I've spent a little time with this revamped script you helped me with DJ. It's working great and I didn't have to tweak it much at all. Only one little problem that I luckily stumbled across the answer.

I noticed that the Split function seems to be Case sensitive. When asking for a return position from the Kangaroo it will return a value starting with either a Capital P showing that the move has been completed or a Lowercase p showing that the move is still in progress. The Split command you showed me was written with the Lowercase p. You skillfully edited my sad little script and added that simple but cool loop that makes it go back and check to see if the Uart buffer has the proper info loaded. That loop makes sure the move is complete before it reads the buffer so I'll always get a Uppercase P returned. In short I needed only to change your lowercase p to an Uppercase P before the script would work properly. Here is a comparison:

``````
Nonworking:
\$position = Split(\$GetP, &quot;p&quot;, 1)
Working:
\$position = Split(\$GetP, &quot;P&quot;, 1)

``````

Here's my final script and assumes the motor is in a different position other than center and will bring it back to center (I'm sure I still need to tweak it a little):

``````
\$Positon = 1290 #Arm Center position Kangaroo is shooting for
\$Getp = &quot;Getp&quot; # Command to get position from Roo

uartWrite(2, 0, &quot;1, Getp&quot;, 0x0d)

:waitForData

\$x = UartAvailable(2, 0)
print(&quot;Bytes in buffer: &quot; + \$x)

if (\$x &lt; 4)
# Go back and wait for data again because we did not receive the least number of expected bytes
goto(waitForData)
endif

Sleep( 1000 )

\$Roo_position = Split(\$GetP, &quot;P&quot;,1) #1,pXXXX is returned. Get everything after the P -(P is case senceitive)

# Send script to proper subscript depending on if position is above or below center
if (\$Roo_position &gt; \$Positon)
goto(Center_Down)
ELSEif (\$Roo_position &lt; \$Positon)
goto(Center_up)
endif

:Center_Down #Center is different depending on if arm comes from below or above.
uartWrite(2, 0, &quot;1, P1290 s600&quot;, 0x0d)
Sleep(4000)
uartWrite(2, 0, &quot;1, Powerdown&quot;, 0x0d)
Halt()

:Center_up #Center is different depending on if arm comes from below or above.
uartWrite(2, 0, &quot;1, P1275 s600&quot;, 0x0d)
Sleep(4000)
uartWrite(2, 0, &quot;1, Powerdown&quot;, 0x0d)
Halt()

``````

Again DJ, thanks. You've opened a door for me. I think this was the basic key I needed to keep going and finish my scripting of my B9 arm.