Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
PIDController.cc File Reference

Unit test for PIDController algorithm class. More...

Include dependency graph for PIDController.cc:

Classes

class  PIDControllerTests
 Unit Test Class for the PIDController. More...

Functions

 TEST_F (PIDControllerTests, DoesNotLeak)
 Check that PIDController doesn't leak any memory.
 TEST_F (PIDControllerTests, Leaks)
 This should fail when the –check_for_leaks command line flag is specified.
 TEST_F (PIDControllerTests, ProportionalControl)
 Test PIDController proportional functionality.
 TEST_F (PIDControllerTests, IntegralControl)
 Test PIDController integral functionality.
 TEST_F (PIDControllerTests, DerivativeControl)
 Test PIDController derivative functionality.
 TEST_F (PIDControllerTests, MaxIEffort)
 Test PIDController max integral effort.
 TEST_F (PIDControllerTests, MaxRampRate)
 Test PIDController ramp rate.
 TEST_F (PIDControllerTests, OutputFilter)
 Test PIDController output filter.
 TEST_F (PIDControllerTests, CheckGainSigns)
 Test PIDController gains sign check.
 TEST_F (PIDControllerTests, ControllerLimits)
 Test PIDController limits adherence.
 TEST_F (PIDControllerTests, ContinuousInput)
 Test PIDController wrap around for rotational positioning.
 TEST_F (PIDControllerTests, MutatorsAndAccessors)
 Test PIDController mutators and accessors.

Detailed Description

Unit test for PIDController algorithm class.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-12

Function Documentation

◆ TEST_F() [1/12]

TEST_F ( PIDControllerTests ,
DoesNotLeak  )

Check that PIDController doesn't leak any memory.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-08-23
80{
81 // Create a new PIDController object.
82 controllers::PIDController* pPIDController = new controllers::PIDController(1.0, 0.1, 0.1, 0.01);
83 // Delete object.
84 delete pPIDController;
85 // Point to null.
86 pPIDController = nullptr;
87}
This class encapsulates the fundamental principles of Proportional-Integral-Derivative (PID) control ...
Definition PIDController.h:50

◆ TEST_F() [2/12]

TEST_F ( PIDControllerTests ,
Leaks  )

This should fail when the –check_for_leaks command line flag is specified.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-08-23
97{
98 // Create a new PIDController object.
99 controllers::PIDController* pPIDController = new controllers::PIDController(1.0, 0.1, 0.1, 0.01);
100 EXPECT_TRUE(pPIDController != nullptr);
101}

◆ TEST_F() [3/12]

TEST_F ( PIDControllerTests ,
ProportionalControl  )

Test PIDController proportional functionality.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-23
111{
112 // Create a new PIDController object.
113 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(1.0, 0.0, 0.0);
114
115 // Create array for storing input and expect output values.
116 const int nTestValuesLength = 5;
117 const double aActualInput[nTestValuesLength] = {0.0, 1.0, 2.0, 3.0, -50.0};
118 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, -49.5};
119 const double aExpectedOutput[nTestValuesLength] = {0.0, 0.0, -1.0, -2.0, 0.5};
120
121 // Loop through each value and compare inputs and outputs.
122 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
123 {
124 // Calculate drive powers.
125 double dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
126 dOutput = pPIDController->Calculate(aActualInput[nIter]);
127
128 // Check that the expected output values were calculated.
129 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
130 }
131}

◆ TEST_F() [4/12]

TEST_F ( PIDControllerTests ,
IntegralControl  )

Test PIDController integral functionality.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-23
141{
142 // Create a new PIDController object.
143 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(0.0, 1.0, 0.0);
144
145 // Create array for storing input and expect output values.
146 const int nTestValuesLength = 5;
147 const double aActualInput[nTestValuesLength] = {0.0, 1.0, 2.0, 3.0, -50.0};
148 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, -49.5};
149 const double aExpectedOutput[nTestValuesLength] = {0.0, 0.0, -1.0, -4.0, -5.5};
150
151 // Loop through each value and compare inputs and outputs.
152 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
153 {
154 // Calculate drive powers a few times.
155 double dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
156 dOutput = pPIDController->Calculate(aActualInput[nIter]);
157
158 // Check that the expected output values were calculated.
159 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
160 }
161}

