Project contents
Telemetry outputs causing loop slowdown
A blocking print statement used most of a 5 ms control loop, and the log it produced was enough to work out how slow the loop really was.
This was probably the most interesting bug I have run into so far, as it was basically caused by the print statements I added to find other control bugs. It was also quite different to what I’m used to, because most of my work has been with web based applications and sites (I have been working with Arduino and Pi based projects over the past 2 years or so, but not an STM board before this project).
To give you a bit of background, the balance car is meant to run its control loop every 5 ms. This matches how often the MPU6050 produces a new reading. However, what I noticed when actually looking at the outputted telemetry was that every 23 or so lines, the angle would freeze and then suddenly jump.
angle=436 bal=159 L=159 R=159 batt=502
angle=436 bal=159 L=159 R=159 batt=592 <- angle frozen, battery still changing
angle=544 bal=264 L=264 R=264 batt=570 <- angle jumps 1.08 degrees
The angle is multiplied by 100 here, so 436 is 4.36 degrees. You’ll notice above that the angle is frozen but the battery value keeps changing (don’t mind the low voltage, that’s still something I am working on). This shows the loop was still running, it just wasn’t getting a new angle. The MPU6050 puts each reading into a FIFO buffer (first in first out queue) and the code I set up takes one out each loop. What is happening here is that because the loop is slower than 5 ms, the queue fills up, and once it’s full the driver throws everything away. The frozen line here indicates that. It’s essentially the empty read after the reset, and the jump is caused by all the readings that got thrown away (basically skipped).
Once I realised the problem (with a bit of help from Claude for debugging, admittedly), I started to work out just how long each loop was. You can see how I calculated the loop length below.
C = 1024 B FIFO / 32 B packet = 32 packets capacity
L = 23 samples per block
P = 1 + C/L = 2.39 packets produced per loop
loop = 2.39 x 5 ms = ~12 ms, against a nominal 5
Basically, the first line calculates how many readings the FIFO can hold. So 1024 / 32 = 32, which means it can hold 32 readings.
The next line, L = 23, is from the log and shows that the FIFO went from empty to full every 23 loops.
The third line is the calculation for the packet readings. Each loop the sensor adds P readings and we take 1 away. This means the queue grows by P - 1 readings each loop. It fills up in 23 loops, so 23 × (P - 1) = 32, which gives P = 1 + 32/23 = 2.39.
Next, the sensor makes one reading every 5 ms, so if 2.39 readings arrive during one loop, the loop takes roughly 12 ms instead of the intended 5 ms.
What was actually causing the issue
Probably the biggest issue was the print statement itself. Again, this is something that I have had to get used to when coming from the web/data world, where I do love a bit of print debugging. At 115200 baud each character takes 86.8 µs, and the processor waits for every character to finish sending. My print statement for the telemetry line was around 40 characters long, so:
40 characters x 86.8 us = 3.47 ms
Yes, just that print statement was taking up around 3.47 ms (70%) of the 5 ms loop. That was pretty wild to me when I realised.
The fix as you can probably guess was pretty simple, just print less. Printing every 20th loop (or around 10 times a second) cut this to around 3% of the 5 ms loop. I also changed the sensor code to read all the waiting readings each loop and only keep the newest, so it catches up if it ever falls behind.
Lesson learned: print statements aren’t free on a microcontroller, so count the characters and do the maths.