-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththings.cpp
213 lines (168 loc) · 5.42 KB
/
things.cpp
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
#include "things.h"
#include "controller.h"
extern Controller controller;
Thing::Thing()
{}
Thing::~Thing()
{}
Valve::Valve(int pin, bool normallyOpen)
: Thing()
, mPin(pin)
, mNormallyOpen(normallyOpen)
, mValue(normallyOpen ? OPEN : CLOSED)
{
// Valves are intially powered off, i.e their initial value depends on whether they are
// normally open or normally closed.
controller.xioPinMode(mPin, OUTPUT);
controller.xioDigitalWrite(mPin, LOW);
}
Valve::~Valve()
{}
void Valve::setValue(uint8_t value)
{
/*
setValue calls xioDigitalWrite, since all valves are connected to
the IO expander. If this weren't the case, then it would be necessary
to handle calling either digitalWrite or xioDigitalWrite based on the
component's pin.
*/
if (value != mValue) {
mValue = value;
int pinLevel = LOW;
if ((value == OPEN && !mNormallyOpen)
|| (value == CLOSED && mNormallyOpen))
pinLevel = HIGH;
Log.notice("Switching valve %s \n", value == OPEN ? "open" : "closed");
controller.xioDigitalWrite(mPin, pinLevel);
}
}
uint8_t Valve::getValue()
{
return mValue;
}
Pump::Pump(int pin)
: Thing()
, mPin(pin)
, mValue(OFF)
{
pinMode(mPin, OUTPUT);
digitalWrite(mPin, LOW);
}
Pump::~Pump()
{}
void Pump::setValue(uint8_t value)
{
if (value != mValue) {
mValue = value;
int pinLevel = (value == ON ? HIGH : LOW);
Log.notice("Switching pump %s \n", value == ON ? "on" : "off");
digitalWrite(mPin, pinLevel);
}
}
uint8_t Pump::getValue()
{
return mValue;
}
PressureController::PressureController(int setPointPin,
int measurementPin,
int setPointMaxValue,
int measurementMaxValue)
: Thing()
, mSetPointPin(setPointPin)
, mMeasurementPin(measurementPin)
, mSetPointValue(0)
, mMeasuredValue(0)
, mSetPointMaxValue(setPointMaxValue)
, mMeasurementMaxValue(measurementMaxValue)
, mInterface(PressureController::analog)
{
pinMode(setPointPin, OUTPUT);
pinMode(measurementPin, INPUT);
if (mSetPointPin == PR3_SETPOINT_PIN) {
ledcAttachPin(PR3_SETPOINT_PIN, 1);
ledcSetup(1, PWM_FREQ, PWM_RESOLUTION);
}
setValue(0);
}
PressureController::PressureController(int i2cAddress)
: Thing()
, mSetPointPin(-1)
, mMeasurementPin(-1)
, mSetPointValue(0)
, mMeasuredValue(0)
, mSetPointMaxValue(-1)
, mMeasurementMaxValue(-1)
, mInterface(PressureController::i2c)
, mI2cAddress(i2cAddress)
{
setValue(0);
}
PressureController::~PressureController()
{}
void PressureController::setValue(uint8_t value)
{
// check that value is between zero and the max value allowed
// rescale to match DAC resolution, if necessary
if (value == mSetPointValue)
return;
if (mInterface == analog) {
mSetPointValue = value;
Log.notice("Setting pressure to %d \n", value);
/*
int toWrite = value;
// Rescale the value if necessary
if (mSetPointMaxValue != UINT8_MAX)
int toWrite = double(value)/double(UINT8_MAX) * mSetPointMaxValue;
*/
double x = double(value)/double(UINT8_MAX);
if (mSetPointPin == DAC0 || mSetPointPin == DAC1) {
double y = 0.3306*pow(x, 3) - 0.428*pow(x, 2) + 1.0961*x - 0.0258;
int toWrite = std::min(mSetPointMaxValue,
std::max(0, int(round(y*mSetPointMaxValue))));
dacWrite(mSetPointPin, toWrite);
}
else {
double y = 0.3689*pow(x, 3) - 0.4627*pow(x, 2) + 1.0525*x - 0.004;
int toWrite = std::min(mSetPointMaxValue,
std::max(0, int(round(y*mSetPointMaxValue))));
ledcWrite(1, toWrite);
}
}
else if (mInterface == i2c) {
Wire.beginTransmission(mI2cAddress);
Wire.write(value);
Wire.endTransmission();
}
}
uint8_t PressureController::getValue()
{
if (mInterface == analog) {
long val = analogRead(mMeasurementPin);
val += analogRead(mMeasurementPin);
val += analogRead(mMeasurementPin);
val = round(double(val)/3.0);
/* The result of analogRead doesn't correspond linearly to the voltage applied;
see: https://github.com/espressif/esp-idf/issues/164
So a calibration curve was established by applying voltages between 0 and 5v
to the pressure controller pins, recording the result of analogRead, and fitting
a polynomial to the data.
If and when Espressif fix the analogRead() function to take into account the
non-linearity, the following fit can be replaced by a simple rescaling
*/
double x = val;
double y = -3e-12*pow(x, 3) - 7e-10*pow(x, 2) + 0.0003*x + 0.0169;
mMeasuredValue = std::min(255, std::max(0, int(round(y*UINT8_MAX))));
// Rescale value to match range of uint8_t (0 to 255)
//mMeasuredValue = double(val)/double(mMeasurementMaxValue) * UINT8_MAX;
return mMeasuredValue;
}
else {
Wire.requestFrom(mI2cAddress, 1);
uint8_t val;
// discard all but the last value received
while(Wire.available()) {
val = Wire.read();
}
return val;
}
}