Skip to main content
NewEpisode 3a is out: Carry the conversation onWatch →
All posts
ServoHub

How a servo drive runs with an empty while(1)

ServoHub's current loop runs 20,000 times per second, yet main() ends in an empty loop. Start tracing the firmware with episode 1a.

A servo drive has work to do even when its main loop appears to do nothing. ServoHub episode 1a opens with exactly that puzzle: a current loop running 20,000 times per second, and a main() function that ends with an empty while (1).

If your first instinct is to search inside that loop for the motor-control algorithm, this is a useful place to change how you read embedded firmware.

Start with the code that is actually there

The course follows ServoHub at commit 94fc13d. In embedded_project/Y01/Core/Src/main.c, lines 203–214, board initialization precedes the endless loop. The loop body itself contains no application work.

That observation leaves a better question than “where is the missing loop body?”: what did initialization enable, and what can make the CPU run code after main() reaches this point?

An empty foreground loop can coexist with active control

The execution model to investigate is interrupt-driven control. Once configured hardware raises an interrupt, the CPU can suspend its foreground work, execute the interrupt handler, and then return. An empty foreground loop therefore says very little about the work happening in those handlers.

This is the distinction behind the opening puzzle. To understand the drive, follow the initialization path, the interrupt entry points, and the functions they invoke. Looking only at the final lines of main() hides the schedule that matters.

Episode 1a introduces that investigation. The detailed execution trace comes in episodes 1b and 1c: first the path from startup into the control loops, then interruption and communication between them. The opening episode gives you the question and the map for following it.

What you are about to read

ServoHub is open-source servo-drive firmware for robot joints. The course's reference hardware is the Y01 board with an STM32H743. Its control algorithms originate in Simulink models; generated C lives alongside the surrounding firmware.

That surrounding code matters. The project also contains communication and motion interfaces including CANopen, CiA402, EtherCAT, and MIT mode. Reading a control equation alone cannot tell you when a command becomes active, which state allows a drive to run, or how the application reaches the controller.

The course assumes familiarity with motor-control concepts such as Clarke and Park transforms, PI control, SVPWM, and encoders. Its focus is how those pieces are connected in this particular implementation, including joint-specific concerns such as dual encoders and gearing.

Read in execution order

Episode 1a lays out a practical way to study the firmware:

  • Keep the source revision fixed so paths and line numbers stay reproducible.
  • Follow calls in the order the CPU executes them.
  • Use software-in-the-loop runs to inspect concrete behavior.
  • Distinguish observed results from conclusions reached by reading code.

For the empty-loop puzzle, start by locating initialization and writing down the next execution entry points you need to inspect. The goal is to connect a file to its caller and its timing, until the apparently idle main() makes sense as part of the complete drive.

Watch the opening episode

Watch ServoHub episode 1a: Opening — what this course is.