• Stars
    star
    139
  • Rank 257,299 (Top 6 %)
  • Language
    C++
  • License
    MIT License
  • Created over 3 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C/C++ PID Controller library for ARM Cortex M (STM32)

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 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);
  • 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);

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);
  
    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

More Repositories

1

i2c-eeprom

Driver for using Serial EEPROM Products (AT24C family) in AVR-ARM microcontrollers.
C
18
star
2

Character-LCD

CH LCD library for use in AVR - ARM Cortex M (STM32)
C
14
star
3

menu-manager

Menu Manager library for manage the menu & items with support click event
C
12
star
4

NRF24L01

Multi-Channel nRF24L01+ Library for ARM Cortex M (STM32) and AVR microcontrollers
C
7
star
5

MPU6050

"Easy To Use" MPU6050 library for using in AVR - ARM Cortex M
C
6
star
6

ST7789

ST7789 LCD Library for ARM Cortex M (STM32) and AVR microcontrollers
C
6
star
7

DS18B20

DS18B20 library for use in ARM Cortex M (STM32)
C
5
star
8

ASK-Decoder

ASK module library for microcontrollers
C
4
star
9

Majid-Derhambakhsh

Software ~ Embedded System Developer
3
star
10

CoOp-Core

Co-Op (Co-Operative) Multitasking Algorithm library for c/c++ application's
C
3
star
11

SSD1306-GLCD

Library to use GLCD with SSD1306 driver for use in AVR - ARM Cortex M (STM32)
C
3
star
12

SerialPortManager

Library to find and use the serial ports
2
star
13

RFID

RFID library for manage Card IDs in AVR/STM32 ARM microcontroller.
C
2
star
14

ESP8266

ESP8266 library for use in ARM/AVR microcontrollers with UART communication protocol.
C
2
star
15

AltiumLibrary

My Custom Altium Library
2
star
16

7Segment-Library

"High Performance" & "Easy To Use" library for 7-Segment display in AVR - ARM Cortex M
C++
2
star
17

BUFFER

This program module is for create your data buffer.
C
2
star
18

Stack

Educational Center Management Software
2
star
19

Windows-Serial-Terminal

Serial Terminal software for work with serial communication protocol in windows.
C#
1
star
20

Ultrasonic-Library

Ultrasonic module library for any type of microcontroller
C
1
star
21

SPI-Flash-Ex

The SPI Flash Extension library to merges a collection of flash memories as a memory bank in MCU
1
star
22

POS-Terminal-Library

POS Device Communication Library via RS232 for STM32, other microcontrollers, and C programs
1
star
23

Sterile-Device-STM8Base

Sterile device based on STM8S003F microcontroller
1
star
24

SmartPlug

Smart Plug based on ESP8266 MCU
1
star
25

i2c-unit

Driver for working with I2C(TWI) unit of AVR microcontroller.
C
1
star