GCC Code Coverage Report


Directory: ./
File: firmware/controllers/lua/lua_pid.h
Date: 2025-10-03 00:57:22
Coverage Exec Excl Total
Lines: 41.1% 23 0 56
Functions: 40.0% 4 0 10
Branches: 25.0% 1 0 4
Decisions: 25.0% 1 - 4

Line Branch Decision Exec Source
1 #pragma once
2
3 #include "pch.h"
4
5 struct LuaPid final {
6 LuaPid() = default;
7
8 2 LuaPid(float kp, float ki, float kd, float min, float max)
9 2 : m_pid(&m_params)
10 {
11 2 m_params.pFactor = kp;
12 2 m_params.iFactor = ki;
13 2 m_params.dFactor = kd;
14
15 2 m_params.offset = 0;
16 2 m_params.periodMs = 0;
17 2 m_params.minValue = min;
18 2 m_params.maxValue = max;
19
20
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 2 times.
2 if (m_params.minValue >= m_params.maxValue) {
21 criticalError("Lua: minValue %d/maxValue %d", m_params.minValue, m_params.maxValue);
22 }
23
24 2 m_lastUpdate.reset();
25 2 }
26
27 5 float get(float target, float input) {
28 #if EFI_UNIT_TEST
29 // this is how we avoid zero dt
30 5 advanceTimeUs(1000);
31 #endif
32
33 5 float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt());
34
35 5 return m_pid.getOutput(target, input, dt);
36 }
37
38 1 void setOffset(float offset) {
39 1 m_params.offset = offset;
40 1 reset();
41 1 }
42
43 1 void reset() {
44 1 m_pid.reset();
45 1 }
46
47 private:
48 Pid m_pid;
49 Timer m_lastUpdate;
50 pid_s m_params;
51 };
52
53 // todo: use templates and reduce duplication between LuaPid and LuaIndustrialPid?
54 struct LuaIndustrialPid final {
55 LuaIndustrialPid() = default;
56
57 LuaIndustrialPid(float kp, float ki, float kd, float min, float max)
58 : m_pid(&m_params)
59 {
60 m_params.pFactor = kp;
61 m_params.iFactor = ki;
62 m_params.dFactor = kd;
63
64 m_params.offset = 0;
65 m_params.periodMs = 0;
66 m_params.minValue = min;
67 m_params.maxValue = max;
68
69 if (m_params.minValue >= m_params.maxValue) {
70 criticalError("Lua: minValue %d/maxValue %d", m_params.minValue, m_params.maxValue);
71 }
72
73 m_lastUpdate.reset();
74 }
75
76 float get(float target, float input) {
77 #if EFI_UNIT_TEST
78 // this is how we avoid zero dt
79 advanceTimeUs(1000);
80 #endif
81
82 float dt = m_lastUpdate.getElapsedSecondsAndReset(getTimeNowNt());
83
84 return m_pid.getOutput(target, input, dt);
85 }
86
87 void setOffset(float offset) {
88 m_params.offset = offset;
89 reset();
90 }
91
92 void setDerivativeFilterLoss(float derivativeFilterLoss) {
93 m_pid.derivativeFilterLoss = derivativeFilterLoss;
94 reset();
95 }
96
97 void setAntiwindupFreq(float antiwindupFreq) {
98 m_pid.antiwindupFreq = antiwindupFreq;
99 reset();
100 }
101
102 void reset() {
103 m_pid.reset();
104 }
105
106 private:
107 PidIndustrial m_pid;
108 Timer m_lastUpdate;
109 pid_s m_params;
110 };
111