Buoyancy Firmware  1.0
Buoyancy Vehicle Firmware Documentation
PID program file

Contain mission module with related structures and functions. More...

Data Structures

struct  pidData_t
 structure for holding PID related data More...
 
struct  measuredData_t
 structure for holding measured data values More...
 
struct  missiondata_t
 structure for holding mission specific data More...
 
struct  mission_t
 structure for mission related structures and data values More...
 
struct  logFile_t
 structure for log file related data More...
 
struct  missionLog_t
 structure for data values written to log file More...
 
struct  pid_controller
 Structure that holds PID all the PID controller data, multiple instances are posible using different structures for each controller. More...
 

Macros

#define PID_kP   0.04
 Default Kp term. More...
 
#define PID_kI   0.0015
 Default Ki term. More...
 
#define PID_kD   0.05
 Default Kd term. More...
 
#define PID_kI_THRESHOLD   1.0
 Default Ki threshold - When measured depth is further away than threshold Ki term is not part of pid calculation. More...
 
#define PID_LIMIT_MIN   0
 PID min limit = minimum piston position of 0.0 cm - End limit switches will stop motor at 0.0xx cm. More...
 
#define PID_LIMIT_MAX   0.055
 PID max limit = maximum piston position of 5.5 cm - End limit switches will stop motor at 5.1xx cm. More...
 
#define MAX_DEPTH   50.0
 Maximum allowable depth [m]. More...
 
#define MIN_DEPTH   0.0
 minimum allowable depth [m] More...
 
#define PSI_RANGE   100.0
 Differential sensor range: 14,7 psi to 114,7 psi. More...
 
#define MEASURED_OFFSET   0.19
 A measured pressure offset of ~ 0.19 psi. More...
 
#define PSI_WEATHER_OFFSET   0.123
 current offset barometric pressure in PSI. More...
 
#define PSI_1ATM_PRESSURE   14.7
 Standard barometric pressure in PSI. More...
 
#define BAR_RANGE   5.89475728
 Differential sensor range: 1 bar to 6,89475728 bar. More...
 
#define SAADC_MAX   16320.0
 
#define SAADC_MIN   1730
 
#define PSI_TO_MH2O   0.703070
 Convert PSI to Water column (MeterH2O) More...
 
#define PSI_TO_PASCAL   6894.76
 Convert PSI to pascal. More...
 
#define SAADC_VOLTAGE_ERROR   0.022
 Average error from calculated SAADC value to actual SAADC value. More...
 
#define PASCAL_MAX   790828.66
 Maximum pressure value in pascal. More...
 
#define PASCAL_MIN   101352.90
 Minimum pressure value in pascal. More...
 
#define PRESSURE_VOLTAGE_MAX   4.5
 Maximum pressure value in volts. More...
 
#define PRESSURE_VOLTAGE_MIN   0.5
 Minimum pressure value in volts. More...
 
#define PRESSURE_VOLTAGE_RANGE   4.0
 Pressure sensor voltage output range. More...
 
#define MAX_NR_OF_MISSIONS   4
 The maximum number of missions to be configured. More...
 
#define DEFAULT_M1_DEPTH   1.5
 Default mission1 depth in meter. More...
 
#define DEFAULT_M1_TIME   180
 Default mission1 time in seconds. More...
 
#define DEFAULT_M2_DEPTH   1.0
 Default mission2 depth in meter. More...
 
#define DEFAULT_M2_TIME   180
 Default mission1 depth in seconds. More...
 
#define EMA_alpha   0.1
 Exponential Moving Average 'alpha' Coefficient. More...
 
#define TICK_SECOND   32768UL
 Instead of using systick, RTC is use, which run at 32,768 kHz. More...
 

Typedefs

typedef struct pid_controllerpid_t
 

Enumerations

enum  pid_control_directions { E_PID_DIRECT, E_PID_REVERSE }
 Defines if the controler is direct or reverse. More...
 

Functions

void missionInit (void)
 Initialize mission module. More...
 
void prepareMission ()
 
More...
 
void runMission ()
 
More...
 
void CalcPressureAndDepth (void)
 
More...
 
void missionLogInit ()
 
More...
 
void updateMissionLog ()
 
More...
 
