Python library issue

General discussion of using Roboclaw motor controllers
Madmatt
Posts: 5
Joined: Sat Jan 26, 2019 9:57 am
Python library issue

Post by Madmatt »

Hi all I have some issues with python library, hope some1 can help me out:
I'm trying to use the python library on a software called touchdesigner(windows 10) to control an engine from serial port.
it supports python and external modules.

I'm trying to do some basic functions but i cant communicate with the board.
At the moment i m still connected via usb serial.

this is the code I m trying to use:

from roboclaw import Roboclaw

#Windows comport name
rc = Roboclaw("COM7",38400)
#Linux comport name
#rc = Roboclaw("/dev/ttyACM0",115200)

rc.Open()

address = 0x80

rc.ForwardM1(address,32)

i got some errors as output:

python >>>
Traceback (most recent call last):
File "</project1/text1:op('/project1/text1').run()>", line 1
td.Error: File "/project1/text1", line 12
File "C:\Program Files\Derivative\TouchDesigner099\bin\lib\roboclaw.py", line 647, in ForwardM1
return self._write1(address,self.Cmd.M1FORWARD,val)
File "C:\Program Files\Derivative\TouchDesigner099\bin\lib\roboclaw.py", line 311, in _write1
self._sendcommand(address,cmd)
File "C:\Program Files\Derivative\TouchDesigner099\bin\lib\roboclaw.py", line 126, in _sendcommand
self._port.write(chr(address))
File "C:\Program Files\Derivative\TouchDesigner099\bin\lib\serial\serialwin32.py", line 308, in write
data = to_bytes(data)
File "C:\Program Files\Derivative\TouchDesigner099\bin\lib\serial\serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: '\x80'
Results of run operation resulted in exception. <type:textDAT path:/project1/text1> <class 'td.textDAT'>
python >>>


Can someone help me to figure out what s happening?
Madmatt
Posts: 5
Joined: Sat Jan 26, 2019 9:57 am
Re: Python library issue

Post by Madmatt »

just to add the issue here seems to be related to unicode string conversion of the address (0x80 - which is expressed in hex seems)
Madmatt
Posts: 5
Joined: Sat Jan 26, 2019 9:57 am
Re: Python library issue

Post by Madmatt »

sorry again forgot to say i m using a roboclaw 2*15 a motor controller
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Python library issue

Post by Basicmicro Support »

What version of Python are you using? My guess is Python 3? Python 3 doenst allow certain conversions that Ptyhon 2.7 supported. Contact support@basicmicro.com for a pre-release of a Python 3 version of the library assuming you are using Python 3.
mattj
Posts: 7
Joined: Thu Aug 01, 2019 8:04 am
Re: Python library issue

Post by mattj »

Hi all,

I am running two motors off of the 2x15A micro-controller. I have had no issues using Motion Studio, but have been unable to communicate with the micro-controller using python. I am currently using python 3.5. My code:

Code: Select all

import time
from roboclaw import Roboclaw

rc = Roboclaw("COM10",9600)

rc.Open()                   
address = 0x80             

while (1):                  
    rc.ForwardM2(address, 64) 
    time.sleep(3)              
    rc.ForwardM2(addess, 0)    
The error I am getting is:

C:\Users\Me\Desktop\Here\roboclaw_python>python PWMTest.py
Traceback (most recent call last):
File "PWMTest.py", line 10, in <module>
rc.ForwardM2(address, 64)
File "C:\Users\Richard R\Desktop\Here\roboclaw_python\roboclaw.py", line 661,
in ForwardM2
return self._write1(address,self.Cmd.M2FORWARD,val)
File "C:\Users\Richard R\Desktop\Here\roboclaw_python\roboclaw.py", line 313,
in _write1
self._sendcommand(address,cmd)
File "C:\Users\Richard R\Desktop\Here\roboclaw_python\roboclaw.py", line 128,
in _sendcommand
self._port.write(chr(address))
File "C:\Users\Richard R\AppData\Local\Programs\Python\Python35-32\lib\site-pa
ckages\serial\serialwin32.py", line 308, in write
data = to_bytes(data)
File "C:\Users\Richard R\AppData\Local\Programs\Python\Python35-32\lib\site-pa
ckages\serial\serialutil.py", line 63, in to_bytes
raise TypeError('unicode strings are not supported, please encode to bytes:
{!r}'.format(seq))
TypeError: unicode strings are not supported, please encode to bytes: '\x80'

I am wondering if the python 3 library has been released or if there is any way to fix this? Thanks in advance!
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Python library issue

Post by Basicmicro Support »

The current python library has been tested with Python 3. We have not seen this error before and it appears the error is happening in the pyserial library. Make sure you have the latest version of that. It thinks the data array of bytes is a Unicode string which is unusual, to say the least.

I recommend downloading the current library again from our website just to be sure you have the latest version. Also download the latest pyserial for Python 3.
mattj
Posts: 7
Joined: Thu Aug 01, 2019 8:04 am
Re: Python library issue

Post by mattj »

I have re-downloaded your python library and updated my pyserial library and still no luck. I have run it on python 3.5.2 as well as on python 3.7.2 on a separate computer. Thanks again for the responses!
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Python library issue

Post by Basicmicro Support »

Post a bare minimum program that shows the problem. With no external dependancies(eg if it requires special hardware other than the Roboclaw I wont be able to test it).
mattj
Posts: 7
Joined: Thu Aug 01, 2019 8:04 am
Re: Python library issue

Post by mattj »

Code: Select all

import time
from roboclaw import Roboclaw

#Windows comport name
rc = Roboclaw("COM14",9600)
#Linux comport name
#rc = Roboclaw("/dev/ttyACM0",115200)

rc.Open()
address = 0x80

rc.ForwardMixed(address, 0)
rc.TurnRightMixed(address, 0)

while(1):
	rc.ForwardMixed(address, 32)
	time.sleep(2)
	rc.BackwardMixed(address, 32)
	time.sleep(2)
	rc.TurnRightMixed(address, 32)
	time.sleep(2)
	rc.TurnLeftMixed(address, 32)
	time.sleep(2)
	rc.ForwardMixed(address, 0)
	rc.TurnRightMixed(address, 32)
	time.sleep(2)
	rc.TurnLeftMixed(address, 32)
	time.sleep(2)
	rc.TurnRightMixed(address, 0)
	time.sleep(2)
I'm just using the provided mixed pwm program
mattj
Posts: 7
Joined: Thu Aug 01, 2019 8:04 am
Re: Python library issue

Post by mattj »

This issue was resolved by making edits to the roboclaw.py code provided in the download

Post Reply