Seite 1 von 1

#1 LUA for Base (transferiert aus anderer Themenkette - von Nils zur besseren Übersicht)

Verfasst: Mi 22. Sep 2010, 22:22
von DOMIQ
Larger example - I'm attaching two files (well, I tired, results below), that should be loaded to scripts directory on the Base. They can later be loaded by "import" call. Latest modules have those files by default.
[warn]pwm.lua ist kein erlaubter Dateityp.
pid.lua ist kein erlaubter Dateityp.[/warn]
  • pwm.lua implements PWM modulation for outputs, that lets use PWM with older LCN modules and simple radiator regulators.
  • pid.lua implements PID regulator, I actually use it at home instead of built-in proportional regulators of LCN modules. For now mostly because of PWM
This is how to use both together:

Code: Alles auswählen

import 'pid'
import 'pwm'


pid {
  kp = 10, trace=true,
  positive = pwm { output = use "LCN.output.0.110.2", period = 360},
  value = use "LCN.value.0.110.r1",
  regulator = use "LCN.regulator.0.110.1"
}


Above code configures PWM output on module 110, output 2 with full cycle equal to 6 minutes. It uses temperature from R1 on module 110 as current temperature, and regulator 1 setting on module 110 as setpoint value.

There are more options to setup, like integration and derivation constants. I need to extend pid.lua with regulator autotuning...

Another option, that could have nice results (I will check this winter) would be to use LCN regulator to control temperature of the radiator in the room and then use above PID regulator (with small changes) to vary temperature of the radiator by changing setpoint inside LCN module. This of course would require two temperature sensors per room, one like now and second stuck to the radiator.

Such setup is frequently used in automation, and should make temperature stabilization much better. And get rid of "cool flow" of air from the windows to floor, which for me is major issue with current LCN setup, especially that "standard" way of radiator control doesn't suffer that problem.

#2 RE: LUA for Base (transferiert aus anderer Themenkette - von Nils zur besseren Übersicht)

Verfasst: Mi 22. Sep 2010, 22:29
von DOMIQ
This is pwm.lua, so you can see how it looks like. pid.lua is much longer, but generally doesn't use anything different.

Code: Alles auswählen

--
-- PWM Output Module
--
-- Copyright 2010 DOMIQ Sp. z o.o.
--

function pwm(tab)
        local dev = {}
        local out = assert(tab.output)
        local per = assert(tab.period)

        local ontime = 0
        local pertime = 0
        local curtime = 0

        dev.value = 0
        dev.trace = tab.trace

        function out:ontick()
                curtime = curtime + 1
                if curtime > per then curtime = 0 end
                ontime = (dev.value * per)/100
                if ontime  per then ontime = per end
                if dev.trace then
                        print(string.format("PWM: out=%d curtime=%d ontime=%d period=%d",out.value,curtime,ontime,per))
                end
                if curtime  ontime and out.value > 0 then out:off() return end
        end

        function dev:set(val)
                print("PWM: value="..val)
                local v
                if val == "on" then
                        v = 100
                elseif val == "off" then
                        v = 0
                else
                        v = tonumber(val)
                end
                assert(v >= 0)
                assert(v <= 100)
                if dev.value == 0 then
                        curtime = 0
                end
                dev.value = val
        end

        function dev:get()
                return dev.value
        end

        out:off()

        return dev
end

#3 RE: LUA for Base (transferiert aus anderer Themenkette - von Nils zur besseren Übersicht)

Verfasst: Do 23. Sep 2010, 07:57
von Nils
Hi Filip,
I transfered this new theme for a better overview ;-)

The upload didn't work because of wrong ending. Please send me the files per mail ans I will add them.

Regards

#4 RE: LUA for Base (transferiert aus anderer Themenkette - von Nils zur besseren Übersicht)

Verfasst: Fr 22. Apr 2011, 22:46
von DOMIQ
How to act on change:

Code: Alles auswählen

sth = use 'LCN.output.0.20.1'

function sth:onchange(new,old)
  print("New value",new,"Old value",old)
end

If you don't need old or new value, you might ignore it. Also, if you need to monitor number of values for change, do it like that:

Code: Alles auswählen

val1 = use 'LCN.relay.0.20.1'
val2 = use 'LCN.relay.0.20.2'

function changed()
  if val1.value == 1 and val2.value == 1 then
    print("Both are ON")
  end
end

val1.onchange = changed
val2.onchange = changed