• Stars
    star
    165
  • Rank 227,610 (Top 5 %)
  • Language
    C++
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automated PID tuning using Ziegler-Nichols/relay method

arduino-pid-autotuner

Automated PID tuning using Ziegler-Nichols/relay method.

Originally written for Arduino and compatible boards, but does not rely on the Arduino standard library.

Disclaimer

Issues have been disabled on this repository due to too many off-topic questions about PID control in general or how to use this code. This project is a simple implementation of the algorithm described here and is not guaranteed to work in every use case. If you don't know what this code is intended to do, you probably don't need to use it.

How does it work?

pidautotuner.h and pidautotuner.cpp are fully commented to explain how the algorithm works.

What PID controller does this work with?

This algorithm should work with any implementation of PID control if it is properly configured.

Example code (Arduino)

#include <pidautotuner.h>

void setup() {

    PIDAutotuner tuner = PIDAutotuner();

    // Set the target value to tune to
    // This will depend on what you are tuning. This should be set to a value within
    // the usual range of the setpoint. For low-inertia systems, values at the lower
    // end of this range usually give better results. For anything else, start with a
    // value at the middle of the range.
    tuner.setTargetInputValue(targetInputValue);

    // Set the loop interval in microseconds
    // This must be the same as the interval the PID control loop will run at
    tuner.setLoopInterval(loopInterval);

    // Set the output range
    // These are the minimum and maximum possible output values of whatever you are
    // using to control the system (Arduino analogWrite, for example, is 0-255)
    tuner.setOutputRange(0, 255);

    // Set the Ziegler-Nichols tuning mode
    // Set it to either PIDAutotuner::ZNModeBasicPID, PIDAutotuner::ZNModeLessOvershoot,
    // or PIDAutotuner::ZNModeNoOvershoot. Defaults to ZNModeNoOvershoot as it is the
    // safest option.
    tuner.setZNMode(PIDAutotuner::ZNModeBasicPID);

    // This must be called immediately before the tuning loop
    // Must be called with the current time in microseconds
    tuner.startTuningLoop(micros());

    // Run a loop until tuner.isFinished() returns true
    long microseconds;
    while (!tuner.isFinished()) {

        // This loop must run at the same speed as the PID control loop being tuned
        long prevMicroseconds = microseconds;
        microseconds = micros();

        // Get input value here (temperature, encoder position, velocity, etc)
        double input = doSomethingToGetInput();

        // Call tunePID() with the input value and current time in microseconds
        double output = tuner.tunePID(input, microseconds);

        // Set the output - tunePid() will return values within the range configured
        // by setOutputRange(). Don't change the value or the tuning results will be
        // incorrect.
        doSomethingToSetOutput(output);

        // This loop must run at the same speed as the PID control loop being tuned
        while (micros() - microseconds < loopInterval) delayMicroseconds(1);
    }

    // Turn the output off here.
    doSomethingToSetOutput(0);

    // Get PID gains - set your PID controller's gains to these
    double kp = tuner.getKp();
    double ki = tuner.getKi();
    double kd = tuner.getKd();
}

void loop() {

    // ...
}

More Repositories

1

led-control

Advanced WS2812/SK6812 RGB/RGBW LED controller with on-the-fly Python animation programming, web code editor/control interface, 1D, 2D, and 3D display support, and E1.31 sACN support
Python
160
star
2

scanlight

A Better Light Source For Scanning Color Negative Film
HTML
88
star
3

smallXY

Experimental CoreXY 3D printer
15
star
4

led-ring-clock

Arduino LED analog clock
C++
13
star
5

MicroMonitor

Full-featured system monitoring menubar app for macOS with support for SMC sensors
Objective-C
12
star
6

glowstick

RGB/White LED tube light for photo, video, and light painting use
C++
9
star
7

fusion360-cycloidal-drive

Parametric cycloidal drive generator script for Fusion 360
Python
8
star
8

triangulator2

SVG triangle art generator engine. Similar to Trianglify but with more point distribution, color scale, and gradient options. MIT licensed.
JavaScript
7
star
9

liora

Modular and extensible Node.js Discord bot with lots of built-in functionality
JavaScript
6
star
10

Triangulator

Procedurally generate triangle art wallpapers for computers and mobile devices
JavaScript
4
star
11

raspi-python-st7735

Python library for using ST7735-based TFT LCDs with a Raspberry Pi
Python
4
star
12

summer-night-vscode-theme

Summer Night theme for VS Code
4
star
13

triangulator2-app

Web app for triangulator2: Versatile procedural SVG triangle art generator
JavaScript
4
star
14

HCLPicker

HCL color picker and converter
CSS
3
star
15

cura-aqua-dark-theme

Dark colorful theme for the Cura slicer v3.1
3
star
16

windows-headset-buttons

Program that allows the media control buttons on headphones/headsets to be used on Windows
Python
3
star
17

ripplecounter

Sensorless brushed DC motor position control with Raspberry Pi Pico
C
3
star
18

summer-night-syntax

Summer Night theme for Atom
CSS
2
star
19

summer-night-colors

HCL-based color palette for code editing and terminal use.
JavaScript
2
star
20

arduino-pid-control

Versatile PID control library for Arduino and Arduino-compatible microcontrollers
C++
2
star
21

npm-license-scraper

Wrapper around npm-license-crawler to download and export text of licenses in JSON format
JavaScript
1
star
22

SuperSimpleSMC

macOS SMC library with a more complete built-in list of keys including voltage/current/power keys
Objective-C
1
star
23

power-led-drivers

Small high power LED drivers (constant-current boost) with USB PD support
1
star
24

testbot86

Mobile robot platform that fits a Raspberry Pi Zero and more into a tiny 86mm square package
1
star
25

robodashboard

Node.js and React web dashboard for displaying data from and controlling teleoperated robots
JavaScript
1
star
26

l3gd20-python

Python library for the L3GD20 I2C 3-axis gyroscope
Python
1
star
27

lsm303-python

Python library for the LSM303 I2C accelerometer/magnetometer
Python
1
star