Add basic hardware simulation

This commit is contained in:
Tanner Collin 2022-09-16 20:23:13 -06:00
parent d9c3b6acaa
commit ace6c671a6
3 changed files with 85 additions and 7 deletions

View 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
View 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

View File

@ -3,7 +3,9 @@
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include <Adafruit_FeatherOLED.h> #include <Adafruit_FeatherOLED.h>
#include "hardware.h"
#define SIMULATE 1
#define DEBUG 0 #define DEBUG 0
#define TIMEOUT_TIME 7000 #define TIMEOUT_TIME 7000
@ -58,6 +60,8 @@ int pressureSetPoint = 69;
void setup() void setup()
{ {
randomSeed(analogRead(A1));
Serial.begin(115200); Serial.begin(115200);
oled.init(); oled.init();
@ -65,18 +69,20 @@ void setup()
pinMode(UP_BUTTON, INPUT_PULLUP); pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(ENTER_BUTTON, INPUT); // Has external pullup pinMode(ENTER_BUTTON, INPUT); // Has external pullup
pinMode(DOWN_BUTTON, INPUT_PULLUP); pinMode(DOWN_BUTTON, INPUT_PULLUP);
#ifdef SIMULATE
initSimulation();
#endif
} }
void loop() { void loop() {
measurePressure(); measurePressure(pressureValue, PRESSURE_SENSOR_PIN);
pollButtons(); pollButtons();
runUI(); runUI();
}
void measurePressure() { #ifdef SIMULATE
int sensorValue = analogRead(PRESSURE_SENSOR_PIN); tickSimulation();
float adjusted = 0.098 * sensorValue - 16.56 + 3.58; #endif
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
} }
void runUI() { void runUI() {
@ -114,7 +120,7 @@ void runUI() {
nextState = PRESSURE; nextState = PRESSURE;
timer = millis(); timer = millis();
} else if (enterButton == HELD) { } else if (enterButton == HELD) {
; setSoleniod(SOLENOID_DEFLATE);
} else if (upButton == PRESSED) { } else if (upButton == PRESSED) {
screenState = SET_POINT; screenState = SET_POINT;
pressureSetPoint = (int) pressureValue; pressureSetPoint = (int) pressureValue;