◆ TEST_F() [5/12]

TEST_F ( PIDControllerTests ,
DerivativeControl  )

Test PIDController derivative functionality.

Author
ClayJay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2023-10-23
171{
172 // Create a new PIDController object.
173 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(0.0, 0.0, 1.0);
174
175 // Create array for storing input and expect output values.
176 const int nTestValuesLength = 5;
177 const double aActualInput[nTestValuesLength] = {0.0, 1.0, 2.0, 3.0, -50.0};
178 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, -49.5};
179 const double aExpectedOutput[nTestValuesLength] = {0.0, 0.0, 1.0, 1.0, -2.5};
180
181 // Loop through each value and compare inputs and outputs.
182 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
183 {
184 // Get last iterator index.
185 int nLastIter;
186 ((nIter - 1) <= 0) ? nLastIter = 0 : nLastIter = (nIter - 1);
187
188 // Calculate drive powers a few times.
189 double dOutput = pPIDController->Calculate(aActualInput[nLastIter], aSetpointInput[nLastIter]);
190 dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
191
192 // Check that the expected output values were calculated.
193 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
194 }
195}

◆ TEST_F() [6/12]

TEST_F ( PIDControllerTests ,
MaxIEffort  )

Test PIDController max integral effort.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-03-04
205{
206 // Create a new PIDController object.
207 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(0.0, 1.0, 0.0);
208 pPIDController->SetMaxIntegralEffort(1.0);
209 pPIDController->SetIntegral(1.0);
210
211 // Create array for storing input and expect output values.
212 const int nTestValuesLength = 5;
213 const double aActualInput[nTestValuesLength] = {0.0, 1.0, 2.0, 3.0, -50.0};
214 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, -49.5};
215 const double aExpectedOutput[nTestValuesLength] = {0.0, 0.0, 0.0, -1.0, -1.0};
216
217 // Loop through each value and compare inputs and outputs.
218 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
219 {
220 // Get last iterator index.
221 int nLastIter;
222 ((nIter - 1) <= 0) ? nLastIter = 0 : nLastIter = (nIter - 1);
223
224 // Calculate drive powers a few times.
225 double dOutput = pPIDController->Calculate(aActualInput[nLastIter], aSetpointInput[nLastIter]);
226 dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
227
228 // Check that the expected output values were calculated.
229 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
230 }
231}

◆ TEST_F() [7/12]

TEST_F ( PIDControllerTests ,
MaxRampRate  )

Test PIDController ramp rate.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-03-04
241{
242 // Create a new PIDController object.
243 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(1.0, 0.0, 0.0);
244 pPIDController->SetOutputRampRate(0.01);
245
246 // Create array for storing input and expect output values.
247 const int nTestValuesLength = 5;
248 const double aActualInput[nTestValuesLength] = {1.0, 1.0, 2.0, 3.0, 50.0};
249 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, 1.0};
250 const double aExpectedOutput[nTestValuesLength] = {-1.0, -0.99, -0.99, -1.01, -1.03};
251
252 // Loop through each value and compare inputs and outputs.
253 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
254 {
255 // Get last iterator index.
256 int nLastIter;
257 ((nIter - 1) <= 0) ? nLastIter = 0 : nLastIter = (nIter - 1);
258
259 // Calculate drive powers a few times.
260 double dOutput = pPIDController->Calculate(aActualInput[nLastIter], aSetpointInput[nLastIter]);
261 dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
262
263 // Check that the expected output values were calculated.
264 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
265 }
266}

◆ TEST_F() [8/12]

TEST_F ( PIDControllerTests ,
OutputFilter  )

