Mercurial > public > ostc4
view Small_CPU/Src/i2c.c @ 224:ceecabfddb57 div-fixes-3
Bugfix, deco: fix 2 (small) problems with calculated ceiling
This fixes 1 trivial, and 1 not really trivial bug in the calculation
of the ceiling. When simulating a bounce dive to 80m, things become
clear (tried this on a CCR dive, fixed setpoint 1.2bar, about 15 minutes
of bottom time). Closely watch the behavior of the ceiling data. At some
point during the ascent, the ceiling begins to decrease in 10cm steps.
Then suddenly (while still ascending), the ceiling increases again with 1m,
does not change for some time, and then suddenly steps 1.1m less deep.
While not very relevant to real deco diving, it is simply wrong.
The reason for this is subtle. The algorithm used to find the ceiling
is a sort of linear search, stepping down a meter, overshoot the depth, and
search back in 10cm steps. It seems some numerical instability. Fixing
this, was a bit more computational intensive search by stepping up down in
equal steps of 10cm. But, I'm pretty sure that things can be speeded up here, as a
ceiling does not change fast, so it should be not that difficult to limit
the search space, or use a binary search algorithm instead.
The trivial second problem fixed, is that the ceiling ends at the surface
and not at 1m depth. This small issue became visible after changing the step
down size above.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Sun, 31 Mar 2019 19:35:51 +0200 |
parents | 7e749084f347 |
children | a9d798e8c11f |
line wrap: on
line source
#include "baseCPU2.h" #include "i2c.h" #include "scheduler.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ // =============================================================================== // I2C addresses - see i2c.h // =============================================================================== I2C_HandleTypeDef I2cHandle; /* static void I2C_Error_Handler(void) { while(1) { } } */ HAL_StatusTypeDef I2C1_Status(void) { return (HAL_StatusTypeDef)global.I2C_SystemStatus; } GPIO_PinState HAL_I2C_Read_Data_PIN(void) { return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); } void HAL_I2C_Send_One_CLOCK(void) { HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET); HAL_Delay(10); HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET); HAL_Delay(10); } GPIO_PinState MX_I2C1_TestAndClear(void) { I2C_DeInit(); HAL_I2C_ManualControl_MspInit(); for(int i=0; i<9;i++) { if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET) HAL_I2C_Send_One_CLOCK(); else break; } return HAL_I2C_Read_Data_PIN(); } HAL_StatusTypeDef MX_I2C1_Init(void) { I2cHandle.Instance = I2Cx; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; I2cHandle.Init.OwnAddress1 = 0x01; global.dataSendToSlaveStopEval = 1; global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); global.dataSendToSlaveStopEval = 0; if(global.dataSendToSlavePending) { scheduleSpecial_Evaluate_DataSendToSlave(); } return (HAL_StatusTypeDef)global.I2C_SystemStatus; } void I2C_DeInit(void) { HAL_I2C_DeInit(&I2cHandle); } uint8_t i2c_errors = 0; void I2C_Error_count(void) { i2c_errors++; } HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) return (HAL_StatusTypeDef)global.I2C_SystemStatus; global.dataSendToSlaveStopEval = 1; global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 0); if(global.I2C_SystemStatus != HAL_OK) { I2C_Error_count(); } global.dataSendToSlaveStopEval = 0; //TODO: REMOVE. // if(global.dataSendToSlavePending) // { // scheduleSpecial_Evaluate_DataSendToSlave(); // } return (HAL_StatusTypeDef)global.I2C_SystemStatus; } HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03); global.dataSendToSlaveStopEval = 1; global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); if(global.I2C_SystemStatus != HAL_OK) { I2C_Error_count(); } global.dataSendToSlaveStopEval = 0; //TODO: REMOVE. // if(global.dataSendToSlavePending) // { // scheduleSpecial_Evaluate_DataSendToSlave(); // } return (HAL_StatusTypeDef)global.I2C_SystemStatus; } HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) return (HAL_StatusTypeDef)global.I2C_SystemStatus; uint8_t localHALstatusReturn = 0xFF; global.dataSendToSlaveStopEval = 1; localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); if(localHALstatusReturn != HAL_OK) { I2C_Error_count(); } global.dataSendToSlaveStopEval = 0; //TODO: REMOVE. // if(global.dataSendToSlavePending) // { // scheduleSpecial_Evaluate_DataSendToSlave(); // } return (HAL_StatusTypeDef)localHALstatusReturn; }