PID in MMBasic


Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11328
Posted: 11:38am 17 Sep 2024      

The Pico2 gives more power than we had previously and potentially opens up new applications. I note that we have serious users of control type technologies so I thought it might be helpful to embed some semi-automatic PID functionality in MMbasic that could run multiple PID loops in parallel.

The concept could be as follows:

DEVICE PID INIT channel%, pid_params!(), callback_function


pid_params!()
float Kp
float Ki
float Kd

' Derivative low-pass filter time constant
float tau

'Output limits
float limMin
float limMax

'Integrator limits
float limMinInt
float limMaxInt

'Sample time (in milliseconds)
float T

'Controller "memory"
float integrator
float prevError 'Required for integrator'
float differentiator
float prevMeasurement 'Required for differentiator

'Controller output
float out

DEVICE PID START channel

DEVICE PID STOP channel

output! = DEVICE(PID channel, setpoint!, measurement!)


The PID init would set up a MMBasic function to be called every T milliseconds.

Following DEVICE PID START the callback function (MMBasic interrupt) would be called as requested and the Basic program should update the algorithm using the DEVICE(PID function.
It is left to the MMBasic user to derive the measurement (e.g. ADC input) and set the output (e.g. adjust PWM duty) in the callback function (NB: slow measurements like ADC input could be run in the main program so the value is always available for the PID callback).

Does this make sense? Would it be useful?