Excellent question. This is a fundamental consideration when working with control algorithms on microcontrollers.
The short answer is: it depends entirely on which specific STM32 processor you are using and your performance requirements.
Let's break it down into the different scenarios.
Scenario 1: Your STM32 has a Hardware FPU
Many modern STM32 series have a hardware Floating-Point Unit (FPU). This is a dedicated part of the processor core designed to handle floating-point math extremely quickly.
- Which processors?
- Cortex-M4F (e.g., STM32F4, STM32G4, STM32L4+)
- Cortex-M7 (e.g., STM32F7, STM32H7)
- Do you need float emulation?
- No. You do not need software emulation because the hardware does the work for you.
- Recommendation:
- You should absolutely use
float variables for your PID controller. It's the easiest, most intuitive, and most efficient way. The hardware FPU will execute float calculations (like error * Kp) in just a few clock cycles.
Important: You must ensure the FPU is enabled in your project settings. In STM32CubeIDE, this is usually handled automatically, but you can check under Project Properties > C/C++ Build > Settings > Tool Settings > MCU Settings. The FPU should be set to "FPv4-SP-D16" (for M4) or "FPv5-SP-D16" (for M7).
Scenario 2: Your STM32 does NOT have a Hardware FPU
Older or lower-power STM32 series are based on cores that lack a hardware FPU.
- Which processors?
- Cortex-M0 (e.g., STM32F0)
- Cortex-M0+ (e.g., STM32L0)
- Cortex-M3 (e.g., STM32F1, STM32L1)
- Do you need float emulation?
- Yes, if you use the
float data type. When the compiler sees you performing math on float variables, it will automatically link in a software library that emulates the behavior of an FPU using integer-only instructions.
- The Trade-off:
- Ease of Use: Writing the PID code is simple and readable, just like in Scenario 1.
- Performance Penalty: Float emulation is very slow. A single floating-point multiplication can take hundreds of clock cycles, compared to just a few on a chip with an FPU. This can consume a significant amount of your CPU's processing time.
- Code Size Penalty: The float emulation library will increase the size of your final binary file, consuming more Flash memory.
The Alternative: Fixed-Point Arithmetic
For processors without an FPU, the high-performance alternative to using floats is fixed-point arithmetic.
- What is it? You use standard integer types (
int16_t, int32_t) to represent fractional numbers. You do this by defining an implicit or "virtual" decimal point. For example, you might decide that the lower 16 bits of a 32-bit integer represent the fractional part.
- How it works in PID:
- You scale up all your PID gains (Kp, Ki, Kd) by a large factor (e.g., 2^10 = 1024).
- You perform all your PID calculations using standard integer math.
- When you need the final output, you scale the result back down by the same factor (using a fast bit-shift right
>> 10).
Comparison of Fixed-Point vs. Float Emulation (on a non-FPU chip):
| Feature | Float Emulation (using float) | Fixed-Point (using int) |
|---|
| Performance | Slow. Consumes many CPU cycles. | Very Fast. Uses native integer instructions. |
| Ease of Use | Easy. Code is intuitive (output = Kp * error). | Complex. Requires careful scaling and bit-shifting. |
| Code Size | Larger. Requires linking a software library. | Smaller. No extra libraries needed. |
| Risk | Low. The float type handles a huge range of values. | High. Prone to overflow/underflow if not scaled correctly. |
| Readability | High. The values in the debugger look like real-world units. | Low. The values are scaled integers and are not intuitive. |
Summary and Final Recommendation
Here is a simple decision guide:
-
Does your STM32 have an FPU (F4, F7, H7, G4, L4+)?
- Yes: Use
float variables. It's the best and easiest solution.
-
Does your STM32 lack an FPU (F0, F1, F3, L0, L1)?
- Is your PID loop slow? (e.g., controlling temperature, running < 100 Hz).
- Yes: Using
float with software emulation is likely perfectly acceptable. Start with this approach because it's easy to implement and debug.
- Is your PID loop fast? (e.g., high-speed motor control, running > 1 kHz) or is your CPU already heavily loaded with other tasks?
- Yes: Float emulation might be too slow and bog down your processor. You should strongly consider implementing a fixed-point PID controller for maximum performance.
Pro Tip: The Arm CMSIS-DSP library, which is included with STM32Cube packages, provides highly optimized PID controller functions for both floating-point and fixed-point (Q15, Q31) formats. Using this library is often the best choice, as it's professionally written, tested, and faster than a simple manual implementation.