Skip to content

Commit af78683

Browse files
committed
Use median of measured values again.
1 parent 3eb0d71 commit af78683

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

OpenBikeSensorFirmware/sensor.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,12 @@ void HCSR04SensorManager::collectSensorResults() {
233233
dist = static_cast<uint16_t>(duration / MICRO_SEC_TO_CM_DIVIDER);
234234
}
235235
sensor->rawDistance = dist;
236-
sensorValues[idx] = correctSensorOffset(dist, sensor->offset);
236+
sensorValues[idx] = correctSensorOffset(medianMeasure(sensor, dist), sensor->offset);
237237

238238
#ifdef DEVELOP
239-
Serial.printf("Raw sensor[%d] distance read %03u / %03u -> *%03ucm*, duration: %zu us - echo pin state: %d\n",
240-
idx, sensor->rawDistance, dist, sensorValues[idx], duration, digitalRead(sensor->echoPin));
239+
Serial.printf("Raw sensor[%d] distance read %03u / %03u (%03u, %03u, %03u) -> *%03ucm*, duration: %zu us - echo pin state: %d\n",
240+
idx, sensor->rawDistance, dist, sensor->distances[0], sensor->distances[1],
241+
sensor->distances[2], sensorValues[idx], duration, digitalRead(sensor->echoPin));
241242
#endif
242243

243244
if (sensorValues[idx] > 0 && sensorValues[idx] < sensor->minDistance)
@@ -331,6 +332,31 @@ uint32_t HCSR04SensorManager::microsSince(uint32_t a) {
331332
return microsBetween(micros(), a);
332333
}
333334

335+
uint16_t HCSR04SensorManager::medianMeasure(HCSR04SensorInfo *const sensor, uint16_t value) {
336+
sensor->distances[sensor->nextMedianDistance++] = value;
337+
if (sensor->nextMedianDistance >= MEDIAN_DISTANCE_MEASURES) {
338+
sensor->nextMedianDistance = 0;
339+
}
340+
return median(sensor->distances[0], sensor->distances[1], sensor->distances[2]);
341+
}
342+
343+
uint16_t HCSR04SensorManager::median(uint16_t a, uint16_t b, uint16_t c) {
344+
if (a < b) {
345+
if (a >= c) {
346+
return a;
347+
} else if (b < c) {
348+
return b;
349+
}
350+
} else {
351+
if (a < c) {
352+
return a;
353+
} else if (b >= c) {
354+
return b;
355+
}
356+
}
357+
return c;
358+
}
359+
334360
void IRAM_ATTR HCSR04SensorManager::isr(int idx) {
335361
// since the measurement of start and stop use the same interrupt
336362
// mechanism we should see a similar delay.

OpenBikeSensorFirmware/sensor.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ struct HCSR04SensorInfo
3434
uint8_t echoPin = 4;
3535
uint16_t offset = 0;
3636
uint16_t rawDistance = 0;
37+
uint16_t distances[MEDIAN_DISTANCE_MEASURES] = { MAX_SENSOR_VALUE, MAX_SENSOR_VALUE, MAX_SENSOR_VALUE };
38+
uint16_t nextMedianDistance = 0;
3739
uint16_t minDistance=MAX_SENSOR_VALUE;
3840
char* sensorLocation;
3941
unsigned long lastMinUpdate=0;
@@ -67,6 +69,8 @@ class HCSR04SensorManager
6769
void sendTriggerToReadySensor();
6870
void IRAM_ATTR isr(int idx);
6971
uint32_t getFixedStart(size_t idx, const HCSR04SensorInfo *sensor);
72+
static uint16_t medianMeasure(HCSR04SensorInfo* const sensor, uint16_t value);
73+
static uint16_t median(uint16_t a, uint16_t b, uint16_t c);
7074
static uint16_t correctSensorOffset(uint16_t dist, uint16_t offset);
7175
static boolean isReadyForStart(HCSR04SensorInfo* sensor);
7276
static uint32_t microsBetween(uint32_t a, uint32_t b);

0 commit comments

Comments
 (0)