comparison Small_CPU/Src/externalInterface.c @ 747:df0d43da1614

Added pressure compensation to CO2 detection: A pressure compensation is needed if the ExplorIR shall be used under extended pressure conditions. The procedure recommended by the application note has been integrated. To keep things simple the focus of the CO2 measurement is not the precision, as it is needed for the decompression calculation, but the indication of a critical increase of CO2 in the breathing loop. That's why only a lookup table with 1000ppm steps has been implemented instead of calculating the polynom for every measurement.
author Ideenmodellierer
date Sun, 05 Mar 2023 22:06:47 +0100
parents 7e84ae1513b6
children 4a28402e4aca
comparison
equal deleted inserted replaced
746:7e84ae1513b6 747:df0d43da1614
28 #include "i2c.h" 28 #include "i2c.h"
29 #include "externalInterface.h" 29 #include "externalInterface.h"
30 #include "scheduler.h" 30 #include "scheduler.h"
31 #include "uart.h" 31 #include "uart.h"
32 #include "data_exchange.h" 32 #include "data_exchange.h"
33 #include "pressure.h"
33 34
34 extern SGlobal global; 35 extern SGlobal global;
35 extern UART_HandleTypeDef huart1; 36 extern UART_HandleTypeDef huart1;
36 37
37 #define ADC_ANSWER_LENGTH (5u) /* 3424 will provide addr + 4 data bytes */ 38 #define ADC_ANSWER_LENGTH (5u) /* 3424 will provide addr + 4 data bytes */
48 #define ADC_RESOLUTION_18BIT (0x0C) 49 #define ADC_RESOLUTION_18BIT (0x0C)
49 #define ADC_RESOLUTION_18BIT_VALUE (18u) 50 #define ADC_RESOLUTION_18BIT_VALUE (18u)
50 51
51 #define ANSWER_CONFBYTE_INDEX (4u) 52 #define ANSWER_CONFBYTE_INDEX (4u)
52 53
54 #define LOOKUP_CO2_CORR_TABLE_SCALE (1000u)
55 #define LOOKUP_CO2_CORR_TABLE_MAX (30000u)
56
53 static uint8_t activeChannel = 0; /* channel which is in request */ 57 static uint8_t activeChannel = 0; /* channel which is in request */
54 static uint8_t recBuf[ADC_ANSWER_LENGTH]; 58 static uint8_t recBuf[ADC_ANSWER_LENGTH];
55 static uint8_t timeoutCnt = 0; 59 static uint8_t timeoutCnt = 0;
56 static uint8_t externalInterfacePresent = 0; 60 static uint8_t externalInterfacePresent = 0;
57 61
64 static uint16_t externalCO2Status = 0; 68 static uint16_t externalCO2Status = 0;
65 69
66 static uint8_t sensorDataId = 0; 70 static uint8_t sensorDataId = 0;
67 static SSensorDataDiveO2 sensorDataDiveO2; 71 static SSensorDataDiveO2 sensorDataDiveO2;
68 static externalInterfaceAutoDetect_t externalAutoDetect = DETECTION_OFF; 72 static externalInterfaceAutoDetect_t externalAutoDetect = DETECTION_OFF;
69 static externalInterfaceSensorType SensorMap[EXT_INTERFACE_SENSOR_CNT] ={ SENSOR_ANALOG, SENSOR_ANALOG, SENSOR_ANALOG, SENSOR_NONE, SENSOR_NONE}; 73 static externalInterfaceSensorType SensorMap[EXT_INTERFACE_SENSOR_CNT] ={ SENSOR_OPTIC, SENSOR_OPTIC, SENSOR_OPTIC, SENSOR_NONE, SENSOR_NONE};
70 static externalInterfaceSensorType tmpSensorMap[EXT_INTERFACE_SENSOR_CNT]; 74 static externalInterfaceSensorType tmpSensorMap[EXT_INTERFACE_SENSOR_CNT];
71 static externalInterfaceSensorType MasterSensorMap[EXT_INTERFACE_SENSOR_CNT]; 75 static externalInterfaceSensorType MasterSensorMap[EXT_INTERFACE_SENSOR_CNT];
72 76
77 static float LookupCO2PressureCorrection[LOOKUP_CO2_CORR_TABLE_MAX / LOOKUP_CO2_CORR_TABLE_SCALE]; /* lookup table for pressure compensation values */
78
73 79
74 void externalInterface_Init(void) 80 void externalInterface_Init(void)
75 { 81 {
82 uint16_t index;
83 uint16_t coeff;
76 activeChannel = 0; 84 activeChannel = 0;
77 timeoutCnt = 0; 85 timeoutCnt = 0;
78 externalInterfacePresent = 0; 86 externalInterfacePresent = 0;
79 if(externalInterface_StartConversion(activeChannel) == HAL_OK) 87 if(externalInterface_StartConversion(activeChannel) == HAL_OK)
80 { 88 {
81 externalInterfacePresent = 1; 89 externalInterfacePresent = 1;
82 global.deviceDataSendToMaster.hw_Info.extADC = 1; 90 global.deviceDataSendToMaster.hw_Info.extADC = 1;
83 } 91 }
84 global.deviceDataSendToMaster.hw_Info.checkADC = 1; 92 global.deviceDataSendToMaster.hw_Info.checkADC = 1;
85 93
94 /* Create a lookup table based on GSS application note AN001: PRESSURE COMPENSATION OF A CO2 SENSOR */
95 /* The main purpose of the sensor in the dive application is to be a warning indicator */
96 /* => no exact values necessary => a lookup table with 50 entries should be sufficient */
97 LookupCO2PressureCorrection [0] = -0.0014;
98 for(index = 1; index < (LOOKUP_CO2_CORR_TABLE_MAX / LOOKUP_CO2_CORR_TABLE_SCALE); index++)
99 {
100 coeff = index * LOOKUP_CO2_CORR_TABLE_SCALE;
101 LookupCO2PressureCorrection[index] = 2.811*pow(10,-38)*pow(coeff,6)- 9.817*pow(10,-32)*pow(coeff,5)+1.304*pow(10,-25)*pow(coeff,4)-8.216*pow(10,-20)*pow(coeff,3)+2.311*pow(10,-14)*pow(coeff,2) - 2.195*pow(10,-9)*coeff - 1.471*pow(10,-3);
102 }
86 externalInterface_InitDatastruct(); 103 externalInterface_InitDatastruct();
87 } 104 }
88 105
89 void externalInterface_InitDatastruct(void) 106 void externalInterface_InitDatastruct(void)
90 { 107 {
337 } 354 }
338 } 355 }
339 356
340 void externalInterface_SetCO2Value(uint16_t CO2_ppm) 357 void externalInterface_SetCO2Value(uint16_t CO2_ppm)
341 { 358 {
342 externalCO2Value = CO2_ppm; 359 float local_ppm = CO2_ppm * 10.0; /* scalfactor */
360
361 #ifndef ENABLE_EXTERNAL_PRESSURE
362 float local_corr = 0.0;
363
364 if (local_ppm >= LOOKUP_CO2_CORR_TABLE_MAX)
365 {
366 local_corr = -0.0014;
367 }
368 else
369 {
370 local_corr = LookupCO2PressureCorrection[((uint16_t) (local_ppm / LOOKUP_CO2_CORR_TABLE_SCALE))];
371 }
372 local_ppm = local_ppm / (1.0 + (local_corr * (get_surface_mbar() - get_pressure_mbar())));
373 #else
374 /* The external pressure value is passed via ADC channel2 and calibration is done at firmware => just forward sensor data */
375 /* compensation is done at firmware side. This is for testing only. Take care the the same algorithm is taken as used for the lookup table */
376 #endif
377 externalCO2Value = local_ppm / 10.0;
343 } 378 }
344 379
345 void externalInterface_SetCO2SignalStrength(uint16_t LED_qa) 380 void externalInterface_SetCO2SignalStrength(uint16_t LED_qa)
346 { 381 {
347 externalCO2SignalStrength = LED_qa; 382 externalCO2SignalStrength = LED_qa;