Test PIDController output filter.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-03-04
276{
277 // Create a new PIDController object.
278 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(1.0, 0.0, 0.0);
279 pPIDController->SetOutputFilter(0.1);
280
281 // Create array for storing input and expect output values.
282 const int nTestValuesLength = 5;
283 const double aActualInput[nTestValuesLength] = {1.0, 1.0, 2.0, 3.0, 50.0};
284 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, 1.0};
285 const double aExpectedOutput[nTestValuesLength] = {-1.0, -0.1, -0.90, -1.89, -44.29};
286
287 // Loop through each value and compare inputs and outputs.
288 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
289 {
290 // Get last iterator index.
291 int nLastIter;
292 ((nIter - 1) <= 0) ? nLastIter = 0 : nLastIter = (nIter - 1);
293
294 // Calculate drive powers a few times.
295 double dOutput = pPIDController->Calculate(aActualInput[nLastIter], aSetpointInput[nLastIter]);
296 dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
297
298 // Check that the expected output values were calculated.
299 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
300 }
301}

◆ TEST_F() [9/12]

TEST_F ( PIDControllerTests ,
CheckGainSigns  )

Test PIDController gains sign check.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2025-03-02
311{
312 // Create a new PIDController object.
313 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(-1.0, -1.0, -1.0);
314
315 // Create array for storing input and expect output values.
316 const int nTestValuesLength = 5;
317 const double aActualInput[nTestValuesLength] = {0.0, 1.0, 2.0, 3.0, -50.0};
318 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, -49.5};
319 const double aExpectedOutput[nTestValuesLength] = {0.0, 0.0, 2.0, 1.0, -9.0};
320
321 // Loop through each value and compare inputs and outputs.
322 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
323 {
324 // Get last iterator index.
325 int nLastIter;
326 ((nIter - 1) <= 0) ? nLastIter = 0 : nLastIter = (nIter - 1);
327
328 // Calculate drive powers a few times.
329 double dOutput = pPIDController->Calculate(aActualInput[nLastIter], aSetpointInput[nLastIter]);
330 dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
331
332 // Check that the expected output values were calculated.
333 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
334 }
335}

◆ TEST_F() [10/12]

TEST_F ( PIDControllerTests ,
ControllerLimits  )

Test PIDController limits adherence.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-11-24
345{
346 // Create a new PIDController object.
347 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(1.0, 0.0, 0.0);
348 // Set controller limits.
349 pPIDController->SetMaxSetpointDifference(0.1);
350 pPIDController->SetMaxIntegralEffort(1);
351 pPIDController->SetOutputLimits(0.1);
352 pPIDController->SetOutputRampRate(0.01);
353
354 // Create array for storing input and expect output values.
355 const int nTestValuesLength = 5;
356 const double aActualInput[nTestValuesLength] = {0.0, 1.0, 2.0, 3.0, -50.0};
357 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, -49.5};
358 const double aExpectedOutput[nTestValuesLength] = {0.0, -0.01, -0.01, -0.02, -0.01};
359
360 // Loop through each value and compare inputs and outputs.
361 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
362 {
363 // Calculate drive powers.
364 double dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
365
366 // Check that the expected output values were calculated.
367 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
368 }
369}

◆ TEST_F() [11/12]

TEST_F ( PIDControllerTests ,
ContinuousInput  )