pid_t pid_create (pid_t pid, float *in, float *out, float *set, float kp, float ki, float kd)
 Creates a new PID controller. More...
 
bool pid_need_compute (pid_t pid)
 Check if PID loop needs to run. More...
 
void pid_compute (pid_t pid)
 Computes the output of the PID control. More...
 
void pid_tune (pid_t pid, float kp, float ki, float kd)
 Sets new PID tuning parameters. More...
 
void pid_sample (pid_t pid, uint32_t time)
 Sets the pid algorithm period. More...
 
void pid_limits (pid_t pid, float min, float max)
 Sets the limits for the PID controller output. More...
 
void pid_auto (pid_t pid)
 Enables automatic control using PID. More...
 
void pid_manual (pid_t pid)
 Disables automatic process control. More...
 
void pid_direction (pid_t pid, enum pid_control_directions dir)
 Configures the PID controller direction. More...
 

Variables

bool motorStopped
 Flag to signal motor stopped. More...
 
bool bottomLimit
 Flag to signal when bottom limit switch is reached. More...
 
bool SAADCdataReady = false
 Flag to signal SAADC data is ready to be read and calculated, and to run PID. More...
 
bool sampleSensorData = false
 Flag to signal sampling battery, pressure, and TMP117. More...
 
bool missionLogUpdated = false
 Flag to signal mission log is updated. More...
 
bool TMP117dataReady = false
 Flag to signal TMP117 data is ready to be read. More...
 
bool receiveTMP117 = false
 Flag to signal that data is expected to be received from TMP117. More...
 
float EMA_state = 0.0
 global filter output variable More...
 
missionLog_t missionLog
 Create mission log instance. More...
 
struct pid_controller ctrlData
 Create PID controller control data instance. More...
 
pid_t pid
 Create PID controller instnace. More...
 
const float SAADC_pressure_range = SAADC_MAX - SAADC_MIN
 
const float bar_pr_saadcValue = BAR_RANGE/SAADC_pressure_range
 
const float pascal_range = PASCAL_MAX-PASCAL_MIN
 
const float pascal_pr_volts = pascal_range/(PRESSURE_VOLTAGE_MAX-PRESSURE_VOLTAGE_MIN)
 
mission_t mission
 mission structure instance More...
 

Detailed Description

Contain mission module with related structures and functions.

Contain TWI/I^2C module with related structures and functions.

Macro Definition Documentation

◆ BAR_RANGE

#define BAR_RANGE   5.89475728

Differential sensor range: 1 bar to 6,89475728 bar.

◆ DEFAULT_M1_DEPTH

#define DEFAULT_M1_DEPTH   1.5

Default mission1 depth in meter.

◆ DEFAULT_M1_TIME

#define DEFAULT_M1_TIME   180

Default mission1 time in seconds.

◆ DEFAULT_M2_DEPTH

#define DEFAULT_M2_DEPTH   1.0

Default mission2 depth in meter.

◆ DEFAULT_M2_TIME

#define DEFAULT_M2_TIME   180

Default mission1 depth in seconds.

◆ EMA_alpha

#define EMA_alpha   0.1

Exponential Moving Average 'alpha' Coefficient.

◆ MAX_DEPTH

#define MAX_DEPTH   50.0

Maximum allowable depth [m].

◆ MAX_NR_OF_MISSIONS

#define MAX_NR_OF_MISSIONS   4

The maximum number of missions to be configured.

◆ MEASURED_OFFSET

#define MEASURED_OFFSET   0.19

A measured pressure offset of ~ 0.19 psi.

◆ MIN_DEPTH

#define MIN_DEPTH   0.0

minimum allowable depth [m]

◆ PASCAL_MAX

#define PASCAL_MAX   790828.66

Maximum pressure value in pascal.

◆ PASCAL_MIN

#define PASCAL_MIN   101352.90

Minimum pressure value in pascal.

◆ PID_kD

#define PID_kD   0.05

Default Kd term.

◆ PID_kI

#define PID_kI   0.0015

Default Ki term.

◆ PID_kI_THRESHOLD

#define PID_kI_THRESHOLD   1.0

Default Ki threshold - When measured depth is further away than threshold Ki term is not part of pid calculation.

◆ PID_kP

