Added some comments.
Added some comments to the barometer code. More will be added.
This commit is contained in:
parent
195b311b88
commit
fcc0bd41ae
@ -8,18 +8,50 @@
|
|||||||
#ifndef DRIVERS_BAROMETER_H_
|
#ifndef DRIVERS_BAROMETER_H_
|
||||||
#define DRIVERS_BAROMETER_H_
|
#define DRIVERS_BAROMETER_H_
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
CALCSTATE_D2_CALCULATION = 0, //Tell the sensor that we want to read D2
|
||||||
|
CALCSTATE_D2_READ, //Read D2 from the sensor
|
||||||
|
CALCSTATE_D1_CALCULATION, //Tell the sensor that we want to read D1
|
||||||
|
CALCSTATE_D1_READ, //Read D1 from the sensor
|
||||||
|
CALCSTATE_CALCULATE_PTA //preassure, temp, altidute calc
|
||||||
|
}calculationState;
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF: Initializes the barometer.
|
||||||
|
* INFORMATION: Initializes the barometer and it needs to be called
|
||||||
|
* before anything else when using the barometer.
|
||||||
|
***********************************************************************/
|
||||||
bool barometer_init();
|
bool barometer_init();
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF: Resets the barometer.
|
||||||
|
* INFORMATION: Resets the barometer needs to be called after the init
|
||||||
|
***********************************************************************/
|
||||||
bool barometer_reset();
|
bool barometer_reset();
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
void barometer_CaclulateValues();
|
void barometer_CaclulateValues();
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
double barometer_GetCurrentPreassure();
|
double barometer_GetCurrentPreassure();
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
double barometer_GetCurrentTemperature();
|
double barometer_GetCurrentTemperature();
|
||||||
|
|
||||||
float barometer_GetCurrentAltitudeBasedOnSeaLevel();
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
|
float barometer_GetCurrentAltitude();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#include "drivers/i2c_soft.h"
|
#include "drivers/i2c_soft.h"
|
||||||
#include "drivers/failsafe_toggles.h"
|
#include "drivers/failsafe_toggles.h"
|
||||||
|
|
||||||
#define Device_address_1 0x56
|
#define Device_address_1 0x56 //Address of our device, not really important in this case
|
||||||
|
|
||||||
#define ADDR_WRITE 0xEE // Module address write mode
|
#define ADDR_WRITE 0xEE // Module address write mode
|
||||||
#define ADDR_READ 0xEF // Module address read mode
|
#define ADDR_READ 0xEF // Module address read mode
|
||||||
@ -31,38 +31,37 @@
|
|||||||
#define CMD_ADC_4096 0x08 // ADC OSR=4096
|
#define CMD_ADC_4096 0x08 // ADC OSR=4096
|
||||||
#define CMD_PROM_RD 0xA0 // Prom read command
|
#define CMD_PROM_RD 0xA0 // Prom read command
|
||||||
|
|
||||||
#define SEA_PRESS 1013.25 //default sea level pressure level in mb
|
#define SEA_PRESS 1013.25 //default sea level pressure level in mb
|
||||||
#define FTMETERS 0.3048 //convert feet to meters
|
#define FTMETERS 0.3048 //convert feet to meters
|
||||||
|
|
||||||
#define CALIBRATION_VAL_AMOUNT 30
|
#define CALIBRATION_VAL_AMOUNT 30
|
||||||
|
|
||||||
I2C_HandleTypeDef baroI2C_handle;
|
I2C_HandleTypeDef baroI2C_handle; //Handle for the HW i2c (NOT USED ATM)
|
||||||
DMA_HandleTypeDef baroI2C_Rx_DMA_handle;
|
DMA_HandleTypeDef baroI2C_Rx_DMA_handle; //Dma handle receive (NOT USED ATM)
|
||||||
DMA_HandleTypeDef baroI2C_Tx_DMA_handle;
|
DMA_HandleTypeDef baroI2C_Tx_DMA_handle; //Dma handle for transmit (NOT USED ATM)
|
||||||
I2C_SOFT_handle_t baroI2C_soft_handle;
|
I2C_SOFT_handle_t baroI2C_soft_handle; //Handle for the SW i2c
|
||||||
|
|
||||||
uint8_t sampleAmount;
|
uint8_t sampleAmount; //The amount of samples to be used when by the barometer to calculate the needed variables
|
||||||
|
double baro_Preassure; // compensated pressure value (mB)
|
||||||
|
double baro_Temperature; // compensated temperature value (degC)
|
||||||
|
double baro_Altitude; // altitude
|
||||||
|
double baro_S; // sea level barometer (mB)
|
||||||
|
|
||||||
double baro_Preassure; // compensated pressure value (mB)
|
float altitudeCalibrationValue = 0; //Value used as calibration value
|
||||||
double baro_Temperature; // compensated temperature value (degC)
|
float calibrationSamples[CALIBRATION_VAL_AMOUNT];//array of stored values to be used for calibration, only samples calibration values when machine is not armed
|
||||||
double baro_Altitude; // altitude (ft)
|
int calibrationSamplesCount = 0; //Counter for the amount of calibration samples
|
||||||
double baro_S; // sea level barometer (mB)
|
int calibrationSamplesIterator = 0; //Iterator for when going through all the samples
|
||||||
|
|
||||||
float altitudeCalibrationValue = 0; //Value used as calibration value
|
|
||||||
float calibrationSamples[CALIBRATION_VAL_AMOUNT]; //array of stored values to be used for calibration, only samples calibration values when machine is not armed
|
|
||||||
int calibrationSamplesCount = 0;
|
|
||||||
int calibrationSamplesIterator = 0;
|
|
||||||
|
|
||||||
//TODO: remove when not used for testing any more
|
|
||||||
uint32_t tempTestCounterStart = 0;
|
|
||||||
|
|
||||||
uint8_t cobuf[3] = {0};
|
|
||||||
|
|
||||||
/* address: 0 = factory data and the setup
|
/* address: 0 = factory data and the setup
|
||||||
* address: 1-6 = calibration coefficients
|
* address: 1-6 = calibration coefficients
|
||||||
* address: 7 = serial code and CRC */
|
* address: 7 = serial code and CRC */
|
||||||
uint32_t coefficients_arr[8]; //coefficient storage
|
uint32_t coefficients_arr[8]; //coefficient storage
|
||||||
|
uint8_t cobuf[3] = {0}; //Array used when writing and reading data over the I2C
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
void barometer_addCalibrationSample()
|
void barometer_addCalibrationSample()
|
||||||
{
|
{
|
||||||
//fisrt check if the amount of samples is greater than the array
|
//fisrt check if the amount of samples is greater than the array
|
||||||
@ -80,6 +79,10 @@ void barometer_addCalibrationSample()
|
|||||||
calibrationSamplesIterator ++;
|
calibrationSamplesIterator ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
bool barometer_Calibrate()
|
bool barometer_Calibrate()
|
||||||
{
|
{
|
||||||
//Check if any calibration values exist
|
//Check if any calibration values exist
|
||||||
@ -108,6 +111,10 @@ bool barometer_Calibrate()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
bool barometer_init()
|
bool barometer_init()
|
||||||
{
|
{
|
||||||
//Set the sample rate of the data that will be calculated on the barometer peripheral
|
//Set the sample rate of the data that will be calculated on the barometer peripheral
|
||||||
@ -118,12 +125,16 @@ bool barometer_init()
|
|||||||
i2c_soft_Init(I2C1, &baroI2C_soft_handle);
|
i2c_soft_Init(I2C1, &baroI2C_soft_handle);
|
||||||
#endif
|
#endif
|
||||||
#ifdef BARO_USE_I2C_HARD
|
#ifdef BARO_USE_I2C_HARD
|
||||||
|
//Todo: needs implementation
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
bool barometer_reset()
|
bool barometer_reset()
|
||||||
{
|
{
|
||||||
/* Send a reset command to the baromter
|
/* Send a reset command to the baromter
|
||||||
@ -142,6 +153,8 @@ bool barometer_reset()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef BARO_USE_I2C_HARD
|
#ifdef BARO_USE_I2C_HARD
|
||||||
|
//Todo: needs implementation
|
||||||
|
|
||||||
uint8_t cobuf2[3] = {0};
|
uint8_t cobuf2[3] = {0};
|
||||||
/* Change to hardware polling mode */
|
/* Change to hardware polling mode */
|
||||||
cobuf2[0] = CMD_ADC_CONV + (CMD_ADC_D2 + sampleAmount);
|
cobuf2[0] = CMD_ADC_CONV + (CMD_ADC_D2 + sampleAmount);
|
||||||
@ -193,21 +206,17 @@ bool barometer_reset()
|
|||||||
|
|
||||||
/* Set the inital calibration value */
|
/* Set the inital calibration value */
|
||||||
barometer_Calibrate();
|
barometer_Calibrate();
|
||||||
|
|
||||||
//force bakc the iscalibrated status to false
|
//force bakc the iscalibrated status to false
|
||||||
flags_Clear_ID(systemFlags_barometerIsCalibrated_id);
|
flags_Clear_ID(systemFlags_barometerIsCalibrated_id);
|
||||||
|
|
||||||
tempTestCounterStart = clock_get_ms();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef enum {
|
/***********************************************************************
|
||||||
CALCSTATE_D2_CALCULATION = 0, //Tell the sensor that we want to read D2
|
* BRIEF:
|
||||||
CALCSTATE_D2_READ, //Read D2 from the sensor
|
* INFORMATION:
|
||||||
CALCSTATE_D1_CALCULATION, //Tell the sensor that we want to read D1
|
***********************************************************************/
|
||||||
CALCSTATE_D1_READ, //Read D1 from the sensor
|
|
||||||
CALCSTATE_CALCULATE_PTA //preassure, temp, altidute calc
|
|
||||||
}calculationState;
|
|
||||||
|
|
||||||
void barometer_CalculatePTA(uint32_t D1, uint32_t D2)
|
void barometer_CalculatePTA(uint32_t D1, uint32_t D2)
|
||||||
{
|
{
|
||||||
/* calculate dT, difference between actual and reference temp: (D2 - C5 * 2^8) */
|
/* calculate dT, difference between actual and reference temp: (D2 - C5 * 2^8) */
|
||||||
@ -247,6 +256,10 @@ void barometer_CalculatePTA(uint32_t D1, uint32_t D2)
|
|||||||
baro_Altitude = (flags_IsSet_ID(systemFlags_barometerIsCalibrated_id)) ? (feet * FTMETERS) - altitudeCalibrationValue : (feet * FTMETERS);
|
baro_Altitude = (flags_IsSet_ID(systemFlags_barometerIsCalibrated_id)) ? (feet * FTMETERS) - altitudeCalibrationValue : (feet * FTMETERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
void barometer_CaclulateValues()
|
void barometer_CaclulateValues()
|
||||||
{
|
{
|
||||||
/*the calculation is in need of different states. This is because the
|
/*the calculation is in need of different states. This is because the
|
||||||
@ -254,7 +267,8 @@ void barometer_CaclulateValues()
|
|||||||
* use a delay wait we need to do parts of the calculation every time
|
* use a delay wait we need to do parts of the calculation every time
|
||||||
* the function is called. The "delay" is handled by the period of
|
* the function is called. The "delay" is handled by the period of
|
||||||
* the task that handles the calculation. It cant have a period faster
|
* the task that handles the calculation. It cant have a period faster
|
||||||
* that 10 ms, or the wait will not be enough in some cases according
|
* that 10 ms or 5ms or 2.5 ms and so on, depending on the CMD_ADC_ assigned
|
||||||
|
* to the variable "sampleAmount" The wait will not be enough in some cases according
|
||||||
* to the datasheet of the sensor http://www.amsys.info/sheets/amsys.en.ms5611_01ba03.pdf*/
|
* to the datasheet of the sensor http://www.amsys.info/sheets/amsys.en.ms5611_01ba03.pdf*/
|
||||||
static uint8_t currentCalculationState = CALCSTATE_D2_CALCULATION;
|
static uint8_t currentCalculationState = CALCSTATE_D2_CALCULATION;
|
||||||
static uint32_t D1 = 0;
|
static uint32_t D1 = 0;
|
||||||
@ -262,7 +276,6 @@ void barometer_CaclulateValues()
|
|||||||
uint8_t cobuf[3] = {0};
|
uint8_t cobuf[3] = {0};
|
||||||
uint32_t startTime;
|
uint32_t startTime;
|
||||||
uint32_t endTime;
|
uint32_t endTime;
|
||||||
//balbalblablalbalb
|
|
||||||
|
|
||||||
//If the machine is armed and not calibrated we perform a calibraton
|
//If the machine is armed and not calibrated we perform a calibraton
|
||||||
if (!flags_IsSet_ID(systemFlags_barometerIsCalibrated_id))
|
if (!flags_IsSet_ID(systemFlags_barometerIsCalibrated_id))
|
||||||
@ -273,8 +286,6 @@ void barometer_CaclulateValues()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
switch (currentCalculationState)
|
switch (currentCalculationState)
|
||||||
{
|
{
|
||||||
case CALCSTATE_D2_CALCULATION:
|
case CALCSTATE_D2_CALCULATION:
|
||||||
@ -365,17 +376,29 @@ void barometer_CaclulateValues()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
double barometer_GetCurrentPreassure()
|
double barometer_GetCurrentPreassure()
|
||||||
{
|
{
|
||||||
return baro_Preassure;
|
return baro_Preassure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
double barometer_GetCurrentTemperature()
|
double barometer_GetCurrentTemperature()
|
||||||
{
|
{
|
||||||
return baro_Temperature;
|
return baro_Temperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
float barometer_GetCurrentAltitudeBasedOnSeaLevel()
|
/***********************************************************************
|
||||||
|
* BRIEF:
|
||||||
|
* INFORMATION:
|
||||||
|
***********************************************************************/
|
||||||
|
float barometer_GetCurrentAltitude()
|
||||||
{
|
{
|
||||||
return baro_Altitude;
|
return baro_Altitude;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user