📄 pid.c
字号:
//*****************************************************************************//// pid.c - PID feedback control algorithm.//// Copyright (c) 2005,2006 Luminary Micro, Inc. All rights reserved.//// Software License Agreement//// Luminary Micro, Inc. (LMI) is supplying this software for use solely and// exclusively on LMI's microcontroller products.//// The software is owned by LMI and/or its suppliers, and is protected under// applicable copyright laws. All rights are reserved. Any use in violation// of the foregoing restrictions may subject the user to criminal sanctions// under applicable laws, as well as to civil liability for the breach of the// terms and conditions of this license.//// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.//// This is part of revision 196 of an01238.////*****************************************************************************#include "pid.h"//*****************************************************************************////! Initialize the PID algorithm state.//!//! \param psState is a pointer to the PID algorithm state structure.//! \param lIntegMax is the maximum value allowed in the integrator.//! \param lIntegMin is the minimum value allowed in the integrator.//! \param lPGain is the proportional gain factor.//! \param lIGain is the integral gain factor.//! \param lDGain is the derivitive gain factor.//!//! This function will initialize the internal state of the PID algorithm.//! This must be done before the PID algorithm can be executed or random//! results will occur.//!//! By using a derivitive gain of zero, this reduces to a simple PI controller.//! By using a integral and derivitive gain of zero, it reduces to an even//! simpler P controller. The response requirements of the process being//! controlled determines the terms required to achieve that level of response.//! Controlling motors can typically be done with a simple PI controller.//!//! \return None.////*****************************************************************************voidPIDInitialize(tPIDState *psState, long lIntegMax, long lIntegMin, long lPGain, long lIGain, long lDGain){ // // Set the internal state, and save the integrator limits and gain factors. // psState->lIntegrator = 0; psState->lIntegMax = lIntegMax; psState->lIntegMin = lIntegMin; psState->lPrevPosition = 0; psState->lPGain = lPGain; psState->lIGain = lIGain; psState->lDGain = lDGain;}//*****************************************************************************////! Change the gain factors.//!//! \param psState is a pointer to the PID algorithm state structure.//! \param lPGain is the proportional gain factor.//! \param lIGain is the integral gain factor.//! \param lDGain is the derivitive gain factor.//!//! This function will change the gain factors used by the PID algorithm.//!//! \return None.////*****************************************************************************voidPIDSetGains(tPIDState *psState, long lPGain, long lIGain, long lDGain){ // // Save the gain factors. // psState->lPGain = lPGain; psState->lIGain = lIGain; psState->lDGain = lDGain;}//*****************************************************************************////! Run another iteration of the PID algorithm.//!//! \param psState is a pointer to the PID algorithm state structure.//! \param lPosition is the current value for the operation being controlled.//! \param lError is the error of the current value relative to the desired//! value.//!//! This function will execute another iteration of the PID algorithm. In//! order to get reliable results from this, the sampled values passed in must//! be captured at fixed intervals (as close as possible). Deviations from a//! fixed capture interval will result in errors in the control output.//!//! \return The new control value////*****************************************************************************longPIDUpdate(tPIDState *psState, long lPosition, long lError){ long lOutput; // // Update the error integrator, saturating the value if necessary. // psState->lIntegrator += lError; if(psState->lIntegrator > psState->lIntegMax) { psState->lIntegrator = psState->lIntegMax; } if(psState->lIntegrator < psState->lIntegMin) { psState->lIntegrator = psState->lIntegMin; } // // Compute the new control value. // lOutput = ((psState->lPGain * lError) + (psState->lIGain * psState->lIntegrator) + (psState->lDGain * (lPosition - psState->lPrevPosition))); // // Save the current position for computing the derivitive on the next // iteration. // psState->lPrevPosition = lPosition; // // Return the control value. // return(lOutput);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -