Dec 10, 2020

[Arduino code] Proportional-Integral-Differential control (PID control)

- Supplies: Arduino board, ... sensor, ... actuator

- Comment: I use 'AutoPID' library and this code is based on the bottom *site.

*https://r-downing.github.io/AutoPID/

- How to install a PID library?

Sketch-> Include Library -> Manage Libraries... -> Write 'pid'


- Example code:

#include <AutoPID.h>

#define p_gain 1
#define i_gain 1
#define d_gain 1

//Min and Max values depend on your actuator function code. 
#define control_input_min -255
#define control_input_max 255

// unit: milliseconds
// ex) 20 ms => 50 Hz
#define control_interval 20 

double measured_value, desired_value, control_input, value_from_sensor;

AutoPID PID_control(&measured_value, &desired_value, &control_input, control_input_min, ...,
control_input_max, p_gain, i_gain, d_gain);
void setup() {
  // put your setup code here, to run once:
  PID_control.setTimeStep(control_interval);
}

void loop() {
  // put your main code here, to run repeatedly:

  desired_value = 0; //It depends on your tasks.
  measured_value = value_from_sensor; //It is measured by sensor, so its type depends on your sensor.
  PID_control.run();
  //Then, 'control_input' is calculated and its value drives your actuators.
  //So, you need to insert your actuator function code to the bottom side.
  //ex) move(1, 1, control_input);
  
}


No comments:

Post a Comment