France
Asked
Resolved Resolved by DJ Sures!

Python Script For The Roomba - UART.Hardwareuartread Issue

Hi,

I am continuing my EZ-scripts to Python conversion and I have begin to work on my Roomba robot. All my command work with the EZ-scripts, but some of them don't with Python. I have in particular some difficulties with the reading of UART data.

Here are some part of scripts used to identify the Roomba working mode:

With EZ-Script


uartinit(0, 1, 115200) #have to have this to clear the buffer
sleep(1000)
$rxdata=0
uartWrite(0,1,142) # Mode check
uartWrite(0,1,35) # Mode check
sleep(50)
$rx = UartAvailable(0, 1)
print($rx)

if ($rx=1)
  $rxdata = UARTRead(0, 1, $rx)
  print( $rxdata )
endif

With this script, I am able to print $rxdata and then the rest of the script continues normally.

With Python


UART.InitHardwareUart(1, 115200) #have to have this to clear the buffer
sleep(1000)
UART.HardwareUartWrite(1,142) # Mode check 1
UART.HardwareUartWrite(1,35) # Mode check 2
sleep(50)
rxdata = 0
rx = UART.HardwareUartAvailable(1)
print(rx)

if rx == 1:
  rxdata = UART.HardwareUartRead(1,rx)
  print(rxdata)

With this script, i am unable to print rxdata and the console prints "Not connected" instead (and the script stop).

So, it seems that UART.HardwareUartRead does not behave like UARTRead (when UART.InitHardwareUart, UART.HardwareUartWrite and UART.HardwareUartAvailable behave as expected like their EZ-scripts equivalents).

Did I missed something ? Thanks

(By the way, the link to the Roomba manual page (https://synthiam.com/Software/Manual/Roomba-Movement-Panel-16116) does not seems to be active anymore)


Related Hardware Roomba

ARC Pro

Upgrade to ARC Pro

Unlock the true power of automation and robotics by becoming a proud subscriber of Synthiam ARC Pro.

#10  

@DJ thanks for the update. No more "not connected message"

@ptp, thanks a lot for your help and advice

Your script does not work: here is the output of the console:

Start
> Array[Byte]((1))
> OI is not responding
Done (00:00:01.3940570)

Here the output with my ezscript (post #6):

Start
> 1
> []
> 1
> OI is in PASSIVE mode
Done (00:00:01.3808620) 
#11  

@ptp, update:

I succeeded in running your code, but only when keeping separated HardwareUartAvailable and HardwareUartRead as I used to do:

import time

UART.InitHardwareUart(1, 115200)
time.sleep(1)

#discard any existing data
rx = UART.HardwareUartReadAvailable(1)

UART.HardwareUartWrite(1,142) # Mode check 1
UART.HardwareUartWrite(1,35) # Mode check 2
time.sleep(.05)

data = 0
rx = UART.HardwareUartAvailable(1)
if rx > 0:
  data = UART.HardwareUartRead(1,rx)
  print(data)
  mode = data[0]
  if mode == 0:
    print("OI is OFF")
  elif mode == 1:
    print("OI is in PASSIVE mode")
  elif mode == 2:
    print("OI is in SAFE mode")
  elif mode == 3:
    print("OI is in FULL mode")
else:
  print("OI is not responding")

I obtain the expected output

Start
> Array[Byte]((1))
> OI is in PASSIVE mode
Done (00:00:01.3202339)

It seems the solution to my script was in the line

mode = data[0]

Could you explain what [0] means ?

Thanks

PRO
USA
#12  

data is an array, data[0] is the first byte, data[1] the second byte etc.

#13  

Oh, OK. It's clear now. Thanks

#14  

Thanks to your help and advice, I have successfully converted most of my Roomba data reading scripts.

I have looked a little closer to "arrays" and using the array module, I have improved the previous script. Indeed, when using 2 lines for UART.HardwareUartWrite(), I had inconstancy in the results. By using biteArray, it is now working every time.

here is my final script:

import time
import array

UART.InitHardwareUart(1, 115200)
time.sleep(1)

#discard any existing data
rx = UART.HardwareUartReadAvailable(1)

list = [142, 35]
arr = bytearray(list)
UART.HardwareUartWrite(1, arr)
time.sleep(.05)

data = 0
rx = UART.HardwareUartAvailable(1)
if rx > 0:
  data = UART.HardwareUartRead(1,rx)
  print(data)
  mode = data[0]
  if mode == 0:
    print("OI is OFF")
  elif mode == 1:
    print("OI is in PASSIVE mode")
  elif mode == 2:
    print("OI is in SAFE mode")
  elif mode == 3:
    print("OI is in FULL mode")
else:
  print("OI is not responding")
PRO
USA
#15   — Edited

Each call to HardwareUartWrite is a round-trip so there is a latency. Visualize the path: Python-ARC-Network-WIFI-EZB-Roomba you can imagine each step adding milliseconds. The roomba firmware must have timeout to avoid getting stuck waiting for a sub-command (second byte). I presume in some cases the sub-command (second byte) is handled as the the command and as consequence ignored. That is why is important to understand all the layers involved. Like DJ said does not make sense to reinvent the wheel. reinvention may be justified if you can learn from the process, later you will appreciate the software/control.

Padawan, you are in search of knowledge, Patient you must be. Your path you must decide.:)