Skip to the content.

Mycila::PID — Detailed Guide

This document describes the Mycila::PID controller provided by MycilaUtilities. It consolidates header documentation (src/MycilaPID.h) and adds examples and best practices.

Overview

Mycila::PID is a compact, efficient, and flexible PID controller designed for embedded applications (Arduino/ESP32). It supports:

PID Simulator available in examples/PIDSimulator (video link)

Key API

Header: #include <MycilaPID.h>

Modes explained

Time sampling

When setTimeSampling(true) is enabled:

The controller measures dt internally using micros() and updates on every compute() call.

Reverse mode

When reverse is enabled, the controller inverts the signs of Kp/Ki/Kd internally, effectively reversing the direction of the control action (useful for cooling systems or inverted actuators).

Output limits and anti-windup

Lifecycle patterns

Quick start example

#include <MycilaPID.h>

Mycila::PID pid;

void setup() {
  pid.setSetpoint(0.0f);
  pid.setTunings(0.1f, 0.2f, 0.05f);
  pid.setOutputLimits(-300.0f, 4000.0f);
  pid.setIntegralCorrectionMode(Mycila::PID::IntegralCorrectionMode::CLAMP);
  pid.setProportionalMode(Mycila::PID::ProportionalMode::ON_INPUT);
  pid.setDerivativeMode(Mycila::PID::DerivativeMode::ON_INPUT);
  pid.setTimeSampling(false);
  pid.setReverse(false);
}

void loop() {
  float measured = readSensor();
  float out = pid.compute(measured);
  applyActuator(out);
}

JSON integration example

#define MYCILA_JSON_SUPPORT
#include <ArduinoJson.h>
#include <MycilaPID.h>

Mycila::PID pid;

void dumpPid(JsonDocument& doc) {
  JsonObject obj = doc.to<JsonObject>();
  pid.toJson(obj);
}

Best practices

Troubleshooting

License

MIT (see project LICENSE)


PIDSimulator example — Solar diversion demo

The PIDSimulator example (examples/PIDSimulator/PIDSimulator.ino) showcases Mycila::PID in a simulated solar-diversion scenario.

What it simulates

Web UI and controls

How to run

  1. Build and flash examples/PIDSimulator/PIDSimulator.ino to an ESP32
  2. Connect to the esp-captive Wi‑Fi access point
  3. Open http://192.168.4.1/
  4. Tune parameters and observe how Solar/Grid/Output/Load evolve in real time

Tip: Start with conservative gains (e.g., small Ki/Kd) and enable output limits with CLAMP integral correction to reduce overshoot.