-
Notifications
You must be signed in to change notification settings - Fork 7
/
device_scale.tcl
1493 lines (1002 loc) · 37.6 KB
/
device_scale.tcl
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package provide de1_device_scale 1.5
package require de1_de1 1.1
package require de1_event 1.0
package require de1_logging 1.0
package require de1_gui 1.3
###
### ::device::scale::event
###
### Events generated by scale operation
###
namespace eval ::device::scale::event::listener {
proc on_connect_add {args} {
::event::listener::_generic_add ::device::scale::event::listener::_on_connect_lists {*}$args
}
proc on_disconnect_add {args} {
::event::listener::_generic_add ::device::scale::event::listener::_on_disconnect_lists {*}$args
}
proc on_update_available_add {args} {
::event::listener::_generic_add ::device::scale::event::listener::_on_update_available_lists {*}$args
}
foreach callback_list [list \
::device::scale::event::listener::_on_connect_lists \
::device::scale::event::listener::_on_disconnect_lists \
::device::scale::event::listener::_on_update_available_lists \
] {
::event::listener::_init_callback_list $callback_list
}
}
namespace eval ::device::scale::event::apply {
proc on_connect_callbacks {args} {
::event::apply::_generic ::device::scale::event::listener::_on_connect_lists {*}$args
}
proc on_disconnect_callbacks {args} {
::event::apply::_generic ::device::scale::event::listener::_on_disconnect_lists {*}$args
}
proc on_update_available_callbacks {args} {
::event::apply::_generic ::device::scale::event::listener::_on_update_available_lists {*}$args
}
}
###
### ::device::scale
###
### State, parameters, and events associated with the scale itself
### Primary functionality includes process_weight_update` and `tare`
###
namespace eval ::device::scale {
# NB: ::scale is the identifier for a Tk proc
# as well as used by math::linearalgebra (see NAMING CONFLICT its man page)
variable autotare_states [list "Espresso" "HotWater"]
# Holdoff for tare requests in ms
# 200 ms should be enough, but
# See commit a8a61e1 Jan-18-2020:
# slightly more delay (500ms) with tare on espresso start,
# to make sure we don't have a ble command splat with decent scale
variable _tare_holdoff 200
# Level in g over which will auto-tare before flow
# also used to detect a tare request returning "close enough" to zero
variable tare_threshold 0.04
# Consider scale "lost" if no weight update within (seconds)
# Impacts ::device::scale::is_reporting
variable warn_if_no_updates_within 1.0
variable run_timer False
variable _last_weight_update_time 0
# Watchdogs for seeing scale updates
variable _watchdog_timeout 1000
variable _watchdog_update_tries 10
variable _watchdog_id ""
variable _watchdog_updates_seen False
variable _tare_last_requested 0
# If a tare is requested and "0" is seen within this limit
# will call ::device::scale::on_tare_seen
# "0" is within less than ::device::scale::tare_threshold
# (scales reporting at 0.005 g may not hit 0.000 under light vibration)
variable _tare_awaiting_zero_ms 1000
variable _tare_awaiting_zero False
variable _delayed_tare_id ""
# See also on_connect callbacks for initialization
proc init {} {
period::init
history::init
}
proc is_connected {} {
expr { [info exists ::de1(scale_device_handle)] == 1 && $::de1(scale_device_handle) != 0 }
}
proc bluetooth_address {} {
expr { $::settings(scale_bluetooth_address) }
}
# Boolean to determine if should be a "problem" that the scale isn't connected and reporting
proc expecting_present {} {
expr { [::device::scale::bluetooth_address] != "" }
}
proc is_reporting {} {
set last_update_ago [expr { ( ([clock milliseconds] / 1000.0) \
- [::device::scale::last_weight_update_time] ) }]
return [expr {$last_update_ago < $::device::scale::warn_if_no_updates_within}]
}
proc type {} {
expr { $::settings(scale_type) }
}
proc sensor_lag {{scale_type ""}} {
# From https://www.youtube.com/watch?v=SIzFhnZ32Y0 (James Hoffmann) at 4:51
#
# Hiroia 0.20
# Skale 0.33
# Felicita 0.45
# Acaia 0.64
#
# then add 1/2 average period (50 ms) for BLE delay
if { $scale_type == "" } { set scale_type [::device::scale::type] }
return [ switch -exact $scale_type {
hiroiajimmy { expr { 0.25 } }
atomaxskale { expr { 0.38 } }
decentscale { expr { 0.38 } }
felicita { expr { 0.50 } }
acaiascale { expr { 0.69 } }
default { expr { 0.38 } }
}]
}
proc is_autotare_state {{state_text "None"}} {
if { $state_text == "None" } { set state_text [::de1::state::current_state] }
expr { $state_text in $::device::scale::autotare_states }
}
proc process_weight_update {reported_weight {event_time 0}} {
if { $event_time == 0 } {set event_time [expr { [clock milliseconds] / 1000.0 }]}
::device::scale::watchdog_tickle
if { [expr { abs($reported_weight) < $::device::scale::tare_threshold }] \
&& $::device::scale::_tare_awaiting_zero \
&& [expr {[clock milliseconds] - $::device::scale::_tare_last_requested}] \
< $::device::scale::_tare_awaiting_zero_ms } {
set ::device::scale::_tare_awaiting_zero False
::device::scale::on_tare_seen
msg -DEBUG [format "Tare delay: %i ms" \
[expr {[clock milliseconds] - $::device::scale::_tare_last_requested}]]
}
# Update the internal registers
::device::scale::period::estimate_update $event_time
::device::scale::history::push_mass $reported_weight $event_time
set ::device::scale::_last_weight_update_time $event_time
# Support use case for two cups, only one on the scale
set cups [expr { $::settings(scale_stop_at_half_shot) == 1 ? 2 : 1 }]
# Collect the estimates, scaled by $cups
# Though Tcl does not have first-class functions, distinguish from local variables
# _weight is always the least processed, but scaled by $cups
# (Future scales may require s/w processing of their raw data)
set _weight [expr {[::device::scale::history::weight] * $cups}]
set _weight_time [::device::scale::history::weight_time]
set _weight_filtered [expr {[::device::scale::history::weight_filtered] * $cups}]
set _weight_filtered_time [::device::scale::history::weight_filtered_time]
set _flow [expr {[::device::scale::history::flow] * $cups}]
set _flow_time [::device::scale::history::flow_time]
set _flow_filtered [expr {[::device::scale::history::flow_filtered] * $cups}]
set _flow_filtered_time [::device::scale::history::flow_filtered_time]
# Copy to existing ::de1() elements
# NB: Previous code scaled both of these by $cups; it has been retained here
set ::de1(scale_sensor_weight) [round_to_two_digits $_weight]
set ::de1(scale_weight) [round_to_two_digits $_weight_filtered]
set ::de1(scale_weight_rate_raw) [round_to_two_digits $_flow]
set ::de1(scale_weight_rate) [round_to_two_digits $_flow_filtered]
#
# During espresso or hot-water flow
#
# NB: "is_recording" is required for SAW to work right now
if { ( [::device::scale::history::is_recording] ) } {
::device::scale::history::capture_espresso_clock_reference
# Check if SAW is needed, but only when there is flow (adding a cup shouldn't trigger SAW)
if { [::de1::state::is_flow_during_state] } {
::device::scale::saw::check_for_saw
}
# is_recording is true until the post-flow period expires
if { ! [::de1::state::is_flow_before_state] } {
::device::scale::history::update_drink_weights
}
}
#
# Preparing for espresso or hot-water flow
#
if { [::de1::state::is_flow_before_state] } {
# Was a cup was added during the warmup stage?
if { abs($reported_weight) > $::device::scale::tare_threshold \
&& [::device::scale::is_autotare_state] } {
::device::scale::tare
}
}
#
# event_dict times account for algorithm delay, but not sensor_delay
#
set event_dict \
[ dict create \
event_time [expr {[clock milliseconds] / 1000.0}] \
reported_weight $reported_weight \
reported_time $event_time \
weight $_weight \
weight_time $_weight_time \
weight_filtered $_weight_filtered \
weight_filtered_time $_weight_filtered_time \
flow $_flow \
flow_time $_flow_time \
flow_filtered $_flow_filtered \
flow_filtered_time $_flow_filtered_time \
scale_is_recording [::device::scale::history::is_recording] \
this_state [::de1::state::current_state] \
this_substate [::de1::state::current_substate] \
]
::device::scale::event::apply::on_update_available_callbacks $event_dict
}
proc tare {args} {
set since_last_tare [expr {[clock milliseconds] - $::device::scale::_tare_last_requested}]
if { $since_last_tare < $::device::scale::_tare_holdoff } {
if { "-force" in $args } {
msg -NOTICE [format "tare request -force after %d ms (%.3f)" \
$since_last_tare \
[ expr { $::device::scale::_tare_last_requested \
/ 1000.0 } ] ]
} else {
msg -NOTICE [format "tare request declined as after %d ms (%.3f)" \
$since_last_tare \
[ expr { $::device::scale::_tare_last_requested \
/ 1000.0 } ] ]
return
}
} else {
msg -INFO "tare request"
}
switch -exact $::settings(scale_type) {
atomaxskale { skale_tare }
decentscale { decentscale_tare }
acaiascale { acaia_tare $::de1(suuid_acaia_ips) $::de1(cuuid_acaia_ips_age)}
acaiapyxis { acaia_tare $::de1(suuid_acaia_pyxis) $::de1(cuuid_acaia_pyxis_cmd)}
felicita { felicita_tare }
hiroiajimmy { hiroia_tare }
}
set ::device::scale::_tare_last_requested [clock milliseconds]
set ::device::scale::_tare_awaiting_zero True
}
# Median time from tare request to zero weight ~330 ms on Skale 2 and can be over 500 ms
# Reset at least history-based estimates due to "external" change
proc on_tare_seen {args} {
::device::scale::history::on_tare_seen {*}$args
}
#
# HACK: Keep requesting scale updates until they start arriving
# every $::device::scale::_watchdog_timeout
# up to $::device::scale::_watchdog_update_tries times
#
proc watchdog_first {args} {
if { ! [::device::scale::is_connected] } {
msg -WARNING "Scale watchdog first skipping start, scale not connected"
return
}
if { $::device::scale::_watchdog_id == "" } {
msg -DEBUG "Scale watchdog for first updates starting with handle $::de1(scale_device_handle)"
} else {
after cancel $::device::scale::_watchdog_id
}
set ::device::scale::_watchdog_id \
[ after $::device::scale::_watchdog_timeout \
[list ::device::scale::_watchdog_first_fire 1] ]
}
proc _watchdog_first_fire {tries} {
if { $tries >= ${::device::scale::_watchdog_update_tries} } {
msg -ERROR "Scale updates not seen, $tries of" \
"${::device::scale::_watchdog_update_tries}, ABANDONING"
::gui::notify::scale_event abandoning_updates
} else {
msg -WARNING "Scale updates not seen, $tries of" \
"${::device::scale::_watchdog_update_tries}"
::gui::notify::scale_event retrying_updates $tries
scale_enable_weight_notifications
set ::device::scale::_watchdog_id \
[ after $::device::scale::_watchdog_timeout \
[list ::device::scale::_watchdog_first_fire [incr tries]] ]
}
}
proc watchdog_tickle {} {
if { ! $::device::scale::_watchdog_updates_seen } {
msg -DEBUG "Scale watchdog starting with handle $::de1(scale_device_handle)"
::gui::notify::scale_event scale_reporting
set ::device::scale::_watchdog_updates_seen True
}
after cancel $::device::scale::_watchdog_id
set ::device::scale::_watchdog_id [ after $::device::scale::_watchdog_timeout \
[list ::device::scale::_watchdog_fire] ]
}
proc _watchdog_fire {} {
msg -WARNING "Scale watchdog TIMEOUT"
::gui::notify::scale_event timeout_updates
set ::device::scale::_watchdog_id ""
set ::device::scale::_watchdog_updates_seen False
}
proc _watchdog_cancel {} {
if { $::device::scale::_watchdog_id != "" } {
after cancel $::device::scale::_watchdog_id
msg -INFO "Scale watchdog cancelled"
} else {
msg -DEBUG "Scale watchdog cancel - no ID to cancel"
}
}
proc last_weight_update_time {} {
expr { $::device::scale::_last_weight_update_time }
}
# Format scale-related data for .shot file
proc format_for_history {data_name} {
upvar $data_name shotfile_list
history::format_for_history shotfile_list
}
} ;# ::device::scale
###
### ::device::scale::period
###
### Estimate the actual scale reporting rate, rather than assume 10 Hz
###
namespace eval ::device::scale::period {
variable _estimate_state
array set _estimate_state {
last_arrival 0.0
new_value_weight 0.0001
moving_average 0.100
threshold 0.350
}
# The Skale 2 seems to clock out weight updates on a ~150 ms clock
# There can be two updates sent in the same time slot, and a slot
# can be skipped. This behavior leads to 300 ms being "expected"
# See https://3.basecamp.com/3671212/buckets/7351439/messages/3331033233
# for a plot of inter-sample times
# A new_value_weight of 0.0001 is a tau of ~10,000 samples, ~17 minutes.
# The high variance of Skale 2 inter-arrival times requires long tau
# to increase the accuracy of the estimate. Setting the period
# for a new scale to 100 ms mitigates "cold start" issues.
variable _scale_period_name None
proc init {args} {
variable _estimate_state
variable _scale_period_name
variable _scale_period_update_weight_name
if { ! [::device::scale::is_connected] } {
msg -DEBUG "No scale to init [join $args {, }]"
return
}
set btaddr [::device::scale::bluetooth_address]
set _scale_period_name "scale_period_${btaddr}"
set _scale_period_update_weight_name "scale_period_update_weight_${btaddr}"
if { [info exists ::settings($_scale_period_name)] \
&& [string is double -strict $::settings($_scale_period_name)] } {
set _estimate_state(moving_average) $::settings($_scale_period_name)
}
if { [info exists ::settings($_scale_period_update_weight_name)] \
&& [string is double -strict $::settings($_scale_period_update_weight_name)] } {
set _estimate_state(new_value_weight) $::settings($_scale_period_update_weight_name)
}
}
proc estimate {} {
variable _estimate_state
expr { $_estimate_state(moving_average) }
}
proc estimate_update { arrival_time } {
variable _estimate_state
variable _scale_period_name
variable _scale_period_update_weight_name
set delta [expr { $arrival_time - $_estimate_state(last_arrival) }]
if { $delta < $_estimate_state(threshold) && $delta > 0 } {
set k $_estimate_state(new_value_weight)
set _estimate_state(moving_average) \
[expr { $_estimate_state(moving_average) * (1.0 - $k) + $delta * $k }]
}
set _estimate_state(last_arrival) $arrival_time
set ::settings($_scale_period_name) $_estimate_state(moving_average)
}
} ;# ::device::scale::period
###
### ::device::scale::history
###
### Estimate the weight and mass-flow rates from previous samples
### Save the received samples and arrival time during a shot
###
namespace eval ::device::scale::history {
# Need to map algorithms to various uses
# Potentially redefine the procs on scale connect, if they differ in requirements
#
# raw scale data always written to .shot file -- DO NOT OVERRIDE weight_raw
#
# shot weight, raw weight ::de1(scale_sensor_weight)
# shot weight, slow weight_filtered ::de1(scale_weight)
# flow rate, fast flow ::de1(scale_weight_rate_raw)
# flow rate, slow flow_filtered ::de1(scale_weight_rate)
# drink weight final_weight_estimate ::de1(final_*_weight), ::settings(drink_weight)
# shot weight, SAW (::saw) weight_now (used internally)
# flow rate, SAW (::saw) flow_now (used internally)
###
### DO NOT OVERRIDE weight -- ALWAYS should record truly raw sensor weight
###
proc setup_default_estimation_mapping {args} {
proc ::device::scale::history::weight_filtered {} {
::device::scale::history::weight
}
proc ::device::scale::history::weight_filtered_time {} {
::device::scale::history::weight_time
}
proc ::device::scale::history::flow {} {
::device::scale::history::flow_fd
}
proc ::device::scale::history::flow_time {} {
::device::scale::history::flow_time_fd
}
proc ::device::scale::history::flow_filtered {} {
::device::scale::history::flow_fd
}
proc ::device::scale::history::flow_filtered_time {} {
::device::scale::history::flow_time_fd
}
proc ::device::scale::history::final_weight_estimate {} {
::device::scale::history::weight_median
}
}
# Make sure there is always some defined mapping
setup_default_estimation_mapping
proc setup_median_estimation_mapping {args} {
proc ::device::scale::history::weight_filtered {} {
::device::scale::history::weight_median
}
proc ::device::scale::history::weight_filtered_time {} {
::device::scale::history::weight_time_median
}
proc ::device::scale::history::flow {} {
::device::scale::history::flow_median
}
proc ::device::scale::history::flow_time {} {
::device::scale::history::flow_time_median
}
proc ::device::scale::history::flow_filtered {} {
::device::scale::history::flow_median
}
proc ::device::scale::history::flow_Filtered_time {} {
::device::scale::history::flow_time_median
}
proc ::device::scale::history::final_weight_estimate {} {
::device::scale::history::weight_median
}
}
# See ::device::scale::saw for other mappings
variable _scale_raw_weight
variable _scale_raw_arrival
variable scale_raw_weight_shot
variable scale_raw_arrival_shot
variable _final_weight_name
variable _espresso_start 0
variable _is_recording_flag False
variable _lslr_state
array set _lslr_state [list valid False m 0 b 0]
# Used for finite-difference, LSLR, as well as most of median estimation
# 11 samples is 10 intervals, ~1 second
proc samples_for_estimate {} {
expr { 11 }
}
# Used for median flow estimation "end points"
# on a base of samples_for_estimate
#
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
# --------- -------------- <= samples_for_median_ends
# |.....................| <= samples_for_estimate
proc samples_for_median_ends {} {
expr { 5 }
}
proc samples_for_shift_register {} {
expr { [samples_for_estimate] + [samples_for_median_ends] - 1 }
}
proc shift_in {shift_register value} {
upvar $shift_register sr
set sr [lreplace $sr 0 0]
lappend sr $value
}
# TODO: Init scale interval based on scale type (or at least exceptions)
proc init {} {
variable _scale_raw_weight [lrepeat [samples_for_shift_register] 0]
variable _scale_raw_arrival [lrepeat [samples_for_shift_register] 0]
_lslr_clear
}
proc on_tare_seen {args} {
variable _scale_raw_weight [lrepeat [samples_for_shift_register] 0]
variable _scale_raw_arrival [lrepeat [samples_for_shift_register] 0]
_lslr_clear
msg -DEBUG "::device::scale::history::on_tare_seen"
}
proc reset_shot_record {} {
variable scale_raw_weight_shot
variable scale_raw_arrival_shot
set scale_raw_weight_shot [list]
set scale_raw_arrival_shot [list]
msg -DEBUG "::device::scale::history::reset_shot_record"
}
proc push_mass { mass t } {
variable _scale_raw_weight
variable _scale_raw_arrival
variable scale_raw_weight_shot
variable scale_raw_arrival_shot
variable _final_weight_name
_lslr_clear
shift_in _scale_raw_weight $mass
shift_in _scale_raw_arrival $t
if { [is_recording] } {
lappend scale_raw_weight_shot $mass
lappend scale_raw_arrival_shot $t
}
}
###
### Various choices for estimators
###
#
# If a specific scale needs s/w filtering, do it here in weight and weight_time
#
proc weight {} {
variable _scale_raw_weight
expr { [lindex $_scale_raw_weight end] }
}
proc weight_time {} {
variable _scale_raw_arrival
expr { [lindex $_scale_raw_arrival end] }
}
#
# Finite-difference flow estimate
#
# Nominal delay flow: 5 -- (samples_for_estimate - 1) / 2
#
proc flow_fd {} {
variable _scale_raw_weight
if {[llength $_scale_raw_weight] < [samples_for_estimate]} {return 0}
set intervals [ expr { [samples_for_estimate] - 1 }]
expr { ( [lindex $_scale_raw_weight end] - [lindex $_scale_raw_weight end-$intervals] ) \
/ ( [::device::scale::period::estimate] * $intervals ) }
}
proc flow_time_fd {} {
# Center of window
variable _scale_raw_arrival
if {[llength $_scale_raw_arrival] < [samples_for_estimate]} {return 0}
set intervals [ expr { [samples_for_estimate] - 1 }]
expr { ( [lindex $_scale_raw_arrival end] + [lindex $_scale_raw_arrival end-$intervals] ) / 2.0 }
}
#
# Least squares linear regression
#
# Nominal delay mass: 0
# Nominal delay flow: 5 -- (samples_for_estimate - 1) / 2
#
proc _lslr_clear {} {
variable _lslr_state
array set _lslr_state [list valid False m 0 b 0]
}
proc _lslr_core {} {
#
# Least Squares Linear Regression
#
# m = (N * sum_xy - sum_x * sum_y) / (N * sum_xx - (sum_x)^2)
# b = (sum_y - m * sum_x) / N
#
# 1 through k
# sum of k = n(n+1)/2
# sum of k^2 = n(n+1)(2n+1)/6
#
# 0 through -(n-1)tau
# sum_x = -tau * n(n-1)/2
# sum_xx = tau^2 * (n)(n-1)(2n-1)/6
#
variable _scale_raw_weight
variable _lslr_state
# Tcl potentially leaks what should be locals over globals of the same name
# (there is no "local" declaration in Tcl either)
variable n_est [samples_for_estimate]
variable sum_n [expr { $n_est * ($n_est - 1) / 2 }]
variable sum_nn [expr { $n_est * ($n_est - 1) * (2 * $n_est - 1) / 6 }]
variable tau [::device::scale::period::estimate]
variable sum_x [expr { -$sum_n * $tau }]
variable sum_xx [expr { $sum_nn * $tau * $tau }]
variable sum_xy 0
variable sum_y 0
variable x
variable y
for {set x [expr { - ( $n_est - 1 ) }]} {$x <= 0} {incr x} {
set y [lindex $_scale_raw_weight end+${x}]
set sum_y [expr {$sum_y + $y}]
set sum_xy [expr {$sum_xy + ($x * $y)}]
}
set sum_xy [expr { $tau * $sum_xy }]
set _lslr_state(m) [expr { ($n_est * $sum_xy - $sum_x * $sum_y) / ($n_est * $sum_xx - $sum_x * $sum_x) }]
set _lslr_state(b) [expr { ($sum_y - $m * $sum_x) / $n_est }]
set _lslr_state(valid) True
}
proc weight_lslr {} {
variable _lslr_state
if { ! $_lslr_state(valid) } { _lslr_core }
return $_lslr_state(b)
}
proc flow_lslr {} {
variable _lslr_state
if { ! $_lslr_state(valid) } { _lslr_core }
return $_lslr_state(m)
}
proc flow_time_lslr {} {
# Center of window
variable _scale_raw_arrival
if {[llength $_scale_raw_arrival] < [samples_for_estimate]} {return 0}
set intervals [ expr { [samples_for_estimate] - 1 }]
expr { ( [lindex $_scale_raw_arrival end] + [lindex $_scale_raw_arrival end-$intervals] ) / 2.0 }
}
#
# Median
#
# Nominal delay mass: 5 -- (samples_for_estimate - 1) / 2
# Nominal delay flow: 7 -- (samples_for_estimate - 1) / 2 + (samples_for_median_ends - 1) / 2
#
proc median {numeric_list} {
if {[llength $numeric_list] == 0} {return 0}
set sorted [lsort -real -increasing $numeric_list]
set nlist [llength $sorted]
set half [expr { $nlist / 2 }]
if { $nlist % 2 } {
return [lindex $sorted [expr { $nlist / 2 }]]
} else {
return [expr { ( [lindex $sorted [expr { $half - 1 }]] + [lindex $sorted $half] ) / 2.0 }]
}
}
proc weight_median {} {
return [median [lrange $::device::scale::history::_scale_raw_weight 0 [expr { [samples_for_estimate] - 1 }]]]
}
proc weight_time_median {} {
# Center of window
variable _scale_raw_arrival
if {[llength $_scale_raw_arrival] < [samples_for_estimate]} {return 0}
set intervals [ expr { [samples_for_estimate] - 1 }]
expr { ( [lindex $_scale_raw_arrival end] + [lindex $_scale_raw_arrival end-$intervals] ) / 2.0 }
}
# Median flow estimates are the difference between medians
# taken at the begining and delayed by (samples_for_estimate - 1)
#
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
# --------- -------------- <= samples_for_median_ends
# |.....................| <= samples_for_estimate
proc flow_median {} {
set new_i0 0
set new_i1 [expr { [samples_for_median_ends] - 1 } ]
set old_i0 [expr { [samples_for_estimate] - 1 } ]
set old_i1 [expr { $old_i0 + [samples_for_median_ends] - 1 } ]
set new_t0 [lindex $::device::scale::history::_scale_raw_arrival end]
set new_t1 [lindex $::device::scale::history::_scale_raw_arrival end-$new_i1]
set old_t0 [lindex $::device::scale::history::_scale_raw_arrival end-$old_i0]
set old_t1 [lindex $::device::scale::history::_scale_raw_arrival end-$old_i1]
set dt [expr { (($new_t0 + $new_t1) / 2.0) - (($old_t0 + $old_t1) / 2.0) }]
# Return 0 until the shift register is filled
if { $old_t1 == 0 } { return 0 }
# Newest elements are at the end of the list, "backwards" from lindex
set new [median [lrange $::device::scale::history::_scale_raw_weight end-$new_i1 end-$new_i0]]
set old [median [lrange $::device::scale::history::_scale_raw_weight end-$old_i1 end-$old_i0]]
return [expr { ($new - $old) / $dt }]
}
proc flow_time_median {} {
set new_i0 0
set new_i1 [expr { [samples_for_median_ends] - 1 } ]
set old_i0 [expr { [samples_for_estimate] - 1 } ]
set old_i1 [expr { $old_i0 + [samples_for_median_ends] - 1 } ]
set new_t0 [lindex $::device::scale::history::_scale_raw_arrival end]
set new_t1 [lindex $::device::scale::history::_scale_raw_arrival end-$new_i1]
set old_t0 [lindex $::device::scale::history::_scale_raw_arrival end-$old_i0]
set old_t1 [lindex $::device::scale::history::_scale_raw_arrival end-$old_i1]
if { $old_t1 == 0 } { return 0 }
return [expr { ((($new_t0 + $new_t1) / 2.0) + (($old_t0 + $old_t1) / 2.0)) / 2.0 }]
}
###
### End of estimator section
###
proc update_drink_weights {} {
if { [::de1::state::is_flow_during_state] } {
set cwe [::device::scale::history::weight]
} else {