Skip to content

Commit a3034d0

Browse files
committed
fixed reed noise filtering - prevent calculating
1 parent 13d5cb3 commit a3034d0

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

main/Tasks/core/calcRideParamsOnISR.task.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ static void vOnSyncFinishHandle(void* handler_args, esp_event_base_t base, int32
2626

2727
if (reset){
2828
rideParams.rotations = 0;
29-
rideParams.msBetweenRotationTicks = 0;
3029
rideParams.totalRideTimeMs = 0;
3130
rideParams.speed = 0.0;
3231
rideParams.distance = 0.0;
@@ -40,6 +39,7 @@ static void vOnSyncFinishHandle(void* handler_args, esp_event_base_t base, int32
4039
void vCalcRideParamsOnISRTask(void* data)
4140
{
4241
float currentSpeed;
42+
uint16_t msBetweenRotationTicks;
4343

4444
ESP_LOGI(TAG, "Init reed switch");
4545

@@ -54,30 +54,33 @@ void vCalcRideParamsOnISRTask(void* data)
5454
if (!rideParams.moving) {
5555
esp_event_post_to(obc_events_loop, OBC_EVENTS, RIDE_START_EVENT, NULL, 0, portMAX_DELAY);
5656
}
57-
rideParams.moving = true;
58-
rideParams.rotations++;
59-
rideParams.msBetweenRotationTicks = ((int) rideParams.rotationTickCount - (int) rideParams.prevRotationTickCount) * (int) portTICK_RATE_MS;
60-
rideParams.totalRideTimeMs += rideParams.msBetweenRotationTicks;
57+
msBetweenRotationTicks = ((int) rideParams.rotationTickCount - (int) rideParams.prevRotationTickCount) * (int) portTICK_RATE_MS;
58+
rideParams.totalRideTimeMs += msBetweenRotationTicks;
6159
// TODO sometimes tick count doesn't update even with queue? prevent speed = inf for now
62-
if (rideParams.msBetweenRotationTicks > 0.00) {
63-
currentSpeed = ( (float) CIRCUMFERENCE/1000000 ) / ( (float) rideParams.msBetweenRotationTicks / 3600000 ); //km/h
60+
if (msBetweenRotationTicks > 0.00) {
61+
currentSpeed = ( (float) CIRCUMFERENCE/1000000 ) / ( (float) msBetweenRotationTicks / 3600000 ); //km/h
6462
// simple, soft filtering of noise (from cables?) - should't introduce any discrepancy to actual data
6563
// and also could be used to detect unplanned, immediate stops
66-
if (abs(currentSpeed - rideParams.speed) < 50) {
67-
rideParams.speed = currentSpeed;
64+
// max reasonable acceleration is 5.8 m/s/s
65+
if (abs(currentSpeed - rideParams.speed) > 30) {
66+
continue;
67+
}
68+
69+
rideParams.moving = true;
70+
rideParams.rotations++;
71+
rideParams.speed = currentSpeed;
72+
rideParams.distance += (float)CIRCUMFERENCE/1000000;
73+
rideParams.totalDistance += (float)CIRCUMFERENCE/1000000;
74+
rideParams.avgSpeed = rideParams.distance / ( (float) rideParams.totalRideTimeMs / 3600000 );
75+
if (rideParams.speed > rideParams.maxSpeed) {
76+
rideParams.maxSpeed = rideParams.speed;
6877
}
69-
70-
}
71-
if (rideParams.speed > rideParams.maxSpeed) {
72-
rideParams.maxSpeed = rideParams.speed;
7378
}
74-
rideParams.distance += (float)CIRCUMFERENCE/1000000;
75-
rideParams.totalDistance += (float)CIRCUMFERENCE/1000000;
76-
rideParams.avgSpeed = rideParams.distance / ( (float) rideParams.totalRideTimeMs / 3600000 );
79+
7780
}
7881
rideParams.prevRotationTickCount = rideParams.rotationTickCount;
7982

80-
// ESP_LOGI(TAG, "[REED] count: %d, speed: %0.2f, diff: %d, distance: %0.2f", rideParams.rotations, rideParams.speed, rideParams.msBetweenRotationTicks, rideParams.distance);
83+
// ESP_LOGI(TAG, "[REED] count: %d, speed: %0.2f, diff: %d, distance: %0.2f", rideParams.rotations, rideParams.speed, msBetweenRotationTicks, rideParams.distance);
8184
// ESP_LOGI(TAG, "[AVGS] speed: %0.2f", rideParams.avgSpeed);
8285
}
8386
}

main/Tasks/core/rideStatusWatchdog.task.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ void vRideStatusWatchdogTask(void *arg) {
2828
if(rideParams.moving && !msg_count && timeInactive > RIDE_TIMEOUT_MS) {
2929
ESP_LOGI(TAG, "[RIDE_STATUS] Stopped moving");
3030
rideParams.speed = 0.0;
31-
rideParams.msBetweenRotationTicks = 0;
3231
rideParams.moving = false;
3332
rideParams.prevRotationTickCount = 0;
3433

main/obc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
uint32_t rotations;
1313
portTickType prevRotationTickCount;
1414
portTickType rotationTickCount;
15-
uint16_t msBetweenRotationTicks;
1615
uint32_t totalRideTimeMs;
1716
float speed;
1817
float avgSpeed;

0 commit comments

Comments
 (0)