tireinflator/firmware/main/hardware.cpp

94 lines
2.3 KiB
C++
Raw Normal View History

2022-09-17 02:23:13 +00:00
// Library for managing hardware
#include <Arduino.h>
#include "hardware.h"
2022-12-15 21:56:07 +00:00
#include "XGZP.h"
2022-09-17 02:23:13 +00:00
bool simulating = false;
2022-10-07 00:26:17 +00:00
double simulatedPressure = 0.0;
double inflatePSIPerSecond = 0.0;
double deflatePSIPerSecond = 0.0;
double pressureChangeRate = 0.0;
double deflateOffsetMultiplier = 0.0;
double inflateOffsetMultiplier = 0.0;
double offsetMultiplier = 0.0;
2022-12-15 21:56:07 +00:00
float value = 1;
XGZP sensor;
void initHardware() {
Serial.println("Initializing hardware...");
if(!sensor.begin()){
Serial.printf("Error initializing Sensor\n Locking up.");
while(1);
}
}
2022-09-17 02:23:13 +00:00
void initSimulation() {
simulating = true;
2022-10-07 00:26:17 +00:00
simulatedPressure = (double) random(10, 30);
inflatePSIPerSecond = 0.0303;
deflatePSIPerSecond = 0.0958;
2022-09-17 02:23:13 +00:00
deflateOffsetMultiplier = random(3, 10) / 100.0;
2022-10-07 00:26:17 +00:00
inflateOffsetMultiplier = random(103, 160) / 100.0;
2022-09-17 02:23:13 +00:00
offsetMultiplier = 1.0;
}
void tickSimulation() {
static unsigned long lastTick = millis();
if (millis() > lastTick + 100) {
2022-10-07 00:26:17 +00:00
double pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
2022-09-17 02:23:13 +00:00
simulatedPressure += pressureDelta;
lastTick = millis();
}
2022-09-17 02:23:13 +00:00
}
2022-10-07 00:26:17 +00:00
void measurePressure(double &pressureValue) {
2022-09-17 02:23:13 +00:00
if (simulating) {
2022-10-07 00:26:17 +00:00
double adjusted = simulatedPressure + (random(-100, 100) / 100.0);
2022-09-17 02:23:13 +00:00
adjusted *= offsetMultiplier;
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
} else {
2022-12-15 21:56:07 +00:00
sensor.read(&value);
double adjusted = 1.368 * value - 6.4;
2022-09-17 02:23:13 +00:00
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
}
}
2022-10-07 00:26:17 +00:00
void resamplePressure(double &pressureValue) {
if (simulating) {
2022-10-07 00:26:17 +00:00
double adjusted = simulatedPressure + (random(-100, 100) / 100.0);
adjusted *= offsetMultiplier;
pressureValue = adjusted;
} else {
2022-12-15 21:56:07 +00:00
sensor.read(&value);
double adjusted = 1.368 * value - 6.4;
pressureValue = adjusted;
}
}
2022-09-17 02:23:13 +00:00
void setSoleniod(int solenoidState) {
if (solenoidState == SOLENOID_STOP) {
pressureChangeRate = 0.0;
offsetMultiplier = 1.0;
2022-09-18 22:09:21 +00:00
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
2022-09-17 02:23:13 +00:00
} else if (solenoidState == SOLENOID_INFLATE) {
pressureChangeRate = inflatePSIPerSecond;
offsetMultiplier = inflateOffsetMultiplier;
2022-09-18 22:09:21 +00:00
digitalWrite(RELAY1_PIN, LOW);
digitalWrite(RELAY2_PIN, HIGH);
2022-09-17 02:23:13 +00:00
} else if (solenoidState == SOLENOID_DEFLATE) {
pressureChangeRate = -deflatePSIPerSecond;
offsetMultiplier = deflatePSIPerSecond;
2022-09-18 22:09:21 +00:00
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, LOW);
2022-09-17 02:23:13 +00:00
}
}