Skip to content

Commit

Permalink
Split preflight into its own task
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ-Koenig committed Apr 26, 2024
1 parent 43da854 commit 78c89c1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Software/src/ossm/Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct Error {};

// Definitions to make the table easier to read.
static auto buttonPress = sml::event<ButtonPress>;
static auto doublePress = sml::event<ButtonPress>;
static auto doublePress = sml::event<DoublePress>;
static auto done = sml::event<Done>;
static auto error = sml::event<Error>;

Expand Down
87 changes: 76 additions & 11 deletions Software/src/ossm/OSSM.PlayControls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ void OSSM::drawPlayControlsTask(void *pvParameters) {

auto isInPreflight = [](OSSM *ossm) {
// Add your preflight checks states here.
return ossm->sm->is("simplePenetration.preflight"_s);
return ossm->sm->is("simplePenetration.preflight"_s) ||
ossm->sm->is("strokeEngine.preflight"_s);
};

do {
speedPercentage =
getAnalogAveragePercent(SampleOnPin{Pins::Remote::speedPotPin, 50});
if (speedPercentage < 0.5) {
if (speedPercentage < Config::Advanced::commandDeadZonePercentage) {
ossm->sm->process_event(Done{});
break;
};
Expand Down Expand Up @@ -67,7 +68,9 @@ void OSSM::drawPlayControlsTask(void *pvParameters) {
auto isInCorrectState = [](OSSM *ossm) {
// Add any states that you want to support here.
return ossm->sm->is("simplePenetration"_s) ||
ossm->sm->is("simplePenetration.idle"_s);
ossm->sm->is("simplePenetration.idle"_s) ||
ossm->sm->is("strokeEngine"_s) ||
ossm->sm->is("strokeEngine.idle"_s);
};

// Prepare the encoder
Expand All @@ -88,7 +91,7 @@ void OSSM::drawPlayControlsTask(void *pvParameters) {
short lh2 = 37;
short lh3 = 56;
short lh4 = 64;
static int speedKnobPercent = 0;
static float speedKnobPercent = 0;

// record session start time rounded to the nearest second
ossm->sessionStartTime = floor(millis() / 1000);
Expand All @@ -98,10 +101,9 @@ void OSSM::drawPlayControlsTask(void *pvParameters) {
bool valueChanged = false;

while (isInCorrectState(ossm)) {
speedKnobPercent = getAnalogAveragePercent(SampleOnPin{
Pins::Remote::speedPotPin, 50});
speedPercentage = 0.3 * speedKnobPercent +
0.7 * speedPercentage;
speedKnobPercent =
getAnalogAveragePercent(SampleOnPin{Pins::Remote::speedPotPin, 50});
speedPercentage = 0.3 * speedKnobPercent + 0.7 * speedPercentage;
strokePercentage = ossm->encoder.readEncoder();
currentTime = floor(millis() / 1000);

Expand Down Expand Up @@ -195,10 +197,73 @@ void OSSM::drawPlayControlsTask(void *pvParameters) {
ossm->encoder.disableAcceleration();

vTaskDelete(nullptr);
}
};


void OSSM::drawPreflightTask(void *pvParameters) {
// parse ossm from the parameters
OSSM *ossm = (OSSM *)pvParameters;
auto menuString = menuStrings[ossm->menuOption];
float speedPercentage;

ossm->speedPercentage = 0;
ossm->strokePercentage = 0;

// Set the stepper to the home position
ossm->stepper.setAccelerationInMillimetersPerSecondPerSecond(1000);
ossm->stepper.setAccelerationInMillimetersPerSecondPerSecond(10000);
ossm->stepper.setTargetPositionInMillimeters(0);

/**
* /////////////////////////////////////////////
* //// Safely Block High Speeds on Startup ///
* /////////////////////////////////////////////
*
* This is a safety feature to prevent the user from accidentally beginning
* a session at max speed. After the user decreases the speed to 0.5% or
* less, the state machine will be allowed to continue.
*/

auto isInPreflight = [](OSSM *ossm) {
// Add your preflight checks states here.
return ossm->sm->is("simplePenetration.preflight"_s) ||
ossm->sm->is("strokeEngine.preflight"_s);
};

do {
speedPercentage =
getAnalogAveragePercent(SampleOnPin{Pins::Remote::speedPotPin, 50});
if (speedPercentage < Config::Advanced::commandDeadZonePercentage) {
ossm->sm->process_event(Done{});
break;
};

ossm->display.clearBuffer();
drawStr::title(menuString);
String speedString = UserConfig::language.Speed + ": " +
String((int)speedPercentage) + "%";
drawStr::centered(25, speedString);
drawStr::multiLine(0, 40, UserConfig::language.SpeedWarning);

ossm->display.sendBuffer();
vTaskDelay(100);
} while (isInPreflight(ossm));

vTaskDelete(nullptr);
};


void OSSM::drawPlayControls() {
int stackSize = 3 * configMINIMAL_STACK_SIZE;
xTaskCreate(drawPlayControlsTask, "drawPlayControlsTask", stackSize,
this, 1, &drawPlayControlsTaskH);
xTaskCreate(drawPlayControlsTask, "drawPlayControlsTask", stackSize, this,
1, &drawPlayControlsTaskH);
}


void OSSM::drawPreflight() {
int stackSize = 3 * configMINIMAL_STACK_SIZE;
xTaskCreate(drawPreflightTask, "drawPlayControlsTask", stackSize, this,
1, &drawPreflightTaskH);
}


11 changes: 5 additions & 6 deletions Software/src/ossm/OSSM.StrokeEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "extensions/u8g2Extensions.h"
#include "utils/StrokeEngineHelper.h"

void OSSM::startStrokeEngine() {
[[noreturn]] void OSSM::startStrokeEngine() {
display.clearBuffer();

drawStr::title(UserConfig::language.StrokeEngine);
Expand All @@ -17,16 +17,15 @@ void OSSM::startStrokeEngine() {
String strokerPatternName = "";
static int strokePattern = 0;
static int strokePatternCount = 0;
static int depthPercentage = 50;
static int strokePercentage = 50;
static int speedPercentage = 50;
static int sensationPercentage = 50;
static float depthPercentage = 50;

static float sensationPercentage = 50;
static int encoderButtonPresses = 0;
static long lastEncoderButtonPressMillis = 0;
static bool modeChanged = false;

float lastSpeedPercentage = speedPercentage;
float lastStrokePercentage = strokePercentage;
long lastStrokePercentage = strokePercentage;
float lastDepthPercentage = depthPercentage;
float lastSensationPercentage = sensationPercentage;
int lastEncoderButtonPresses = encoderButtonPresses;
Expand Down
44 changes: 32 additions & 12 deletions Software/src/ossm/OSSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
#include "U8g2lib.h"
#include "WiFiManager.h"
#include "boost/sml.hpp"
#include "constants/Config.h"
#include "constants/Menu.h"
#include "constants/Pins.h"
#include "services/tasks.h"
#include "utils/RecusiveMutex.h"
#include "utils/StateLogger.h"
#include "utils/analog.h"
#include "utils/update.h"

namespace sml = boost::sml;

class OSSM {
private:
void drawUpdate();
/**
* ///////////////////////////////////////////
* ////
Expand Down Expand Up @@ -66,6 +68,7 @@ class OSSM {
o.startHoming();
};
auto drawPlayControls = [](OSSM &o) { o.drawPlayControls(); };
auto drawPreflight = [](OSSM &o) { o.drawPreflight(); };
auto startSimplePenetration = [](OSSM &o) {
o.startSimplePenetration();
};
Expand All @@ -89,11 +92,11 @@ class OSSM {
// immediately.
o.wm.setConfigPortalTimeout(1);
o.wm.setConnectTimeout(1);
o.wm.setConnectRetries(1);
o.wm.setConnectRetries(2);
o.wm.setConfigPortalBlocking(false);
// if (!o.wm.autoConnect()) {
// ESP_LOGD("UTILS", "failed to connect and hit timeout");
// }
if (!o.wm.autoConnect()) {
ESP_LOGD("UTILS", "failed to connect and hit timeout");
}
ESP_LOGD("UTILS", "exiting autoconnect");
};

Expand All @@ -106,6 +109,12 @@ class OSSM {
return [option](OSSM &o) { return o.menuOption == option; };
};

auto isPreflightSafe = [](OSSM &o) {
return getAnalogAveragePercent(
{Pins::Remote::speedPotPin, 50}) <
Config::Advanced::commandDeadZonePercentage;
};

return make_transition_table(
// clang-format off
*"idle"_s + done / drawHello = "homing"_s,
Expand All @@ -125,12 +134,15 @@ class OSSM {
"menu.idle"_s + buttonPress[isOption(Menu::Help)] = "help"_s,
"menu.idle"_s + buttonPress[(isOption(Menu::Restart))] = "restart"_s,

"simplePenetration"_s / drawPlayControls = "simplePenetration.preflight"_s,
"simplePenetration.preflight"_s + done / startSimplePenetration = "simplePenetration.idle"_s,
"simplePenetration"_s [isPreflightSafe] / (drawPlayControls, startSimplePenetration) = "simplePenetration.idle"_s,
"simplePenetration"_s / drawPreflight = "simplePenetration.preflight"_s,
"simplePenetration.preflight"_s + done / (drawPlayControls, startSimplePenetration) = "simplePenetration.idle"_s,
"simplePenetration.idle"_s + doublePress / emergencyStop = "menu"_s,

"strokeEngine"_s + on_entry<_> / startStrokeEngine,
"strokeEngine"_s + buttonPress = "menu"_s,
"strokeEngine"_s [isPreflightSafe] / (drawPlayControls, startStrokeEngine) = "strokeEngine.idle"_s,
"strokeEngine"_s / drawPreflight = "strokeEngine.preflight"_s,
"strokeEngine.preflight"_s + done / (drawPlayControls, startStrokeEngine) = "strokeEngine.idle"_s,
"strokeEngine.idle"_s + doublePress / emergencyStop = "menu"_s,

"update"_s [isOnline] / drawUpdate = "update.checking"_s,
"update"_s = "wifi"_s,
Expand All @@ -150,6 +162,7 @@ class OSSM {
"error.help"_s + buttonPress / restart = X,

"restart"_s / restart = X);

// clang-format on
}
};
Expand Down Expand Up @@ -227,12 +240,22 @@ class OSSM {
*/
static void startHomingTask(void *pvParameters);

[[noreturn]] void startStrokeEngine();

static void drawHelloTask(void *pvParameters);

static void drawMenuTask(void *pvParameters);

static void drawPlayControlsTask(void *pvParameters);

void drawUpdate();
void drawNoUpdate();

void drawUpdating();

void drawPreflight();
static void drawPreflightTask(void *pvParameters);

static void startSimplePenetrationTask(void *pvParameters);

public:
Expand All @@ -245,9 +268,6 @@ class OSSM {
sm = nullptr; // The state machine

WiFiManager wm;
void startStrokeEngine();
void drawNoUpdate();
void drawUpdating();
};

#endif // OSSM_SOFTWARE_OSSM_H
1 change: 1 addition & 0 deletions Software/src/services/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
static TaskHandle_t drawHelloTaskH = nullptr;
static TaskHandle_t drawMenuTaskH = nullptr;
static TaskHandle_t drawPlayControlsTaskH = nullptr;
static TaskHandle_t drawPreflightTaskH = nullptr;

static TaskHandle_t runHomingTaskH = nullptr;
static TaskHandle_t runSimplePenetrationTaskH = nullptr;
Expand Down

0 comments on commit 78c89c1

Please sign in to comment.