Skip to content

Commit bfc5723

Browse files
committed
Make the external interface "pure"
This removes all internal calls to millis() and puts the responsibility of getting the millisecond count to the caller. This change removes the dependency on the Arduino standard library and allows this code to run on any platform where a millis() function is implemented.
1 parent 00b4bb4 commit bfc5723

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

PID_v1.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
* This Library is licensed under the MIT License
66
**********************************************************************************************/
77

8-
#if ARDUINO >= 100
9-
#include "Arduino.h"
10-
#else
11-
#include "WProgram.h"
12-
#endif
13-
148
#include "PID_v1.h"
159

1610
// The parameters specified here are those for for which we can't set up
@@ -31,8 +25,6 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
3125

3226
PID::SetControllerDirection(ControllerDirection);
3327
PID::SetTunings(Kp, Ki, Kd, POn);
34-
35-
lastTime = millis() - SampleTime;
3628
}
3729

3830
// To allow backwards compatability for v1.1, or for people that just want to
@@ -42,17 +34,23 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
4234
: PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E,
4335
ControllerDirection) {}
4436

37+
void PID::Begin(unsigned long now) { lastTime = now - SampleTime; }
38+
39+
void PID::Begin(PIDControllerMode mode, unsigned long now) {
40+
Begin(now);
41+
SetMode(mode);
42+
}
43+
4544
// This, as they say, is where the magic happens.
4645
// This function should be called every time "void loop()" executes.
4746
// The function will decide for itself whether a new pid Output needs to be
4847
// computed. returns true when the output is computed, false when nothing has
4948
// been done.
50-
bool PID::Compute() {
49+
bool PID::Compute(unsigned long now) {
5150
if (mode != AUTOMATIC) {
5251
return false;
5352
}
5453

55-
unsigned long now = millis();
5654
unsigned long timeChange = (now - lastTime);
5755
if (timeChange >= SampleTime) {
5856
/*Compute all the working error variables*/

PID_v1.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#ifndef PID_v1_h
2-
#define PID_v1_h
3-
#define LIBRARY_VERSION 1.2.1
1+
#pragma once
42

53
enum PIDControllerDirection { DIRECT, REVERSE };
64
enum PIDControllerMode { MANUAL, AUTOMATIC };
@@ -21,14 +19,18 @@ class PID {
2119
PID(double *, double *, double *, double, double, double,
2220
PIDControllerDirection);
2321

22+
// Sets initial state relative to current time
23+
void Begin(PIDControllerMode, unsigned long);
24+
void Begin(unsigned long);
25+
2426
// sets PID to either Manual (0) or Auto (non-0)
2527
void SetMode(PIDControllerMode Mode);
2628

2729
// performs the PID calculation. it should be
2830
// called every time loop() cycles. ON/OFF and
2931
// calculation frequency can be set using SetMode
3032
// SetSampleTime respectively
31-
bool Compute();
33+
bool Compute(unsigned long);
3234

3335
// clamps the output to a specific range. 0-255 by default, but
3436
// it's likely the user will want to change this depending on
@@ -94,4 +96,3 @@ class PID {
9496
unsigned long SampleTime;
9597
double outMin, outMax;
9698
};
97-
#endif

examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void setup() {
3030
Setpoint = 100;
3131

3232
// turn the PID on
33-
myPID.SetMode(AUTOMATIC);
33+
myPID.Begin(AUTOMATIC, millis());
3434
}
3535

3636
void loop() {
@@ -44,6 +44,6 @@ void loop() {
4444
myPID.SetTunings(aggKp, aggKi, aggKd);
4545
}
4646

47-
myPID.Compute();
47+
myPID.Compute(millis());
4848
analogWrite(PIN_OUTPUT, Output);
4949
}

examples/PID_Basic/PID_Basic.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ double Setpoint, Input, Output;
1313

1414
// Specify the links and initial tuning parameters
1515
double Kp = 2, Ki = 5, Kd = 1;
16-
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
16+
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd,
17+
PIDControllerDirection::DIRECT);
1718

1819
void setup() {
1920
// initialize the variables we're linked to
2021
Input = analogRead(PIN_INPUT);
2122
Setpoint = 100;
2223

2324
// turn the PID on
24-
myPID.SetMode(AUTOMATIC);
25+
myPID.Begin(PIDControllerMode::AUTOMATIC, millis());
2526
}
2627

2728
void loop() {
2829
Input = analogRead(PIN_INPUT);
29-
myPID.Compute();
30+
myPID.Compute(millis());
3031
analogWrite(PIN_OUTPUT, Output);
3132
}

examples/PID_PonM/PID_PonM.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ void setup() {
2424
Setpoint = 100;
2525

2626
// turn the PID on
27-
myPID.SetMode(PIDControllerMode::AUTOMATIC);
27+
myPID.Begin(PIDControllerMode::AUTOMATIC, millis());
2828
}
2929

3030
void loop() {
3131
Input = analogRead(0);
32-
myPID.Compute();
32+
myPID.Compute(millis());
3333
analogWrite(3, Output);
3434
}

examples/PID_RelayOutput/PID_RelayOutput.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ void setup() {
4040
myPID.SetOutputLimits(0, WindowSize);
4141

4242
// turn the PID on
43-
myPID.SetMode(PIDControllerMode::AUTOMATIC);
43+
myPID.Begint(PIDControllerMode::AUTOMATIC, millis());
4444
}
4545

4646
void loop() {
4747
Input = analogRead(PIN_INPUT);
48-
myPID.Compute();
48+
myPID.Compute(millis());
4949

5050
/************************************************
5151
* turn the output pin on/off based on pid output

0 commit comments

Comments
 (0)