Skip to content

Commit 668bf52

Browse files
committed
Cleaned up tach versus rpm
1 parent 5aefa5d commit 668bf52

File tree

8 files changed

+65
-47
lines changed

8 files changed

+65
-47
lines changed

include/ArduinoFanControl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ class ArduinoFanControl : public FanControl
1414
virtual RESULT initialise();
1515
virtual RESULT setPWMForAll(const uint16_t dutyCycle);
1616
virtual RESULT setPWM(const uint8_t fanid, const uint16_t dutyCycle);
17-
virtual RESULT getTachCount(const uint8_t fanid, uint16_t& tachCount);
17+
virtual RESULT getTachHz(const uint8_t fanid, uint16_t& tachHz);
18+
virtual RESULT getRPM(const uint8_t fanid, uint16_t& rpm);
1819

1920
private:
2021
void measureTach(const uint8_t fanid, unsigned long ms);
21-
float getTach(const uint8_t fanid, unsigned long ms);
22+
float readTach(const uint8_t fanid, unsigned long ms);
2223

2324
uint16_t _pwmPeriod;
2425

include/ErrCodes.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33

44
// err codes
55
typedef int RESULT;
6-
#define RES_OK 0
6+
#define RES_OK 0
7+
#define ERR_METHOD_NOT_IMPLEMENTED -1
78

89
// I2C for MAX
9-
#define ERR_BAD_TRANSMISSION -1
10-
#define ERR_BAD_READ -2
11-
#define ERR_BAD_PARAM -3
10+
#define ERR_BAD_TRANSMISSION -11
11+
#define ERR_BAD_READ -12
12+
#define ERR_BAD_PARAM -13
1213

1314
// DS18B
14-
#define ERR_FAILED_TO_READ_TEMP -10
15-
#define ERR_FAILED_TO_FIND_DEVICE -11
15+
#define ERR_FAILED_TO_READ_TEMP -20
16+
#define ERR_FAILED_TO_FIND_DEVICE -21
1617

1718
// Ethernet
18-
#define ERR_FAILED_TO_GET_IP_FROM_DHCP -20
19-
#define ERR_NO_ETHERNET_HW -21
20-
#define ERR_NO_CABLE_DETECTED -22
19+
#define ERR_FAILED_TO_GET_IP_FROM_DHCP -30
20+
#define ERR_NO_ETHERNET_HW -31
21+
#define ERR_NO_CABLE_DETECTED -32
2122

2223
// Fanstate
23-
#define ERR_FAN_NOT_OPERATIONAL -30
24-
#define ERR_FAN_TACH -31
24+
#define ERR_FAN_NOT_OPERATIONAL -40
25+
#define ERR_FAN_TACH -41
26+
27+
2528

2629
#endif

include/FanControl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class FanControl
3333
virtual RESULT initialise() = 0;
3434
virtual RESULT setPWMForAll(const uint16_t dutyCycle) = 0;
3535
virtual RESULT setPWM(const uint8_t fanid, const uint16_t dutyCycle) = 0;
36-
virtual RESULT getTachCount(const uint8_t fanid, uint16_t& tachCount) = 0;
36+
virtual RESULT getTachHz(const uint8_t fanid, uint16_t& tachHz) = 0;
37+
virtual RESULT getRPM(const uint8_t fanid, uint16_t& rpm) = 0;
3738

3839
const uint8_t getFanCount() const {
3940
return _fans;

include/MAX31790FanControl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class MAX31790 : public FanControl
7474
virtual RESULT setPWM(const uint8_t fanid, const uint16_t dutyCycle);
7575
virtual RESULT getTachCount(const uint8_t fanid, uint16_t& tachCount);
7676

77+
virtual RESULT getTachHz(const uint8_t fanid, uint16_t& tachHz);
78+
virtual RESULT getRPM(const uint8_t fanid, uint16_t& rpm);
79+
7780
RESULT getGlobalConfiguration(GlobalConfig& config);
7881
//RESULT setGlobalConfiguration(const GlobalConfig& config);
7982
//RESULT setFanConfigForAll(const FanConfigStruct& config);

src/ArduinoFanControl.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,22 @@ RESULT ArduinoFanControl::setPWMForAll(const uint16_t dutyCycle)
116116
return res;
117117
}
118118

