Example: PID thermostat with LM2

PID function

There is a PID function already added by default in Logic Machine -> Scripts -> Tools menu.

Usage

  1. p = PID:init(parameters)
  2. p:run()

Parameters

Mandatory:

  • current – (object address or name) current temperature value (2 byte float or any numeric value)
  • setpoint – (object address or name) temperature set point value (2 byte float or any numeric value)

Optional:

  • manual – (object address or name) PID algorithm is stopped when this object value is 1
  • output – (object address or name, can be a table with multiple objects) output object (1 byte scaled)
  • inverted – (boolean, defaults to false) invert algorithm, can be used for cooling
  • min – (number, defaults to 0) minimum output value
  • max – (number, defaults to 100) maximum output value
  • kp – (number, defaults to 1) proportional gain
  • ki – (number, defaults to 1) integral gain
  • kd – (number, defaults to 1) derivative gain

Adding Residential script

PID algorithm should be placed inside a Scripts -> Resident –> Add new script.

Script example:

  1. -- init pid algorithm
  2. if not p then
  3. p = PID:init({
  4. current = '1/1/1',
  5. setpoint = '1/1/2',
  6. output = '1/1/3'
  7. })
  8. end
  9.  
  10. -- run algorithm
  11. p:run()

Script example with multiple output objects:

  1. -- init pid algorithm
  2. if not p then
  3. p = PID:init({
  4. current = '1/1/1',
  5. setpoint = '1/1/2',
  6. output = { 'PWM 1', 'PWM 2', '1/1/5' }
  7. })
  8. end
  9.  
  10. -- run algorithm
  11. p:run()

Output value:
p:run() returns output value. If output parameter is not set, you can use the return value to control output objects manually in the script.