#define PID_kP   0.04

Default Kp term.

◆ PID_LIMIT_MAX

#define PID_LIMIT_MAX   0.055

PID max limit = maximum piston position of 5.5 cm - End limit switches will stop motor at 5.1xx cm.

◆ PID_LIMIT_MIN

#define PID_LIMIT_MIN   0

PID min limit = minimum piston position of 0.0 cm - End limit switches will stop motor at 0.0xx cm.

◆ PRESSURE_VOLTAGE_MAX

#define PRESSURE_VOLTAGE_MAX   4.5

Maximum pressure value in volts.

◆ PRESSURE_VOLTAGE_MIN

#define PRESSURE_VOLTAGE_MIN   0.5

Minimum pressure value in volts.

◆ PRESSURE_VOLTAGE_RANGE

#define PRESSURE_VOLTAGE_RANGE   4.0

Pressure sensor voltage output range.

◆ PSI_1ATM_PRESSURE

#define PSI_1ATM_PRESSURE   14.7

Standard barometric pressure in PSI.

Weight of air pushing on the water

◆ PSI_RANGE

#define PSI_RANGE   100.0

Differential sensor range: 14,7 psi to 114,7 psi.

◆ PSI_TO_MH2O

#define PSI_TO_MH2O   0.703070

Convert PSI to Water column (MeterH2O)

◆ PSI_TO_PASCAL

#define PSI_TO_PASCAL   6894.76

Convert PSI to pascal.

◆ PSI_WEATHER_OFFSET

#define PSI_WEATHER_OFFSET   0.123

current offset barometric pressure in PSI.

Weight of air pushing on the water

◆ SAADC_MAX

#define SAADC_MAX   16320.0

◆ SAADC_MIN

#define SAADC_MIN   1730

◆ SAADC_VOLTAGE_ERROR

#define SAADC_VOLTAGE_ERROR   0.022

Average error from calculated SAADC value to actual SAADC value.

◆ TICK_SECOND

#define TICK_SECOND   32768UL

Instead of using systick, RTC is use, which run at 32,768 kHz.

Typedef Documentation

◆ pid_t

typedef struct pid_controller* pid_t

Enumeration Type Documentation

◆ pid_control_directions

Defines if the controler is direct or reverse.

Enumerator
E_PID_DIRECT 
E_PID_REVERSE 

Function Documentation

◆ CalcPressureAndDepth()

void CalcPressureAndDepth ( void  )

Calculate pressure and current depth based on raw pressure from SAADC.

< Exponential Moving Average (EMA) filtering

◆ missionInit()

void missionInit ( void  )

Initialize mission module.

Set data structures to NULL and populate with default data values. Create pid and configure pid regulator.

◆ missionLogInit()

void missionLogInit ( )

Initialize mission log by setting the structure to zero.

◆ pid_auto()

void pid_auto ( pid_t  pid)

Enables automatic control using PID.

Enables the PID control loop. If manual output adjustment is needed you can disable the PID control loop using pid_manual(). This function enables PID automatic control at program start or after calling pid_manual()

Parameters
pidThe PID controller instance to enable

◆ pid_compute()

void pid_compute ( pid_t  pid)

Computes the output of the PID control.

This function computes the PID output based on the parameters, setpoint and current system input.

Parameters
pidThe PID controller instance which will be used for computation

◆ pid_create()

pid_t pid_create ( pid_t  pid,
float *  in,
float *  out,
float *  set,
float  kp,
float  ki,
float  kd 
)

Creates a new PID controller.

Creates a new pid controller and initializes it�s input, output and internal variables. Also we set the tuning parameters

Parameters
pidA pointer to a pid_controller structure
inPointer to float value for the process input
outPoiter to put the controller output value
setPointer float with the process setpoint value
kpProportional gain
kiIntegral gain
kdDiferential gain
Returns
returns a pid_t controller handle

◆ pid_direction()

void pid_direction ( pid_t  pid,
enum pid_control_directions  dir 
)

Configures the PID controller direction.

Sets the direction of the PID controller. The direction is "DIRECT" when a increase of the output will cause a increase on the measured value and "REVERSE" when a increase on the controller output will cause a decrease on the measured value.

Parameters
pidThe PID controller instance to modify
directionThe new direction of the PID controller

