Email me
A missing pair of braces killed one wheel
← Self-balancing robot

Learning & Issues

A missing pair of braces killed one wheel

Uninitialised timer structs left one motor dead, with no error, and the same code worked in Yahboom's build.

Most of the failures and bugs I have experienced so far during the rebuild in C++ have been pretty silly really. Things like a missing pair of braces that killed one of the wheels (see code below) and some other minor stuff. The missing pair of braces actually took a bit of time to sort out, as even with Claude and Codex helping me out, I didn’t spot the problem initially. In fact, the AI models probably made it worse, as they kept stating it was probably a motor failure or some sort of battery issue (I have been having some issues with the battery readings). It was only after flashing the old Yahboom code and proving that both motors did indeed work that they stopped suggesting that. What it really took was reading the code thoroughly from top to bottom and then actually working through the system flow (lesson learned here).

// was
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef       TIM_OCInitStructure;

// now
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure{};
TIM_OCInitTypeDef       TIM_OCInitStructure{};

The error was essentially caused by the fact that the timer configuration structs were declared without initialisers. This meant that whatever field I did not set held whatever happened to be on the stack at the time.

Both motors are driven by TIM8, with each motor using two PWM pins to set its direction (see below).

WheelPinTIM8 channel
LeftPC6CH1
LeftPC7CH2
RightPC8CH3
RightPC9CH4

TIM8 is an advanced-control timer, so the channels 1 through to 3 have comlimentary outputs. If you are wondering, a complimentary output is a second pin that carries the inverted copy of a channel’s PWM signal. When CH1 is high, CH1N is low and vice versa (see my very rough diagram below).

CH1N is CH1 inverted, on a second pin

In addition to the above, the library’s setup functions for those channels read the complimentary-output and idle state settings from the struct. As these fields were never set due to my mistake, they were essentially filled with garbage values.

The library then writes those values into registers that all four channels share, without masking them first. In short, this means that random bits could end up changing another channel’s settings such as its output polarity. What happended in my case is that left one wheel dead, while the other worked fine.

What made this even more confusing is that this same error worked in the code provided by Yahboom with Keil. I understand that an uninitialised viariable is undefined behaviour, so it can work with one complier and not another, but if you have any more knowledge on this I would love to know so please send a message.

The fix ultiamtely was just adding the to both declarations. The funny thing is that I had already done this in other parts of the codebase, so it was just a silly error that lead to quite a bit of lost time. Using initialises the value to 0 in C++ if no other value is provided.