A program that uses a package: a thermostat
The 3 files of examples/thermostat/, and what zeno run examples/thermostat/main.zeno prints.
# A program that uses a package: a thermostat.
#
# zeno run examples/thermostat/main.zeno
#
# The file `zeno.package` beside this one says the program needs the
# package `filters`, which is the folder examples/filters. An import
# whose first word is a needed package reads a file of that package:
# `filters` is its module `filters.zeno`, and `filters.lowpass` is its
# module `lowpass.zeno`. Any other import is a file of this program.
import filters
import filters.lowpass
import control
from control import Heater, On, Off
# What the sensor read, once a minute. It is noisy.
let readings = [18.2, 18.9, 17.6, 19.4, 20.8, 20.1, 21.9, 22.6, 21.8, 23.1]
let smooth = filters.lowpass.start(0.4, readings[0])
var heater: Heater = Off
var minutes_on = 0
for reading in readings {
let temperature = smooth.step(reading)
heater = control.decide(heater, temperature, 19.0, 21.0)
if heater == On {
minutes_on += 1
}
print(f"read {reading:>5.1f} smoothed {temperature:>5.1f} heater {heater}")
}
print(f"mean {filters.mean(readings):.2f}, heater on for {minutes_on} of {len(readings)} minutes")# The module `control` of the thermostat itself: when the heater goes on
# and off. It is a file of this program, so `import control` finds it in
# this folder.
enum Heater {
On
Off
}
# The heater goes on under `low` and off over `high`. Between the two it
# stays as it is, so that it does not flicker around one temperature.
function decide(now: Heater, temperature: float, low: float, high: float) -> Heater {
if temperature < low {
return On
}
if temperature > high {
return Off
}
return now
}zeno run examples/thermostat/main.zeno
read 18.2 smoothed 18.2 heater On
read 18.9 smoothed 18.5 heater On
read 17.6 smoothed 18.1 heater On
read 19.4 smoothed 18.6 heater On
read 20.8 smoothed 19.5 heater On
read 20.1 smoothed 19.7 heater On
read 21.9 smoothed 20.6 heater On
read 22.6 smoothed 21.4 heater Off
read 21.8 smoothed 21.6 heater Off
read 23.1 smoothed 22.2 heater Off
mean 20.44, heater on for 7 of 10 minutes# What this program is called, and the packages it needs.
#
# `needs filters in "../filters"` uses the package in that folder, where
# it is. A package that somebody else keeps is named by its git
# repository and a version of it:
#
# needs units from "https://github.com/someone/zeno-units" at "v1.2.0"
#
# and `zeno fetch` gets it into the folder `packages` beside this file.
package thermostat
needs filters in "../filters"See it on GitHub: examples/thermostat/.