
Dunning-Kruger

I am have a little issue with the below code.... I am attempting to read 3 different single byte return sensors on my new Create 2.... Now, it works perfectly if I read only one sensor in the repeatuntil loop. However if I do more than one read (like the code below).... the readings report back false... i.e. the bump sensor will indicate a bump when none occurred or the IR detect will indicate a bump instead of a indicating a close proximity of an object....
So am I daft and my code is wrong or does maybe the UART buffer need to be flushed after each read?
I am stumped....
Thanks guysCode:
REPEATUNTIL(1=2)
$x=0
$y=0
$z=0
uartWrite(0,1,142,45) # Read Roomba IR sensors single byte
sleep(10)
IF (UARTAvailable(0,1)>0)
$x=UARTRead(0,1,1)
$x=GetByte($x)
ENDIF
sleep(10)
uartWrite(0,1,142,7) #Roomba Bump and wheel drop sensor single byte
sleep(10)
IF (UARTAvailable(0,1)>0)
$y=UARTRead(0,1,1)
$y=GetByte($y)
ENDIF
sleep(10)
uartWrite(0,1,142,58) # Stasis (Rommba moving forward) single byte
sleep(10)
IF (UARTAvailable(0,1)>0)
$z=UARTRead(0,1,1)
$z=GetByte($z)
ENDIF
print($x+" IR light object detected")
print($y+" bump detected")
print($z+" Is moving forward")
sleep(200)
ENDREPEATUNTIL
$x = UartAvailable(0, 1)
print("Bytes in buffer: " + $x) # checks how many bytes are in the buffer
$RX_DATA = UARTRead(0, 1, $x) # places all the bytes RX into $RX_DATA
# read 3 data bytes
$RX_0=GetByteAt($RX_DATA,0)
RX_1=GetByteAt($RX_DATA,1)
RX_2=GetByteAt($RX_DATA,2)
***Edit*** I am testing this on a Roomba 500 series and not my Create 2 yet.... The Create 2 is a 600 series so that may be the difference in the Stasis reporting different numbers that it is supposed to....
if($rx=2)
$LSB=GetByteAt($rx,0)
$MSB=GetByteAt($rx,1)
$voltage=$LSB+(256*$MSB)
Just change positions if its the other way around.
By the way my Create 2 came with only the brushes and brush cage retainer missing (in order to make it a full blown Roomba vacuum.... )... For $200 bucks, not bad.... I would buy a second one if it weren't for the fact the Canadian $ is so weak compared to the US dollar....
Code:
Code:
Either I am doing something wrong of you are correct the UART port doesn't work correctly on the EZB4
if($rx=2)
$RX_DATA = UARTRead(0, 1, $rx)
$MSB=GetByteAt($RX_DATA,0)
$LSB=GetByteAt($RX_DATA,1)
Ok the code below seems to work, however if you don't have the uartinit() in the loop the buffer just continues to grow...
Code: