This repository was archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRPMMeasure.cpp
219 lines (172 loc) · 4.9 KB
/
RPMMeasure.cpp
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
#include "RPMMeasure.h"
static bool showOne = true;
inline uint32_t readInterruptInterval(){
return InterruptFreqMeasure.read();
};
inline uint32_t readTimerInterval(){
return FreqMeasure.read();
};
RPMMeasure::RPMMeasure(uint8_t _interruptPin, uint8_t _interruptNum, uint8_t _timer1Pin) {
setPulsesPerRevolution(DEFAULT_PPR);
setAveragingDepth(DEFAULT_AVERAGING_DEPTH);
measureMode=RPMMeasureMode::None;
interruptPin = _interruptPin;
interruptNum = _interruptNum;
timer1Pin = _timer1Pin;
begun=false;
}
RPMMeasure::~RPMMeasure() {
end();
if(rpmBuffer){
delete [] rpmBuffer;
rpmBuffer = NULL;
}
}
void RPMMeasure::setAveragingDepth(uint8_t depth) {
if(rpmBuffer){
delete [] rpmBuffer;
rpmBuffer = NULL;
}
averagingDepth = depth+1;
bufferHead = 0;
if(averagingDepth > 1){
rpmBuffer = new uint16_t[averagingDepth];
}
}
void RPMMeasure::setPulsesPerRevolution(uint8_t ppr) {
pulsesPerRevolution = ppr;
}
void RPMMeasure::setMeasureMode(measureMode_t mode){
end();
measureMode=mode;
}
void RPMMeasure::update(){
if(measureMode == RPMMeasureMode::None || !begun) return;
uint8_t available;
IntervalReader readInterval;
bool needsUpdate;
//Depending on our mode we use different classes.
switch (measureMode){
case RPMMeasureMode::Interrupt:
available = InterruptFreqMeasure.available();
readInterval = readInterruptInterval;
break;
case RPMMeasureMode::Timer1:
available = FreqMeasure.available();
readInterval = readTimerInterval;
break;
}
//Store our time.
uint32_t now = millis();
//If we don't have any values to read, there's only the question whether we should insert a dead RPM (0).
if(available == 0){
//If the DEAD_RPM_TIME value was exceeded, insert a 0 and reset the timer.
if(lastReading + DEAD_RPM_TIME > now){
lastReading=now;
rpmBuffer[bufferHead] = 0;
bufferHead++;
//Wrap back around if we need to.
if(bufferHead >= averagingDepth){
bufferHead = 0;
}
rpm=0;
}
//Return either way, because there's nothing to read.
return;
}
//Since we do have readings available, bump the lastReading value.
lastReading=now;
//If we have more readings available than we want to use, first flush excess readings.
if(available > averagingDepth){
int flushCount = available-averagingDepth;
for(int i = 0; i < flushCount; i++){
readInterval();
}
available -= flushCount;
}
//See if we need to average the RPM or not.
if(averagingDepth > 1){
//Insert the (remaining) new values into the buffer.
while(available > 0){
rpmBuffer[bufferHead] = intervalToRPM(readInterval());
bufferHead++;
//Wrap back around if we need to.
if(bufferHead >= averagingDepth){
bufferHead = 0;
}
available--;
}
showOne = true;
//Perform the averaging to store our updated RPM value.
uint32_t rpmSum = 0;
//Rewind the head through our averaging depth.
long avgStart = (long)bufferHead - averagingDepth;
//Loop it around to the start if need be.
if(avgStart < 0){
avgStart = averagingDepth+avgStart;
}
//Now convert to 8 bits, and iterate until we reach the head.
uint8_t avgHead = avgStart;
uint8_t readCount = averagingDepth;
while(readCount > 0){
rpmSum += rpmBuffer[avgHead];
readCount--;
avgHead++;
if(avgHead >= averagingDepth){
avgHead=0;
}
}
//Store the average.
rpm = rpmSum / averagingDepth;
}
//Otherwise simply store the latest value.
else{
rpm = intervalToRPM(readInterval());
}
}
void RPMMeasure::begin(){
switch (measureMode){
case RPMMeasureMode::None: return;
case RPMMeasureMode::Interrupt:
InterruptFreqMeasure.begin(interruptPin, interruptNum);
break;
case RPMMeasureMode::Timer1:
pinMode(timer1Pin, INPUT);
FreqMeasure.setOverflowIgnoreTicks(FREQMEASURE_OVERFLOW_IGNORE_TICKS);
FreqMeasure.begin();
break;
}
lastReading=millis();
begun = true;
}
void RPMMeasure::end(){
switch (measureMode){
case RPMMeasureMode::Interrupt:
InterruptFreqMeasure.end();
break;
case RPMMeasureMode::Timer1:
FreqMeasure.end();
break;
}
measureMode=RPMMeasureMode::None;
rpm=0;
begun = false;
}
uint16_t RPMMeasure::getRPM(){
return rpm;
}
uint16_t RPMMeasure::intervalToRPM(uint32_t interval){
switch(measureMode){
case RPMMeasureMode::Interrupt:
return 60e6/interval/pulsesPerRevolution;
case RPMMeasureMode::Timer1:
float clockSpeed;
#if defined(__AVR__)
clockSpeed = F_CPU;
#elif defined(__arm__) && defined(TEENSYDUINO)
clockSpeed = F_BUS;
#endif
return clockSpeed*60/interval/pulsesPerRevolution;
}
return 0;
}