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 = "Getp" #Command to get position from Roo
#UARTWrite(2, 0, "1, P"+$Positon, "S"+$Arm_Speed, 0x0d) #Send serial command w/wanted variables
sleep(200)
uartWrite(2, 0, "1, Getp", 0x0d)
Sleep(100)
$x = UartAvailable(2, 0)
print("Bytes in buffer: " + $x)
Sleep(200)
$GetP = UARTRead(2, 0, $x)
print("Received: " + $Getp)
Sleep( 1000 )
if($Getp >($getp + ($getp + 19/100)))
goto(Center_Down)
ELSEif ($Getp < ($getp + ($getp + 19/100)))
goto(Center_up)
endif
:Center_Down
uartWrite(2, 0, "1, P1290 s600", 0x0d)
Sleep(4000)
uartWrite(2, 0, "1, Powerdown", 0x0d)
Halt()
:Center_up
uartWrite(2, 0, "1, P1256 s600", 0x0d)
Sleep(4000)
uartWrite(2, 0, "1, Powerdown", 0x0d)
Halt()
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.
Lol no prob
I used the split() command to receive only the data after the letter p.
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:
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):
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.