Skip to content

Commit

Permalink
Tweak speeds and bucket sizes more
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickpeterse committed Jul 12, 2023
1 parent 510051c commit 955da46
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/openflow/co2.inko
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let LOW = 400
let HIGH = 1200

# The size/ppm per bucket.
let pub SIZE = 50
let pub SIZE = 25

# The number of buckets to use for the CO2 histogram.
let BUCKETS = HIGH - LOW / SIZE + 1
Expand Down
47 changes: 31 additions & 16 deletions src/openflow/inputs/co2.inko
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import std::time::(Duration, Instant)
# How many samples we should gather before updating the CO2 levels.
let pub SAMPLES = 20

fn co2_status(co2: Int) -> Status {
if co2 >= 900 {
Status.Maximum
} else if co2 >= 850 {
Status.Active(75)
} else if co2 >= 800 {
Status.Active(70)
} else if co2 >= 750 {
Status.Active(55)
} else if co2 >= 700 {
Status.Active(40)
} else if co2 >= 650 {
Status.Active(20)
} else {
Status.Default
}
}

# The state of a single CO2 sensor.
class pub Sensor {
let @name: String
Expand Down Expand Up @@ -121,22 +139,19 @@ class pub async Input {
let updates = @sensors.iter_mut.reduce(recover Map.new) fn (map, sensor) {
let old = sensor.value
let new = sensor.update_co2
let status = recover {
if new >= 900 {
Status.Maximum
} else if new >= 850 {
Status.Active(75)
} else if new >= 800 {
Status.Active(70)
} else if new >= 750 {
Status.Active(55)
} else if new >= 700 {
Status.Active(40)
} else if new >= 650 {
Status.Active(20)
} else {
Status.Default
}

# A common pattern I'm seeing is CO2 bouncing between 800 and 700 ppm when
# there are two people in a large room (e.g. a living room). The pattern
# is almost always the same: CO2 goes to 800, is reduced to 700 (resulting
# in a speed reduction), then increases to 750, and back to 800.
#
# This doesn't happen when CO2 is higher, as that's usually the case only
# for smaller rooms, and when it's below 700 it doesn't really matter. As
# such we handle this specific scenario by setting the speed in between.
let status = if old == 800 and new == 700 {
recover co2_status(750)
} else {
recover co2_status(new)
}

if status >= sensor.status or sensor.reduce?(@reduce_wait_time) {
Expand Down
22 changes: 22 additions & 0 deletions test/openflow/inputs/test_co2.inko
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,26 @@ fn pub tests(t: mut Tests) {
run(input)
t.equal(Snapshot.of(state).rooms.get('office').status, Status.Active(40))
}

t.test('Adjusting ventilation for CO2 that goes up and down') fn (t) {
let state = state(allow_api_calls)
let samples = recover [
# The first update.
800, 800, 800, 800, 800, 800, 800, 800, 800, 800,
800, 800, 800, 800, 800, 800, 800, 800, 800, 800,

# The second update.
700, 700, 700, 700, 700, 700, 700, 700, 700, 700,
700, 700, 700, 700, 700, 700, 700, 700, 700, 700,
]

let input = input(state, samples: samples)

run(input)
input.reset_reduce_wait_time
t.equal(Snapshot.of(state).rooms.get('office').status, Status.Active(70))

run(input)
t.equal(Snapshot.of(state).rooms.get('office').status, Status.Active(55))
}
}
16 changes: 8 additions & 8 deletions test/openflow/test_co2.inko
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ fn pub tests(t: mut Tests) {
co2.add(720)
co2.add(720)
co2.update
t.equal(co2.value, 700)
t.equal(co2.value, 725)

co2.add(735)
co2.add(735)
co2.update
t.equal(co2.value, 750)
t.equal(co2.value, 725)

co2.add(760)
co2.add(760)
Expand All @@ -41,12 +41,12 @@ fn pub tests(t: mut Tests) {
co2.add(775)
co2.add(775)
co2.update
t.equal(co2.value, 750)
t.equal(co2.value, 775)

co2.add(785)
co2.add(785)
co2.update
t.equal(co2.value, 800)
t.equal(co2.value, 775)

co2.add(740)
co2.add(740)
Expand All @@ -60,13 +60,13 @@ fn pub tests(t: mut Tests) {
co2.add(710)
co2.add(780)
co2.update
t.equal(co2.value, 750)
t.equal(co2.value, 725)

co2.add(750)
co2.add(803)
co2.add(810)
co2.update
t.equal(co2.value, 800)
t.equal(co2.value, 775)

co2.add(1250)
co2.add(1500)
Expand Down Expand Up @@ -95,7 +95,7 @@ fn pub tests(t: mut Tests) {
co2.add(1200)
co2.add(1200)
co2.update
t.equal(co2.value, 800)
t.equal(co2.value, 825)
}

t.test('Co2.update with a short increase in CO2') fn (t) {
Expand Down Expand Up @@ -128,6 +128,6 @@ fn pub tests(t: mut Tests) {
co2.add(900)

co2.update
t.equal(co2.value, 750)
t.equal(co2.value, 725)
}
}

0 comments on commit 955da46

Please sign in to comment.