11.09.2018, 07:05
You can modify PID library compute function like this. Then you need to specify night_mode (boolean, true = night mode) and setpoint_night (float16 or similar) addresses in PID init. After that restart your script via enable/disable so new library and parameters are loaded.
Code:
function PID:compute()
local params, current, setpoint, deltasc, deltain, output
params = self.params
-- get input values
current = grp.getvalue(params.current)
setpoint = grp.getvalue(params.setpoint)
if params.night_mode and grp.getvalue(params.night_mode) then
setpoint = grp.getvalue(params.setpoint_night)
end
-- delta between setpoint and current
deltasc = setpoint - current
-- calculate new iterm
self.iterm = self.iterm + params.ki * self.deltatime * deltasc
self:clampiterm()
-- delta between current and previous value
deltain = current - self.previous
-- calculate output value
self.output = params.kp * deltasc + self.iterm
self.output = self.output - params.kd / self.deltatime * deltain
-- write to output
self:setoutput()
-- save previous value
self.previous = current
return self.output
end