119-
/**
120-
* Get tachcount per fan
121-
*/
122-
RESULT ArduinoFanControl::getTachCount(const uint8_t fanid, uint16_t &tachCount)
123-
{
119+
RESULT ArduinoFanControl::getTachHz(const uint8_t fanid, uint16_t& tachHz) {
124120
ASSERT_RANGE_FAN_ID(fanid, getFanCount());
125-
121+
126122
unsigned long tachTime = 750; //ms
127123
measureTach(fanid, tachTime);
128-
float f = getTach(fanid, tachTime);
129-
tachCount = round(f);
124+
float tHz = readTach(fanid, tachTime);
125+
tachHz = round(tHz);
126+
return RES_OK;
127+
}
128+
129+
RESULT ArduinoFanControl::getRPM(const uint8_t fanid, uint16_t& rpm) {
130+
uint16_t tachHz;
131+
getTachHz(fanid, tachHz);
132+
// Div 2 since TACH returns 2 pulses per revolution:
133+
// https://noctua.at/media/wysiwyg/Noctua_PWM_specifications_white_paper.pdf
134+
rpm = (tachHz * 60.0) / 2;
130135
return RES_OK;
131136
}
132137

@@ -157,10 +162,11 @@ void ArduinoFanControl::measureTach(const uint8_t fanid, unsigned long msWait)
157162
} while ((millis() - t1) < msWait);
158163
}
159164

160-
float ArduinoFanControl::getTach(const uint8_t fanid, unsigned long ms)
165+
float ArduinoFanControl::readTach(const uint8_t fanid, unsigned long ms)
161166
{
162167
// See https://www.pjrc.com/teensy/td_libs_TimerOne.html Interrupt Context Issues
163168
// noInterrupts();
169+
164170
uint16_t tach = 0;
165171
switch (fanid) {
166172
case 1:
@@ -178,9 +184,7 @@ float ArduinoFanControl::getTach(const uint8_t fanid, unsigned long ms)
178184
}
179185
// interrupts();
180186

181-
float r = (float)(60 * tach) / ((float)ms / 1000);
182-
// Divide by 4: since TACH returns 2 pulses per revolution AND
183-
// interrupt is CHANGE: H-L and L-H for accuracy.
184-
// https://noctua.at/media/wysiwyg/Noctua_PWM_specifications_white_paper.pdf
185-
return r / 4;
187+
// Divide by 2: since interrupt is CHANGE: H-L and L-H for accuracy.
188+
float tachHz = (tach * 1000.0 ) / (2 * ms);
189+
return tachHz;
186190
}

src/MAX31790FanControl.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,28 @@ RESULT MAX31790::setPWMForAll(const uint16_t dutyCycle)
102102
}
103103

