-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamp_monitor.cpp
More file actions
86 lines (71 loc) · 2.33 KB
/
amp_monitor.cpp
File metadata and controls
86 lines (71 loc) · 2.33 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
/*
* TT Control, advanced sinusoidal control of multi-phase turntable motors
* Created by Ashley Cox at The Blind Man’s Workshop
* https://theblindmansworkshop.com
* No part of this code may be used or reproduced for commercial purposes without written permission and contractual agreement
* All external libraries and frameworks are the property of their respective authors and governed by their respective licenses
*/
#include "amp_monitor.h"
#include "error_handler.h"
#include "hal.h"
#include "motor.h"
#include "settings.h"
extern MotorController motor;
extern Settings settings;
AmplifierMonitor ampMonitor;
AmplifierMonitor::AmplifierMonitor() {
_lastSampleMs = 0;
_temperatureC = 0.0;
_thermalOk = true;
_warned = false;
_shutdown = false;
}
void AmplifierMonitor::begin() {
#if AMP_MONITOR_ENABLE
hal.setPinMode(PIN_AMP_THERM_OK, INPUT_PULLDOWN);
hal.setPinMode(PIN_AMP_TEMP, INPUT);
#endif
}
void AmplifierMonitor::update() {
#if AMP_MONITOR_ENABLE
uint32_t now = hal.getMillis();
if (now - _lastSampleMs < 500) return;
_lastSampleMs = now;
if (_shutdown) {
motor.emergencyStop();
return;
}
_thermalOk = hal.digitalRead(PIN_AMP_THERM_OK) == HIGH;
_temperatureC = readTmp36C();
float warnThresholdC = settings.get().ampTempWarnC;
float shutdownThresholdC = settings.get().ampTempShutdownC;
if (shutdownThresholdC < warnThresholdC + AMP_TEMP_MIN_SHUTDOWN_MARGIN_C) {
shutdownThresholdC = warnThresholdC + AMP_TEMP_MIN_SHUTDOWN_MARGIN_C;
}
if (!_thermalOk) {
shutdownOutputs("Amp thermal cutout");
return;
}
if (_temperatureC >= shutdownThresholdC) {
shutdownOutputs("Amp over temperature");
return;
}
if (!_warned && _temperatureC >= warnThresholdC) {
_warned = true;
errorHandler.report(ERR_AMP_THERMAL, "Amp temperature high", false);
}
if (_temperatureC < (warnThresholdC - AMP_TEMP_WARN_HYSTERESIS_C)) {
_warned = false;
}
#endif
}
float AmplifierMonitor::readTmp36C() {
int raw = analogRead(PIN_AMP_TEMP);
float voltage = (raw * 3.3f) / 1023.0f;
return (voltage - 0.5f) * 100.0f;
}
void AmplifierMonitor::shutdownOutputs(const char* message) {
if (_shutdown) return;
_shutdown = true;
errorHandler.report(ERR_AMP_THERMAL, message, true);
}