I am running a relatively fast control loop on a Teensy 4.1 which is controlling 2 BLDC motors based on some sensor inputs. The cycle time of the control loop is about 600 microseconds. I have a monitoring mode where the Teensy sends some data to the connected computer over the USB serial every loop cycle (less than 100 bytes). This data is received by a Python program that plots it (similar to the Serial Plotter in the Arduino IDE, but with a little bit more control).
This is mostly working fine, but in certain situations I experience an issue where the plot and the motor control is repeatedly halting for short periods (sub-second duration). This is quite repeatable, but it only happens when in monitoring mode. The only thing the Teensy does differently in the monitoring mode is sending the data over Serial, and there should be plenty of time for this (the Teensy is not particularly busy during most of the loop cycle, it's just waiting for interrupts from sensors). I'm not so surprised that the Python program is struggling with plotting all this data (I have needed to optimize that code a bit get it to work decently), but I wasn't expecting that to spill over to the Teensy control loop. So I'm guessing that because the Python program is busy with graphics, it slips behind on reading the Serial data, and because of this the Teensy somehow gets stuck trying to send data. That's the most reasonable explanation I can think of, but I don't know enough about serial communication to tell if this is actually possible.
So my question is if the explanation I outlined above seems plausible, and if so, what can I do to solve the issue? The monitoring mode is important, but it's acceptable to have these occasional short halts in plotting or missing monitoring data. What I really want to avoid is that it affects the performance of the Teensy's main task (the control loop). If it's something like a buffer that gets full, maybe my Teensy code could just check for that and skip sending data whenever the buffer is full?
I have run the Python program on both Linux and Windows, but only had the problems on Windows. This could be because the Linux computer is more powerful or because I have used it less. The serial data sent consists of 4 long integer values sent as text using a Serial.printf like this:
This is mostly working fine, but in certain situations I experience an issue where the plot and the motor control is repeatedly halting for short periods (sub-second duration). This is quite repeatable, but it only happens when in monitoring mode. The only thing the Teensy does differently in the monitoring mode is sending the data over Serial, and there should be plenty of time for this (the Teensy is not particularly busy during most of the loop cycle, it's just waiting for interrupts from sensors). I'm not so surprised that the Python program is struggling with plotting all this data (I have needed to optimize that code a bit get it to work decently), but I wasn't expecting that to spill over to the Teensy control loop. So I'm guessing that because the Python program is busy with graphics, it slips behind on reading the Serial data, and because of this the Teensy somehow gets stuck trying to send data. That's the most reasonable explanation I can think of, but I don't know enough about serial communication to tell if this is actually possible.
So my question is if the explanation I outlined above seems plausible, and if so, what can I do to solve the issue? The monitoring mode is important, but it's acceptable to have these occasional short halts in plotting or missing monitoring data. What I really want to avoid is that it affects the performance of the Teensy's main task (the control loop). If it's something like a buffer that gets full, maybe my Teensy code could just check for that and skip sending data whenever the buffer is full?
I have run the Python program on both Linux and Windows, but only had the problems on Windows. This could be because the Linux computer is more powerful or because I have used it less. The serial data sent consists of 4 long integer values sent as text using a Serial.printf like this:
Code:
Serial.printf("data %d %d %d %d\n", value1, value2, value3, value4);