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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* LocalFan.cpp
*
* Created on: 3 Sep 2019
* Author: David
*/
#include "LocalFan.h"
#include <GCodes/GCodeBuffer/GCodeBuffer.h>
#include <Hardware/IoPorts.h>
#include <Movement/StepTimer.h>
#include <Platform/RepRap.h>
#include <Platform/Platform.h>
#include <Heating/Heat.h>
#include <Heating/Sensors/TemperatureSensor.h>
void FanInterrupt(CallbackParameter cb) noexcept
{
static_cast<LocalFan *>(cb.vp)->Interrupt();
}
LocalFan::LocalFan(unsigned int fanNum) noexcept
: Fan(fanNum),
lastPwm(-1.0), // force a refresh
lastVal(-1.0),
fanInterruptCount(0), fanLastResetTime(0), fanInterval(0),
blipping(false)
{
}
LocalFan::~LocalFan() noexcept
{
port.WriteAnalog(0.0);
port.Release();
tachoPort.Release();
}
GCodeResult LocalFan::ReportPortDetails(const StringRef& str) const noexcept
{
str.printf("Fan %u", fanNumber);
port.AppendFullDetails(str);
if (tachoPort.IsValid())
{
str.cat(" tacho");
tachoPort.AppendBasicDetails(str);
}
return GCodeResult::ok;
}
GCodeResult LocalFan::SetPwmFrequency(PwmFrequency freq, const StringRef& reply) noexcept
{
port.SetFrequency(freq);
return GCodeResult::ok;
}
// Set the hardware PWM
// If you want make sure that the PWM is definitely updated, set lastPWM negative before calling this
void LocalFan::SetHardwarePwm(float pwmVal) noexcept
{
// Only set the PWM if it has changed, to avoid a lot of I2C traffic when we have a DueX5 connected
if (pwmVal != lastPwm)
{
lastPwm = pwmVal;
port.WriteAnalog(pwmVal);
}
}
// Refresh the fan PWM
// Checking all the sensors is expensive, so only do this if checkSensors is true.
void LocalFan::InternalRefresh(bool checkSensors) noexcept
{
float reqVal;
if (sensorsMonitored.IsEmpty())
{
reqVal = (val <= 0.0) ? 0.0 : max<float>(val * maxVal, minVal); // scale the requested PWM by the maximum, enforce the minimum
}
else if (!checkSensors)
{
reqVal = lastVal;
}
else
{
reqVal = 0.0;
const bool bangBangMode = (triggerTemperatures[1] <= triggerTemperatures[0]);
sensorsMonitored.Iterate
([&reqVal, bangBangMode, this](unsigned int sensorNum, unsigned int) noexcept
{
const auto sensor = reprap.GetHeat().FindSensor(sensorNum);
if (sensor.IsNull())
{
reqVal = maxVal; // sensor not found, so turn the fan fully on
}
else
{
//TODO we used to turn the fan on if the associated heater was being tuned
float ht;
const TemperatureError err = sensor->GetLatestTemperature(ht);
if (err != TemperatureError::success || ht < BadLowTemperature || ht >= triggerTemperatures[1])
{
reqVal = maxVal;
}
else if (!bangBangMode && ht >= triggerTemperatures[0])
{
// We already know that ht < triggerTemperatures[1], therefore unless we have NaNs it is safe to divide by (triggerTemperatures[1] - triggerTemperatures[0])
const float newVal = minVal + (maxVal - minVal) * (ht - triggerTemperatures[0])/(triggerTemperatures[1] - triggerTemperatures[0]);
if (newVal > reqVal)
{
reqVal = newVal;
}
}
else if (lastVal > 0.0 && ht + ThermostatHysteresis > triggerTemperatures[0]) // if the fan is on, add a hysteresis before turning it off
{
const float newVal = (bangBangMode) ? maxVal : minVal;
if (newVal > reqVal)
{
reqVal = newVal;
}
}
}
}
);
}
if (reqVal > 0.0)
{
if (lastVal <= 0.0)
{
// We are turning this fan on
if (reqVal < 1.0 && blipTime != 0)
{
// Starting the fan from standstill, so blip the fan
blipping = true;
blipStartTime = millis();
}
}
else if (blipping && millis() - blipStartTime >= blipTime)
{
blipping = false;
}
}
else
{
blipping = false;
}
lastVal = reqVal;
SetHardwarePwm((blipping) ? 1.0 : reqVal);
}
GCodeResult LocalFan::Refresh(const StringRef& reply) noexcept
{
InternalRefresh(true);
return GCodeResult::ok;
}
bool LocalFan::UpdateFanConfiguration(const StringRef& reply) noexcept
{
InternalRefresh(true);
return true;
}
// Update the fan, returning true if the fan is thermostatic and running.
// Checking the sensors is expensive, so only check them if checkSensors is true.
bool LocalFan::Check(bool checkSensors) noexcept
{
InternalRefresh(checkSensors);
return sensorsMonitored.IsNonEmpty() && lastVal > 0.0;
}
bool LocalFan::AssignPorts(const char *pinNames, const StringRef& reply) noexcept
{
IoPort* const ports[] = { &port, &tachoPort };
const PinAccess access1[] = { PinAccess::pwm, PinAccess::read};
if (IoPort::AssignPorts(pinNames, reply, PinUsedBy::fan, 2, ports, access1) == 0)
{
const PinAccess access2[] = { PinAccess::write0, PinAccess::read};
if (IoPort::AssignPorts(pinNames, reply, PinUsedBy::fan, 2, ports, access2) == 0)
{
return false;
}
}
// Tacho initialisation
if (tachoPort.IsValid())
{
tachoPort.AttachInterrupt(FanInterrupt, InterruptMode::falling, CallbackParameter(this));
}
InternalRefresh(true);
return true;
}
// Tacho support
int32_t LocalFan::GetRPM() const noexcept
{
// The ISR sets fanInterval to the number of step interrupt clocks it took to get fanMaxInterruptCount interrupts.
// We get 2 tacho pulses per revolution, hence 2 interrupts per revolution.
// When the fan stops, we get no interrupts and fanInterval stops getting updated. We must recognise this and return zero.
return (!tachoPort.IsValid())
? -1 // we return -1 if there is no tacho configured
: (fanInterval != 0 && StepTimer::GetTimerTicks() - fanLastResetTime < 3 * StepClockRate) // if we have a reading and it is less than 3 seconds old
? (StepClockRate * fanMaxInterruptCount * (60/2))/fanInterval // then calculate RPM assuming 2 interrupts per rev
: 0; // else assume fan is off or tacho not connected
}
void LocalFan::Interrupt() noexcept
{
++fanInterruptCount;
if (fanInterruptCount == fanMaxInterruptCount)
{
const uint32_t now = StepTimer::GetTimerTicks();
fanInterval = now - fanLastResetTime;
fanLastResetTime = now;
fanInterruptCount = 0;
}
}
// End
|