class::calc::PidF32

class calc::PidF32
  : public api::WorkObject

This class implements a PID control loop using floating point values.

A PID control loop uses three constant values in order to calculate a control variable such that the input converges on the target variable.

The following terminology is used in this documentation.

  • kp: Proportional constant
  • ki: Integral constant
  • kd: Differential constant
  • present value: the current value of the system output
  • target variable: desired output value of the system
  • control variable: calculated value to apply such that the input tends to the target variable
  • error variable: the error variable is the difference between the target variable and the present value

An example for using a PID control loop is temperature control. Consider a system that uses a PWM value to heat an element and a temperature sensor to read the output. The above terminology is applied as follows:

  • kp: Value selected by the developer to tune the control loop
  • ki: Value selected by the developer to tune the control loop
  • kd: Value selected by the developer to tune the control loop
  • present value: the value of the temperature sensor output
  • target variable: the desired temperature sensor output
  • control variable: the PWM duty cycle that should be applied to the heater
  • error variable: the different between the desired temperature output and the present temperature output
//md2code:include
#include <sapi/calc.hpp>
#include <sapi/hal.hpp>
//md2code:main
PidF32 pid_loop;
Pwm control(0);
Adc present_value(0);
u16 adc_value;

volatile bool is_active = true;

pid_loop.set_kp(1.0f);
pid_loop.set_ki(0.1f);
pid_loop.set_kd(0.001f);
pid_loop.set_maximum(1000.0f); //max duty cycle
pid_loop.set_minimum(0.0f); //min duty cycle

 //Init the PWM output and ADC input

while( is_active ){
 present_value.read(
  arg::Location(0),
  arg::DestinationBuffer(&adc_value),
  arg::Size(2)
  );
 control.set_channel(
   0,
   pid_loop.calculate_control_variable(
     3.3f * adc_value / 4096.0f
     )
   );
}

Summary

PidF32()

float calculate_control_variable(

float present_value
)

float kd() const

float ki() const

float kp() const

float max() const

float min() const

void reset()

PidF32 & set_kd(

float v
)

PidF32 & set_ki(

float v
)

PidF32 & set_kp(

float v
)

PidF32 & set_maximum(

float v
)

PidF32 & set_minimum(

float v
)

PidF32 & set_target(

float v
)

float target() const

Members

PidF32()

Constructs a new PID (float) object.


float calculate_control_variable(

float present_value
)

Calculates the control variable based on the current state of the system.

Parameters

  • present_value The present value of the system

Returns

The updated control variable to be applied to the system


float kd() const

Returns the differential constant.


float ki() const

Returns the integral constant.


float kp() const

Returns the proportional constant.


float max() const

Returns the maximum value for the control variable.


float min() const

Returns the minimum value for the control variable.


void reset()

Resets the state of the PID control loop.


PidF32 & set_kd(

float v
)

Sets the differential constant value.


PidF32 & set_ki(

float v
)

Sets the integral constant value.


PidF32 & set_kp(

float v
)

Sets the proportional constant value.


PidF32 & set_maximum(

float v
)

Sets the maximum allowed value of the target variable.


PidF32 & set_minimum(

float v
)

Sets the minimum allowed value of the target variable.


PidF32 & set_target(

float v
)

Sets the value for the target variable.


float target() const

Returns the target variable.


X

Thanks for Coming!

Subscribe to news and updates