2017-09-14 04:58:58 -07:00
|
|
|
/*
|
|
|
|
* test_pid_auto.cpp
|
|
|
|
*
|
|
|
|
* Created on: Sep 14, 2017
|
2018-01-20 17:42:19 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
2017-09-14 04:58:58 -07:00
|
|
|
*/
|
|
|
|
|
2018-09-16 19:39:46 -07:00
|
|
|
#include "global.h"
|
2017-09-14 18:00:28 -07:00
|
|
|
#include "test_pid_auto.h"
|
2017-09-14 18:49:26 -07:00
|
|
|
#include "pid_auto_tune.h"
|
2018-03-04 20:08:32 -08:00
|
|
|
#include "unit_test_framework.h"
|
2017-09-14 04:58:58 -07:00
|
|
|
|
|
|
|
efitimems_t mockTimeMs = 0;
|
|
|
|
|
|
|
|
efitimems_t currentTimeMillis(void) {
|
|
|
|
return mockTimeMs;
|
|
|
|
}
|
|
|
|
|
2017-09-14 18:49:26 -07:00
|
|
|
Logging logging;
|
|
|
|
|
2017-09-17 16:34:50 -07:00
|
|
|
static float zigZagOffset = 0;
|
2017-09-14 18:49:26 -07:00
|
|
|
static zigZagValue(int index) {
|
|
|
|
int i = index % 20;
|
|
|
|
if ( i <= 10)
|
2017-09-17 16:34:50 -07:00
|
|
|
return i * 10 + zigZagOffset;
|
|
|
|
return (20 - i) * 10 + zigZagOffset;
|
2017-09-14 18:49:26 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-14 18:00:28 -07:00
|
|
|
void testPidAutoZigZag() {
|
|
|
|
mockTimeMs = 0;
|
2017-09-14 04:58:58 -07:00
|
|
|
|
2017-09-14 18:49:26 -07:00
|
|
|
PID_AutoTune at;
|
|
|
|
|
|
|
|
at.outputStart = 50;
|
|
|
|
|
|
|
|
at.input = zigZagValue(mockTimeMs);
|
|
|
|
at.Runtime(&logging);
|
|
|
|
|
|
|
|
mockTimeMs++;
|
|
|
|
at.input = zigZagValue(mockTimeMs);
|
|
|
|
at.Runtime(&logging);
|
|
|
|
assertEqualsLM("min@1", 0, at.absMin);
|
|
|
|
assertEqualsLM("max@1", 10, at.absMax);
|
|
|
|
assertEqualsM("peakCount", 0, at.peakCount);
|
|
|
|
|
2017-09-17 16:34:50 -07:00
|
|
|
for (; mockTimeMs <= 11; mockTimeMs++) {
|
2017-09-14 18:49:26 -07:00
|
|
|
at.input = zigZagValue(mockTimeMs);
|
|
|
|
at.Runtime(&logging);
|
|
|
|
|
|
|
|
}
|
|
|
|
assertEqualsLM("min@11", 0, at.absMin);
|
|
|
|
assertEqualsLM("max@11", 100, at.absMax);
|
|
|
|
assertEqualsM("peakCount", 0, at.peakCount);
|
|
|
|
|
2017-09-17 16:34:50 -07:00
|
|
|
for (; mockTimeMs <= 21; mockTimeMs++) {
|
2017-09-14 18:49:26 -07:00
|
|
|
at.input = zigZagValue(mockTimeMs);
|
|
|
|
at.Runtime(&logging);
|
|
|
|
}
|
2017-09-14 19:07:26 -07:00
|
|
|
assertEqualsM("peakCount@21", 1, at.peakCount);
|
2017-09-14 18:49:26 -07:00
|
|
|
|
2017-09-17 16:34:50 -07:00
|
|
|
for (; mockTimeMs <= 41; mockTimeMs++) {
|
2017-09-14 18:49:26 -07:00
|
|
|
at.input = zigZagValue(mockTimeMs);
|
|
|
|
at.Runtime(&logging);
|
|
|
|
}
|
2017-09-14 19:07:26 -07:00
|
|
|
assertEqualsM("peakCount@41", 2, at.peakCount);
|
|
|
|
assertEqualsM("Pu@41", 1, cisnan(at.Pu));
|
2017-09-14 18:49:26 -07:00
|
|
|
|
2017-09-17 16:34:50 -07:00
|
|
|
for (; mockTimeMs <= 60; mockTimeMs++) {
|
2017-09-14 19:07:26 -07:00
|
|
|
at.input = zigZagValue(mockTimeMs);
|
|
|
|
at.Runtime(&logging);
|
|
|
|
}
|
|
|
|
assertEqualsM("peakCount@60", 3, at.peakCount);
|
|
|
|
assertEqualsM("Pu@60", 0.02, at.Pu);
|
2017-09-14 18:49:26 -07:00
|
|
|
|
2017-09-17 16:34:50 -07:00
|
|
|
// zigZagOffset = 10;
|
|
|
|
|
|
|
|
for (; mockTimeMs <= 80; mockTimeMs++) {
|
|
|
|
at.input = zigZagValue(mockTimeMs);
|
|
|
|
at.Runtime(&logging);
|
|
|
|
}
|
|
|
|
assertEqualsM("peakCount@80", 1, at.peakCount);
|
|
|
|
|
2017-10-08 15:18:07 -07:00
|
|
|
// todo: test the same code with noisy zig-zag function
|
2017-09-14 18:00:28 -07:00
|
|
|
}
|