diff --git a/UAV-ControlSystem/inc/drivers/barometer.h b/UAV-ControlSystem/inc/drivers/barometer.h index 2c47476..41e9172 100644 --- a/UAV-ControlSystem/inc/drivers/barometer.h +++ b/UAV-ControlSystem/inc/drivers/barometer.h @@ -8,18 +8,50 @@ #ifndef 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(); +/*********************************************************************** + * BRIEF: Resets the barometer. + * INFORMATION: Resets the barometer needs to be called after the init + ***********************************************************************/ bool barometer_reset(); +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ void barometer_CaclulateValues(); +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ double barometer_GetCurrentPreassure(); +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ double barometer_GetCurrentTemperature(); -float barometer_GetCurrentAltitudeBasedOnSeaLevel(); +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ +float barometer_GetCurrentAltitude(); diff --git a/UAV-ControlSystem/src/drivers/barometer.c b/UAV-ControlSystem/src/drivers/barometer.c index 9c07bab..d9c7660 100644 --- a/UAV-ControlSystem/src/drivers/barometer.c +++ b/UAV-ControlSystem/src/drivers/barometer.c @@ -13,7 +13,7 @@ #include "drivers/i2c_soft.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_READ 0xEF // Module address read mode @@ -31,38 +31,37 @@ #define CMD_ADC_4096 0x08 // ADC OSR=4096 #define CMD_PROM_RD 0xA0 // Prom read command -#define SEA_PRESS 1013.25 //default sea level pressure level in mb -#define FTMETERS 0.3048 //convert feet to meters +#define SEA_PRESS 1013.25 //default sea level pressure level in mb +#define FTMETERS 0.3048 //convert feet to meters #define CALIBRATION_VAL_AMOUNT 30 -I2C_HandleTypeDef baroI2C_handle; -DMA_HandleTypeDef baroI2C_Rx_DMA_handle; -DMA_HandleTypeDef baroI2C_Tx_DMA_handle; -I2C_SOFT_handle_t baroI2C_soft_handle; +I2C_HandleTypeDef baroI2C_handle; //Handle for the HW i2c (NOT USED ATM) +DMA_HandleTypeDef baroI2C_Rx_DMA_handle; //Dma handle receive (NOT USED ATM) +DMA_HandleTypeDef baroI2C_Tx_DMA_handle; //Dma handle for transmit (NOT USED ATM) +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) -double baro_Temperature; // compensated temperature value (degC) -double baro_Altitude; // altitude (ft) -double baro_S; // sea level barometer (mB) - -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}; +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; //Counter for the amount of calibration samples +int calibrationSamplesIterator = 0; //Iterator for when going through all the samples /* address: 0 = factory data and the setup * address: 1-6 = calibration coefficients * 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() { //fisrt check if the amount of samples is greater than the array @@ -80,6 +79,10 @@ void barometer_addCalibrationSample() calibrationSamplesIterator ++; } +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ bool barometer_Calibrate() { //Check if any calibration values exist @@ -108,6 +111,10 @@ bool barometer_Calibrate() return true; } +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ bool barometer_init() { //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); #endif #ifdef BARO_USE_I2C_HARD - + //Todo: needs implementation #endif return true; } +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ bool barometer_reset() { /* Send a reset command to the baromter @@ -142,6 +153,8 @@ bool barometer_reset() #endif #ifdef BARO_USE_I2C_HARD + //Todo: needs implementation + uint8_t cobuf2[3] = {0}; /* Change to hardware polling mode */ cobuf2[0] = CMD_ADC_CONV + (CMD_ADC_D2 + sampleAmount); @@ -193,21 +206,17 @@ bool barometer_reset() /* Set the inital calibration value */ barometer_Calibrate(); + //force bakc the iscalibrated status to false flags_Clear_ID(systemFlags_barometerIsCalibrated_id); - tempTestCounterStart = clock_get_ms(); return true; } -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: + * INFORMATION: + ***********************************************************************/ void barometer_CalculatePTA(uint32_t D1, uint32_t D2) { /* 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); } +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ void barometer_CaclulateValues() { /*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 * the function is called. The "delay" is handled by the period of * 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*/ static uint8_t currentCalculationState = CALCSTATE_D2_CALCULATION; static uint32_t D1 = 0; @@ -262,7 +276,6 @@ void barometer_CaclulateValues() uint8_t cobuf[3] = {0}; uint32_t startTime; uint32_t endTime; -//balbalblablalbalb //If the machine is armed and not calibrated we perform a calibraton if (!flags_IsSet_ID(systemFlags_barometerIsCalibrated_id)) @@ -273,8 +286,6 @@ void barometer_CaclulateValues() } } - - switch (currentCalculationState) { case CALCSTATE_D2_CALCULATION: @@ -365,17 +376,29 @@ void barometer_CaclulateValues() } +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ double barometer_GetCurrentPreassure() { return baro_Preassure; } +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ double barometer_GetCurrentTemperature() { return baro_Temperature; } -float barometer_GetCurrentAltitudeBasedOnSeaLevel() +/*********************************************************************** + * BRIEF: + * INFORMATION: + ***********************************************************************/ +float barometer_GetCurrentAltitude() { return baro_Altitude; }