@@ -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
4039void 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 }
0 commit comments