comparison Discovery/Src/data_exchange_main.c @ 173:05c770dc2911 max-depth

Bugfix: make max depth move with current depth (part 1) The display in dive mode of the max depth was updated before the actual depth, which looks very strange. The reason for this was conceptually simple. The depth value was averaged over a set of depth samples, but the current depth was only taken from the current sample. So, per definition, on an initial descend, the current depth is always bigger (deeper) than any average from previous shallower samples. This part 1 commit introduces a new function that is used immediate after reception of the new sample from the RTE. This function does the trivial average of a set of samples. Notice that also the surface and ambient mbar pressures are taken into account (which are used heavily over the entire code). This is a consistency thing. We should base any further calculation from the data presented in the UI, instead of presenting A, and use A' for further calculations. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Mon, 11 Mar 2019 19:48:57 +0100
parents 1fbdb45db701
children 8b8074080d7b
comparison
equal deleted inserted replaced
172:c659fda83e44 173:05c770dc2911
71 #include "timer.h" 71 #include "timer.h"
72 #include "buehlmann.h" 72 #include "buehlmann.h"
73 #include "externLogbookFlash.h" 73 #include "externLogbookFlash.h"
74 74
75 75
76 /* Expoted variables --------------------------------------------------------*/ 76 /* Exported variables --------------------------------------------------------*/
77 uint8_t wasPowerOn = 0; 77 uint8_t wasPowerOn = 0;
78 confirmbit8_Type requestNecessary = { .uw = 0 }; 78 confirmbit8_Type requestNecessary = { .uw = 0 };
79 uint8_t wasUpdateNotPowerOn = 0; 79 uint8_t wasUpdateNotPowerOn = 0;
80 80
81 /* Private variables with external access ------------------------------------*/ 81 /* Private variables with external access ------------------------------------*/
747 data_old__lost_connection_to_slave_counter_total += 1; 747 data_old__lost_connection_to_slave_counter_total += 1;
748 } 748 }
749 } 749 }
750 } 750 }
751 751
752 #define AVERAGE_COUNT 4
753 static float getSampleDepth(SDataExchangeSlaveToMaster *d, SDiveState *ds)
754 {
755 static uint8_t c = 0;
756 static float ambient[AVERAGE_COUNT] = {0};
757 static float surface[AVERAGE_COUNT]= {0};
758 static float depth[AVERAGE_COUNT]= {0};
759
760 ambient[c] = d->data[d->boolPressureData].pressure_mbar / 1000.0f;
761 surface[c] = d->data[d->boolPressureData].surface_mbar / 1000.0f;
762 float density = ((float)( 100 + settingsGetPointer()->salinity)) / 100.0f;
763
764 ds->lifeData.pressure_ambient_bar = (ambient[0] + ambient[1] + ambient[2] + ambient[3])/4.0f;
765 ds->lifeData.pressure_surface_bar = (surface[0] + surface[1] + surface[2] + surface[3])/4.0f;
766 depth[c] = (ambient[c] - surface[c]) / (0.09807f * density);
767
768 c++;
769 if (c == AVERAGE_COUNT) c = 0;
770
771 return (depth[0] + depth[1] + depth[2] + depth[3])/4.0f;
772 }
752 773
753 void DataEX_copy_to_LifeData(_Bool *modeChangeFlag) 774 void DataEX_copy_to_LifeData(_Bool *modeChangeFlag)
754 { 775 {
755 SDiveState * pStateReal = stateRealGetPointerWrite(); 776 SDiveState *pStateReal = stateRealGetPointerWrite();
756 static uint16_t getDeviceDataAfterStartOfMainCPU = 20; 777 static uint16_t getDeviceDataAfterStartOfMainCPU = 20;
757 778
758 /* internal sensor: HUD data 779 /* internal sensor: HUD data
759 */ 780 */
760 for(int i=0;i<3;i++) 781 for(int i=0;i<3;i++)
778 else 799 else
779 pStateReal->lifeData.bottle_bar_age_MilliSeconds[i] += 100; 800 pStateReal->lifeData.bottle_bar_age_MilliSeconds[i] += 100;
780 } 801 }
781 } 802 }
782 803
783 /* Why? hw 8.6.2015
784 if(DataEX_check_header_and_footer_ok() && dataIn.power_on_reset)
785 {
786 return;
787 }
788 */
789 if(!DataEX_check_header_and_footer_ok()) 804 if(!DataEX_check_header_and_footer_ok())
790 { 805 {
791 if(DataEX_check_header_and_footer_devicedata()) 806 if(DataEX_check_header_and_footer_devicedata())
792 { 807 {
793 DataEX_copy_to_DeviceData(); 808 DataEX_copy_to_DeviceData();
836 851
837 if(requestNecessary.ub.button == 1) 852 if(requestNecessary.ub.button == 1)
838 { 853 {
839 setButtonResponsiveness(settingsGetPointer()->ButtonResponsiveness); 854 setButtonResponsiveness(settingsGetPointer()->ButtonResponsiveness);
840 } 855 }
841 /*
842 }
843 if((dataIn.confirmRequest.ub.clearDeco != 1) && (requestNecessary.ub.clearDeco == 1))
844 {
845 clearDeco(); // is dataOut.clearDecoNow = 1;
846 }
847 */
848 } 856 }
849 requestNecessary.uw = 0; // clear all 857 requestNecessary.uw = 0; // clear all
850 858
851 float ambient, surface, density, meter; 859 float meter = 0;
852 SSettings *pSettings; 860 SSettings *pSettings;
853
854 ambient = 0;
855 surface = 0;
856 meter = 0;
857 861
858 /* uint8_t IAmStolenPleaseKillMe; 862 /* uint8_t IAmStolenPleaseKillMe;
859 */ 863 */
860 pSettings = settingsGetPointer(); 864 pSettings = settingsGetPointer();
861 865
867 dataIn.mode = MODE_DIVE; 871 dataIn.mode = MODE_DIVE;
868 } 872 }
869 873
870 if(pStateReal->data_old__lost_connection_to_slave == 0) 874 if(pStateReal->data_old__lost_connection_to_slave == 0)
871 { 875 {
872 ambient = dataIn.data[dataIn.boolPressureData].pressure_mbar / 1000.0f; 876 meter = getSampleDepth(&dataIn, pStateReal);
873 surface = dataIn.data[dataIn.boolPressureData].surface_mbar / 1000.0f;
874
875 density = ((float)( 100 + pSettings->salinity)) / 100.0f;
876 meter = (ambient - surface);
877 meter /= (0.09807f * density);
878
879 877
880 pStateReal->pressure_uTick_old = pStateReal->pressure_uTick_new; 878 pStateReal->pressure_uTick_old = pStateReal->pressure_uTick_new;
881 pStateReal->pressure_uTick_new = dataIn.data[dataIn.boolPressureData].pressure_uTick; 879 pStateReal->pressure_uTick_new = dataIn.data[dataIn.boolPressureData].pressure_uTick;
882 pStateReal->pressure_uTick_local_new = HAL_GetTick(); 880 pStateReal->pressure_uTick_local_new = HAL_GetTick();
883
884 /* what was the code behind this if statement ? */
885 /* if(ambient < (surface + 0.04f)) */
886 881
887 pStateReal->lifeData.dateBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_dr; 882 pStateReal->lifeData.dateBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_dr;
888 pStateReal->lifeData.timeBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_tr; 883 pStateReal->lifeData.timeBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_tr;
889 } 884 }
890 dataOut.setAccidentFlag = 0; 885 dataOut.setAccidentFlag = 0;
931 dataOut.setAccidentFlag += ACCIDENT_DECOSTOP; 926 dataOut.setAccidentFlag += ACCIDENT_DECOSTOP;
932 } 927 }
933 pStateReal->mode = dataIn.mode; 928 pStateReal->mode = dataIn.mode;
934 pStateReal->chargeStatus = dataIn.chargeStatus; 929 pStateReal->chargeStatus = dataIn.chargeStatus;
935 930
936 pStateReal->lifeData.pressure_ambient_bar = ambient;
937 pStateReal->lifeData.pressure_surface_bar = surface;
938 if(is_ambient_pressure_close_to_surface(&pStateReal->lifeData)) 931 if(is_ambient_pressure_close_to_surface(&pStateReal->lifeData))
939 { 932 {
940 pStateReal->lifeData.depth_meter = 0; 933 pStateReal->lifeData.depth_meter = 0;
941 } 934 }
942 else 935 else
969 if(settingsGetPointer()->FlipDisplay) /* consider that diver is targeting into the opposite direction */ 962 if(settingsGetPointer()->FlipDisplay) /* consider that diver is targeting into the opposite direction */
970 { 963 {
971 pStateReal->lifeData.compass_heading -= 180.0; 964 pStateReal->lifeData.compass_heading -= 180.0;
972 if (pStateReal->lifeData.compass_heading < 0) pStateReal->lifeData.compass_heading +=360.0; 965 if (pStateReal->lifeData.compass_heading < 0) pStateReal->lifeData.compass_heading +=360.0;
973 } 966 }
974
975 967
976 pStateReal->lifeData.compass_roll = dataIn.data[dataIn.boolCompassData].compass_roll; 968 pStateReal->lifeData.compass_roll = dataIn.data[dataIn.boolCompassData].compass_roll;
977 pStateReal->lifeData.compass_pitch = dataIn.data[dataIn.boolCompassData].compass_pitch; 969 pStateReal->lifeData.compass_pitch = dataIn.data[dataIn.boolCompassData].compass_pitch;
978 970
979 pStateReal->lifeData.compass_DX_f = dataIn.data[dataIn.boolCompassData].compass_DX_f; 971 pStateReal->lifeData.compass_DX_f = dataIn.data[dataIn.boolCompassData].compass_DX_f;
1054 { 1046 {
1055 if(pStateReal->lifeData.max_depth_meter > 1.5f) 1047 if(pStateReal->lifeData.max_depth_meter > 1.5f)
1056 { 1048 {
1057 pStateReal->lifeData.apnea_last_max_depth_meter = pStateReal->lifeData.max_depth_meter; 1049 pStateReal->lifeData.apnea_last_max_depth_meter = pStateReal->lifeData.max_depth_meter;
1058 } 1050 }
1059 // eset max_depth_meter, average_depth_meter and internal values 1051 // reset max_depth_meter, average_depth_meter and internal values
1060 pStateReal->lifeData.max_depth_meter = 0; 1052 pStateReal->lifeData.max_depth_meter = 0;
1061 pStateReal->lifeData.boolResetAverageDepth = 1; 1053 pStateReal->lifeData.boolResetAverageDepth = 1;
1062 pStateReal->lifeData.boolResetStopwatch = 1; 1054 pStateReal->lifeData.boolResetStopwatch = 1;
1063 } 1055 }
1064 } 1056 }