Switch float to double

This commit is contained in:
Tanner Collin 2022-10-06 18:26:17 -06:00
parent cc123773b5
commit aba9954053
3 changed files with 54 additions and 27 deletions

View File

@ -4,21 +4,21 @@
#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;
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;
void initSimulation() {
simulating = true;
simulatedPressure = (float) random(10, 30);
simulatedPressure = (double) random(10, 30);
inflatePSIPerSecond = 0.0303;
deflatePSIPerSecond = 0.0958;
deflateOffsetMultiplier = random(3, 10) / 100.0;
inflateOffsetMultiplier = 1.0 - deflateOffsetMultiplier;
inflateOffsetMultiplier = random(103, 160) / 100.0;
offsetMultiplier = 1.0;
}
@ -26,33 +26,33 @@ void tickSimulation() {
static unsigned long lastTick = millis();
if (millis() > lastTick + 100) {
float pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
double pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
simulatedPressure += pressureDelta;
lastTick = millis();
}
}
void measurePressure(float &pressureValue) {
void measurePressure(double &pressureValue) {
if (simulating) {
float adjusted = simulatedPressure + (random(-100, 100) / 100.0);
double adjusted = simulatedPressure + (random(-100, 100) / 100.0);
adjusted *= offsetMultiplier;
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
} else {
int sensorValue = analogRead(PRESSURE_SENSOR_PIN);
float adjusted = 0.098 * sensorValue - 16.56 + 3.58;
double adjusted = 0.098 * sensorValue - 16.56 + 3.58;
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
}
}
void resamplePressure(float &pressureValue) {
void resamplePressure(double &pressureValue) {
if (simulating) {
float adjusted = simulatedPressure + (random(-100, 100) / 100.0);
double adjusted = simulatedPressure + (random(-100, 100) / 100.0);
adjusted *= offsetMultiplier;
pressureValue = adjusted;
} else {
int sensorValue = analogRead(PRESSURE_SENSOR_PIN);
float adjusted = 0.098 * sensorValue - 16.56 + 3.58;
double adjusted = 0.098 * sensorValue - 16.56 + 3.58;
pressureValue = adjusted;
}
}

View File

@ -7,17 +7,17 @@
#define SOLENOID_INFLATE 1
#define SOLENOID_DEFLATE 2
#define RELAY1_PIN 11
#define RELAY2_PIN 7
#define RELAY1_PIN 7
#define RELAY2_PIN 11
#define PRESSURE_SENSOR_PIN A0
extern float simulatedPressure;
extern double simulatedPressure;
void initSimulation();
void tickSimulation();
void measurePressure(float &pressureValue);
void resamplePressure(float &pressureValue);
void measurePressure(double &pressureValue);
void resamplePressure(double &pressureValue);
void setSoleniod(int solenoidState);
#endif

View File

@ -72,13 +72,14 @@ enum buttonStates enterButton = OPEN;
enum buttonStates downButton = OPEN;
enum screenStates screenState = BOOT_UP;
float pressureValue = 0.0;
double pressureValue = 0.0;
int pressureSetPoint = 0;
//float initialPressure = 0.0;
//float runningPressure = 0.0;
float sampledPressure = 0.0;
double initialPressure = 0.0;
//double runningPressure = 0.0;
double sampledPressure = 0.0;
double runningRateInv = 0.0;
int debounceValue(float value) {
int debounceValue(double value) {
static int prevValue = (int) value;
if (abs(prevValue - value) > 0.4) {
@ -135,6 +136,8 @@ void logData() {
#endif
Serial.print(", sampled: ");
Serial.print(sampledPressure);
Serial.print(", rate (inv): ");
Serial.print(runningRateInv);
Serial.print(", setpoint: ");
Serial.print(pressureSetPoint);
Serial.print(", state: ");
@ -151,6 +154,7 @@ void runUI() {
static unsigned long timer = millis();
static unsigned long startTime = millis();
static unsigned long stopTime = millis();
static bool isInflating = false;
static bool isDeflating = false;
@ -249,7 +253,7 @@ void runUI() {
break;
case INIT_RUN:
//initialPressure = pressureValue;
initialPressure = pressureValue;
startTime = millis();
timer = millis();
screenState = BEGIN_RUN;
@ -284,6 +288,7 @@ void runUI() {
setSoleniod(SOLENOID_STOP);
screenState = MEASURING;
timer = millis();
stopTime = millis();
}
break;
@ -292,6 +297,22 @@ void runUI() {
if (millis() >= timer + 3000) {
sampledPressure = pressureValue;
runningRateInv = (stopTime - startTime) / (sampledPressure - initialPressure);
Serial.print("stopTime: ");
Serial.print(stopTime);
Serial.print(", startTime: ");
Serial.print(startTime);
Serial.print(", sampledPressure: ");
Serial.print(sampledPressure);
Serial.print(", initialPressure: ");
Serial.print(initialPressure);
Serial.print(", rate: ");
Serial.print(runningRateInv);
Serial.println("");
initialPressure = sampledPressure;
if (isInflating && (int) sampledPressure >= pressureSetPoint) {
screenState = SAY_DONE;
nextState = PRESSURE;
@ -303,6 +324,7 @@ void runUI() {
}
timer = millis();
startTime = millis();
}
if (millis() < timer + 500) {
@ -323,14 +345,17 @@ void runUI() {
case RUNNING:
if (enterButton == PRESSED) {
setSoleniod(SOLENOID_STOP);
screenState = SAY_CANCEL;
nextState = PRESSURE;
timer = millis();
} else if (upButton == PRESSED) {
setSoleniod(SOLENOID_STOP);
screenState = SAY_CANCEL;
nextState = PRESSURE;
timer = millis();
} else if (downButton == PRESSED) {
setSoleniod(SOLENOID_STOP);
screenState = SAY_CANCEL;
nextState = PRESSURE;
timer = millis();
@ -339,9 +364,11 @@ void runUI() {
if (isInflating && millis() >= timer + 20000) {
screenState = MEASURING;
timer = millis();
stopTime = millis();
} else if (isDeflating && millis() >= timer + 10000) {
screenState = MEASURING;
timer = millis();
stopTime = millis();
}
if (isInflating) {