Skip to content

Commit 5aefaae

Browse files
committed
Add doctest based tests ported from br3ttb/Arduino-PID-Library#107
1 parent 8f46d27 commit 5aefaae

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

PIDController_test.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2+
#include <doctest/doctest.h>
3+
4+
// Test cases adapted from https://github.com/br3ttb/Arduino-PID-Library/pull/107
5+
6+
#include "PID_v1.h"
7+
8+
TEST_CASE("direct") {
9+
// Define Variables we'll be connecting to
10+
double Setpoint, Input, Output;
11+
12+
// Specify the links and initial tuning parameters
13+
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, DIRECT);
14+
15+
// initialize the variables we're linked to
16+
Input = 50;
17+
Setpoint = 100;
18+
19+
unsigned long ms = 0;
20+
21+
// turn the PID on
22+
myPID.Begin(AUTOMATIC, ms);
23+
for (int i = 0; i < 1000; i++) {
24+
ms += 200;
25+
26+
if (myPID.Compute(ms)) {
27+
Input = Input + (Output - Input) / 25.6;
28+
}
29+
}
30+
31+
CHECK(Setpoint == round(Input));
32+
}
33+
34+
TEST_CASE("reverse") {
35+
// Define Variables we'll be connecting to
36+
double Setpoint, Input, Output;
37+
38+
// Specify the links and initial tuning parameters
39+
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);
40+
41+
// initialize the variables we're linked to
42+
Input = 50;
43+
Setpoint = 100;
44+
45+
unsigned long ms = 0;
46+
47+
// turn the PID on
48+
myPID.Begin(AUTOMATIC, ms);
49+
for (int i = 0; i < 1000; i++) {
50+
ms += 200;
51+
52+
if (myPID.Compute(ms)) {
53+
Input = Input + (Input - Output) / 25.6;
54+
}
55+
}
56+
57+
CHECK(Setpoint == round(Input));
58+
}
59+
60+
TEST_CASE("mode") {
61+
// Define Variables we'll be connecting to
62+
double Setpoint, Input, Output;
63+
64+
// Specify the links and initial tuning parameters
65+
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);
66+
67+
// initialize the variables we're linked to
68+
Input = 50;
69+
Setpoint = 100;
70+
71+
unsigned long ms = 0;
72+
73+
// turn the PID on
74+
myPID.Begin(MANUAL, ms);
75+
for (int i = 0; i < 1000; i++) {
76+
ms += 200;
77+
78+
if (myPID.Compute(ms)) {
79+
Input = Input + (Input - Output) / 25.6;
80+
}
81+
}
82+
83+
CHECK(50 == round(Input));
84+
}
85+
86+
87+
TEST_CASE("getFunctions") {
88+
89+
// Define Variables we'll be connecting to
90+
double Setpoint, Input, Output;
91+
92+
// Specify the links and initial tuning parameters
93+
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);
94+
95+
// initialize the variables we're linked to
96+
Input = 50;
97+
Setpoint = 100;
98+
99+
// turn the PID on
100+
myPID.SetMode(AUTOMATIC);
101+
102+
// std::cout << "Input = " << Input << "; Output = " << Output << std::endl;
103+
CHECK(2 == myPID.GetKp());
104+
CHECK(5 == myPID.GetKi());
105+
CHECK(1 == myPID.GetKd());
106+
CHECK(REVERSE == myPID.GetDirection());
107+
CHECK(AUTOMATIC == myPID.GetMode());
108+
}
109+
110+
TEST_CASE("sampleTimeWorks") {
111+
112+
// Define Variables we'll be connecting to
113+
double Setpoint, Input, Output;
114+
115+
// Specify the links and initial tuning parameters
116+
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);
117+
118+
// initialize the variables we're linked to
119+
Input = 50;
120+
Setpoint = 100;
121+
122+
unsigned long ms = 0;
123+
124+
bool flag = false;
125+
126+
127+
// turn the PID on
128+
myPID.SetMode(AUTOMATIC);
129+
for (int i = 0; i < 1000; i++) {
130+
ms += 200;
131+
132+
if (myPID.Compute(ms)) {
133+
flag = true;
134+
Input = Input + (Input - Output) / 25.6;
135+
}
136+
}
137+
138+
CHECK(flag == true);
139+
}
140+
141+
TEST_CASE("sampleTimeNotWorks") {
142+
143+
// Define Variables we'll be connecting to
144+
double Setpoint, Input, Output;
145+
146+
// Specify the links and initial tuning parameters
147+
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, REVERSE);
148+
149+
// initialize the variables we're linked to
150+
Input = 50;
151+
Setpoint = 100;
152+
153+
unsigned long ms = 0;
154+
155+
bool flag = false;
156+
157+
// turn the PID on
158+
myPID.Begin(MANUAL, ms);
159+
for (int i = 0; i < 1000; i++) {
160+
ms += 199;
161+
if (myPID.Compute(ms)) {
162+
flag = true;
163+
Input = Input + (Input - Output) / 25.6;
164+
}
165+
}
166+
167+
CHECK(flag == false);
168+
}

shell.nix

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let
2+
pkgs = import <nixpkgs> { };
3+
in pkgs.mkShell {
4+
packages = [
5+
pkgs.doctest
6+
];
7+
}

0 commit comments

Comments
 (0)