feat: 增加速度环

This commit is contained in:
bmy
2024-04-26 16:13:07 +08:00
parent 05cab94699
commit 6bac98da0a
12 changed files with 303 additions and 136 deletions

View File

@@ -1,4 +1,4 @@
/*
/*
------------------------------------------------------------------------------
~ File : pid.h
~ Author : Majid Derhambakhsh
@@ -6,9 +6,9 @@
~ Created: 02/11/2021 03:43:00 AM
~ Brief :
~ Support:
E-Mail : Majid.do16@gmail.com (subject : Embedded Library Support)
E-Mail : Majid.do16@gmail.com (subject : Embedded Library Support)
Github : https://github.com/Majid-Derhambakhsh
Github : https://github.com/Majid-Derhambakhsh
------------------------------------------------------------------------------
~ Description:
@@ -25,11 +25,9 @@
#include <stdint.h>
#include <string.h>
// #include "zf_common_headfile.h"
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Defines ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ------------------------ Library ------------------------ */
#define _PID_LIBRARY_VERSION 1.0.0
#define _PID_LIBRARY_VERSION 1.0.0
/* ------------------------ Public ------------------------- */
#define _PID_8BIT_PWM_MAX UINT8_MAX
@@ -37,37 +35,36 @@
#ifndef _FALSE
#define _FALSE 0
#define _FALSE 0
#endif
#ifndef _TRUE
#define _TRUE 1
#define _TRUE 1
#endif
/* ---------------------- By compiler ---------------------- */
#ifndef GetTime
/* ---------------------- By compiler ---------------------- */
/* ---------------------- By compiler ---------------------- */
#ifdef __CODEVISIONAVR__ /* Check compiler */
#ifdef __CODEVISIONAVR__ /* Check compiler */
#define GetTime() 0
#define GetTime() 0
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#elif defined(__GNUC__) /* Check compiler */
#elif defined(__GNUC__) /* Check compiler */
#define GetTime() 0
#define GetTime() 0
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#else
#endif /* __CODEVISIONAVR__ */
/* ------------------------------------------------------------------ */
#else
#endif /* __CODEVISIONAVR__ */
/* ------------------------------------------------------------------ */
#endif
@@ -75,64 +72,61 @@
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Types ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* PID Mode */
typedef enum
{
_PID_MODE_MANUAL = 0,
_PID_MODE_AUTOMATIC = 1
}PIDMode_TypeDef;
typedef enum {
_PID_MODE_MANUAL = 0,
_PID_MODE_AUTOMATIC = 1
} PIDMode_TypeDef;
/* PID P On x */
typedef enum
{
_PID_P_ON_M = 0, /* Proportional on Measurement */
_PID_P_ON_E = 1
}PIDPON_TypeDef;
typedef enum {
_PID_P_ON_M = 0, /* Proportional on Measurement */
_PID_P_ON_E = 1
} PIDPON_TypeDef;
/* PID Control direction */
typedef enum
{
_PID_CD_DIRECT = 0,
_PID_CD_REVERSE = 1
}PIDCD_TypeDef;
typedef enum {
_PID_CD_DIRECT = 0,
_PID_CD_REVERSE = 1
} PIDCD_TypeDef;
/* PID Structure */
typedef struct
{
PIDPON_TypeDef POnE;
PIDMode_TypeDef InAuto;
PIDPON_TypeDef POn;
PIDCD_TypeDef ControllerDirection;
PIDPON_TypeDef POnE;
PIDMode_TypeDef InAuto;
uint32_t LastTime;
uint32_t SampleTime;
PIDPON_TypeDef POn;
PIDCD_TypeDef ControllerDirection;
float DispKp;
float DispKi;
float DispKd;
uint32_t LastTime;
uint32_t SampleTime;
float Kp;
float Ki;
float Kd;
float DispKp;
float DispKi;
float DispKd;
float *MyInput;
float *MyOutput;
float *MySetpoint;
float Kp;
float Ki;
float Kd;
float OutputSum;
float LastInput;
float *MyInput;
float *MyOutput;
float *MySetpoint;
float OutMin;
float OutMax;
}PID_TypeDef;
float OutputSum;
float LastInput;
float OutMin;
float OutMax;
} PID_TypeDef;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enum ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
@@ -141,14 +135,15 @@ typedef struct
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Prototype ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* :::::::::::::: Init ::::::::::::: */
void PID_Init(PID_TypeDef *uPID);
void PID(PID_TypeDef *uPID, float *Input, float *Output, float *Setpoint, float Kp, float Ki, float Kd, PIDPON_TypeDef POn, PIDCD_TypeDef ControllerDirection);
void PID2(PID_TypeDef *uPID, float *Input, float *Output, float *Setpoint, float Kp, float Ki, float Kd, PIDCD_TypeDef ControllerDirection);
void PID(PID_TypeDef *uPID, const float *Input, float *Output, const float *Setpoint, float Kp, float Ki, float Kd, PIDPON_TypeDef POn, PIDCD_TypeDef ControllerDirection);
void PID2(PID_TypeDef *uPID, const float *Input, float *Output, const float *Setpoint, float Kp, float Ki, float Kd, PIDCD_TypeDef ControllerDirection);
/* ::::::::::: Computing ::::::::::: */
uint8_t PID_Compute(PID_TypeDef *uPID);
/* ::::::::::: PID Mode :::::::::::: */
void PID_SetMode(PID_TypeDef *uPID, PIDMode_TypeDef Mode);
void PID_SetMode(PID_TypeDef *uPID, PIDMode_TypeDef Mode);
PIDMode_TypeDef PID_GetMode(PID_TypeDef *uPID);
/* :::::::::: PID Limits ::::::::::: */
@@ -159,7 +154,7 @@ void PID_SetTunings(PID_TypeDef *uPID, float Kp, float Ki, float Kd);
void PID_SetTunings2(PID_TypeDef *uPID, float Kp, float Ki, float Kd, PIDPON_TypeDef POn);
/* ::::::::: PID Direction ::::::::: */
void PID_SetControllerDirection(PID_TypeDef *uPID, PIDCD_TypeDef Direction);
void PID_SetControllerDirection(PID_TypeDef *uPID, PIDCD_TypeDef Direction);
PIDCD_TypeDef PID_GetDirection(PID_TypeDef *uPID);
/* ::::::::: PID Sampling :::::::::: */