Giter Club home page Giter Club logo

pid-library's Introduction

Banner

PID-Library (C Version)

PID Controller library for ARM Cortex M (STM32)

Download Arduino Library : Arduino-PID-Library

Release

  • Version : 1.0.0

  • Type : Embedded Software.

  • Support :

             - ARM STM32 series  
    
  • Program Language : C/C++

  • Properties :

  • Changes :

  • Required Library/Driver :

Overview

Initialization and de-initialization functions:

void PID(PID_TypeDef *uPID, double *Input, double *Output, double *Setpoint, double Kp, double Ki, double Kd, PIDPON_TypeDef POn, PIDCD_TypeDef ControllerDirection);
void PID2(PID_TypeDef *uPID, double *Input, double *Output, double *Setpoint, double Kp, double Ki, double Kd, PIDCD_TypeDef ControllerDirection);

Operation functions:

/* ::::::::::: Computing ::::::::::: */
uint8_t PID_Compute(PID_TypeDef *uPID);

/* ::::::::::: PID Mode :::::::::::: */
void            PID_SetMode(PID_TypeDef *uPID, PIDMode_TypeDef Mode);
PIDMode_TypeDef PID_GetMode(PID_TypeDef *uPID);

/* :::::::::: PID Limits ::::::::::: */
void PID_SetOutputLimits(PID_TypeDef *uPID, double Min, double Max);

/* ~~~~~~~~~~~~~~~~ PID I-windup Limits ~~~~~~~~~~~~~~~~~ */
void PID_SetILimits(PID_TypeDef *uPID, double Min, double Max)

/* :::::::::: PID Tunings :::::::::: */
void PID_SetTunings(PID_TypeDef *uPID, double Kp, double Ki, double Kd);
void PID_SetTunings2(PID_TypeDef *uPID, double Kp, double Ki, double Kd, PIDPON_TypeDef POn);

/* ::::::::: PID Direction ::::::::: */
void          PID_SetControllerDirection(PID_TypeDef *uPID, PIDCD_TypeDef Direction);
PIDCD_TypeDef PID_GetDirection(PID_TypeDef *uPID);

/* ::::::::: PID Sampling :::::::::: */
void PID_SetSampleTime(PID_TypeDef *uPID, int32_t NewSampleTime);

/* ::::::: Get Tunings Param ::::::: */
double PID_GetKp(PID_TypeDef *uPID);
double PID_GetKi(PID_TypeDef *uPID);
double PID_GetKd(PID_TypeDef *uPID);

Macros:

non   

Guide

This library can be used as follows:

1. Add pid.h header

2. Create PID struct and initialize it, for example:

  • Initializer:

    PID(PID_TypeDef *uPID, double *Input, double *Output, double *Setpoint, double Kp, double Ki, double Kd, PIDPON_TypeDef POn, PIDCD_TypeDef ControllerDirection);
  • Parameters:

    • uPID : Pointer to pid struct
    • Input : The variable we're trying to control (double)
    • Output : The variable that will be adjusted by the pid (double)
    • Setpoint : The value we want to Input to maintain (double)
    • Kp,Ki,Kd : Tuning Parameters. these affect how the pid will change the output (double>=0)
    • POn : Either P_ON_E (Default) or P_ON_M. Allows Proportional on Measurement to be specified.
    • ControllerDirection : Either DIRECT or REVERSE. determines which direction the output will move when faced with a given error. DIRECT is most common
  • Example:

    PID_TypeDef TPID;
    
    double Temp, PIDOut, TempSetpoint;
    
    PID(&TPID, &Temp, &PIDOut, &TempSetpoint, 2, 5, 1, _PID_P_ON_E, _PID_CD_DIRECT);

3. Set 'mode', 'sample time' and 'output limit', for example:

  • Functions:

    void PID_SetMode(PID_TypeDef *uPID, PIDMode_TypeDef Mode);
    void PID_SetOutputLimits(PID_TypeDef *uPID, double Min, double Max);
    void PID_SetSampleTime(PID_TypeDef *uPID, int32_t NewSampleTime);
    void PID_SetILimits(PID_TypeDef *uPID, double Min, double Max)
  • Parameters:

    • uPID : Pointer to pid struct
    • Mode : _PID_MODE_AUTOMATIC or _PID_MODE_MANUAL
    • Min : Low end of the range. must be < max (double)
    • Max : High end of the range. must be > min (double)
    • NewSampleTime : How often, in milliseconds, the PID will be evaluated. (int>0)
  • Example:

    PID_SetMode(&TPID, _PID_MODE_AUTOMATIC);
    PID_SetSampleTime(&TPID, 500);
    PID_SetOutputLimits(&TPID, 1, 100);
    PID_SetILimits(&TPID, 0, 20); // Set max and min I limit
    

4. Using Compute function, for example:

PID_Compute(&TPID);

Examples

Example 1: PID Compute for temperature

#include "main.h"
#include "pid.h"

PID_TypeDef TPID;

char OutBuf[50];
double Temp, PIDOut, TempSetpoint;

int main(void)
{
	
  HW_Init();
  
  PID(&TPID, &Temp, &PIDOut, &TempSetpoint, 2, 5, 1, _PID_P_ON_E, _PID_CD_DIRECT);
	
  PID_SetMode(&TPID, _PID_MODE_AUTOMATIC);
  PID_SetSampleTime(&TPID, 500);
  PID_SetOutputLimits(&TPID, 1, 100);
  PID_SetILimits(&TPID, -20, 20); // Set max and min I limit

    while (1) 
    {
  
      Temp = GetTemp();
      PID_Compute(&TPID);
      
      sprintf(OutBuf, "Temp%3.2f : %u\n", Temp, (uint16_t)PIDOut);
      UART_Transmit((uint8_t *)OutBuf, strlen(OutBuf));
      
      Delay_ms(500);
    
    }
}
   

Tests performed:

  • Run on STM32 Fx cores

Developers:

  • Majid Derhambakhsh

pid-library's People

Contributors

axxaxx avatar majid-derhambakhsh avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.