Simple PID Controller with Integral Windup Supports creating a diagnostics message to send to a GUI Prints to Binary

Dependents:   ApexPID

Fork of PidControllerV2 by James Batchelar

PidController.h

Committer:
batchee7
Date:
2018-05-07
Revision:
6:99403113343f
Parent:
5:1206105e20bd

File content as of revision 6:99403113343f:

/**
 * @author James Batchelar
 *
 * @section LICENSE
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @section DESCRIPTION
 *
 * PID Controller with Integral Clamping
 * 
 * 
 *     
 */

#ifndef PidController_H
#define PidController_H
#include "mbed.h"

//-- Constants used in system
const int RATE = 100;        //--(ms) Time that Calculate mehtod is being called       
const int AUTOMATIC = 0;    //-- In automatic then PID Controls Output
const int MANUAL = 1;       //-- In Manual then User Controls Output directly

//-- Constructor 

class PidController{
    public:
        // Public Methods
    
        // Constructor
        PidController();
        
        /** Performs the PID Calculation
        * @param SP - Setpoint (target value) units depends on what PID is controlling
        * @param PV - Process Variable (feedback/ measure value) units depends on what PID is controlling
        * @return Returns If Controller is in Automatic then returns PID controlled signal. In manual returns the user SP.
        */
        float Calculate(float SP, float PV);
        
        /** Update Internal Settings
        * @param Bias - Added to the PID Calculation
        * @param PropGain - Proportional Gain
        * @param IntGain - Integral Gain
        * @param DiffGain - Differential Gain
        * @param OutputMin - Minimum Limit for the Output (units are same as setpoint)
        * @param OutputMax - Maximum Limit for the Output (units are same as setpoint)
        * @param OutputScale - Multiplier at End of PID loop to convert from engineering units to signal (eg PWM duty cycle)   
        */    
        void UpdateSettings(float Bias, float PropGain, float IntGain, float DiffGain, float OutputMin, float OutputMax);
        
        /** Update Internal Settings
        * @param OutputMin - Minimum Limit for the Output (units are same as setpoint)
        * @param OutputMax - Maximum Limit for the Output (units are same as setpoint)
        */    
        void UpdateSettings(float OutputMin, float OutputMax);
        
        /** Get the controller mode
        * @return 1 = AUTOMATIC, 0 = MANUAL(User has direct contol on output).
        */
        int GetMode(void){return (int)mode;}
        
        /** Set the controller in Manual Mode (PID turned off SP is writted directly to output)
        */
        void SetManual(void){mode = MANUAL;}
        
        /** Set the controller in Automatic Mode (PID turned on)
        */
        void SetAutomatic(void){mode = AUTOMATIC;}
        
        /** Enable Diagnostics for HMI
        */
        void StartDiag(void);
        
        /** Disable Diagnostics for HMI
        */
        void EndDiag(void);
        
        /** Check to see if the controller is collecting diagnostics data
        * @return true then a diagnostics message is been created.
        */
        bool DiagnosticsEnabled(void){return collectDiagnostics;}
        
        /** Check to see if the controller is collecting diagnostics data
        * @param Data - Starting address of target recieve buffer (must be at least 25char long)
        */
        void GetDiagnosticsMessage(char *data);
        
    private:
        bool mode;
        //-- For Diagnostics to GUI
        bool collectDiagnostics;
        uint16_t elapsedTime;
        char diagMsg[28];
        
        //-- Settings
        float bias;
        float minLimit, maxLimit;
        float K_p,K_i,K_d;
        
        //-- For PID Calculations
        float error;
        float accumError;
        float prevError;
        
        //-- Private Helper functions
        void float_to_byte(char *data, float *val);
        void int_to_byte(char *data, uint16_t val);
        };
    
#endif