Python RCInput/PWM problem with threads

I have written some custom multirotor control code in Python3 and after having some stabilization problems I have traced the problem to the RCInput and PWM output libraries. It is taking approximately 0.02 and 0.08sec to complete a read of and write to four channels. I went back to the example scripts provided by EMLID and added some timing code to them and it took approximately 0.00032sec to read four channels and 0.00068sec to write to four channels so clearly the problem is originating in my code.

In the custom code I have a thread running some AHRS code that uses the MPU9250 and I noticed that when I prevented the thread from starting, the coprocessor read and write times were what they were in the example scripts, but with the AHRS thread running the times were 0.02 and 0.08 respectively. Based on the pinout for the Navio2 shown in the docs, the coprocessor is on SPI1 with the MPU9250 on SPI0. However, according to the Raspberry Pi foundation (SPI Documentation) only SPI0 is accessible on the GPIO of the Pi3. I’m assuming then that the coprocessor is actually on the same SPI bus as the MPU9250 which is probably why the read and write times are slow.

My questions are:
Are the coprocessor and MPU9250 on the same SPI bus?

Is there any way to speed up the read and write times with the MPU9250 running in a separate thread? Right now the main control loop is barely running at 10Hz which is way to slow to provide stable control.

They are on different buses SPI1 and SPI0. RCIO processor has a bus to itself, it also performs writes asynchronously, so that your code execution never waits.

Managed to solve the problem. I was using the ‘threading’ module which apparently does not play well with the GIL. I switch over to using the ‘multiprocessing’ module and now both the AHRS and control loops are running at 500+Hz.

1 Like