-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.ino
449 lines (317 loc) · 9.32 KB
/
sketch.ino
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
/*
sketch.ino
Copyright 2020 - Suraj, Iason, Arslan, Arnas
*/
#include <stdio.h>
#include <string.h>
#include <Wire.h> // for pH
// These define which pins are connected to what device on the virtual bioreactor
//
const byte lightgatePin = 2;
const byte stirrerPin = 5;
const byte heaterPin = 6;
const byte thermistorPin = A0;
const byte pHPin = A1;
// Initialise setpoints
double heatingSetpoint = 25;
int stirringSetpoint = 0;
float pHSetpoint = 5;
/* Heating subsystem constants */
const int windowSamples = 70;
float voltageMultiplier = 5.0;
float lowerTemp = 25.0;
float higherTemp = 35.0;
float setPoint;
//PID constants and vars
float pidErrorOld,pidError;
float pidP,pidI,pidD;
float pConst = 70;
float iConst = 40;
float dConst = 40;
float Time,TimeOld;
/* stirring subsystem constants */
int pwm;
double TrueRPM = 0;
unsigned long currentTimePhoto; // timing to get TrueRPM
unsigned long previousTimePhoto = 0; // subtract from currentTimePhoto
// to get time difference since last collection of data over 1000ms
volatile unsigned long ticker = 0, firstTick = 0; // ticks +1 for every Rising pin2
volatile unsigned long tickNum; // ticks in (currentTimePhoto - previousTimePhoto) time
volatile unsigned long prevTicker = 0; // prev tickNum at last collection
// PID
unsigned long currentTimePID;
unsigned long prevTimePID = 0;
int elapsedPID;
int error = 0;
int prevError = 0;
int errorIntegral;
float errorDeriv;
/* pH subsystem constants */
#define MODE1 0x00
#define PRESCALE 0xFE
#define OSCILLATORF 25000000
bool changepH = 1;
const byte PCA_addr = 0x40; //default PCA address
int delay_counter = 0;
// The PCA9685 is connected to the default I2C connections. There is no need
// to set these explicitly.
void setup() {
Serial.begin(9600);
pinMode(lightgatePin, INPUT);
pinMode(stirrerPin, OUTPUT);
pinMode(heaterPin, OUTPUT);
pinMode(thermistorPin, INPUT);
pinMode(pHPin, INPUT);
/* Heating subsystem setup */
Time = millis();
/* Stirring subsystem setup */
attachInterrupt(digitalPinToInterrupt(lightgatePin), tickCount, RISING);
/* pH subsystem setup */
Wire.begin();
write(MODE1, 0x80); //Reset PCA
float prescaleval = ((OSCILLATORF/(1000*4096.0))+0.5)-1; //Set PWM frequency to 1000Hz
uint8_t prescale = (uint8_t)prescaleval;
Wire.beginTransmission(PCA_addr);
Wire.write(MODE1);
Wire.endTransmission();
Wire.requestFrom((uint8_t)PCA_addr, (uint8_t)1);
uint8_t oldmode = Wire.read();
uint8_t newmode = (oldmode & 0x80) | 0x10;
write(MODE1, newmode);
write(PRESCALE, prescale);
write(MODE1, oldmode);
write(MODE1, oldmode | 0x80); //Reset PCA
Serial.println("Start Bioreactor Interface...");
delay(3000);
}
/* HEATING SUBSYSTEM FUNCTIONS */
float calibrateTemp(float temp)
{
temp = 1.07*(temp) + -21.3;
return temp;
}
float getVoltage()
{
int inputValue = analogRead(thermistorPin);
float inputVoltage = inputValue * voltageMultiplier;
inputVoltage /= 1024.0;
inputVoltage = (inputVoltage);
return inputVoltage;
}
float calcTemp(float inputVoltage)
{
float totalTemp = 0;
for(int index =0;index<5;index++)
{
float temperatureK = -17.7*(inputVoltage) + 70 + 273.15; //THIS NEEDS TO CHANGE
temperatureK = calibrateTemp(temperatureK);
totalTemp+=temperatureK;
}
float avgTemperatureK = totalTemp/5.0;
//Serial.print(inputVoltage*1000.0);
//Serial.println("mV ");
//Serial.print(avgTemperatureK);
//Serial.println("K");
return avgTemperatureK;
}
void manageHeater(float heatingSetPoint,float currentTemp)
{
//P
pidError = (heatingSetPoint+273.15)-currentTemp; //negative denotes we are above the setpoint and positve denotes being below the heatingSetPoint.
pidErrorOld = pidError;
pidP = pidError*pConst;
//I
if(fabs(pidError)<5)
{
float pidI = (pidError*iConst) + pidI;
}
//D
TimeOld = Time;
Time = millis();
pidD = ((pidError-pidError)/((Time-TimeOld)/1000))*dConst;
float PID = pidP+pidI+pidD;
//Serial.print("The PID is");
//Serial.println(PID);
if(PID>255) // we are below the setpoint MUST ACTUATE
{
PID =255; //prevent actuator saturation
}
else if (PID<0) //we are above the setpoint MUST TURN OFF
{
PID = 0; //prevent actuator starvation
}
analogWrite(heaterPin, PID);
}
float AverageVoltage()
{
float voltageTotal = 0.0;
for(int index =0;index<windowSamples;index++)
{
float voltageReading = getVoltage();
voltageTotal= voltageTotal+voltageReading;
delay(1);
}
float voltageAverage = voltageTotal/windowSamples;
return voltageAverage;
}
/* Stirring SUBSYSTEM FUNCITONS */
void tickCount() {
ticker++;
//firstTick++;
}
/* pH SUBSYSTEM FUNCTIONS */
float getpHData(){
int n = 10; //Moving average 15 is enough, less could be used, if speed is required
float currentpH = 0;
for(int i = 0; i < n; i++){
currentpH = currentpH + float(analogRead(pHPin));
}
currentpH = (currentpH/n - 65.7)/61.4; //from error analysis, we have to only account for two types of errors (datasheet), so only two variables used
return currentpH;
}
void write(uint8_t addr, uint8_t d){
//Moving pointer to register addr, and putting data d there
Wire.beginTransmission(PCA_addr);
Wire.write(addr);
Wire.write(d);
Wire.endTransmission();
}
void setMotorSpeed(float PWM, uint8_t n){
if (PWM == 0){
//Turning off motor by changing 4th bit to 1 in register LED0_OFF_H
write(0x09 + n*4, 0x10);
}
else {
//Read datasheet of PCA for more explanation
int onTime = PWM * 4095;
int offTime = 410 + onTime - 1;
uint8_t L = offTime;
uint8_t H = (offTime >> 8) & B00001111;
write(0x06 + 4*n, 0x99);
write(0x07 + 4*n, 0x01);
write(0x08 + 4*n, L);
write(0x09 + 4*n, H);
}
}
String heating_subsystem() {
//Serial.print("The setpoint is ");
//Serial.print(heatingSetpoint +273.15);
//Serial.print(" | The Temperature is ");
float inputVoltage = AverageVoltage();
float currentTemp = calcTemp(inputVoltage);
manageHeater(heatingSetpoint,currentTemp);
//Serial.println("=======");
return String(currentTemp-273.15);
}
String stirring_subsystem() {
currentTimePhoto = millis();
unsigned long interval = currentTimePhoto - previousTimePhoto;
if (interval >= 1000) {
if (interval > 0) {
TrueRPM = ticker * 30000.0 / interval;
previousTimePhoto = currentTimePhoto;
ticker = 0;
}
else {
TrueRPM = 0;
}
}
// PID
currentTimePID = millis();
elapsedPID = (currentTimePID - prevTimePID);
error = stirringSetpoint - TrueRPM;
errorIntegral = error * (elapsedPID / 100);
errorDeriv = -(prevError - error)/elapsedPID;
prevError = error;
prevTimePID = currentTimePID;
if (error > 10 || error < -10) {
pwm = (int)(0.008*error + 0.009*errorIntegral + 0.03*errorDeriv) + pwm;
}
if (pwm >= 255) {pwm = 255;}
if (pwm <= 0) {pwm = 0;}
analogWrite(stirrerPin, pwm);
/*
Serial.print("Current RPM: ");
Serial.print(TrueRPM);
Serial.print(" | Setpoint RPM: ");
Serial.print(stirringSetpoint);
Serial.print(" | PWM: ");
Serial.println(pwm);
*/
return String(TrueRPM);
}
String pH_subsystem() {
float currentpH = getpHData();
currentpH = int(currentpH *100.0)/100.0;
//Serial.println(currentpH);
//if (currentpH == pHSetpoint){}
if (!(currentpH - pHSetpoint < 0.1 & currentpH - pHSetpoint > -0.1) && changepH){
float A = 6.5;
float B = 2.302;
float e = 2.71828;
bool negativeWaitTime = 0;
int waitTime = pow(e, A*B)*(pow(e, -B*pHSetpoint)-pow(e, -B*currentpH));
if (waitTime < 0){
waitTime = -1* waitTime;
negativeWaitTime = 1;
}
waitTime = waitTime / 10;
//Serial.println(waitTime);
if (waitTime == 0){
waitTime = 1;
}
if (!negativeWaitTime){
setMotorSpeed(0.1, 0);
delay(waitTime);
setMotorSpeed(0,0);
}
else{
setMotorSpeed(0.1, 1);
delay(waitTime);
setMotorSpeed(0,1);
}
//delay(100);
}
return String(currentpH);
}
/*
The following ID's mean:
HR - Heating Data for python to READ
SR - Stirring Data for python to READ
PR - pH Data for python to READ
HW - Heating Data written from Python
SW - Heating Data written from Python
PW - Heating Data written from Python
*/
void readData() {
if (Serial.available() > 0) {
String line = Serial.readString();
String ID = line.substring(0,2); // First 2 characters
String data = line.substring(2);
if (ID == "HW") {
heatingSetpoint = data.toFloat();
}
else if (ID == "SW") {
stirringSetpoint = data.toInt();
}
else if (ID == "PW") {
pHSetpoint = data.toFloat();
}
}
}
void writeToPython(String heatingCurrent, String stirringCurrent, String phCurrent) {
// Bytes format: HR298.01;SR600;PR5
String bytes = "HR"+heatingCurrent+";"+"SR"+stirringCurrent+";"+"PR"+phCurrent;
Serial.println(bytes);
}
void loop() {
readData();
String heatingCurrentVal = heating_subsystem();
String stirringCurrentVal = stirring_subsystem();
String pHCurrentVal = pH_subsystem();
if (delay_counter % 10 == 0) {
writeToPython(heatingCurrentVal, stirringCurrentVal, pHCurrentVal);
}
delay_counter += 1;
delay(100);
}