Add basic hardware simulation
This commit is contained in:
parent
d9c3b6acaa
commit
ace6c671a6
57
firmware/main/hardware.cpp
Normal file
57
firmware/main/hardware.cpp
Normal file
|
@ -0,0 +1,57 @@
|
|||
// 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;
|
||||
}
|
||||
}
|
15
firmware/main/hardware.h
Normal file
15
firmware/main/hardware.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Library for managing hardware
|
||||
|
||||
#ifndef _hardware_H_
|
||||
#define _hardware_H_
|
||||
|
||||
#define SOLENOID_STOP 0
|
||||
#define SOLENOID_INFLATE 1
|
||||
#define SOLENOID_DEFLATE 2
|
||||
|
||||
void initSimulation();
|
||||
void tickSimulation();
|
||||
void measurePressure(float &pressureValue, int pin);
|
||||
void setSoleniod(int solenoidState);
|
||||
|
||||
#endif
|
|
@ -3,7 +3,9 @@
|
|||
#include <Adafruit_SSD1306.h>
|
||||
#include <Adafruit_FeatherOLED.h>
|
||||
|
||||
#include "hardware.h"
|
||||
|
||||
#define SIMULATE 1
|
||||
#define DEBUG 0
|
||||
|
||||
#define TIMEOUT_TIME 7000
|
||||
|
@ -58,6 +60,8 @@ int pressureSetPoint = 69;
|
|||
|
||||
void setup()
|
||||
{
|
||||
randomSeed(analogRead(A1));
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
oled.init();
|
||||
|
@ -65,18 +69,20 @@ void setup()
|
|||
pinMode(UP_BUTTON, INPUT_PULLUP);
|
||||
pinMode(ENTER_BUTTON, INPUT); // Has external pullup
|
||||
pinMode(DOWN_BUTTON, INPUT_PULLUP);
|
||||
|
||||
#ifdef SIMULATE
|
||||
initSimulation();
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
measurePressure();
|
||||
measurePressure(pressureValue, PRESSURE_SENSOR_PIN);
|
||||
pollButtons();
|
||||
runUI();
|
||||
}
|
||||
|
||||
void measurePressure() {
|
||||
int sensorValue = analogRead(PRESSURE_SENSOR_PIN);
|
||||
float adjusted = 0.098 * sensorValue - 16.56 + 3.58;
|
||||
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
|
||||
#ifdef SIMULATE
|
||||
tickSimulation();
|
||||
#endif
|
||||
}
|
||||
|
||||
void runUI() {
|
||||
|
@ -114,7 +120,7 @@ void runUI() {
|
|||
nextState = PRESSURE;
|
||||
timer = millis();
|
||||
} else if (enterButton == HELD) {
|
||||
;
|
||||
setSoleniod(SOLENOID_DEFLATE);
|
||||
} else if (upButton == PRESSED) {
|
||||
screenState = SET_POINT;
|
||||
pressureSetPoint = (int) pressureValue;
|
||||
|
|
Loading…
Reference in New Issue
Block a user