Skip to content

Commit a45b2eb

Browse files
author
Marcus Sonestedt
committed
ServoPID: Test Mass-Spring system, write to CSV and plot
1 parent 028f2c6 commit a45b2eb

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
**/bin
1212
**/obj
1313
ServoPID/.vs/
14+
ServoPID/TestServoPID/pid_ms.csv

ServoPID/PID_Plot.png

16.7 KB
Loading

ServoPID/TestServoPID/TestPID.cpp

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ TEST(TestPID, StaysOnZero)
66
{
77
auto pid = PID(1.0f, 2.0f, 3.0f, 0.5f);
88

9-
for (auto i = 0; i < 10; ++i) {
9+
for (auto i = 0; i < 10; ++i)
10+
{
1011
const auto output = pid.regulate(42.0f, 42.0f, 1.0f);
1112
ASSERT_FLOAT_EQ(output, 0.0f);
1213
}
1314
}
1415

1516
TEST(TestPID, NoStartFlutter)
1617
{
17-
auto pid = PID(1.0f, 2.0f, 3.0f, 0.5f);
18+
auto pid = PID(1.0f, 2.0f, 3.0f, 0.5f);
1819
const auto output = pid.regulate(42.0f, 42.0f, 1.0f);
1920
ASSERT_FLOAT_EQ(output, 0.0f);
2021
}
@@ -23,24 +24,58 @@ TEST(TestPID, RegulateScaled)
2324
{
2425
auto pid = PID(1.0f, 2.0f, 0.0f, 0.5f);
2526

26-
auto process = 0.0f;
27-
for (auto i = 0; i < 1000; ++i) {
28-
const auto output = pid.regulate(process, 42.0f, 0.1f);
29-
process = output * 0.5f;
27+
auto input = 0.0f;
28+
for (auto i = 0; i < 1000; ++i)
29+
{
30+
const auto output = pid.regulate(input, 42.0f, 0.1f);
31+
input = output * 0.5f;
3032
}
31-
32-
ASSERT_FLOAT_EQ(process, 42.0f);
33+
34+
ASSERT_FLOAT_EQ(input, 42.0f);
3335
}
3436

3537
TEST(TestPID, RegulateOffset)
3638
{
3739
auto pid = PID(0.1f, 10.0f, 0.1f, 0.2f);
3840

39-
auto process = 0.0f;
40-
for (auto i = 0; i < 1000; ++i) {
41-
const auto output = pid.regulate(process, 42.0f, 0.1f);
42-
process = output + 10.0f;
41+
auto input = 0.0f;
42+
for (auto i = 0; i < 1000; ++i)
43+
{
44+
const auto output = pid.regulate(input, 42.0f, 0.1f);
45+
input = output + 10.0f;
4346
}
44-
45-
ASSERT_FLOAT_EQ(process, 42.0f);
47+
48+
ASSERT_FLOAT_EQ(input, 42.0f);
49+
}
50+
51+
TEST(TestPID, RegulateMassSpringBounce)
52+
{
53+
auto pid = PID(1.0f, 10.0f, 1.0f, 1.0f);
54+
55+
const auto m = 1.0f;
56+
const auto k = 20.0f;
57+
const auto dt = 0.05f;
58+
59+
auto pos = 20.0f;
60+
auto vel = 0.0f;
61+
62+
std::ofstream csv;
63+
csv.open("pid_ms.csv", std::ios::out);
64+
ASSERT_TRUE(csv.is_open());
65+
66+
for (auto i = 0; i < 400; ++i)
67+
{
68+
const auto setPos = ((i / 100) % 3) * 10.0f;
69+
const auto attachPos = constrain(pid.regulate(pos, setPos, 0.1f), -30, 30);
70+
71+
const auto force = (attachPos - pos) * k;
72+
const auto acc = force / m;
73+
74+
vel += acc * dt;
75+
pos += vel * dt;
76+
77+
csv << setPos << "," << attachPos << "," << pos << std::endl;
78+
}
79+
80+
ASSERT_NEAR(pos, 0, 1);
4681
}

ServoPID/TestServoPID/pch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
#include <string>
88
#include <sstream>
99
#include <array>
10-
10+
#include <fstream>

0 commit comments

Comments
 (0)