I'm having a bit of "sometimes" issue with calling for feedback through the EZB's Uart port from my Kangaroo. Maybe I'm not writing my script properly. I guess I'm a little unsure how the bytes are read by EZ Builder and how fast to set a loop at and send it through the Uarts.
I'm using the Kangaroo for position and speed control and it's attached to a Sabertooth. I'll start the motor moving that it's controlling and start the below script in a loop in EZ Builder. The script is asking for motor position feedback from the Kangaroo (the Roo is using an encoder for feedback). I want to know when the motor gets to a spot or while moving or even to the end of a move.
Most of the time the script will run and preform properly. Sometimes the script will stop and I'll get an error on the last line where it's told to:
print("Received: " + $Getp) . The error says something like an improper command or string quots " ".
Here's the Script that I know has no errors (see below it for my observations):
uartWrite(2, 0, "1, Getp", 0x0d)
$x = UartAvailable(2, 0)
print("Bytes in buffer: " + $x)
if ($x < 7) # Go back and wait for data again because we did not receive the least number of expected bytes
$Getp = UARTRead(2, 0, $x)
print("Received: " + $Getp)
I've noticed that:
*When run just once I'll never error and always get a position response.
*It helps (I think) but does not stop the error to place a small sleep() in the script but this slows down the loop.
I seem to get the error if there are more Bytes in the buffer before my " if ($x < 7)" command sent.
*If I know from past successful attempts that I'll need 8 bytes to get a correct reading and if I set the if "($x < 7)" four bytes to hi or low I'll either get the error or the script will loop for ever.
How is the best way to figure out the value of the bites I need to set the "if ($x < 7)" command at?
I've also noticed that sometimes when I know I'll get back a position feedback like 1,P850 , I'll only get back 1,P8 or even 1,P85. Has this got something to do with the bytes returned? Am I just asking for data too fast through the Uart ports? *confused*