-
Notifications
You must be signed in to change notification settings - Fork 0
/
brew_controller.swift
290 lines (249 loc) · 11.8 KB
/
brew_controller.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import Foundation
protocol CoffeeMachineInterface {
func can_send() -> Bool
func print_to_string(message: Data) -> String
func get_runtime(message: Data) ->Int!
func has_just_started_brewing(message: Data) -> Bool
func has_just_stopped_brewing(message: Data) -> Bool
func set_boiler_temperature_target_for_brewing(degrees_celcius: Double)
func set_added_power_when_brewing(percent: Int)
func set_maximum_shot_time(seconds: Int)
func set_initial_pressure(percentage: Int)
func set_final_pressure(percentage: Int)
func set_pressure_rampup_time(seconds: Int)
func get_temperature(message: Data) -> Double!
}
protocol ScaleInterface {
func can_send() -> Bool
func get_weight(message: Data) -> Double?
func tare()
func start_timer()
func reset_timer()
func stop_timer()
func restart()
}
class BrewController {
let scale: ScaleInterface
let coffee_machine: CoffeeMachineInterface
var minutes_since_coffee_machine_power_on: Double = Double.nan
var coffee_machine_temperature: Double = Double.nan
var brewing = false { didSet { if !brewing { end_time_last_shot = Date() } } }
var current_brew_weight = Double.nan
private var brew_weight_history = TimeSeries(times: [0.0], values: [0.0])
private var coffee_machine_messages: [String] = []
private var end_time_last_shot = Date() - Double.infinity
private var last_brew_start_time = Date() - Double.infinity
class func requires_scale() -> Bool {
return false
}
required init(scale: ScaleInterface, coffee_machine: CoffeeMachineInterface) {
self.scale = scale
self.coffee_machine = coffee_machine
DispatchQueue.global(qos: .background).async{self.set_idle_coffee_machine_parameters()}
}
func process_coffee_machine_message(message: Data) {
if let temperature = coffee_machine.get_temperature(message: message) {
coffee_machine_temperature = temperature
}
if let runtime = coffee_machine.get_runtime(message: message) {
minutes_since_coffee_machine_power_on = Double(runtime) / 60.0
}
if !brewing && coffee_machine.has_just_started_brewing(message: message) {
self.last_brew_start_time = Date()
DispatchQueue.global(qos: .background).async{
self.brewing = true
self.start_brewing_profile()
self.brewing = false
}
}
else if coffee_machine.has_just_stopped_brewing(message: message) { brewing = false }
if recording() {
coffee_machine_messages.append(String(
format: "At %.4fs: %@",
seconds_from_brew_start(),
coffee_machine.print_to_string(message: message)
))
}
}
func process_scale_message(message: Data) {
if let weight = scale.get_weight(message: message) {
current_brew_weight = weight
if !recording() { offload_logging_data() }
else if is_valid(weight) {
brew_weight_history.values.append(weight)
brew_weight_history.times.append(seconds_from_brew_start())
}
}
}
private func recording() -> Bool {
// Record 4.0s after the end of brewing, as coffee continues to flow after pump is off.
return brewing || (Date().timeIntervalSince(end_time_last_shot) <= 4.0)
}
private func offload_logging_data() {
var history = brew_weight_history
let messages = coffee_machine_messages
brew_weight_history = TimeSeries(times: [0.0], values: [0.0])
coffee_machine_messages = []
let history_seconds = history.times.last! - history.times.first!
if history_seconds < 10 && history.times.count > 5 { return }
let history_range = history.values.max()! - history.values.min()!
if history_range < 5.0 { return }
history.times = history.times.map{$0 - history.times[0]}
for i in 0...5 { history.values[i] = 0.0 } // Zero out values before tare
save_shot_weight(history)
save_coffee_machine_messages(messages)
}
func start_brewing_profile() { // This function runs in a separate thread
precondition(false, "No brewing profile defined")
}
func set_idle_coffee_machine_parameters() { // This function runs in a separate thread
if coffee_machine.can_send() {
coffee_machine.set_boiler_temperature_target_for_brewing(
degrees_celcius: setting("boilerTemperatureSetpoint")
)
}
}
func desired_weight_was_reached() -> Bool {
return is_valid(current_brew_weight) &&
current_brew_weight > setting("desiredBrewWeight") - 1.5
}
func is_weight_positive() -> Bool {
return is_valid(current_brew_weight) && current_brew_weight > setting("nonzeroWeightThreshold")
}
func wait_for(seconds: Double, sleep_microseconds: UInt32 = 10000) {
let start_time = Date()
while brewing && -start_time.timeIntervalSinceNow < seconds {
usleep(sleep_microseconds)
}
}
func wait_until(_ condition: () -> Bool, sleep_microseconds: UInt32 = 10000) {
while brewing && !condition() {
usleep(sleep_microseconds)
}
}
func seconds_from_brew_start() -> Double {
return -last_brew_start_time.timeIntervalSinceNow
}
func stop_brewing() {
if brewing {
brewing = false
sleep(3) // Wait 3.0 seconds for finilization of brewing method(send final commands etc.)
}
}
func is_valid(_ weight: Double) -> Bool {
let delta = weight - brew_weight_history.values.last!
return weight >= 0 && weight <= 60 && delta >= -0.5 && delta <= 5
}
}
class NoActionBrewController: BrewController {
override func start_brewing_profile() {} // This function runs in a separate thread
}
class PlainBrewController: BrewController {
override class func requires_scale() -> Bool {
return true
}
override func set_idle_coffee_machine_parameters() { // This function runs in a separate thread
super.set_idle_coffee_machine_parameters()
coffee_machine.set_initial_pressure(percentage: 100)
coffee_machine.set_final_pressure(percentage: 100)
}
override func start_brewing_profile() { // This function runs in a separate thread
scale.restart()
coffee_machine.set_maximum_shot_time(seconds: 60)
if is_true("disablePIDWhenBrewing") { coffee_machine.set_boiler_temperature_target_for_brewing(degrees_celcius: 0) }
wait_until{desired_weight_was_reached()}
coffee_machine.set_maximum_shot_time(seconds: max(Int(seconds_from_brew_start()), 6)) // 6 sec min to allow enough initialization time in next shot
sleep(1) // To account for the fact that the scale timer started a bit after brewing
scale.stop_timer()
coffee_machine.set_boiler_temperature_target_for_brewing(
degrees_celcius: setting("boilerTemperatureSetpoint")
)
}
}
class ProfiledBrewController: BrewController {
override class func requires_scale() -> Bool {
return true
}
override func start_brewing_profile() { // This function runs in a separate thread
scale.restart()
coffee_machine.set_maximum_shot_time(seconds: 60)
wait_until({seconds_from_brew_start() >= 1.3})
coffee_machine.set_initial_pressure(percentage: Int(round(setting("initialPumpPower"))))
if is_true("disablePIDWhenBrewing") { coffee_machine.set_boiler_temperature_target_for_brewing(degrees_celcius: 0) }
wait_until({is_weight_positive()})
full_power_with_delayed_rampdown()
wait_until({desired_weight_was_reached()})
coffee_machine.set_maximum_shot_time(seconds: max(Int(seconds_from_brew_start()), 6)) // 6 sec min to allow enough initialization time in next shot
sleep(1) // To account for the fact that the scale timer started a bit after brewing
scale.stop_timer()
set_idle_coffee_machine_parameters()
}
func full_power_with_delayed_rampdown() {
if !brewing {return}
let duration = 33.0
let ramp_down_parameters = get_pump_parameters_for_ramp_at(
shot_time: self.seconds_from_brew_start() + setting("stayAtFullPowerDuration"),
power_start: 100.0,
power_end: 100.0 - setting("rampDownRate")*duration,
duration: duration
)
coffee_machine.set_initial_pressure(percentage: ramp_down_parameters["initial_power"]!)
wait_for(seconds: 1.0) // Avoid bursting BLE messages
coffee_machine.set_pressure_rampup_time(seconds: ramp_down_parameters["duration"]!)
coffee_machine.set_final_pressure(percentage: ramp_down_parameters["final_power"]!)
}
override func set_idle_coffee_machine_parameters() { // This function runs in a separate thread
super.set_idle_coffee_machine_parameters()
coffee_machine.set_initial_pressure(percentage: 100)
coffee_machine.set_final_pressure(percentage: 100)
let rampup_time = (100.0 - setting("initialPumpPower"))/setting("initialRampUpRate")
coffee_machine.set_pressure_rampup_time(seconds: Int(rampup_time))
coffee_machine.set_added_power_when_brewing(percent: Int(setting("addedBoilerPowerWhileBrewing")))
}
}
class BackFlushingBrewController: BrewController {
private let pause_time = 10
private let brew_time = 4
override func process_coffee_machine_message(message: Data) {
// Don't listen to the stopped brewing messages
// Many messages like this will be generated while backflushign
// because we repeatedly start and stop brewing
if !coffee_machine.has_just_stopped_brewing(message: message) {
super.process_coffee_machine_message(message: message)
}
}
override func set_idle_coffee_machine_parameters() { // This function runs in a separate thread
super.set_idle_coffee_machine_parameters()
coffee_machine.set_initial_pressure(percentage: 100)
coffee_machine.set_final_pressure(percentage: 100)
coffee_machine.set_maximum_shot_time(seconds: brew_time + 1)
}
override func start_brewing_profile() { // This function runs in a separate thread
let backflush_iterations = 6
for backflush_iteration in 1...backflush_iterations {
let current_shot_end_time = (backflush_iteration - 1)*(brew_time + pause_time) + brew_time
if brewing && backflush_iteration > 1 { // First shot slightly longer than the rest
coffee_machine.set_maximum_shot_time(seconds: current_shot_end_time)
}
if backflush_iteration < backflush_iterations {
wait_until(
{seconds_from_brew_start() > Double(current_shot_end_time + pause_time) - 0.5}
)
}
else {
wait_until({seconds_from_brew_start() > Double(current_shot_end_time)})
}
}
coffee_machine.set_maximum_shot_time(seconds: brew_time + 1)
}
}
class WarmupCoffeeController: BrewController {
override func set_idle_coffee_machine_parameters() { // This function runs in a separate thread
super.set_idle_coffee_machine_parameters()
coffee_machine.set_initial_pressure(percentage: Int(setting("warmupPumpPower")))
coffee_machine.set_final_pressure(percentage: Int(setting("warmupPumpPower")))
coffee_machine.set_added_power_when_brewing(percent: Int(setting("warmupAddedBoilerPower")))
coffee_machine.set_maximum_shot_time(seconds: Int(setting("warmupDuration")))
}
override func start_brewing_profile() {} // This function runs in a separate thread
}