-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsousvide.go
205 lines (181 loc) · 3.84 KB
/
sousvide.go
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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math"
"os"
"sync"
"time"
)
const (
InterruptDelay = 1 * time.Second
LogFile = "runlog.txt"
HistoryLength = 8192
LowpassSamples = 2
AccErrorWindow = 32
)
var StubGpio = flag.Bool("stub_gpio", false, "stub GPIO calls for testing")
var FakeTemp = flag.Bool("fake_temp", false, "use fake temperature values")
var PidFile = flag.String("pid_file", "pid.json", "file to save PID values in")
var StartEnabled = flag.Bool("enabled", false, "start with heater enabled")
var StartTarget = flag.Float64("target", 0, "initial target temperature, in C")
var Stream chan HistorySample
type SousVide struct {
Heating bool
Enabled bool
Temp Celsius
Target Celsius
History []HistorySample
Pid PidParams
Gpio GpioParams
DataLock sync.Mutex
AccError float64
MaxError float64
lastPOutput float64
lastIOutput float64
lastDOutput float64
lastControl float64
}
type HistorySample struct {
Time time.Time
Enabled bool
Heating bool
Temp float64
Target float64
AbsError float64
AccError float64
MaxError float64
Pid PidParams
POutput float64
IOutput float64
DOutput float64
COutput float64
}
type PidParams struct {
P float64
I float64
D float64
}
type GpioParams struct {
ThermFd *os.File
ThermReader *bufio.Reader
HeaterFd *os.File
Stub bool
}
type Celsius float64
func New() *SousVide {
s := new(SousVide)
s.History = make([]HistorySample, 0, HistoryLength)
return s
}
func (s *SousVide) Snapshot() HistorySample {
return HistorySample{
Time: time.Now(),
Enabled: s.Enabled,
Heating: s.Heating,
Temp: float64(s.Temp),
Target: float64(s.Target),
AbsError: float64(s.Error()),
AccError: s.AccError,
MaxError: s.MaxError,
Pid: s.Pid,
POutput: s.lastPOutput,
IOutput: s.lastIOutput,
DOutput: s.lastDOutput,
COutput: s.lastControl,
}
}
func (s *SousVide) checkpoint() {
snapshot := s.Snapshot()
if len(s.History) == HistoryLength {
for i := 0; i < HistoryLength-1; i++ {
s.History[i] = s.History[i+1]
}
s.History[len(s.History)-1] = snapshot
} else {
s.History = append(s.History, snapshot)
}
Stream <- snapshot
s.AccError = 0
s.MaxError = 0
N := len(s.History)
l := float64(0)
for i := N - 1; i >= N-AccErrorWindow-1 && i >= 0; i-- {
ae := s.History[i].AbsError
s.AccError += math.Abs(ae)
if ae < s.MaxError {
// find the most negative error
s.MaxError = ae
}
l++
}
s.MaxError *= -1
s.AccError /= l
}
func (s *SousVide) SetTarget(target Celsius) {
s.DataLock.Lock()
defer s.DataLock.Unlock()
s.Target = target
s.checkpoint()
}
func (s *SousVide) Error() Celsius {
return s.Target - s.Temp
}
func (s *SousVide) SavePid() {
fd, err := os.Create(*PidFile)
if err != nil {
fmt.Printf("could not save PID values: %v\n", err)
return
}
defer fd.Close()
b, err := json.Marshal(s.Pid)
if err != nil {
fmt.Printf("could not save PID values: %v\n", err)
return
}
fd.Write(b)
}
func (s *SousVide) LoadPid() error {
bytes, err := ioutil.ReadFile(*PidFile)
if err != nil {
fmt.Printf("could not load PID values: %v\n", err)
return err
}
err = json.Unmarshal(bytes, &s.Pid)
if err != nil {
fmt.Printf("could not load PID values: %v\n", err)
return err
}
return nil
}
func main() {
flag.Parse()
s := New()
err := s.LoadPid()
if err != nil {
s.Pid.P = 10
s.Pid.D = 20
s.SavePid()
}
s.Gpio.Stub = *StubGpio
s.Target = Celsius(*StartTarget)
s.Enabled = *StartEnabled
err = s.InitGpio()
if err != nil {
fmt.Printf("could not initialize gpio: %v\n", err)
return
}
err = s.InitTherm()
if err != nil {
fmt.Printf("could not initialize thermocouple: %v\n", err)
return
}
Stream = StartSockServer()
go StartTimerUpdateLoop()
go s.StartControlLoop()
go StartBroadcast()
s.StartServer()
}