You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.6 KiB
58 lines
1.6 KiB
2 years ago
|
// 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 = random(8, 12) / 100.0;
|
||
|
deflatePSIPerSecond = random(35, 45) / 100.0;
|
||
|
deflateOffsetMultiplier = random(3, 10) / 100.0;
|
||
|
inflateOffsetMultiplier = 1.0 - deflateOffsetMultiplier;
|
||
|
offsetMultiplier = 1.0;
|
||
|
}
|
||
|
|
||
|
void tickSimulation() {
|
||
|
static unsigned long lastTick = millis();
|
||
|
|
||
|
float pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
|
||
|
|
||
|
simulatedPressure += pressureDelta;
|
||
|
lastTick = millis();
|
||
|
}
|
||
|
|
||
|
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 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;
|
||
|
}
|
||
|
}
|