Coder Social home page Coder Social logo

pryme / simplepid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from merose/simplepid

0.0 2.0 0.0 16 KB

A PID controller library independent of run environment, but one which works well with Arduino.

License: BSD 3-Clause "New" or "Revised" License

C++ 100.00%

simplepid's Introduction

SimplePID

A PID controller library independent of run environment, but one which works well with Arduino.

Installation

To install the library into your Arduino installation, go to the Releases tab and download the latest release. This will be a ZIP file called SimplePID-version.zip, for some version number. Unzip this into your sketchbook/libraries folder, where sketchbook is the root of your Arduino sketches. On OS X and Windows it is usually ~/Documents/Arduino. This will create a directory called SimplePID-version. Rename that directory to SimplePID.

You should then have a directory under your sketchbook directory containing the library files at:

libraries/SimplePID/

Usage

To use the SimplePID library, add #include <SimplePID.h> to the includes in your sketch. Then create an instance of the PID controller by defining a global variable:

SimplePID myPID(pConstant, iConstant, dConstant);

(Replace pConstant, iConstant, and dConstant with the proportional, integral, and differential constants you desire.) For a P-controller, set the integral and differential constants to zero, for example.

Setting the Target

The target value you want to track is usually called the setpoint. At any time you can change the setpoint by calling SimplePID.setSetPoint(someValue). For example, using the PID controller defined in the example above, we could set the target at 3.4 as follows:

myPID.setSetPoint(3.4);

Getting the Control Value

Your code needs to have some way of keeping track of time in order to determine the delta time from the last control value. A recommended way of doing that is farther down. You also need to have a way of sensing the actual error value, the difference in speed of the motor versus the desired value, or the process error. Once you have the actual error value and the delta time, in seconds, you get the control value to use like this:

float controlValue = myPID.getControlValue(actualError, dt);

For a motor, you will usually add the control value to the current control value and change the motor speed.

Keeping Track of Delta Time

One way of doing that is as follows:

#include <SimplePID.h>

SimplePID myPID(...);
unsigned long lastLoopTime;

void setup() {
    ...
    lastLoopTime = micros();
}

void loop() {
    delay(howeverLongYouLike);

    float error = ... some way of getting the actual error value ...

    unsigned long curLoopTime = micros();
    float dt = (curLoopTime - lastLoopTime) / 1E6;

    float controlValue = myPID.getControlValue(error, dt);
    ... code to control the motor or other process ...

    lastLoopTime = curLoopTime;
}

In this case dt is set to the delta time since the last control, in seconds.

An Example

There is an example of using the library to investigate PID tunings for a motor. It is specific to the DFRobot Romeo v2 Arduino board and motor driver, but should give a reasonable impression of how to use SimplePID. Open that sketch by going to Files > Examples > SimplePID > RomeoPIDTest.

simplepid's People

Contributors

merose avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.