view OtherSources/data_exchange_main_mini.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 7801c5d8a562
children
line wrap: on
line source

/**
  ******************************************************************************
  * @file    data_exchange_main.c
  * @author  heinrichs weikamp gmbh
  * @date    13-Oct-2014
  * @version V0.0.2
  * @since   27-May-2015

	* @brief   Communication with the second CPU == RTE system
  *
  @verbatim
  ==============================================================================
                        ##### How to use #####
  ==============================================================================

  ==============================================================================
                        ##### Device Data #####
  ==============================================================================
	
	main CPU always sends the device data info that it has at the moment

		on start it is INT32_MIN, INT32_MAX and 0 
		as initialized  in data_central.c variable declaration
	
	second small CPU gets request to send its device data
		
		on receiption the data is merged with the data in externLogbookFlash,
		stored on the externLogbookFlash and from now on send to small CPU

	@endverbatim
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h> // for memcopy
#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "ostc.h"
#include "data_central.h"
#include "data_exchange_main.h"
#include "base.h"
#include "externLogbookFlash.h"


/* Expoted variables --------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

SDataReceiveFromMaster dataOut;
SDataExchangeSlaveToMaster dataIn;

uint8_t data_old__lost_connection_to_slave_counter_temp = 0;
/* Private types -------------------------------------------------------------*/

uint8_t DataEX_check_header_and_footer_ok(void);
void DataEX_control_connection_while_asking_for_sleep(void);

/* Exported functions --------------------------------------------------------*/

uint8_t DataEX_call(void)
{
	DataEX_control_connection_while_asking_for_sleep();
	
	for(int i=0;i<EXCHANGE_BUFFERSIZE;i++)
		*(uint8_t *)(((uint32_t)&dataOut) + i)  = 0;

	dataOut.mode = MODE_SHUTDOWN;

	dataOut.header.checkCode[0] = 0xBB;
	dataOut.header.checkCode[1] = 0x01;
	dataOut.header.checkCode[2] = 0x01;
	dataOut.header.checkCode[3] = 0xBB;

	dataOut.footer.checkCode[0] = 0xF4;
	dataOut.footer.checkCode[1] = 0xF3;
	dataOut.footer.checkCode[2] = 0xF2;
	dataOut.footer.checkCode[3] = 0xF1;

	HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_SET);
	delayMicros(10);

	if(data_old__lost_connection_to_slave_counter_temp >= 3)
	{
		data_old__lost_connection_to_slave_counter_temp = 0;
	}
	else
	{
		HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
	}

	HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE+1);
	return 1;
}


void DataEX_control_connection_while_asking_for_sleep(void)
{
 	if(!DataEX_check_header_and_footer_ok())
	{
		data_old__lost_connection_to_slave_counter_temp += 1;
	}
}

uint8_t DataEX_check_header_and_footer_ok(void)
{
	if(dataIn.header.checkCode[0] != 0xA1)
		return 0;
	if(dataIn.header.checkCode[1] != 0xA2)
		return 0;
	if(dataIn.header.checkCode[2] != 0xA3)
		return 0;
	if(dataIn.header.checkCode[3] != 0xA4)
		return 0;
	if(dataIn.footer.checkCode[0] != 0xE1)
		return 0;
	if(dataIn.footer.checkCode[1] != 0xE2)
		return 0;
	if(dataIn.footer.checkCode[2] != 0xE3)
		return 0;
	if(dataIn.footer.checkCode[3] != 0xE4)
		return 0;

	return 1;
}