miniFOC 1.0.3
This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.
pid.c
Go to the documentation of this file.
1/**************************************************************************/
9#include "pid.h"
10#include "fast_math.h"
11#include "system.h"
12
20volatile unsigned char pid_control_mode_flag = 0;
29
33void pid_config(unsigned char mode) {
34 /* clear the value of the PID handler */
35 user_memset((void *) &speed_pid_handler, 0x00, sizeof(PID_Structure_t));
36 user_memset((void *) &angle_pid_handler, 0x00, sizeof(PID_Structure_t));
37
38 /* update the PID closed loop flag byte */
40
41 /* set maximum and minimum output torque */
44
45 speed_pid_handler.kp = 0.06f;
46 speed_pid_handler.ki = 0.08f;
47 speed_pid_handler.kd = 0.01f;
49
50 /* set maximum and minimum output speed */
52 angle_pid_handler.minimum = -100.0f;
53}
54
61float pid_calculate_result(PID_Structure_t *pid_handler, float collect) {
62 /* calculate PID error value */
63 float current_result, error = pid_handler->expect - collect;
64
65 /* calculate the integral and realize anti integral saturation */
66 pid_handler->summary = pid_handler->summary + error;
67 pid_handler->summary = fast_constrain(pid_handler->summary, pid_handler->sum_maximum, -pid_handler->sum_maximum);
68
69 /* calculate PID output value */
70 current_result = pid_handler->kp * error + pid_handler->ki * pid_handler->summary
71 + pid_handler->kd * (error - pid_handler->last_error);
72
73 /* Update last time error of PID to calculate the differential term */
74 pid_handler->last_error = error;
75
76 /* Implementation of output limiting algorithm */
77 current_result = fast_constrain(current_result, pid_handler->minimum, pid_handler->maximum);
78 return current_result;
79}
this is the header file of fast_math.c, the macros of fast amplitude limiting algorithm and cosine fu...
#define fast_constrain(x, low, high)
fast clipping algorithm
Definition: fast_math.h:20
volatile unsigned char pid_control_mode_flag
flag variable of PID closed loop mode
Definition: pid.c:20
unsigned char pid_parameter_available_flag
flag variable for PID parameter availability
Definition: pid.c:16
void pid_config(unsigned char mode)
configure pid loop parameters
Definition: pid.c:33
volatile PID_Structure_t angle_pid_handler
algorithm handler of PID angle loop
Definition: pid.c:28
volatile PID_Structure_t speed_pid_handler
algorithm handler of PID speed loop
Definition: pid.c:24
float pid_calculate_result(PID_Structure_t *pid_handler, float collect)
calculate result using sampling value
Definition: pid.c:61
this is the header file of pid.c, which defines the structure of PID algorithm and closed-loop state ...
structure of PID algorithm
Definition: pid.h:24
float ki
integral term coefficient in PID
Definition: pid.h:26
float kp
proportional term coefficient in PID
Definition: pid.h:25
float minimum
minimum output in PID
Definition: pid.h:31
float maximum
maximum output in PID
Definition: pid.h:30
float sum_maximum
maximum of anti saturation integral in PID
Definition: pid.h:32
float last_error
error value of previous calculation in PID
Definition: pid.h:33
float summary
value of integral term in PID
Definition: pid.h:28
float expect
user expectations in PID
Definition: pid.h:29
float kd
differential term coefficient in PID
Definition: pid.h:27
void user_memset(void *buf, unsigned char data, unsigned char num)
format buffer array to fixed value, replace memset function
Definition: system.c:49
system basic function header file