◆ pid_limits()

void pid_limits ( pid_t  pid,
float  min,
float  max 
)

Sets the limits for the PID controller output.

Parameters
pidThe PID controller instance to modify
minThe minimum output value for the PID controller
maxThe maximum output value for the PID controller

◆ pid_manual()

void pid_manual ( pid_t  pid)

Disables automatic process control.

Disables the PID control loop. User can modify the value of the output variable and the controller will not overwrite it.

Parameters
pidThe PID controller instance to disable

◆ pid_need_compute()

bool pid_need_compute ( pid_t  pid)

Check if PID loop needs to run.

Determines if the PID control algorithm should compute a new output value, if this returs true, the user should read process feedback (sensors) and place the reading in the input variable, then call the pid_compute() function.

Returns
return Return true if PID control algorithm is required to run

◆ pid_sample()

void pid_sample ( pid_t  pid,
uint32_t  time 
)

Sets the pid algorithm period.

Changes the between PID control loop computations.

Parameters
pidThe PID controller instance to modify
timeThe time in milliseconds between computations

◆ pid_tune()

void pid_tune ( pid_t  pid,
float  kp,
float  ki,
float  kd 
)

Sets new PID tuning parameters.

Sets the gain for the Proportional (Kp), Integral (Ki) and Derivative (Kd) terms.

Parameters
pidThe PID controller instance to modify
kpProportional gain
kiIntegral gain
kdDerivative gain

◆ prepareMission()

void prepareMission ( void  )

prepare mission related settings.

Called before each new dive to calculate the number of missions based on configuration, clear previously stored data, and start/stop timers

< Enable PID controller.

◆ runMission()

void runMission ( )

run mission - measure data, calculate pid, and set piston position accordingly.

< If x meters away from target, run as PD regulator, otherwise run as PID regulator. This is so that it regulates quicker towards target

<Check for and handle RTC wrap around

< Divide 128 counts pr/ 1 *C

< In case bottom limit switch is already triggered

◆ updateMissionLog()

void updateMissionLog ( void  )

update mission log structure.

In order to get a consisten data set with values represented at the same time instance the mission log is updated with a snapshot of relevant data, then written to SD card.

Note
ICM20948 sensor module is not yet configured and is therfor constant 0
Attention
TMP117 sensor module is read but not yet configured so the sampled value is not reliable

Variable Documentation

◆ bar_pr_saadcValue

const float bar_pr_saadcValue = BAR_RANGE/SAADC_pressure_range

◆ bottomLimit

bool bottomLimit

Flag to signal when bottom limit switch is reached.

◆ ctrlData

struct pid_controller ctrlData

Create PID controller control data instance.

◆ EMA_state

float EMA_state = 0.0

global filter output variable

◆ mission

mission_t mission

mission structure instance

mission structure instance

◆ missionLog

missionLog_t missionLog

Create mission log instance.

◆ missionLogUpdated

bool missionLogUpdated = false

Flag to signal mission log is updated.

◆ motorStopped

bool motorStopped

Flag to signal motor stopped.

Flag to signal motor stopped.

◆ pascal_pr_volts

const float pascal_pr_volts = pascal_range/(PRESSURE_VOLTAGE_MAX-PRESSURE_VOLTAGE_MIN)

◆ pascal_range

const float pascal_range = PASCAL_MAX-PASCAL_MIN

◆ pid

pid_t pid

Create PID controller instnace.

◆ receiveTMP117

bool receiveTMP117 = false

Flag to signal that data is expected to be received from TMP117.

Flag to signal waiting for data from TMP117 temmperature sensor is ready to be read.

◆ SAADC_pressure_range

const float SAADC_pressure_range = SAADC_MAX - SAADC_MIN

◆ SAADCdataReady

bool SAADCdataReady = false

Flag to signal SAADC data is ready to be read and calculated, and to run PID.

Flag to signal SAADC is ready to be read.

◆ sampleSensorData

bool sampleSensorData = false

Flag to signal sampling battery, pressure, and TMP117.

◆ TMP117dataReady

bool TMP117dataReady = false

Flag to signal TMP117 data is ready to be read.

Flag to signal data from TMP117 temmperature sensor is ready to be read.