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
use super::*;
use std::time::Duration;
#[derive(Debug, Clone, PartialEq)]
pub struct IoGate {
pub id: String,
pub mapping: Option<ValueMapping>,
pub cropping: Option<Cropping>,
pub calib: Option<Calibration>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ValueMapping {
pub from: ValueBounds,
pub to: ValueBounds,
}
impl ValueMapping {
pub fn map(&self, x: f64) -> f64 {
util::map_value(x, self.from.low, self.from.high, self.to.low, self.to.high)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ValueBounds {
pub low: f64,
pub high: f64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Cropping {
pub low: Option<f64>,
pub high: Option<f64>,
}
impl Cropping {
pub fn crop(&self, x: f64) -> f64 {
util::limit(self.low, self.high, x)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Calibration {
pub a: Option<f64>,
pub b: Option<f64>,
pub c: Option<f64>,
}
impl IoGate {
pub fn new(id: String) -> Self {
IoGate {
id,
mapping: None,
cropping: None,
calib: None,
}
}
}
impl<'a> From<&'a str> for IoGate {
fn from(id: &'a str) -> Self {
IoGate::new(id.into())
}
}
#[derive(Debug, Clone)]
pub struct Loop {
pub id: String,
pub inputs: Vec<String>,
pub outputs: Vec<String>,
pub controller: ControllerConfig,
}
#[derive(Debug, Clone)]
pub struct Interval {
pub id: String,
pub duration: Duration,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Rule {
pub id: String,
pub condition: BoolExpr<Comparison>,
pub actions: Vec<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Action {
pub id: String,
pub outputs: HashMap<String, Source>,
pub memory: HashMap<String, Source>,
pub setpoints: HashMap<String, Source>,
pub controllers: HashMap<String, ControllerAction>,
pub timeouts: HashMap<String, Option<Duration>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ControllerAction {
pub reset: bool,
pub active: Option<bool>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn map_input() {
let map = ValueMapping {
from: ValueBounds {
low: 4.0,
high: 20.0,
},
to: ValueBounds {
low: 0.0,
high: 100.0,
},
};
assert_eq!(map.map(4.0), 0.0);
assert_eq!(map.map(12.0), 50.0);
assert_eq!(map.map(20.0), 100.0);
}
#[test]
fn crop_input() {
let cropping = Cropping {
low: Some(2.0),
high: Some(3.0),
};
assert_eq!(cropping.crop(1.9), 2.0);
assert_eq!(cropping.crop(-1.9), 2.0);
assert_eq!(cropping.crop(2.0), 2.0);
assert_eq!(cropping.crop(2.1), 2.1);
assert_eq!(cropping.crop(3.0), 3.0);
assert_eq!(cropping.crop(3.1), 3.0);
}
}