tireinflator/firmware/main/hardware.cpp

75 lines
2.0 KiB
C++

// Library for managing hardware
#include <Arduino.h>
#include "hardware.h"
bool simulating = false;
float simulatedPressure = 0.0;
float inflatePSIPerSecond = 0.0;
float deflatePSIPerSecond = 0.0;
float pressureChangeRate = 0.0;
float deflateOffsetMultiplier = 0.0;
float inflateOffsetMultiplier = 0.0;
float offsetMultiplier = 0.0;
void initSimulation() {
simulating = true;
simulatedPressure = (float) random(10, 30);
inflatePSIPerSecond = 0.0303;
deflatePSIPerSecond = 0.0958;
deflateOffsetMultiplier = random(3, 10) / 100.0;
inflateOffsetMultiplier = 1.0 - deflateOffsetMultiplier;
offsetMultiplier = 1.0;
}
void tickSimulation() {
static unsigned long lastTick = millis();
if (millis() > lastTick + 100) {
float pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
simulatedPressure += pressureDelta;
lastTick = millis();
Serial.print("Simulated pressure: ");
Serial.println(simulatedPressure);
}
}
void measurePressure(float &pressureValue, int pin) {
if (simulating) {
float adjusted = simulatedPressure + (random(-100, 100) / 100.0);
adjusted *= offsetMultiplier;
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
} else {
int sensorValue = analogRead(pin);
float adjusted = 0.098 * sensorValue - 16.56 + 3.58;
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
}
}
void resamplePressure(float &pressureValue, int pin) {
if (simulating) {
float adjusted = simulatedPressure + (random(-100, 100) / 100.0);
adjusted *= offsetMultiplier;
pressureValue = adjusted;
} else {
int sensorValue = analogRead(pin);
float adjusted = 0.098 * sensorValue - 16.56 + 3.58;
pressureValue = adjusted;
}
}
void setSoleniod(int solenoidState) {
if (solenoidState == SOLENOID_STOP) {
pressureChangeRate = 0.0;
offsetMultiplier = 1.0;
} else if (solenoidState == SOLENOID_INFLATE) {
pressureChangeRate = inflatePSIPerSecond;
offsetMultiplier = inflateOffsetMultiplier;
} else if (solenoidState == SOLENOID_DEFLATE) {
pressureChangeRate = -deflatePSIPerSecond;
offsetMultiplier = deflatePSIPerSecond;
}
}