104104
/**
105-
* GetTachCount per fan
105+
* GetTachHz per fan
106106
*/
107-
RESULT MAX31790::getTachCount(const uint8_t fanid, uint16_t &tachCount)
108-
{
107+
RESULT MAX31790::getTachHz(const uint8_t fanid, uint16_t& tachHz) {
108+
109109
ASSERT_RANGE_FAN_ID(fanid, getFanCount());
110110
uint8_t buffer[2];
111111
RESULT res = readBytes(TACH_COUNT(fanid), 2, &buffer[0]);
112112
if (res != RES_OK)
113113
return res;
114114

115-
tachCount = buffer[0];
116-
tachCount = tachCount << 8;
117-
tachCount |= buffer[1];
118-
tachCount = tachCount >> 5;
115+
tachHz = buffer[0];
116+
tachHz = tachHz << 8;
117+
tachHz |= buffer[1];
118+
tachHz = tachHz >> 5;
119+
119120
return RES_OK;
120121
}
121122

123+
RESULT MAX31790::getRPM(const uint8_t fanid, uint16_t& rpm) {
124+
return ERR_METHOD_NOT_IMPLEMENTED;
125+
}
126+
122127
/**
123128
* Scan all i2c devices between 1 and 127.
124129
*/

src/OLEDDisplay.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void OLEDDisplay::internalRender(RackState_t&rs, const NetworkState_t& ns) {
6363
OLED_Colour LINE_COL = DARKGRAY;
6464

6565
_oled.drawString(x, 128-18, F("TEMP C"), WHITE, BLACK);
66-
_oled.drawString(x, y-1-18, F("TACH %"), WHITE, BLACK);
66+
_oled.drawString(x, y-1-18, F("RPM %"), WHITE, BLACK);
6767
_oled.drawCircle(38, 128-4, 2, WHITE); // degree symbol
6868
_oled.drawLine(x+65, 0, x+65, 128, LINE_COL);
6969
_oled.drawLine(0, y-2, 128, y-2, LINE_COL);
@@ -152,7 +152,8 @@ uint8_t OLEDDisplay::getPercentageRPM(const FanState_t& fan) const {
152152
else if (fan.rpm > fan.maxRpm) // maybe due to "noise" on tach pin
153153
return 100;
154154
else
155-
return round(((float)(fan.rpm - fan.minRpm) / (fan.maxRpm - fan.minRpm)) * 100.0);
155+
//return round(((float)(fan.rpm - fan.minRpm) / (fan.maxRpm - fan.minRpm)) * 100.0);
156+
return round( ((float)fan.rpm / fan.maxRpm) * 100 );
156157
}
157158

158159
void OLEDDisplay::drawPercentage(int x, int y, uint8_t pc) {

src/RackTempController.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ RESULT RackTempController::checkRpm(FanState_t& fs) const {
7373
}
7474

7575
// is the fan within expected RPM variance given dutyCycle?
76-
uint16_t expectedRpm = round((float)(fs.maxRpm - fs.minRpm) * ((float)fs.pwm/100)) + fs.minRpm;
77-
76+
uint16_t expectedRpm = round( fs.maxRpm * (float)fs.pwm/100 );
77+
7878
uint16_t minExpectedRpm = round(expectedRpm - expectedRpm * _rpmVariance);
7979
uint16_t maxExpectedRpm = round(expectedRpm + expectedRpm * _rpmVariance);
8080
if (fs.rpm < minExpectedRpm || fs.rpm > maxExpectedRpm) {
@@ -106,7 +106,7 @@ void RackTempController::adjustFanSpeeds(RackState_t& rs) {
106106
uint8_t dutyCycle = 50;
107107
for (auto it = rs.thermos.begin(); it != rs.thermos.end(); it++) {
108108
Temperature_t& thermo = it->second;
109-
if (thermo.tempCelsuis > 20) {
109+
if (thermo.tempCelsuis > 22) {
110110
dutyCycle = 100;
111111
}
112112
}
@@ -122,20 +122,20 @@ void RackTempController::adjustFanSpeeds(RackState_t& rs) {
122122

123123
/**
124124
* Get tach/rpm for all fans
125-
* Delay of 750ms per fan within getTachCount()
125+
* Delay of 750ms per fan within getRPM()
126126
*/
127127
void RackTempController::readFanSpeeds(Fans_t& fans) {
128128

129-
uint16_t tachCount = 0;
130-
129+
uint16_t rpm = 0;
130+
131131
Log.notice(F("Reading fan rpms"));
132132

133133
for (auto it = fans.begin(); it != fans.end(); it++) {
134-
_fanControl.getTachCount(it->first, tachCount);
135-
it->second.rpm = tachCount;
136-
Log.notice(F("Fan %s tach rpm - %d"), it->second.position.c_str(), tachCount);
134+
_fanControl.getRPM(it->first, rpm);
135+
it->second.rpm = rpm;
136+
Log.notice(F("Fan %s rpm - %d"), it->second.position.c_str(), rpm);
137137
}
138-
};
138+
};
139139

140140
void RackTempController::readTempStates(Thermos_t& thermos) {
141141

0 commit comments

Comments
 (0)