comparison Small_CPU/Src/pressure.c @ 482:230aed360da0

Merged in Ideenmodellierer/ostc4/Improve_Button_Sleep (pull request #45) Improve Button Sleep
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Tue, 19 May 2020 07:27:18 +0000
parents 3fe9cc747c5c
children 9eeab3fead8f
comparison
equal deleted inserted replaced
472:8a375f0544d9 482:230aed360da0
48 48
49 /* remove comment to use a predefined profile for pressure changes instead of real world data */ 49 /* remove comment to use a predefined profile for pressure changes instead of real world data */
50 /* #define SIMULATE_PRESSURE */ 50 /* #define SIMULATE_PRESSURE */
51 51
52 #define PRESSURE_SURFACE_MAX_MBAR (1030.0f) /* It is unlikely that pressure at surface is greater than this value => clip to it */ 52 #define PRESSURE_SURFACE_MAX_MBAR (1030.0f) /* It is unlikely that pressure at surface is greater than this value => clip to it */
53 #define PRESSURE_HISTORY_SIZE (8u)
54 53
55 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */ 54 #define PRESSURE_SURFACE_QUE (30u) /* history buffer [minutes] for past pressure measurements */
56 #define PRESSURE_SURFACE_EVA_WINDOW (15u) /* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */ 55 #define PRESSURE_SURFACE_EVA_WINDOW (15u) /* Number of entries evaluated during instability test. Used to avoid detection while dive enters water */
57 #define PRESSURE_SURFACE_STABLE_LIMIT (10u) /* Define pressure as stable if delta (mBar) is below this value */ 56 #define PRESSURE_SURFACE_STABLE_LIMIT (10u) /* Define pressure as stable if delta (mBar) is below this value */
58 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */ 57 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */
98 97
99 static uint8_t surface_pressure_writeIndex = 0; 98 static uint8_t surface_pressure_writeIndex = 0;
100 static float surface_pressure_stable_value = 0; 99 static float surface_pressure_stable_value = 0;
101 static uint8_t surface_pressure_stable = 0; 100 static uint8_t surface_pressure_stable = 0;
102 101
103 static float pressure_history_mbar[PRESSURE_HISTORY_SIZE];
104
105 static uint8_t secondCounterSurfaceRing = 0; 102 static uint8_t secondCounterSurfaceRing = 0;
106 static uint8_t avgCount = 0; 103 static uint8_t avgCount = 0;
107 static float runningAvg = 0; 104 static float runningAvg = 0;
108 105
109 float get_temperature(void) 106 float get_temperature(void)
132 129
133 for(int i=0; i<PRESSURE_SURFACE_QUE; i++) 130 for(int i=0; i<PRESSURE_SURFACE_QUE; i++)
134 surface_ring_mbar[i] = ambient_pressure_mbar; 131 surface_ring_mbar[i] = ambient_pressure_mbar;
135 surface_pressure_mbar = ambient_pressure_mbar; 132 surface_pressure_mbar = ambient_pressure_mbar;
136 surface_pressure_writeIndex = 0; /* index of the oldest value in the ring buffer */ 133 surface_pressure_writeIndex = 0; /* index of the oldest value in the ring buffer */
137 }
138 }
139
140 void init_pressure_history(void)
141 {
142 for(int i=0; i<PRESSURE_HISTORY_SIZE; i++)
143 {
144 pressure_history_mbar[i] = 1000.0;
145 } 134 }
146 } 135 }
147 136
148 uint8_t is_surface_pressure_stable(void) 137 uint8_t is_surface_pressure_stable(void)
149 { 138 {
218 EvaluatedValues++; 207 EvaluatedValues++;
219 } while (index != surface_pressure_writeIndex); 208 } while (index != surface_pressure_writeIndex);
220 } 209 }
221 void update_surface_pressure(uint8_t call_rhythm_seconds) 210 void update_surface_pressure(uint8_t call_rhythm_seconds)
222 { 211 {
223
224
225 if(is_init_pressure_done()) 212 if(is_init_pressure_done())
226 { 213 {
227 runningAvg = (runningAvg * avgCount + ambient_pressure_mbar) / (avgCount +1); 214 runningAvg = (runningAvg * avgCount + ambient_pressure_mbar) / (avgCount +1);
228 avgCount++; 215 avgCount++;
229 secondCounterSurfaceRing += call_rhythm_seconds; 216 secondCounterSurfaceRing += call_rhythm_seconds;
341 uint8_t buffer[1]; 328 uint8_t buffer[1];
342 buffer[0] = 0x1E; // Reset Command 329 buffer[0] = 0x1E; // Reset Command
343 uint8_t retValue = 0xFF; 330 uint8_t retValue = 0xFF;
344 331
345 pressureSensorInitSuccess = false; 332 pressureSensorInitSuccess = false;
346 init_pressure_history();
347 333
348 /* Probe new sensor first */ 334 /* Probe new sensor first */
349 retValue = I2C_Master_Transmit( DEVICE_PRESSURE_MS5837, buffer, 1); 335 retValue = I2C_Master_Transmit( DEVICE_PRESSURE_MS5837, buffer, 1);
350 if(retValue != HAL_OK) 336 if(retValue != HAL_OK)
351 { 337 {
389 375
390 if(global.I2C_SystemStatus == HAL_OK) 376 if(global.I2C_SystemStatus == HAL_OK)
391 { 377 {
392 pressureSensorInitSuccess = 1; 378 pressureSensorInitSuccess = 1;
393 retValue = pressure_update(); 379 retValue = pressure_update();
394
395 } 380 }
396 return retValue; 381 return retValue;
397 } 382 }
398 383
399 384
400 static uint32_t get_adc(void) 385 static uint32_t get_adc(void)
401 { 386 {
402 uint8_t buffer[1]; 387 uint8_t buffer[1];
403 uint8_t resivebuf[4]; 388 uint8_t resivebuf[4];
404 uint32_t answer = 0; 389 uint32_t answer = 0xFFFFFFFF;
405 390
406 buffer[0] = 0x00; // Get ADC 391 buffer[0] = 0x00; // Get ADC
407 I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1); 392 if(I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1) == HAL_OK)
408 I2C_Master_Receive( PRESSURE_ADDRESS, resivebuf, 4); 393 {
409 resivebuf[3] = 0; 394 if(I2C_Master_Receive( PRESSURE_ADDRESS, resivebuf, 4) == HAL_OK)
410 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2]; 395 {
411 396 resivebuf[3] = 0;
397 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
398 }
399 }
412 return answer; 400 return answer;
413 } 401 }
414 402
415 403
416 static uint16_t get_ci_by_coef_num(uint8_t coef_num) 404 static uint16_t get_ci_by_coef_num(uint8_t coef_num)
458 } 446 }
459 447
460 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn) 448 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn)
461 { 449 {
462 uint8_t command = CMD_ADC_CONV + cmd; 450 uint8_t command = CMD_ADC_CONV + cmd;
451 uint32_t adcValue = 0;
463 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT; 452 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT;
464 453
465 statusReturnTemp = I2C_Master_Transmit( PRESSURE_ADDRESS, &command, 1); 454 statusReturnTemp = I2C_Master_Transmit( PRESSURE_ADDRESS, &command, 1);
466 455
467 if(statusReturn) 456 if(statusReturn)
468 { 457 {
469 *statusReturn = statusReturnTemp; 458 *statusReturn = statusReturnTemp;
470 } 459 }
471 460
472 switch (cmd & 0x0f) // wait necessary conversion time 461 switch (cmd & 0x0f) // wait necessary conversion time
473 { 462 {
474 case CMD_ADC_256 : HAL_Delay(1); break; 463 case CMD_ADC_256 : HAL_Delay(1); break;
475 case CMD_ADC_512 : HAL_Delay(3); break; 464 case CMD_ADC_512 : HAL_Delay(3); break;
476 case CMD_ADC_1024: HAL_Delay(4); break; 465 case CMD_ADC_1024: HAL_Delay(4); break;
477 case CMD_ADC_2048: HAL_Delay(6); break; 466 case CMD_ADC_2048: HAL_Delay(6); break;
478 case CMD_ADC_4096: HAL_Delay(10); break; 467 case CMD_ADC_4096: HAL_Delay(10); break;
479 } 468 default:
480 return get_adc(); 469 break;
470 }
471 adcValue = get_adc();
472 if(adcValue == 0xFFFFFFFF)
473 {
474 if(statusReturn)
475 {
476 *statusReturn = HAL_ERROR;
477 }
478 }
479 return adcValue;
481 } 480 }
482 481
483 482
484 static HAL_StatusTypeDef pressure_sensor_get_data(void) 483 static HAL_StatusTypeDef pressure_sensor_get_data(void)
485 { 484 {
604 #else 603 #else
605 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); 604 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015();
606 #endif 605 #endif
607 } 606 }
608 607
609 static uint8_t pressure_plausible(float pressurevalue)
610 {
611 static uint8_t pressurewriteindex = 0;
612 uint8_t retval = 0;
613 uint8_t index;
614 float pressure_average = 0;
615
616 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++)
617 {
618 pressure_average += pressure_history_mbar[index];
619 }
620 pressure_average /= PRESSURE_HISTORY_SIZE;
621 if(pressure_average == 1000.0) /* first pressure calculation */
622 {
623 if(fabs(pressurevalue - pressure_average) < 11000.0) /* just in case a reset occur during dive assume value equal < 100m as valid */
624 {
625 for(index = 0; index < PRESSURE_HISTORY_SIZE; index++)
626 {
627 pressure_history_mbar[index] = pressurevalue; /* set history to current value */
628 retval = 1;
629 }
630 }
631 }
632 else
633 {
634 pressure_history_mbar[pressurewriteindex++] = pressurevalue;
635 pressurewriteindex &= 0x7; /* wrap around if necessary */
636 retval = 1;
637 }
638
639 return retval;
640 }
641
642 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void) 608 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void)
643 { 609 {
644 static float runningAvg = 0; 610 static float runningAvg = 0;
645 static uint8_t avgCnt = 0; 611 static uint8_t avgCnt = 0;
646 612
710 ambient_temperature += temperature_offset; 676 ambient_temperature += temperature_offset;
711 677
712 calc_pressure = ((float)local_Px10) / 10; 678 calc_pressure = ((float)local_Px10) / 10;
713 calc_pressure += pressure_offset; 679 calc_pressure += pressure_offset;
714 680
715 if(pressure_plausible(calc_pressure)) 681 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1);
716 { 682 if (avgCnt < 10) /* build an average considering the last measurements to have a weight "1 of 10" */
717 runningAvg = (avgCnt * runningAvg + calc_pressure) / (avgCnt + 1); 683 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */
718 if (avgCnt < 10) /* build an average considering the last measurements to have a weight "1 of 10" */ 684 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */
719 { /* Main reason for this is the jitter of up to +-10 HPa in surface mode which is caused */ 685 }
720 avgCnt++; /* by the measurement range of the sensor which is focused on under water pressure measurement */ 686 ambient_pressure_mbar = runningAvg;
721 }
722 ambient_pressure_mbar = runningAvg;
723 }
724 } 687 }
725 688
726 689
727 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011 690 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011
728 * short and int are both 16bit according to AVR/GCC google results 691 * short and int are both 16bit according to AVR/GCC google results