Motor Driver Evaluation Kit NEVB-MTR1-t01-1.3.1
Firmware for NEVB-MTR1-KIT1 for trapezoidal control of BLDC motors using Hall-effect sensors
Loading...
Searching...
No Matches
pid.cpp File Reference

PID controller source file. More...

#include "config.h"
#include "pid.h"
Include dependency graph for pid.cpp:

Go to the source code of this file.

Functions

void PIDInit (int16_t p_factor, int16_t i_factor, int16_t d_factor, pidData_t *pid)
 Initialisation of PID controller parameters.
 
uint16_t PIDController (int16_t setPoint, int16_t processValue, pidData_t *pid_st)
 PID control algorithm.
 
void PIDResetIntegrator (pidData_t *pid_st)
 Resets the integrator in the PID regulator.
 

Detailed Description

PID controller source file.

This file contains the implementation of the PID controller.

Author
Nexperia: http://www.nexperia.com
Support Page
For additional support, visit: https://www.nexperia.com/support
Author
Aanas Sayed
Date
2024/03/08


Definition in file pid.cpp.

Function Documentation

◆ PIDController()

uint16_t PIDController ( int16_t setPoint,
int16_t processValue,
pidData_t * pid_st )

PID control algorithm.

Calculates output from set point, process value, and PID status.

Parameters
setPointDesired value.
processValueMeasured value.
pid_stPID status struct.
Returns
Calculated control output as a 16-bit unsigned integer.

Definition at line 56 of file pid.cpp.

57{
58 int32_t ret;
59 int32_t temp;
60 int16_t error;
61
62 error = setPoint - processValue;
63
64 // Calculate "P" term and limit error overflow
65 if (error > pid_st->maxError)
66 {
67 pid_st->p_term = MAX_INT;
68 }
69 else if (error < -pid_st->maxError)
70 {
71 pid_st->p_term = -MAX_INT;
72 }
73 else
74 {
75 pid_st->p_term = pid_st->P_Factor * error / 1000;
76 }
77
78 // Calculate "I" term and limit integral runaway.
79 // i_term is always derived from sumError so the clamp is continuous.
80 temp = pid_st->sumError + error;
81 if (temp > pid_st->maxSumError)
82 {
83 pid_st->sumError = pid_st->maxSumError;
84 }
85 else if (temp < -pid_st->maxSumError)
86 {
87 pid_st->sumError = -pid_st->maxSumError;
88 }
89 else
90 {
91 pid_st->sumError = temp;
92 }
93 pid_st->i_term = pid_st->I_Factor * pid_st->sumError / 1000;
94
95#if (PID_K_D_ENABLE == TRUE)
96 // Calculate "D" term
97 pid_st->d_term = pid_st->D_Factor * (pid_st->lastProcessValue - processValue) / 1000;
98#endif
99
100 pid_st->lastProcessValue = processValue;
101
102#if (PID_K_D_ENABLE == TRUE)
103 ret = (pid_st->p_term + pid_st->i_term + pid_st->d_term);
104#else
105 ret = (pid_st->p_term + pid_st->i_term);
106#endif
107
108#if (SCALING_FACTOR_ENABLED == TRUE)
109 ret = ret / SCALING_FACTOR;
110#endif
111 if (ret > MAX_INT)
112 {
113 ret = MAX_INT;
114 }
115 // Since the return type is uint16_t
116 else if (ret < 0)
117 {
118 ret = 0;
119 }
120
121 return ((uint16_t)ret);
122}
#define MAX_INT
Maximum value of integers.
Definition filter.h:29
#define SCALING_FACTOR
Scaling division factor for PID controller.
Definition pid.h:33
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
Here is the caller graph for this function:

◆ PIDInit()

void PIDInit ( int16_t p_factor,
int16_t i_factor,
int16_t d_factor,
pidData_t * pid )

Initialisation of PID controller parameters.

Initialise the variables used by the PID algorithm.

Parameters
p_factorProportional term.
i_factorIntegral term.
d_factorDerivate term.
pidStruct with PID status.

Definition at line 33 of file pid.cpp.

35{
36 // Start values for PID controller
37 pid->sumError = 0;
38 pid->lastProcessValue = 0;
39 // Tuning constants for PID loop
40 pid->P_Factor = p_factor;
41 pid->I_Factor = i_factor;
42 pid->D_Factor = d_factor;
43 // Limits to avoid overflow
44 pid->maxError = MAX_INT / pid->P_Factor;
45 pid->maxSumError = MAX_I_TERM / pid->I_Factor;
46}
#define MAX_I_TERM
PID integrator clamp value passed to the PID controller.
Definition config.h:1351
Here is the caller graph for this function:

◆ PIDResetIntegrator()

void PIDResetIntegrator ( pidData_t * pid_st)

Resets the integrator in the PID regulator.

Calling this function will reset the integrator in the PID regulator.

Parameters
pid_stPointer to the PID status struct for which the integrator will be reset.

Definition at line 131 of file pid.cpp.

132{
133 pid_st->sumError = 0;
134 pid_st->p_term = 0;
135 pid_st->i_term = 0;
136#if (PID_K_D_ENABLE == TRUE)
137 pid_st->d_term = 0;
138#endif
139}
Here is the caller graph for this function: