Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to cleanup code a bit #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 18 additions & 83 deletions coffee-machine.ino
Original file line number Diff line number Diff line change
@@ -1,52 +1,16 @@
#include <SoftwareSerial.h>
#include "coffee-pins.hh"

class LevelSensor
{
public:
LevelSensor(int pin) : sensePin_(pin) {}

bool checkState()
{
bool measured = digitalRead(sensePin_);
measured = failOpen_ ? !measured : measured;

uint64_t curTime = millis();

if (measured == curState_)
{
transitionTime = curTime;
}

if (curTime - transitionTime_ > debounceTime_)
{
transitionTime = curTime;
curState_ = measured;
}

return curState_;
}

private:
int sensePin_;
bool failOpen_;
uint64_t transitionTime_;
bool curState_;
static constexpr debounceTime_ = 250; // millis
};


enum class States { Standby, Pump, Failure };

class StateMachine
{
private:
State current{Standby};
State state_{Standby};

static constexpr uint64_t maxFillTime_;
static constexpr uint64_t minEmptyTime_;
static constexpr uint64_t maxFillTime_{24000};
uint64_t elapsedPump_;
uint64_t elapsedLastFill_;
uint64_t startPump_;

LevelSensor lowSensor_{LOW_LEVEL_PIN};
LevelSensor highSensor_{HIGH_LEVEL_PIN};
Expand All @@ -55,21 +19,19 @@ private:

bool checkFailures()
{
// how long we've been pumping water
if (elampsedPump_ > maxFillTime_)
if (elampsedPump_ - startPump_ > maxFillTime_)
return true;

// checking the sanity of our switch state

// bad state: any time the sensors above see something the sensors below do
// or the overflow sensor is on
// Sensor Sanity-Check
// **** bad state: any time the sensor above sees water while the lower
// **** sensor doesn't OR any time the overflow sensor detects water
if (overflowSensor_.checkState() || (highSensor_.checkState() && !lowSensor_.checkState()))
return true;

return false;
}
public:
void update(uint64_t dt)
void update(uint64_t ts)
{
digitalRead(LOW_LEVEL_PIN);

Expand All @@ -81,35 +43,32 @@ public:

void transition(State next)
{
// on certain transitions we enable/disable stuff
auto prev = current;
auto from = state_;

// transition functions
if (prev == States::Failure) //can't escape failure
if (from == States::Failure) //can't escape failure
return;
else if (prev == States::Standby && next == States::Pump)
else if (from == States::Standby && next == States::Pump)
{
elapsedPump_ = 0;
startPump_ = millis();
digitalWrite(WATER_PUMP_PIN, FILL);
}
else if (prev == States::Pump)
else if (from == States::Pump)
{
elapsedLastFill_ = 0;
digitalWrite(WATER_PUMP_PIN, STOP);
}
current = dst;
state_ = dst;
};

void stateop(uint64_t dt)
void stateop(uint64_t ts)
{
switch(current)
switch(state_)
{
case States::Standby:
return;
case States::Pump:
elapsedPump_ += dt;
// keep pumping,
// update dt for failsafes or something
elapsedPump_ = ts;
// update time pumped and keep pumping
return;
case States::Failure:
// blink error light
Expand All @@ -118,15 +77,6 @@ public:
};
};


// TODO: Abstract these guys away
// TODO: Make state machine with debounce time for transitions
// TODO: Make low-pass filter
// TODO: Implement project well (good)
constexpr unsigned DEBOUNCE_INTERVAL_MS 733;
constexpr unsigned MAX_FLOW_TIME 24000;
constexpr unsigned DEBOUNCE 500;

unsigned int prev_time = 0;
unsigned int curr_time = 0;
unsigned int flow_time = 0;
Expand All @@ -135,23 +85,8 @@ void setup() {
pinMode(TRIGGER_PIN,OUTPUT);
pinMode(ECHO_PIN,INPUT);
pinMode(RELAY_PIN,OUTPUT);
Serial.begin(9600);
}
void loop() {
unsigned int now = millis();
flow_time = millis() - prev_time;
Serial.print(distance);
Serial.println("cm");
if(debounce < DEBOUNCE_INTERVAL_MS)
return;
if(distance <= 10) {
//digitalWrite(RELAY_PIN,STOP);
Serial.println("STOP!");
flow_time = 0;
}
if(distance >= 22 && now-prev_time < DEBOUNCE){
//digitalWrite(RELAY_PIN,FILL);
Serial.println("FILL!");
prev_time = millis();
}
}
34 changes: 34 additions & 0 deletions coffee-pins.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,37 @@ constexpr unsigned WATER_PUMP_PIN = 5;

constexpr unsigned FILL = HIGH;
constexpr unsigned STOP = LOW;

class LevelSensor
{
public:
LevelSensor(int pin) : sensePin_(pin) {}

bool checkState()
{
bool measured = digitalRead(sensePin_);
measured = failOpen_ ? !measured : measured;

uint64_t curTime = millis();

if (measured == curState_)
{
transitionTime = curTime;
}

if (curTime - transitionTime_ > debounceTime_)
{
transitionTime = curTime;
curState_ = measured;
}

return curState_;
}

private:
int sensePin_;
bool failOpen_;
uint64_t transitionTime_{0};
bool curState_{false};
static constexpr debounceTime_ {250}; // millis
};