Complete tire inflation / deflation state machine
This commit is contained in:
parent
ace6c671a6
commit
0755bcfe40
|
@ -15,8 +15,8 @@ float offsetMultiplier = 0.0;
|
||||||
void initSimulation() {
|
void initSimulation() {
|
||||||
simulating = true;
|
simulating = true;
|
||||||
simulatedPressure = (float) random(10, 30);
|
simulatedPressure = (float) random(10, 30);
|
||||||
inflatePSIPerSecond = random(8, 12) / 100.0;
|
inflatePSIPerSecond = 0.0303;
|
||||||
deflatePSIPerSecond = random(35, 45) / 100.0;
|
deflatePSIPerSecond = 0.0958;
|
||||||
deflateOffsetMultiplier = random(3, 10) / 100.0;
|
deflateOffsetMultiplier = random(3, 10) / 100.0;
|
||||||
inflateOffsetMultiplier = 1.0 - deflateOffsetMultiplier;
|
inflateOffsetMultiplier = 1.0 - deflateOffsetMultiplier;
|
||||||
offsetMultiplier = 1.0;
|
offsetMultiplier = 1.0;
|
||||||
|
@ -25,10 +25,15 @@ void initSimulation() {
|
||||||
void tickSimulation() {
|
void tickSimulation() {
|
||||||
static unsigned long lastTick = millis();
|
static unsigned long lastTick = millis();
|
||||||
|
|
||||||
|
if (millis() > lastTick + 100) {
|
||||||
float pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
|
float pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
|
||||||
|
|
||||||
simulatedPressure += pressureDelta;
|
simulatedPressure += pressureDelta;
|
||||||
lastTick = millis();
|
lastTick = millis();
|
||||||
|
|
||||||
|
Serial.print("Simulated pressure: ");
|
||||||
|
Serial.println(simulatedPressure);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void measurePressure(float &pressureValue, int pin) {
|
void measurePressure(float &pressureValue, int pin) {
|
||||||
|
@ -43,6 +48,18 @@ void measurePressure(float &pressureValue, int pin) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
void setSoleniod(int solenoidState) {
|
||||||
if (solenoidState == SOLENOID_STOP) {
|
if (solenoidState == SOLENOID_STOP) {
|
||||||
pressureChangeRate = 0.0;
|
pressureChangeRate = 0.0;
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
void initSimulation();
|
void initSimulation();
|
||||||
void tickSimulation();
|
void tickSimulation();
|
||||||
void measurePressure(float &pressureValue, int pin);
|
void measurePressure(float &pressureValue, int pin);
|
||||||
|
void resamplePressure(float &pressureValue, int pin);
|
||||||
void setSoleniod(int solenoidState);
|
void setSoleniod(int solenoidState);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -42,8 +42,11 @@ enum screenStates {
|
||||||
BOOT_UP,
|
BOOT_UP,
|
||||||
PRESSURE,
|
PRESSURE,
|
||||||
SET_POINT,
|
SET_POINT,
|
||||||
|
INIT_RUN,
|
||||||
BEGIN_RUN,
|
BEGIN_RUN,
|
||||||
|
MEASURING,
|
||||||
RUNNING,
|
RUNNING,
|
||||||
|
SAY_DONE,
|
||||||
SAY_CANCEL,
|
SAY_CANCEL,
|
||||||
SAY_HOLD,
|
SAY_HOLD,
|
||||||
SAY_TIMEOUT,
|
SAY_TIMEOUT,
|
||||||
|
@ -56,7 +59,16 @@ enum buttonStates enterButton = OPEN;
|
||||||
enum buttonStates downButton = OPEN;
|
enum buttonStates downButton = OPEN;
|
||||||
|
|
||||||
float pressureValue = 0.0;
|
float pressureValue = 0.0;
|
||||||
int pressureSetPoint = 69;
|
|
||||||
|
int debounceValue(float value) {
|
||||||
|
static int prevValue = (int) value;
|
||||||
|
|
||||||
|
if (abs(prevValue - value) > 0.4) {
|
||||||
|
prevValue = (int) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return prevValue;
|
||||||
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
|
@ -73,6 +85,8 @@ void setup()
|
||||||
#ifdef SIMULATE
|
#ifdef SIMULATE
|
||||||
initSimulation();
|
initSimulation();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
resamplePressure(pressureValue, PRESSURE_SENSOR_PIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -91,12 +105,27 @@ void runUI() {
|
||||||
|
|
||||||
static unsigned long timer = millis();
|
static unsigned long timer = millis();
|
||||||
|
|
||||||
static unsigned long runTime = millis();
|
static int pressureSetPoint = 0;
|
||||||
|
static unsigned long startTime = millis();
|
||||||
|
//static float initialPressure = 0.0;
|
||||||
|
//static float runningPressure = 0.0;
|
||||||
|
static float sampledPressure = 0.0;
|
||||||
|
static bool isInflating = false;
|
||||||
|
static bool isDeflating = false;
|
||||||
|
|
||||||
int num_dots = 0;
|
int num_dots = 0;
|
||||||
|
int debouncedPressureValue = debounceValue(pressureValue);
|
||||||
|
|
||||||
oled.clearDisplay();
|
oled.clearDisplay();
|
||||||
|
|
||||||
|
Serial.print("pressure: ");
|
||||||
|
Serial.print(pressureValue);
|
||||||
|
Serial.print(", sampled: ");
|
||||||
|
Serial.print(sampledPressure);
|
||||||
|
Serial.print(", setpoint: ");
|
||||||
|
Serial.print(pressureSetPoint);
|
||||||
|
Serial.println("");
|
||||||
|
|
||||||
switch (screenState) {
|
switch (screenState) {
|
||||||
case BOOT_UP:
|
case BOOT_UP:
|
||||||
if (millis() >= timer + 2000) {
|
if (millis() >= timer + 2000) {
|
||||||
|
@ -120,28 +149,28 @@ void runUI() {
|
||||||
nextState = PRESSURE;
|
nextState = PRESSURE;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
} else if (enterButton == HELD) {
|
} else if (enterButton == HELD) {
|
||||||
setSoleniod(SOLENOID_DEFLATE);
|
; // settings?
|
||||||
} else if (upButton == PRESSED) {
|
} else if (upButton == PRESSED) {
|
||||||
screenState = SET_POINT;
|
screenState = SET_POINT;
|
||||||
pressureSetPoint = (int) pressureValue;
|
pressureSetPoint = debouncedPressureValue+1;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
} else if (downButton == PRESSED) {
|
} else if (downButton == PRESSED) {
|
||||||
screenState = SET_POINT;
|
screenState = SET_POINT;
|
||||||
pressureSetPoint = (int) pressureValue;
|
pressureSetPoint = debouncedPressureValue-1;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
} else if (upButton == HELD) {
|
} else if (upButton == HELD) {
|
||||||
screenState = SET_POINT;
|
screenState = SET_POINT;
|
||||||
pressureSetPoint = (int) pressureValue;
|
pressureSetPoint = debouncedPressureValue+1;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
} else if (downButton == HELD) {
|
} else if (downButton == HELD) {
|
||||||
screenState = SET_POINT;
|
screenState = SET_POINT;
|
||||||
pressureSetPoint = (int) pressureValue;
|
pressureSetPoint = debouncedPressureValue-1;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
oled.setCursor(0,0);
|
oled.setCursor(0,0);
|
||||||
oled.setTextSize(3);
|
oled.setTextSize(3);
|
||||||
oled.print((int) pressureValue);
|
oled.print(debouncedPressureValue);
|
||||||
oled.print(" PSI");
|
oled.print(" PSI");
|
||||||
|
|
||||||
oled.display();
|
oled.display();
|
||||||
|
@ -153,8 +182,8 @@ void runUI() {
|
||||||
nextState = SET_POINT;
|
nextState = SET_POINT;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
} else if (enterButton == HELD) {
|
} else if (enterButton == HELD) {
|
||||||
screenState = BEGIN_RUN;
|
|
||||||
timer = millis();
|
timer = millis();
|
||||||
|
screenState = INIT_RUN;
|
||||||
} else if (upButton == PRESSED) {
|
} else if (upButton == PRESSED) {
|
||||||
timer = millis();
|
timer = millis();
|
||||||
pressureSetPoint++;
|
pressureSetPoint++;
|
||||||
|
@ -186,28 +215,79 @@ void runUI() {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BEGIN_RUN:
|
case INIT_RUN:
|
||||||
if (millis() >= timer + 2000) {
|
//initialPressure = pressureValue;
|
||||||
screenState = RUNNING;
|
startTime = millis();
|
||||||
runTime = millis();
|
timer = millis();
|
||||||
}
|
screenState = BEGIN_RUN;
|
||||||
|
|
||||||
oled.setCursor(0,0);
|
oled.setCursor(0,0);
|
||||||
oled.setTextSize(1);
|
oled.setTextSize(1);
|
||||||
oled.println("");
|
oled.println("");
|
||||||
oled.setTextSize(2);
|
oled.setTextSize(2);
|
||||||
if (pressureSetPoint > (int) pressureValue) {
|
if (pressureSetPoint > debouncedPressureValue) {
|
||||||
oled.print("INFLATING");
|
oled.print("INFLATING");
|
||||||
} else if (pressureSetPoint < (int) pressureValue) {
|
setSoleniod(SOLENOID_INFLATE);
|
||||||
|
isInflating = true;
|
||||||
|
isDeflating = false;
|
||||||
|
} else if (pressureSetPoint < debouncedPressureValue) {
|
||||||
oled.print("DEFLATING");
|
oled.print("DEFLATING");
|
||||||
|
setSoleniod(SOLENOID_DEFLATE);
|
||||||
|
isDeflating = true;
|
||||||
|
isInflating = false;
|
||||||
} else {
|
} else {
|
||||||
oled.print("DONE");
|
setSoleniod(SOLENOID_STOP);
|
||||||
|
screenState = SAY_DONE;
|
||||||
|
nextState = PRESSURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
oled.display();
|
oled.display();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case BEGIN_RUN:
|
||||||
|
if (millis() >= timer + 5000) {
|
||||||
|
//runningPressure = pressureValue;
|
||||||
|
setSoleniod(SOLENOID_STOP);
|
||||||
|
screenState = MEASURING;
|
||||||
|
timer = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MEASURING:
|
||||||
|
if (millis() >= timer + 3000) {
|
||||||
|
sampledPressure = pressureValue;
|
||||||
|
|
||||||
|
if (isInflating && (int) sampledPressure >= pressureSetPoint) {
|
||||||
|
screenState = SAY_DONE;
|
||||||
|
nextState = PRESSURE;
|
||||||
|
} else if (isDeflating && (int) sampledPressure <= pressureSetPoint) {
|
||||||
|
screenState = SAY_DONE;
|
||||||
|
nextState = PRESSURE;
|
||||||
|
} else {
|
||||||
|
screenState = RUNNING;
|
||||||
|
}
|
||||||
|
|
||||||
|
timer = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (millis() < timer + 500) {
|
||||||
|
// wait for solenoids to settle before averaging
|
||||||
|
resamplePressure(pressureValue, PRESSURE_SENSOR_PIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
setSoleniod(SOLENOID_STOP);
|
||||||
|
|
||||||
|
oled.setCursor(0,0);
|
||||||
|
oled.setTextSize(1);
|
||||||
|
oled.println("");
|
||||||
|
oled.setTextSize(2);
|
||||||
|
oled.print("MEASURING");
|
||||||
|
oled.display();
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case RUNNING:
|
case RUNNING:
|
||||||
if (enterButton == PRESSED) {
|
if (enterButton == PRESSED) {
|
||||||
screenState = SAY_CANCEL;
|
screenState = SAY_CANCEL;
|
||||||
|
@ -223,21 +303,32 @@ void runUI() {
|
||||||
timer = millis();
|
timer = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isInflating && millis() >= timer + 20000) {
|
||||||
|
screenState = MEASURING;
|
||||||
|
timer = millis();
|
||||||
|
} else if (isDeflating && millis() >= timer + 10000) {
|
||||||
|
screenState = MEASURING;
|
||||||
|
timer = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isInflating) {
|
||||||
|
setSoleniod(SOLENOID_INFLATE);
|
||||||
|
} else if (isDeflating) {
|
||||||
|
setSoleniod(SOLENOID_DEFLATE);
|
||||||
|
}
|
||||||
|
|
||||||
oled.setCursor(0,0);
|
oled.setCursor(0,0);
|
||||||
oled.setTextSize(3);
|
oled.setTextSize(3);
|
||||||
oled.print((int) pressureValue);
|
oled.print((int) sampledPressure);
|
||||||
oled.println(" PSI");
|
oled.println(" PSI");
|
||||||
|
|
||||||
oled.setTextSize(1);
|
oled.setTextSize(1);
|
||||||
if (pressureSetPoint > (int) pressureValue) {
|
if (isInflating) {
|
||||||
oled.print("INFLATING");
|
oled.print("INFLATING");
|
||||||
} else if (pressureSetPoint < (int) pressureValue) {
|
} else if (isDeflating) {
|
||||||
oled.print("DEFLATING");
|
oled.print("DEFLATING");
|
||||||
} else {
|
|
||||||
oled.print("DONE");
|
|
||||||
}
|
}
|
||||||
|
num_dots = (int) ((millis() - startTime) / 400) % 4;
|
||||||
num_dots = (int) ((millis() - runTime) / 400) % 4;
|
|
||||||
for (int i = 0; i < num_dots; i++) {
|
for (int i = 0; i < num_dots; i++) {
|
||||||
oled.print(".");
|
oled.print(".");
|
||||||
}
|
}
|
||||||
|
@ -246,6 +337,22 @@ void runUI() {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
case SAY_DONE:
|
||||||
|
if (millis() >= timer + 3000) {
|
||||||
|
screenState = nextState;
|
||||||
|
timer = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
oled.setCursor(0,0);
|
||||||
|
oled.setTextSize(3);
|
||||||
|
oled.print("DONE");
|
||||||
|
|
||||||
|
oled.display();
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case SAY_CANCEL:
|
case SAY_CANCEL:
|
||||||
if (millis() >= timer + 1000) {
|
if (millis() >= timer + 1000) {
|
||||||
screenState = nextState;
|
screenState = nextState;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user