@@ -37,7 +37,7 @@ static void vOnSyncFinishHandle(void *handler_args, esp_event_base_t base, int32
3737
3838void vCalcRideParamsOnISRTask (void * data ) {
3939 float currentSpeed ;
40- uint16_t msBetweenRotationTicks ;
40+ uint16_t msBetweenRotationTicks = 0 ;
4141
4242 ESP_LOGI (TAG , "Init reed switch" );
4343
@@ -46,38 +46,37 @@ void vCalcRideParamsOnISRTask(void *data) {
4646
4747 for (;;) {
4848 if (xQueueReceive (reed_evt_queue , & rideParams .rotationTickCount , portMAX_DELAY ) && !ignoreReed ) {
49- // TODO create buffer for reed time impulses before calculating time and speed
50- // TODO block rideParams while calculating?
51- if (rideParams .prevRotationTickCount != 0 ) {
52- if (!rideParams .moving ) {
53- esp_event_post_to (obc_events_loop , OBC_EVENTS , RIDE_START_EVENT , NULL , 0 , portMAX_DELAY );
54- }
55- msBetweenRotationTicks = ((int )rideParams .rotationTickCount - (int )rideParams .prevRotationTickCount ) * (int )portTICK_PERIOD_MS ;
56- rideParams .totalRideTimeMs += msBetweenRotationTicks ;
57- // TODO sometimes tick count doesn't update even with queue? prevent speed = inf for now
58- if (msBetweenRotationTicks > 0.00 ) {
59- currentSpeed = ((float )CIRCUMFERENCE / 1000000 ) / ((float )msBetweenRotationTicks / 3600000 ); // km/h
60- // simple, soft filtering of noise (from cables?) - should't introduce any discrepancy to actual data
61- // and also could be used to detect unplanned, immediate stops
62- // max reasonable acceleration is 5.8 m/s/s
63- if (abs ((int )(currentSpeed - rideParams .speed )) > 30 ) {
64- continue ;
65- }
66-
67- rideParams .moving = true;
68- rideParams .rotations ++ ;
69- rideParams .speed = currentSpeed ;
70- rideParams .distance += (float )CIRCUMFERENCE / 1000000 ;
71- rideParams .totalDistance += (float )CIRCUMFERENCE / 1000000 ;
72- rideParams .avgSpeed = rideParams .distance / ((float )rideParams .totalRideTimeMs / 3600000 );
73- if (rideParams .speed > rideParams .maxSpeed ) {
74- rideParams .maxSpeed = rideParams .speed ;
75- }
76- }
49+ if (rideParams .rotationTickCount == rideParams .prevRotationTickCount || rideParams .rotationTickCount == 0 ) {
50+ continue ;
51+ };
52+
53+ msBetweenRotationTicks = ((int )rideParams .rotationTickCount - (int )rideParams .prevRotationTickCount ) * (int )portTICK_PERIOD_MS ;
54+
55+ if (msBetweenRotationTicks <= REED_BOUNCE_MS ) {
56+ continue ;
7757 }
58+
7859 rideParams .prevRotationTickCount = rideParams .rotationTickCount ;
60+ // ESP_LOGI(TAG, "Reed switch triggered %" PRIu32 " diff: %" PRIu16, rideParams.rotationTickCount, msBetweenRotationTicks);
61+
62+ // TODO block rideParams while calculating?
63+ if (!rideParams .moving ) {
64+ esp_event_post_to (obc_events_loop , OBC_EVENTS , RIDE_START_EVENT , NULL , 0 , portMAX_DELAY );
65+ }
66+
67+ currentSpeed = ((float )CIRCUMFERENCE / 1000000 ) / ((float )msBetweenRotationTicks / 3600000 ); // km/h
68+ rideParams .moving = true;
69+ rideParams .rotations ++ ;
70+ rideParams .speed = currentSpeed ;
71+ rideParams .totalRideTimeMs += msBetweenRotationTicks ;
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 ;
77+ }
7978
80- // ESP_LOGI(TAG, "[REED] count: %d , speed: %0.2f, diff: %d , distance: %0.2f", rideParams.rotations, rideParams.speed, msBetweenRotationTicks, rideParams.distance);
79+ // ESP_LOGI(TAG, "[REED] count: %" PRIu32 " , speed: %0.2f, diff: %" PRIu16 " , distance: %0.2f", rideParams.rotations, rideParams.speed, msBetweenRotationTicks, rideParams.distance);
8180 // ESP_LOGI(TAG, "[AVGS] speed: %0.2f", rideParams.avgSpeed);
8281 }
8382 }
0 commit comments