Buoyancy Firmware  1.0
Buoyancy Vehicle Firmware Documentation
PID.h
Go to the documentation of this file.
1 /* Floating point PID control loop for Microcontrollers
2  Copyright (C) 2014 Jesus Ruben Santa Anna Zamudio.
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17  Author website: http://www.geekfactory.mx
18  Author e-mail: ruben at geekfactory dot mx
19  */
20 #ifndef PID_H
21 #define PID_H
22 /*-------------------------------------------------------------*/
23 /* Includes and dependencies */
24 /*-------------------------------------------------------------*/
25 #include "main.h"
26 #include "nrf_systick.h"
27 #include <stdbool.h>
28 #include <stdint.h>
29 
39 /*-------------------------------------------------------------*/
40 /* Macros and definitions */
41 /*-------------------------------------------------------------*/
42 #define TICK_SECOND 32768UL
43 /*-------------------------------------------------------------*/
44 /* Typedefs enums & structs */
45 /*-------------------------------------------------------------*/
46 
53 };
54 
60  // Input, output and setpoint
61  float * input;
62  float * output;
63  float * setpoint;
64  // Tuning parameters
65  float Kp;
66  float Ki;
67  float Kd;
68  // Output minimum and maximum values
69  float omin;
70  float omax;
71  // Variables for PID algorithm
72  float iterm;
73  float lastin;
74  // Time related
77  // Operation mode
80 };
81 
82 typedef struct pid_controller * pid_t;
83 
84 /*-------------------------------------------------------------*/
85 /* Function prototypes */
86 /*-------------------------------------------------------------*/
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
106  pid_t pid_create(pid_t pid, float* in, float* out, float* set, float kp, float ki, float kd);
107 
117  bool pid_need_compute(pid_t pid);
118 
127  void pid_compute(pid_t pid);
128 
140  void pid_tune(pid_t pid, float kp, float ki, float kd);
141 
150  void pid_sample(pid_t pid, uint32_t time);
151 
159  void pid_limits(pid_t pid, float min, float max);
160 
170  void pid_auto(pid_t pid);
171 
180  void pid_manual(pid_t pid);
181 
193  void pid_direction(pid_t pid, enum pid_control_directions dir);
194 
195 #ifdef __cplusplus
196 }
197 #endif
198 
199 #endif
200 // End of Header file
201 
void pid_tune(pid_t pid, float kp, float ki, float kd)
Sets new PID tuning parameters.
Definition: PID.c:97
void pid_limits(pid_t pid, float min, float max)
Sets the limits for the PID controller output.
Definition: PID.c:127
enum pid_control_directions direction
Definition: PID.h:79
unsigned char uint8_t
Definition: nrf_drv_uart_PP.c:22
pid_t pid_create(pid_t pid, float *in, float *out, float *set, float kp, float ki, float kd)
Creates a new PID controller.
Definition: PID.c:32
float omin
Maximum value allowed at the output.
Definition: PID.h:69
float * input
Current Process Value.
Definition: PID.h:61
void pid_manual(pid_t pid)
Disables automatic process control.
Definition: PID.c:160
void pid_compute(pid_t pid)
Computes the output of the PID control.
Definition: PID.c:59
pid_t pid
Create PID controller instnace.
Definition: mission.c:23
float Kd
Stores the gain for the Derivative term.
Definition: PID.h:67
float Kp
Stores the gain for the Proportional term.
Definition: PID.h:65
float omax
Minimum value allowed at the output.
Definition: PID.h:70
void pid_direction(pid_t pid, enum pid_control_directions dir)
Configures the PID controller direction.
Definition: PID.c:165
Definition: PID.h:51
struct pid_controller * pid_t
Definition: PID.h:82
float * setpoint
Controller Setpoint.
Definition: PID.h:63
Structure that holds PID all the PID controller data, multiple instances are posible using different ...
Definition: PID.h:59
float Ki
Stores the gain for the Integral term.
Definition: PID.h:66
void pid_sample(pid_t pid, uint32_t time)
Sets the pid algorithm period.
Definition: PID.c:117
void pid_auto(pid_t pid)
Enables automatic control using PID.
Definition: PID.c:146
uint32_t sampletime
Defines the PID sample time.
Definition: PID.h:76
Definition: PID.h:52
float iterm
Accumulator for integral term.
Definition: PID.h:72
uint32_t lasttime
Stores the time when the control loop ran last time.
Definition: PID.h:75
pid_control_directions
Defines if the controler is direct or reverse.
Definition: PID.h:50
float lastin
Last input value for differential term.
Definition: PID.h:73
bool pid_need_compute(pid_t pid)
Check if PID loop needs to run.
Definition: PID.c:52
unsigned int uint32_t
Definition: nrf_drv_uart_PP.c:30
uint8_t automode
Defines if the PID controller is enabled or disabled.
Definition: PID.h:78
float * output
Corrective Output from PID Controller.
Definition: PID.h:62