Try Embedded

Pico 2: a PID speed loop in float, double and fixed point

A Raspberry Pi Pico 2 times one PID speed controller three ways, float on the Cortex-M33's FPU, double on the RP2350's coprocessor and Q16.16 fixed point,…

Motors · Intermediate

In a real product: Motor speed controller (Robotics): A robot's wheel controller runs a PID loop every millisecond: the maths has to fit in that time, every time.

A PID controller works out a motor's drive from the error between the speed wanted and the speed there is, a thousand times a second. On the Pico's Cortex-M0+, float maths is a library call for every add and multiply, so firmware for small cores is written in fixed point. The Pico 2's Cortex-M33 has a floating-point unit, and the RP2350 adds a double-precision coprocessor (DCP) beside each core. This sketch runs the same controller and motor model for 20 000 steps in float, in double and in Q16.16 integers, prints what each costs, and then runs the float one live: a knob on GP26 sets the target speed and the LED on GP15 shows the drive.

What you'll learn

  • A PID step: proportional, integral (kept inside the drive's range, so it does not wind up) and derivative terms
  • Q16.16 fixed point: a value times 65536 in an int32_t, and why a product needs 64 bits before it is shifted back
  • What the M33's FPU and the RP2350's DCP change: on a Pico 2, float is about as fast as fixed point
  • Timing code with micros(), and volatile to stop the compiler working a loop out ahead

How it works

The motor is modelled in the sketch: its speed follows five times the drive with a 50 ms lag (speed += (5 × drive − speed) × 0.02 each millisecond). The controller's gains are for that 1 ms period: drive = 0.8 × error + integral + 2.0 × (error − last error), the integral growing by 0.012 × error each step, and both clamped to the drive's 0 to 255.

PidF, PidD and PidQ are the same controller in float, double and Q16.16. In Q16.16 a gain such as 0.8 is 0.8 × 65536 = 52428, and qmul() multiplies two such numbers in 64 bits and shifts the product back by 16. All three settle at the target of 1000. At start-up the sketch times 20 000 steps of each (20 s of control) and prints:

20000 PID steps, each way:
float  (FPU)     8830 us  speed 1000.001
double (DCP)    61025 us  speed 1000.000
Q16.16 (ints)    6023 us  speed 1000.000
double takes 6.9x float's time, fixed point 0.7x
target  821.1  speed  839.2  drive 165.3
target  821.1  speed  821.6  drive 164.2

A float step takes about 0.44 µs, some 66 clock cycles at 150 MHz. double goes through the DCP's instructions, still in hardware but several per operation, and fixed point is a little quicker than float. Built for a Pico (RP2040) the same sketch prints about 135 000 µs for float, 254 000 µs for double and 20 000 µs for fixed point: there, fixed point is seven times faster than float, which is why it was the rule on small cores. The times above are the simulator's, which counts each instruction's cycles; a real board's are close, and change a little with the compiler.

After that the live loop runs one float step a millisecond. The knob sets the target from 0 to 1200, the drive goes to the LED with analogWrite, and a line is printed every 250 ms. Turn the knob and the speed follows within about 100 ms, with no overshoot.

Try this

  • Raise KP to 3: the speed reaches the target faster, then overshoots and rings.
  • Set KI_DT to 0 and watch the speed settle short of the target: without the integral a P-D controller keeps an error.
  • Build the sketch for the Pico (its board in the diagram) and compare the three times.

sketch.ino

// Raspberry Pi Pico 2 (Arduino core): one PID speed loop timed three ways (float on the Cortex-M33's FPU, double on the
// RP2350's double-precision coprocessor, Q16.16 fixed point in integers), then run live: a knob on GP26 sets the
// target speed and an LED on GP15 shows the motor drive.
const int KNOB = 26, DRIVE_LED = 15;
const int STEPS = 20000;            // 20 s of control at 1 kHz

// The controller (gains for a 1 ms period) and the motor it drives: the speed follows GAIN x drive with a 50 ms lag.
const float KP = 0.8f, KI_DT = 0.012f, KD_DT = 2.0f, GAIN = 5.0f, ALPHA = 0.02f;
volatile float target = 1000.0f;    // volatile: the compiler cannot work the loops out ahead

template <typename T> T clampDrive(T v) { return v < 0 ? 0 : v > 255 ? 255 : v; }

// One controller step: the drive for this error.
struct PidF { float iterm = 0, last = 0; float step(float error) {
  iterm = clampDrive(iterm + KI_DT * error);                 // the integral, kept inside the drive's range
  float drive = clampDrive(KP * error + iterm + KD_DT * (error - last));
  last = error;
  return drive;
} };

struct PidD { double iterm = 0, last = 0; double step(double error) {
  iterm = clampDrive(iterm + 0.012 * error);
  double drive = clampDrive(0.8 * error + iterm + 2.0 * (error - last));
  last = error;
  return drive;
} };

// Q16.16: a value times 65536 in an int32_t. A product needs 64 bits before it is shifted back.
const int32_t Q = 65536;
int32_t qmul(int32_t a, int32_t b) { return (int32_t)(((int64_t)a * b) >> 16); }
int32_t toQ(float v) { return (int32_t)(v * Q); }
int32_t clampQ(int32_t v) { return v < 0 ? 0 : v > 255 * Q ? 255 * Q : v; }
struct PidQ { int32_t iterm = 0, last = 0; int32_t step(int32_t error) {
  iterm = clampQ(iterm + qmul(toQ(0.012f), error));
  int32_t drive = clampQ(qmul(toQ(0.8f), error) + iterm + qmul(toQ(2.0f), error - last));
  last = error;
  return drive;
} };

__attribute__((noinline)) float runFloat() {
  PidF pid; float speed = 0;
  for (int i = 0; i < STEPS; i++) speed += (GAIN * pid.step(target - speed) - speed) * ALPHA;
  return speed;
}

__attribute__((noinline)) double runDouble() {
  PidD pid; double speed = 0, goal = target;
  for (int i = 0; i < STEPS; i++) speed += (5.0 * pid.step(goal - speed) - speed) * 0.02;
  return speed;
}

__attribute__((noinline)) float runFixed() {
  PidQ pid; int32_t speed = 0, goal = toQ(target), gain = toQ(GAIN), alpha = toQ(ALPHA);
  for (int i = 0; i < STEPS; i++) speed += qmul(qmul(gain, pid.step(goal - speed)) - speed, alpha);
  return speed / (float)Q;
}

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 3000) delay(10);   // the USB port open (up to 3 s), so the first lines are seen
  analogReadResolution(12);
  pinMode(DRIVE_LED, OUTPUT);

  Serial.printf("%d PID steps, each way:\n", STEPS);
  uint32_t t0 = micros(); float f = runFloat();  uint32_t tf = micros() - t0;
  t0 = micros(); double d = runDouble();         uint32_t td = micros() - t0;
  t0 = micros(); float q = runFixed();           uint32_t tq = micros() - t0;
  Serial.printf("float  (FPU)   %6lu us  speed %.3f\n", tf, f);
  Serial.printf("double (DCP)   %6lu us  speed %.3f\n", td, d);
  Serial.printf("Q16.16 (ints)  %6lu us  speed %.3f\n", tq, q);
  Serial.printf("double takes %.1fx float's time, fixed point %.1fx\n", (float)td / tf, (float)tq / tf);
}

// Live: one float step a millisecond towards the knob's speed (0 to 1200), the drive on the LED.
PidF live;
float speed = 0;
uint32_t printed = 0;

void loop() {
  target = analogRead(KNOB) * 1200.0f / 4095;
  float drive = live.step(target - speed);
  speed += (GAIN * drive - speed) * ALPHA;
  analogWrite(DRIVE_LED, (int)drive);
  if (millis() - printed >= 250) {
    printed = millis();
    Serial.printf("target %6.1f  speed %6.1f  drive %5.1f\n", target, speed, drive);
  }
  delay(1);
}

Lessons behind it