Added a full set of comments to the barometer code.
Fully commented the code for the barometer.
This commit is contained in:
parent
fcc0bd41ae
commit
9024a8d425
@ -1,9 +1,24 @@
|
||||
/*
|
||||
* barometer.h
|
||||
*
|
||||
* Created on: 18 okt. 2016
|
||||
* Author: holmis
|
||||
*/
|
||||
/**************************************************************************
|
||||
* NAME: barometer.h *
|
||||
* *
|
||||
* AUTHOR: Jonas Holmberg *
|
||||
* *
|
||||
* PURPOSE: Used to provide an estimated altitude, in regards to the *
|
||||
* lift of height that would represent zero meters in height. *
|
||||
* *
|
||||
* INFORMATION: Using I2C to communicate with the barometer a pressure and *
|
||||
* temperature value can be obtained. These values can then be*
|
||||
* used to estimate an altitude. Note that this is not an *
|
||||
* altitude value relative to the ground underneath it. It is *
|
||||
* relative to the position where the system was started from.*
|
||||
* The start position of the system will indicate the zero *
|
||||
* height. It is that position and only that one which will *
|
||||
* be the compared height. *
|
||||
* *
|
||||
* GLOBAL VARIABLES: *
|
||||
* Variable Type Description *
|
||||
* -------- ---- ----------- *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef DRIVERS_BAROMETER_H_
|
||||
#define DRIVERS_BAROMETER_H_
|
||||
@ -17,42 +32,75 @@ typedef enum {
|
||||
}calculationState;
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Initializes the barometer.
|
||||
* INFORMATION: Initializes the barometer and it needs to be called
|
||||
* before anything else when using the barometer.
|
||||
* 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
|
||||
* BRIEF: Resets the barometer. *
|
||||
* INFORMATION: Resets the barometer needs to be called after the init.*
|
||||
* It will send a reset message over the I2C to the *
|
||||
* barometer telling it that is should perform a reset. *
|
||||
* This needs to be done or it wont be possible to read *
|
||||
* data from the barometer. *
|
||||
***********************************************************************/
|
||||
bool barometer_reset();
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Calculates the values of the preassure, temperature and*
|
||||
* altitude. *
|
||||
* INFORMATION: This function needs to be called five times for the *
|
||||
* data to be updated. This is because of some limitations*
|
||||
* and to ensure the schedulability of the system it needs*
|
||||
* to be divided. Firstly there is an inherit delay inside*
|
||||
* the barometer sensor. To get data from the barometer a *
|
||||
* message needs to be sent that tells the barometer to *
|
||||
* prepare the data. This takes, depending on the amount *
|
||||
* of sampling that is done up to 10 ms for the highest *
|
||||
* amount of sampling. This also needs to be done two *
|
||||
* times before that data can be calculated. Also since *
|
||||
* the implementation uses a software I2C at the moment *
|
||||
* because of some problems with the DMA implementation *
|
||||
* the speed is not very high. Therefore sending several *
|
||||
* messages and reading at the same time may take to long *
|
||||
* time and could cause the system to be unschedulable. *
|
||||
* Because of this the function is divided into different *
|
||||
* cases: *
|
||||
* 1: Prepare data. *
|
||||
* 2: Read data. *
|
||||
* 3: Prepare data. *
|
||||
* 4: Read data. *
|
||||
* 5: Calculate temperature, pressure and altitude. *
|
||||
***********************************************************************/
|
||||
void barometer_CaclulateValues();
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Retrieves the previously calculated pressure. *
|
||||
* INFORMATION: Returns the last calculated pressure value. No *
|
||||
* calculation is performed here so calling this will give*
|
||||
* the same value until a new calculation has been *
|
||||
* performed. *
|
||||
***********************************************************************/
|
||||
double barometer_GetCurrentPreassure();
|
||||
double barometer_GetCurrentPressure();
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Retrieves the previously calculated temperature. *
|
||||
* INFORMATION: Returns the last calculated temperature value. No *
|
||||
* calculation is performed here so calling this will give*
|
||||
* the same value until a new calculation has been *
|
||||
* performed. *
|
||||
***********************************************************************/
|
||||
double barometer_GetCurrentTemperature();
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Retrieves the previously calculated altitude. *
|
||||
* INFORMATION: Returns the last calculated altitude value. No *
|
||||
* calculation is performed here so calling this will give*
|
||||
* the same value until a new calculation has been *
|
||||
* performed. *
|
||||
***********************************************************************/
|
||||
float barometer_GetCurrentAltitude();
|
||||
|
||||
|
||||
|
||||
#endif /* DRIVERS_BAROMETER_H_ */
|
||||
|
@ -1,15 +1,24 @@
|
||||
/*
|
||||
* i2c_soft.h
|
||||
*
|
||||
* Created on: 27 okt. 2016
|
||||
* Author: holmis
|
||||
*/
|
||||
/**************************************************************************
|
||||
* NAME: i2c_soft.h *
|
||||
* *
|
||||
* AUTHOR: Jonas Holmberg *
|
||||
* *
|
||||
* PURPOSE: Used to communicate via I2C in a SW simulated manner. *
|
||||
* *
|
||||
* INFORMATION: A software implementation of the I2C. It toggles the pins *
|
||||
* that are used on and of to generate I2C messages. *
|
||||
* *
|
||||
* GLOBAL VARIABLES: *
|
||||
* Variable Type Description *
|
||||
* -------- ---- ----------- *
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef DRIVERS_I2C_SOFT_H_
|
||||
#define DRIVERS_I2C_SOFT_H_
|
||||
|
||||
#include "stm32f4xx.h"
|
||||
|
||||
/* Struct used to create a soft i2c handler */
|
||||
typedef struct
|
||||
{
|
||||
GPIO_TypeDef * i2c_Port;
|
||||
@ -17,12 +26,28 @@ typedef struct
|
||||
uint16_t i2c_sda_pin;
|
||||
}I2C_SOFT_handle_t;
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Initializes the SW I2C.
|
||||
* INFORMATION: Initializes the SW I2C, needs to be done before any
|
||||
* thing else.
|
||||
***********************************************************************/
|
||||
void i2c_soft_Init(I2C_TypeDef *i2c, I2C_SOFT_handle_t *out_profile);
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Writes a message.
|
||||
* INFORMATION: Tries to write to an address. reg is the message that is
|
||||
* written to the addr. data is the size of the data that
|
||||
* is written.
|
||||
***********************************************************************/
|
||||
bool i2c_soft_Write(I2C_SOFT_handle_t *handle, uint8_t addr, uint8_t reg, uint8_t data);
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Reads a message.
|
||||
* INFORMATION: Tries to read a message from addr. reg is the message
|
||||
* that says a read is desired. len is the length of the
|
||||
* message that should be read and buf is the buffer that
|
||||
* will store the read data.
|
||||
***********************************************************************/
|
||||
bool i2c_soft_Read(I2C_SOFT_handle_t *handle, uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf);
|
||||
|
||||
|
||||
#endif /* DRIVERS_I2C_SOFT_H_ */
|
||||
|
@ -1,9 +1,23 @@
|
||||
/*
|
||||
* barometer.c
|
||||
*
|
||||
* Created on: 18 okt. 2016
|
||||
* Author: holmis
|
||||
*/
|
||||
/**************************************************************************
|
||||
* *
|
||||
* AUTHOR: Jonas Holmberg *
|
||||
* *
|
||||
* PURPOSE: Used to provide an estimated altitude, in regards to the *
|
||||
* lift of height that would represent zero meters in height. *
|
||||
* *
|
||||
* INFORMATION: Using I2C to communicate with the barometer a pressure and *
|
||||
* temperature value can be obtained. These values can then be*
|
||||
* used to estimate an altitude. Note that this is not an *
|
||||
* altitude value relative to the ground underneath it. It is *
|
||||
* relative to the position where the system was started from.*
|
||||
* The start position of the system will indicate the zero *
|
||||
* height. It is that position and only that one which will *
|
||||
* be the compared height. *
|
||||
* *
|
||||
* GLOBAL VARIABLES: *
|
||||
* Variable Type Description *
|
||||
* -------- ---- ----------- *
|
||||
***************************************************************************/
|
||||
|
||||
#include "drivers/barometer.h"
|
||||
#include "drivers/I2C.h"
|
||||
@ -59,8 +73,9 @@ uint32_t coefficients_arr[8]; //coefficient storage
|
||||
uint8_t cobuf[3] = {0}; //Array used when writing and reading data over the I2C
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Adds a new altitude value to the calibration samples. *
|
||||
* INFORMATION: Will add the last calculated altitude value to the *
|
||||
* buffer used to provide a calibration value. *
|
||||
***********************************************************************/
|
||||
void barometer_addCalibrationSample()
|
||||
{
|
||||
@ -80,8 +95,14 @@ void barometer_addCalibrationSample()
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Calibrates the barometers start position. *
|
||||
* INFORMATION: An array of values are sampled as long as the system *
|
||||
* is not armed. Upon arming the system the values in *
|
||||
* the buffer will be averaged and this will give the *
|
||||
* calibration value. In other words it will give the *
|
||||
* height that represents zero. This value will be *
|
||||
* subtracted from every altitude calculation that is *
|
||||
* performed. *
|
||||
***********************************************************************/
|
||||
bool barometer_Calibrate()
|
||||
{
|
||||
@ -112,8 +133,9 @@ bool barometer_Calibrate()
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Initializes the barometer. *
|
||||
* INFORMATION: Initializes the barometer and it needs to be called *
|
||||
* before anything else when using the barometer. *
|
||||
***********************************************************************/
|
||||
bool barometer_init()
|
||||
{
|
||||
@ -132,8 +154,12 @@ bool barometer_init()
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Resets the barometer. *
|
||||
* INFORMATION: Resets the barometer needs to be called after the init.*
|
||||
* It will send a reset message over the I2C to the *
|
||||
* barometer telling it that is should perform a reset. *
|
||||
* This needs to be done or it wont be possible to read *
|
||||
* data from the barometer. *
|
||||
***********************************************************************/
|
||||
bool barometer_reset()
|
||||
{
|
||||
@ -214,8 +240,12 @@ bool barometer_reset()
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Calculates the values of temp, pres, and altitude. *
|
||||
* INFORMATION: It takes in two values D1 and D2 which are the values *
|
||||
* that have been read from the barometer. This values are*
|
||||
* then used to perform the calculations together with *
|
||||
* the coefficients that have been read in the reset *
|
||||
* function. *
|
||||
***********************************************************************/
|
||||
void barometer_CalculatePTA(uint32_t D1, uint32_t D2)
|
||||
{
|
||||
@ -257,8 +287,30 @@ void barometer_CalculatePTA(uint32_t D1, uint32_t D2)
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Calculates the values of the preassure, temperature and*
|
||||
* altitude. *
|
||||
* INFORMATION: This function needs to be called five times for the *
|
||||
* data to be updated. This is because of some limitations*
|
||||
* and to ensure the schedulability of the system it needs*
|
||||
* to be divided. Firstly there is an inherit delay inside*
|
||||
* the barometer sensor. To get data from the barometer a *
|
||||
* message needs to be sent that tells the barometer to *
|
||||
* prepare the data. This takes, depending on the amount *
|
||||
* of sampling that is done up to 10 ms for the highest *
|
||||
* amount of sampling. This also needs to be done two *
|
||||
* times before that data can be calculated. Also since *
|
||||
* the implementation uses a software I2C at the moment *
|
||||
* because of some problems with the DMA implementation *
|
||||
* the speed is not very high. Therefore sending several *
|
||||
* messages and reading at the same time may take to long *
|
||||
* time and could cause the system to be unschedulable. *
|
||||
* Because of this the function is divided into different *
|
||||
* cases: *
|
||||
* 1: Prepare data. *
|
||||
* 2: Read data. *
|
||||
* 3: Prepare data. *
|
||||
* 4: Read data. *
|
||||
* 5: Calculate temperature, pressure and altitude. *
|
||||
***********************************************************************/
|
||||
void barometer_CaclulateValues()
|
||||
{
|
||||
@ -377,17 +429,23 @@ void barometer_CaclulateValues()
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Retrieves the previously calculated pressure. *
|
||||
* INFORMATION: Returns the last calculated pressure value. No *
|
||||
* calculation is performed here so calling this will give*
|
||||
* the same value until a new calculation has been *
|
||||
* performed. *
|
||||
***********************************************************************/
|
||||
double barometer_GetCurrentPreassure()
|
||||
double barometer_GetCurrentPressure()
|
||||
{
|
||||
return baro_Preassure;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Retrieves the previously calculated temperature. *
|
||||
* INFORMATION: Returns the last calculated temperature value. No *
|
||||
* calculation is performed here so calling this will give*
|
||||
* the same value until a new calculation has been *
|
||||
* performed. *
|
||||
***********************************************************************/
|
||||
double barometer_GetCurrentTemperature()
|
||||
{
|
||||
@ -395,8 +453,11 @@ double barometer_GetCurrentTemperature()
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF:
|
||||
* INFORMATION:
|
||||
* BRIEF: Retrieves the previously calculated altitude. *
|
||||
* INFORMATION: Returns the last calculated altitude value. No *
|
||||
* calculation is performed here so calling this will give*
|
||||
* the same value until a new calculation has been *
|
||||
* performed. *
|
||||
***********************************************************************/
|
||||
float barometer_GetCurrentAltitude()
|
||||
{
|
||||
|
@ -11,21 +11,37 @@
|
||||
#define WRITE_INDICATOR 0
|
||||
#define READ_INDICATOR 1
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: set given pin to high
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static void IOHi(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Set given pin to low
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static void IOLo(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Read given ii pin
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static bool IORead(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
|
||||
{
|
||||
return !! (GPIOx->IDR & GPIO_Pin);
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Delay for a few micros
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static void i2c_soft_delay(void)
|
||||
{
|
||||
volatile int i = 1;
|
||||
@ -34,6 +50,11 @@ static void i2c_soft_delay(void)
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Initializes the SW I2C.
|
||||
* INFORMATION: Initializes the SW I2C, needs to be done before any
|
||||
* thing else.
|
||||
***********************************************************************/
|
||||
void i2c_soft_Init(I2C_TypeDef *i2c, I2C_SOFT_handle_t *out_profile)
|
||||
{
|
||||
uint16_t sda_pin, scl_pin;
|
||||
@ -67,6 +88,10 @@ void i2c_soft_Init(I2C_TypeDef *i2c, I2C_SOFT_handle_t *out_profile)
|
||||
out_profile->i2c_sda_pin = sda_pin;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Starts the i2c
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static bool i2c_soft_Start(I2C_SOFT_handle_t *handle)
|
||||
{
|
||||
IOHi(handle->i2c_Port, handle->i2c_sda_pin);
|
||||
@ -85,6 +110,10 @@ static bool i2c_soft_Start(I2C_SOFT_handle_t *handle)
|
||||
return true;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Stops the i2c
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static void i2c_soft_Stop(I2C_SOFT_handle_t *handle)
|
||||
{
|
||||
IOLo(handle->i2c_Port, handle->i2c_scl_pin);
|
||||
@ -97,6 +126,10 @@ static void i2c_soft_Stop(I2C_SOFT_handle_t *handle)
|
||||
asm ("nop"); // i2c_soft_delay();
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Sends ack
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static void i2c_soft_Ack(I2C_SOFT_handle_t *handle)
|
||||
{
|
||||
IOLo(handle->i2c_Port, handle->i2c_scl_pin);
|
||||
@ -109,6 +142,10 @@ static void i2c_soft_Ack(I2C_SOFT_handle_t *handle)
|
||||
asm ("nop"); // i2c_soft_delay();
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Sends no ack
|
||||
* INFORMATION:
|
||||
***********************************************************************/
|
||||
static void i2c_soft_NoAck(I2C_SOFT_handle_t *handle)
|
||||
{
|
||||
IOLo(handle->i2c_Port, handle->i2c_scl_pin);
|
||||
@ -121,6 +158,10 @@ static void i2c_soft_NoAck(I2C_SOFT_handle_t *handle)
|
||||
asm ("nop"); // i2c_soft_delay();
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Wait for an acknowledge.
|
||||
* INFORMATION: Waits for an acknowledge when a message has been sent.
|
||||
***********************************************************************/
|
||||
static bool i2c_soft_WaitAck(I2C_SOFT_handle_t *handle)
|
||||
{
|
||||
IOLo(handle->i2c_Port, handle->i2c_scl_pin);
|
||||
@ -137,6 +178,10 @@ static bool i2c_soft_WaitAck(I2C_SOFT_handle_t *handle)
|
||||
return true;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Sends a byte.
|
||||
* INFORMATION: Sends the value byte over the i2c.
|
||||
***********************************************************************/
|
||||
static void i2c_soft_SendByte(I2C_SOFT_handle_t *handle, uint8_t byte)
|
||||
{
|
||||
uint8_t i = 8;
|
||||
@ -157,6 +202,10 @@ static void i2c_soft_SendByte(I2C_SOFT_handle_t *handle, uint8_t byte)
|
||||
IOLo(handle->i2c_Port, handle->i2c_scl_pin);
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Receives a byte.
|
||||
* INFORMATION: Receives a byte and stores is in the byte value.
|
||||
***********************************************************************/
|
||||
static uint8_t i2c_soft_ReceiveByte(I2C_SOFT_handle_t *handle)
|
||||
{
|
||||
uint8_t i = 8;
|
||||
@ -177,6 +226,13 @@ static uint8_t i2c_soft_ReceiveByte(I2C_SOFT_handle_t *handle)
|
||||
return byte;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Reads a message.
|
||||
* INFORMATION: Tries to read a message from addr. reg is the message
|
||||
* that says a read is desired. len is the length of the
|
||||
* message that should be read and buf is the buffer that
|
||||
* will store the read data.
|
||||
***********************************************************************/
|
||||
bool i2c_soft_Read(I2C_SOFT_handle_t *handle, uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf)
|
||||
{
|
||||
//just send the addres 0x77
|
||||
@ -212,6 +268,12 @@ bool i2c_soft_Read(I2C_SOFT_handle_t *handle, uint8_t addr, uint8_t reg, uint8_t
|
||||
return true;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Writes a message.
|
||||
* INFORMATION: Tries to write to an address. reg is the message that is
|
||||
* written to the addr. data is the size of the data that
|
||||
* is written.
|
||||
***********************************************************************/
|
||||
bool i2c_soft_Write(I2C_SOFT_handle_t *handle, uint8_t addr, uint8_t reg, uint8_t data)
|
||||
{
|
||||
//just send the addres 0x77
|
||||
|
Reference in New Issue
Block a user