简体   繁体   中英

Python fast calculating and slow Serial writing : Multithread or Multiprocess

I have a robotics project, basically a path tracking problem. In PC, a reference generation algorithm is implemented in Python3.65. The algorithm takes Indoor GPS data and use these continuously-updated data to calculate the reference path for a robot-car. Of course, the algorithm runs in a while True: .... framework. The algorithm can work well only if the sampling frequency is quite high, say 0.001s. However, the problem is that, after calculating the reference path, the path info needs to be written to Serial port of the PC, byte by byte, via Serial.write(). This serial.write() function is still a for loop. And this writing process is very slow (more than 0.02s for 16 bytes). If the for loop is included in the while True: framework, something like:


while True:
  Data = Ref_generation()
  Bytes_Full = Float_2_Bytes_List(Data)
  for ele in Bytes_Full:
    Serial.write(ele)  # VERY SLOW!!!
  sleep(0.001)

Then, the Data can not be calculated correctly since the cycle period is much longer than 0.001s.

In a nutshell, how can I separate the fast calculating algorithm from the slow serial.wtite()? I tried multithreads, but not works.

Anyhelp will be appreciated, thanks a lot!

You don't need to leverage multiple cpu cores, all you want is to wait for the serial port... Your CPU will be idle, waiting ... Spawning new threads/processes to just wait is a waste...

That's why you should try to use some asynchronous IO solution.

Example, use https://github.com/pyserial/pyserial-asyncio or https://twistedmatrix.com/documents/16.1.0/api/twisted.internet.serialport.SerialPort.html

These asynchronous frameworks allow you to register events and have your function called automatically when they finish, all in a single thread/process .

They also allow you to schedule events in the time you want.

I had a similar issue, I used daemon thread for running Serial communication part. So try running Serial communication on a daemon thread which will run in the background of the host process. This will enable your program to run tasks in while independently of the wait time required for Serial communication. This way you can separate fast calculating algorithm from the slow serial.wtite().

You can find more about daemon thread here . Further to avoid buffer lag you can use reset_output_buffer() from pySerial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM