Calling roboclaw functions from 2 python threads

General discussion of using Roboclaw motor controllers
Post Reply
rudasi
Posts: 5
Joined: Thu May 12, 2016 5:03 pm
Calling roboclaw functions from 2 python threads

Post by rudasi »

I have 2 separate python threads that are calling roboclaw functions to read current data. Each thread locks before calling the respective roboclaw function then unlocks. I am getting the following error,

the stack trace goes from ReadCurrents to _read4 to _readlong to _readbyte with a final error in read of
buf = os.read(self.fd, size-len(read))
OSError: [Error 11] Resource temporarily unavailable.

Has anyone seen this and know how to fix this. I found some posts online suggesting to open the serial port with
a timeout=None but that didnt work. Is there a way to make the read call blocking so it waits till its available?

Thanks,
Rahul
rudasi
Posts: 5
Joined: Thu May 12, 2016 5:03 pm
Re: Calling roboclaw functions from 2 python threads

Post by rudasi »

I was using my locks incorrectly, it works now.
marcuso
Posts: 23
Joined: Sat Jun 18, 2016 2:36 pm
Re: Calling roboclaw functions from 2 python threads

Post by marcuso »

Hi,

I'm curious as to how you are dealing with multiple threads, as I'm going to be doing something similar soon, not only reads though, but also driving the motors from different threads as well (not sharing the same motor across threads, but each thread having it's own motor for example).

I presume you are only locking when calling into the SDK due to the fact the robo claw SDK is not thread safe code ? Do you have to lock everything before calling anything in the SDK if using multiple threads ? I presume this is due to statics/globals being used in the SDK, or it not locking critical sections itself.

Have you put timeouts on your object locks to make sure you get no deadlocks/livelocks/starvation ?

Thanks.
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Calling roboclaw functions from 2 python threads

Post by Basicmicro Support »

There are two problems with the Roboclaw Python library in regards to using it from multiple threads.

1. The global variables. The global port variable needs to be changed to a member variable and the library into a class. I beleive there are one or two other global variables that would need to be changed as well. This is primaryly if you need to use multple roboclaws on different USB/Serial ports.

2. Serial ports are by definition unsafe to used form multiple threads without locking. You must prevent one thread from trying to use the same serial port at the same time as another thread is trying to use it.

Eventually we will upgrade the python library into a class. Im not sure if we will ever add thread safe support though.
marcuso
Posts: 23
Joined: Sat Jun 18, 2016 2:36 pm
Re: Calling roboclaw functions from 2 python threads

Post by marcuso »

Is the code public ? I could make it thread safe.
User avatar
Basicmicro Support
Posts: 1594
Joined: Thu Feb 26, 2015 9:45 pm
Re: Calling roboclaw functions from 2 python threads

Post by Basicmicro Support »

Of course. All the libraries we provide the source code to are open source. if you want to modify it and then submit a copy, we'll run our normal tests on it and replace the old library or add that one as a second option.

Post Reply