Test PIDController wrap around for rotational positioning.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-11-24
379{
380 // Create a new PIDController object.
381 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(1.0, 0.0, 0.0);
382 // Enable continuous input.
383 pPIDController->EnableContinuousInput(-180.0, 180.0);
384
385 // Create array for storing input and expect output values.
386 const int nTestValuesLength = 5;
387 const double aActualInput[nTestValuesLength] = {0.0, 1.0, 2.0, 3.0, -50.0};
388 const double aSetpointInput[nTestValuesLength] = {0.0, 1.0, 1.0, 1.0, -49.5};
389 const double aExpectedOutput[nTestValuesLength] = {0.0, 0.0, -1.0, -2.0, 0.5};
390
391 // Loop through each value and compare inputs and outputs.
392 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
393 {
394 // Calculate drive powers.
395 double dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
396
397 // Check that the expected output values were calculated.
398 EXPECT_NEAR(dOutput, aExpectedOutput[nIter], 0.01); // Left output check.
399 }
400
401 // Disable continuous input.
402 pPIDController->DisableContinuousInput();
403 // Reset controller.
404 pPIDController->Reset();
405
406 // Create array for storing input and expect output values.
407 const double aExpectedOutput2[nTestValuesLength] = {0.0, 0.0, -1.0, -2.0, 0.5};
408
409 // Loop through each value and compare inputs and outputs.
410 for (int nIter = 0; nIter < nTestValuesLength; ++nIter)
411 {
412 // Calculate drive powers.
413 double dOutput = pPIDController->Calculate(aActualInput[nIter], aSetpointInput[nIter]);
414
415 // Check that the expected output values were calculated.
416 EXPECT_NEAR(dOutput, aExpectedOutput2[nIter], 0.01); // Left output check.
417 }
418}

◆ TEST_F() [12/12]

TEST_F ( PIDControllerTests ,
MutatorsAndAccessors  )

Test PIDController mutators and accessors.

Author
clayjay3 (clayt.nosp@m.onra.nosp@m.ycowe.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om)
Date
2024-11-25
428{
429 // Create a new PIDController object.
430 std::unique_ptr<controllers::PIDController> pPIDController = std::make_unique<controllers::PIDController>(1.0, 0.0, 0.0);
431
433 // Test mutators and then see if accessors return the same values.
435
436 // PID gains.
437 pPIDController->SetProportional(2.0);
438 EXPECT_NEAR(pPIDController->GetProportional(), 2.0, 0.01);
439 pPIDController->SetIntegral(3.0);
440 EXPECT_NEAR(pPIDController->GetIntegral(), 3.0, 0.01);
441 pPIDController->SetDerivative(4.0);
442 EXPECT_NEAR(pPIDController->GetDerivative(), 4.0, 0.01);
443 pPIDController->SetFeedforward(5.0);
444 EXPECT_NEAR(pPIDController->GetFeedforward(), 5.0, 0.01);
445 pPIDController->SetPID(6.0, 7.0, 8.0);
446 EXPECT_NEAR(pPIDController->GetProportional(), 6.0, 0.01);
447 EXPECT_NEAR(pPIDController->GetIntegral(), 7.0, 0.01);
448 EXPECT_NEAR(pPIDController->GetDerivative(), 8.0, 0.01);
449 pPIDController->SetPID(9.0, 10.0, 11.0, 12.0);
450 EXPECT_NEAR(pPIDController->GetProportional(), 9.0, 0.01);
451 EXPECT_NEAR(pPIDController->GetIntegral(), 10.0, 0.01);
452 EXPECT_NEAR(pPIDController->GetDerivative(), 11.0, 0.01);
453 EXPECT_NEAR(pPIDController->GetFeedforward(), 12.0, 0.01);
454
455 // Other settings.
456 pPIDController->SetSetpoint(6.0);
457 EXPECT_NEAR(pPIDController->GetSetpoint(), 6.0, 0.01);
458 pPIDController->SetMaxSetpointDifference(7.0);
459 EXPECT_NEAR(pPIDController->GetMaxSetpointDifference(), 7.0, 0.01);
460 pPIDController->SetMaxIntegralEffort(8.0);
461 EXPECT_NEAR(pPIDController->GetMaxIntegralEffort(), 8.0, 0.01);
462 pPIDController->SetOutputRampRate(9.0);
463 EXPECT_NEAR(pPIDController->GetOutputRampRate(), 9.0, 0.01);
464 pPIDController->SetOutputFilter(0.1);
465 EXPECT_NEAR(pPIDController->GetOutputFilter(), 0.1, 0.01);
466 pPIDController->SetDirection(true);
467 EXPECT_TRUE(pPIDController->GetReversed());
468
469 // No accessors for these.
470 pPIDController->SetOutputLimits(11.0, 12.0);
471 pPIDController->SetOutputLimits(13.0);
472}