Motor Evaluation Kit NEVC-MCTRL-100-t01-1.0.0
Firmware for NEVB-MCTRL-100-01 for trapezoidal control of BLDC motors using Hall-effect sensors
Loading...
Searching...
No Matches
pid.cpp
Go to the documentation of this file.
1/* This file has been prepared for Doxygen automatic documentation generation.*/
21// Include PID header
22#include "pid.h"
23
31void PIDInit(int16_t p_factor, int16_t i_factor, int16_t d_factor, pidData_t *pid)
32// Set up PID controller parameters
33{
34 // Start values for PID controller
35 pid->sumError = 0;
36 pid->lastProcessValue = 0;
37 // Tuning constants for PID loop
38 pid->P_Factor = p_factor;
39 pid->I_Factor = i_factor;
40 pid->D_Factor = d_factor;
41 // Limits to avoid overflow
42 pid->maxError = MAX_INT / pid->P_Factor;
43 pid->maxSumError = MAX_I_TERM / pid->I_Factor;
44}
45
54uint16_t PIDController(int16_t setPoint, int16_t processValue, pidData_t *pid_st)
55{
56 int32_t ret;
57 int32_t temp;
58 int16_t error;
59
60 error = setPoint - processValue;
61
62 // Calculate "P" term and limit error overflow
63 if (error > pid_st->maxError)
64 {
65 pid_st->p_term = MAX_INT;
66 }
67 else if (error < -pid_st->maxError)
68 {
69 pid_st->p_term = -MAX_INT;
70 }
71 else
72 {
73 pid_st->p_term = pid_st->P_Factor * error / 1000;
74 }
75
76 // Calculate "I" term and limit integral runaway
77 temp = pid_st->sumError + error;
78 if (temp > pid_st->maxSumError)
79 {
80 pid_st->i_term = MAX_I_TERM;
81 pid_st->sumError = pid_st->maxSumError;
82 }
83 else if (temp < -pid_st->maxSumError)
84 {
85 pid_st->i_term = -MAX_I_TERM;
86 pid_st->sumError = -pid_st->maxSumError;
87 }
88 else
89 {
90 pid_st->sumError = temp;
91 pid_st->i_term = pid_st->I_Factor * pid_st->sumError / 1000;
92 }
93
94#if (PID_K_D_ENABLE == TRUE)
95 // Calculate "D" term
96 pid_st->d_term = pid_st->D_Factor * (pid_st->lastProcessValue - processValue) / 1000;
97#endif
98
99 pid_st->lastProcessValue = processValue;
100
101#if (PID_K_D_ENABLE == TRUE)
102 ret = (pid_st->p_term + pid_st->i_term + pid_st->d_term);
103#else
104 ret = (pid_st->p_term + pid_st->i_term);
105#endif
106
107#if (SCALING_FACTOR_ENABLED == TRUE)
108 ret = ret / SCALING_FACTOR;
109#endif
110 if (ret > MAX_INT)
111 {
112 ret = MAX_INT;
113 }
114 // Since the return type is uint16_t
115 else if (ret < 0)
116 {
117 ret = 0;
118 }
119
120 return ((uint16_t)ret);
121}
122
131{
132 pid_st->sumError = 0;
133 pid_st->p_term = 0;
134 pid_st->i_term = 0;
135#if (PID_K_D_ENABLE == TRUE)
136 pid_st->d_term = 0;
137#endif
138}
#define MAX_INT
Maximum value of integers.
Definition filter.h:29
void PIDInit(int16_t p_factor, int16_t i_factor, int16_t d_factor, pidData_t *pid)
Initialisation of PID controller parameters.
Definition pid.cpp:31
uint16_t PIDController(int16_t setPoint, int16_t processValue, pidData_t *pid_st)
PID control algorithm.
Definition pid.cpp:54
void PIDResetIntegrator(pidData_t *pid_st)
Resets the integrator in the PID regulator.
Definition pid.cpp:130
PID controller header file.
#define SCALING_FACTOR
Scaling division factor for PID controller.
Definition pid.h:33
#define MAX_I_TERM
Maximum value of the I-term.
Definition pid.h:86
PID Status.
Definition pid.h:47
int32_t maxSumError
Maximum allowed sum error, avoid overflow.
Definition pid.h:61
int16_t P_Factor
The Proportional tuning constant, given in x100.
Definition pid.h:53
int16_t maxError
Maximum allowed error, avoid overflow.
Definition pid.h:59
int16_t D_Factor
The Derivative tuning constant, given in x100.
Definition pid.h:57
int16_t d_term
The D-term represents the rate of change of the error.
Definition pid.h:64
int32_t sumError
Summation of errors, used for integrate calculations.
Definition pid.h:51
int32_t i_term
Definition pid.h:70
int16_t I_Factor
The Integral tuning constant, given in x100.
Definition pid.h:55
int16_t p_term
The P-term represents the immediate response to the current error.
Definition pid.h:67
int16_t lastProcessValue
Last process value, used to find derivative of process value.
Definition pid.h:49