Asked — Edited
Resolved Resolved by mtiberia!

Uart Receive Query. Buffer Flush Command?

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 guys


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


ARC Pro

Upgrade to ARC Pro

Get access to the latest features and updates before they're released. You'll have everything that's needed to unleash your robot's potential!

#9  

Interesting try placing the initiate UART inside the loop. That should flush the buffer.

#10  

@mtiberia That didn't work either... the first read gives me the out of bounds of the array error.... This is the same problem I had way back when I first tried to do this with my old create 1... I was able to read single byte data but not anything with more than 1 byte.... This works on an Arduino and on my ancient Basic Atom Pro....

Either I am doing something wrong of you are correct the UART port doesn't work correctly on the EZB4

#11  

I just noticed an error in your code, I'm fighting the need for reading glasses, but try changing your code to the following.

if($rx=2) $RX_DATA = UARTRead(0, 1, $rx) $MSB=GetByteAt($RX_DATA,0)
$LSB=GetByteAt($RX_DATA,1)

#12  

That was my bad the above should work for you.

#13  

You're eyes are just fine... good catch dude....Can't believe I missed that as well....

Ok the code below seems to work, however if you don't have the uartinit() in the loop the buffer just continues to grow...


REPEATUNTIL(1=2)
 uartinit(0, 1, 115200) #used to flush the buffer
sleep(20)
   $RX_DATA=0
  uartWrite(0,1,142,22) # Volatge check
  sleep(10)
  $rx = UartAvailable(0, 1)
  
  print($rx+" bytes in the buffer")
  
  IF ($rx=2)
    $RX_DATA = UARTRead(0, 1, $rx)
    $MSB=GetByteAt($RX_DATA,0)
    $LSB=GetByteAt($RX_DATA,1)
    $voltage=$LSB+(256*$MSB)
    print($voltage)
  ENDIF 
  sleep(225)
  
ENDREPEATUNTIL