-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathfan_controller.cpp
More file actions
165 lines (133 loc) · 5.43 KB
/
Copy pathfan_controller.cpp
File metadata and controls
165 lines (133 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "fan_controller.h"
#include <algorithm>
#include <math.h>
#include "esp_log.h"
#include "nvs_config.h"
static const char* TAG = "fan_ctrl";
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
void FanController::applyConfig(int ch)
{
if (!m_pid[ch]) return;
float p = m_config[ch].pid.p / 100.0f;
float i = m_config[ch].pid.i / 100.0f;
float d = m_config[ch].pid.d / 100.0f;
m_pid[ch]->SetTunings(p, i, d);
m_pid[ch]->SetTarget(static_cast<float>(m_config[ch].pid.targetTemp));
ESP_LOGI(TAG, "ch%d PID: target=%.1f°C p=%.2f i=%.2f d=%.2f",
ch, static_cast<float>(m_config[ch].pid.targetTemp), p, i, d);
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
void FanController::init(Board* board, int sampleTimeMs)
{
m_board = board;
m_sampleTimeMs = sampleTimeMs;
m_numChannels = std::min(board->getNumFans(), MAX_FANS);
ESP_LOGI(TAG, "initialising for %d fan channel(s)", m_numChannels);
// Load config from NVS (PID instances don't exist yet, applyConfig is skipped)
loadSettings();
// Create PID instances using the loaded config
for (int ch = 0; ch < m_numChannels; ch++) {
m_pidTarget[ch] = static_cast<float>(m_config[ch].pid.targetTemp);
float p = m_config[ch].pid.p / 100.0f;
float i = m_config[ch].pid.i / 100.0f;
float d = m_config[ch].pid.d / 100.0f;
m_pid[ch] = new PID(&m_pidInput[ch], &m_pidOutput[ch], &m_pidTarget[ch],
p, i, d, PID_P_ON_E, PID_DIRECT);
m_pid[ch]->SetSampleTime(sampleTimeMs);
m_pid[ch]->SetOutputLimits(15, 100);
m_pid[ch]->SetMode(PID_AUTOMATIC);
m_pid[ch]->SetControllerDirection(PID_REVERSE);
m_pid[ch]->Initialize();
ESP_LOGI(TAG, "ch%d: mode=%d manual=%d%% overheat=%d°C pid-target=%d°C p=%.2f i=%.2f d=%.2f",
ch, static_cast<int>(m_config[ch].mode),
m_config[ch].manualSpeed, m_config[ch].overheatTemp,
m_config[ch].pid.targetTemp, p, i, d);
}
}
void FanController::loadSettings()
{
for (int ch = 0; ch < m_numChannels; ch++) {
PidSettings* bp = m_board->getPidSettings(ch);
m_config[ch].mode = static_cast<Mode>(Config::getFanMode(ch));
m_config[ch].manualSpeed = Config::getFanManualSpeed(ch);
m_config[ch].overheatTemp = Config::getFanOverheatTemp(ch);
m_config[ch].pid.targetTemp = Config::getFanPidTargetTemp(ch, bp->targetTemp);
m_config[ch].pid.p = Config::getFanPidP(ch, bp->p);
m_config[ch].pid.i = Config::getFanPidI(ch, bp->i);
m_config[ch].pid.d = Config::getFanPidD(ch, bp->d);
applyConfig(ch); // no-op if PID not yet created
}
}
void FanController::update(float chipTempMax, float vrTemp)
{
// Overheat thresholds use original per-channel temps (ch0=chip, ch1=VR)
float overheatInput[MAX_FANS] = { chipTempMax, vrTemp };
// PID temperature input per channel: ch0=chip, ch1=VR
float tempInput[MAX_FANS] = { chipTempMax, vrTemp };
// only 2nd channel can be linked
if (m_config[1].mode == Mode::LINKED) {
tempInput[0] = fmaxf(chipTempMax, vrTemp);
}
for (int ch = 0; ch < m_numChannels; ch++) {
// Read current RPM
m_board->getFanSpeedCh(ch, &m_fanRPM[ch]);
// Always compute PID for bumpless transfer when switching modes
m_pidInput[ch] = tempInput[ch];
if (m_pid[ch]) {
m_pid[ch]->Compute();
}
// Overheat: use original temps, not PID-mixed temps
if (m_config[ch].overheatTemp && overheatInput[ch] > m_config[ch].overheatTemp) {
m_overheated[ch] = true;
m_fanPerc[ch] = 100;
m_board->setFanSpeedCh(ch, 1.0f);
continue;
}
m_overheated[ch] = false;
// LINKED: mirror channel-0 output (ch0 must be processed first)
if (m_config[ch].mode == Mode::LINKED && ch > 0) {
m_fanPerc[ch] = m_fanPerc[0];
m_board->setFanSpeedCh(ch, static_cast<float>(m_fanPerc[ch]) / 100.0f);
continue;
}
m_overheated[ch] = false;
// Normal control
switch (m_config[ch].mode) {
case Mode::MANUAL:
m_fanPerc[ch] = m_config[ch].manualSpeed;
break;
case Mode::PID:
m_fanPerc[ch] = static_cast<uint16_t>(roundf(m_pidOutput[ch]));
break;
default:
ESP_LOGE(TAG, "ch%d: unknown mode %d, defaulting to 100%%", ch, static_cast<int>(m_config[ch].mode));
m_fanPerc[ch] = 100;
break;
}
m_board->setFanSpeedCh(ch, static_cast<float>(m_fanPerc[ch]) / 100.0f);
}
}
uint16_t FanController::getRPM(int ch) const
{
if (ch < 0 || ch >= m_numChannels) return 0;
return m_fanRPM[ch];
}
uint16_t FanController::getSpeedPerc(int ch) const
{
if (ch < 0 || ch >= m_numChannels) return 0;
return m_fanPerc[ch];
}
bool FanController::isOverheated(int ch) const
{
if (ch < 0 || ch >= m_numChannels) return false;
return m_overheated[ch];
}
uint16_t FanController::getOverheatTemp(int ch) const
{
if (ch < 0 || ch >= m_numChannels) return 0;
return m_config[ch].overheatTemp;
}