Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*/__pycache__/
*.DS_Store
100 changes: 67 additions & 33 deletions Arduino Sketches/Main/Incuvers_Incubator/Env_CO2_COZIR.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifdef INCLUDE_CO2
#ifdef INCLUDE_CO2
#define CO2_STEP_THRESH 0.7
#define CO2_MULTIPLIER 10.0
#define CO2_DELTA_JUMP 3000
Expand All @@ -13,22 +13,24 @@ class IncuversCO2System {
private:
int pinAssignment_Valve;
int mode;

long tickTime;
long actionpoint;
long startCO2At;
long shutCO2At;
long lastCalibration;

boolean recentlyCalibrated;
boolean enabled;
boolean on;
boolean stepping;
boolean started;
boolean alarmOver;
boolean alarmUnder;

float level;
float setPoint;

IncuversSerialSensor* iSS;

void CheckJumpStatus() {
Expand All @@ -39,7 +41,7 @@ class IncuversCO2System {
if (this->on) {
if (this->tickTime >= this->shutCO2At) {
digitalWrite(pinAssignment_Valve, LOW);
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.print(F("CO2 shut "));
Serial.print((this->tickTime-this->shutCO2At));
Serial.println(F("ms late"));
Expand All @@ -51,24 +53,24 @@ class IncuversCO2System {
}

void GetCO2Reading_Cozir() {
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.println(F("GetCO2Reading_Cozir"));
#endif

String cozirString = "";
int reading;

for (int i = 0; i < 2; i++)
{
cozirString = this->iSS->GetSerialSensorReading(6, 10);
// Data in the stream looks like "Z 00400"
// The first number is the filtered value and the number after
// The first number is the filtered value and the number after
// the 'z' is the raw value. We want the filtered value

reading = GetIntegerSensorReading('Z', cozirString, -100);

if (reading > 0 && reading < 300000) {
level = (float)((CO2_MULTIPLIER * reading)/10000);
level = (float)((CO2_MULTIPLIER * reading)/10000);
#ifdef DEBUG_CO2
Serial.print(" CO2 level: ");
Serial.println(level);
Expand All @@ -78,13 +80,13 @@ class IncuversCO2System {
#ifdef DEBUG_CO2
Serial.print(F("\tCO2 sensor returned invalid read, attempt"));
Serial.println(i);
#endif
#endif
}
}
}

void CheckCO2Maintenance() {
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.print(F("CO2Maintenance()"));
#endif
if (level < setPoint && level >= 0) {
Expand All @@ -95,37 +97,37 @@ class IncuversCO2System {
delay(CO2_DELTA_STEPPING);
digitalWrite(pinAssignment_Valve, LOW);
this->actionpoint = this->tickTime;
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.println(F("\tCO2 step mode"));
#endif
} // there is no else, we need to wait for the bleedtime to expire.
} else {
// below the setpoint and the stepping threshold,
// below the setpoint and the stepping threshold,
if (!this->on && this->tickTime > (this->actionpoint + CO2_BLEEDTIME_JUMP)) {
if (this->started == false) {
this->started = true;
this->startCO2At = this->tickTime;
} else {
if (this->startCO2At + ALARM_CO2_OPEN_PERIOD < this->tickTime) {
alarmUnder = true;
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.println(F("\tCO2 under-saturation alarm thrown"));
#endif
}
}
this->on = true;
this->shutCO2At = (this->tickTime + CO2_DELTA_JUMP);
digitalWrite(pinAssignment_Valve, HIGH);
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.print(F("\tCO2 opening from "));
Serial.print(this->tickTime);
Serial.print(F(" until "));
Serial.println(this->shutCO2At);
#endif
} else {
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.println(F("\tCO2 under but bleed remaining"));
#endif
#endif
}
}
} else {
Expand All @@ -135,7 +137,7 @@ class IncuversCO2System {
if (level > (setPoint * ALARM_THRESH)) {
// Alarm
alarmOver = true;
#ifdef DEBUG_CO2
#ifdef DEBUG_CO2
Serial.println(F("\tO2 over-saturation alarm"));
#endif
}
Expand All @@ -147,17 +149,19 @@ class IncuversCO2System {
#ifdef DEBUG_CO2
Serial.println(F("CO2::Setup"));
#endif

this->enabled = false;
level = -100;
this->recentlyCalibrated = false;
this->lastCalibration=0;
// Setup Serial Interface
this->iSS = new IncuversSerialSensor();
this->iSS->Initialize(rxPin, txPin, "K 2", "Z");
this->iSS->Initialize(rxPin, txPin, "K 2", "Z");

//Setup the gas system
this->pinAssignment_Valve = relayPin;
pinMode(this->pinAssignment_Valve, OUTPUT);

#ifdef DEBUG_CO2
Serial.println(F("Enabled"));
Serial.print(F(" Rx: "));
Expand All @@ -167,14 +171,14 @@ class IncuversCO2System {
Serial.print(F(" Relay: "));
Serial.println(relayPin);
#endif

this->MakeSafeState();
}

void SetSetPoint(float tempSetPoint) {
this->setPoint = tempSetPoint;
}

void MakeSafeState() {
if (this->enabled) {
digitalWrite(this->pinAssignment_Valve, LOW); // Set LOW (solenoid closed off)
Expand All @@ -187,17 +191,23 @@ class IncuversCO2System {
void DoTick() {
if (this->enabled) {
this->tickTime = millis();

if (mode == 2 && !this->stepping) {
this->CheckJumpStatus();
this->CheckJumpStatus();
}

this->GetCO2Reading_Cozir();

if (mode == 2) {
this->CheckCO2Maintenance();
}

}
// keep recently_calibrated flag true for 5 days
if (recentlyCalibrated and abs(millis() - this->lastCalibration)>432000000) {
recentlyCalibrated = false;
}

}

float getCO2Level() {
Expand Down Expand Up @@ -237,19 +247,40 @@ class IncuversCO2System {
alarmUnder = false;
}

void CalibrateFreshAir() {
String cozirString = "";
#ifdef DEBUG_SERIAL
Serial.print(F("Calibrate CO2 with fresh-air has been invoked"));
Serial.println(F("calling GetSerialSensorReading()..."));
#endif
cozirString = this->iSS->SendStringCommand("G");
#ifdef DEBUG_SERIAL
Serial.print(F("Returned from GetSerialSensorReading()"));
Serial.print(cozirString);
Serial.print(F("should be: G 32950\r\n"));
#endif
this->recentlyCalibrated= true;
this->lastCalibration = millis();
}

boolean isRecentlyCalibrated() {
return this->recentlyCalibrated;
}


};

#else
class IncuversCO2System {
public:
void SetupCO2(int rxPin, int txPin, int relayPin) {
pinMode(relayPin, OUTPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Set LOW (solenoid closed off)
}

void SetSetPoint(float tempSetPoint) {
}

void MakeSafeState() {
}

Expand Down Expand Up @@ -280,6 +311,9 @@ class IncuversCO2System {

void ResetAlarms() {
}

void CalibrateFreshAir() {
}

};
#endif

Loading