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

Updated Stroke Engine to create the pattern on the fly #102

Merged
merged 1 commit into from
May 30, 2024
Merged
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
279 changes: 127 additions & 152 deletions Software/lib/StrokeEngine/src/StrokeEngine.cpp

Large diffs are not rendered by default.

740 changes: 376 additions & 364 deletions Software/lib/StrokeEngine/src/StrokeEngine.h

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions Software/lib/StrokeEngine/src/pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <math.h>

#include "PatternMath.h"
#include "StrokeEngine.h"

#define DEBUG_PATTERN // Print some debug informations over Serial

Expand Down Expand Up @@ -648,22 +647,3 @@ class Insist : public Pattern {
_realStroke = int((float)_stroke * _strokeFraction);
}
};

/**************************************************************************/
/*
Array holding all different patterns. Please include any custom pattern here.
*/
/**************************************************************************/
static Pattern *patternTable[] = {
new SimpleStroke("Simple Stroke"),
new TeasingPounding("Teasing or Pounding"),
new RoboStroke("Robo Stroke"),
new HalfnHalf("Half'n'Half"),
new Deeper("Deeper"),
new StopNGo("Stop'n'Go"),
new Insist("Insist")
// <-- insert your new pattern class here!
};

static const unsigned int patternTableSize =
sizeof(patternTable) / sizeof(patternTable[0]);
1 change: 0 additions & 1 deletion Software/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ board = esp32dev
framework = arduino
monitor_speed = 115200
monitor_filters = colorize, esp32_exception_decoder, time
upload_port = /dev/cu.usbserial-0001

[env:debugWithoutMotor]
build_flags =
Expand Down
11 changes: 10 additions & 1 deletion Software/src/constants/copy/en-us.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ static const LanguageStruct enUs = {
"Stroke depth increases per cycle; sensation sets count.",
"Pauses between strokes; sensation adjusts length.",
"Modifies length, maintains speed; sensation influences direction."
}
},
.StrokeEngineNames = {
"Simple Stroke",
"Teasing Pounding",
"Robo Stroke",
"Half'n'Half",
"Deeper",
"Stop'n'Go",
"Insist"
},
};

#endif // OSSM_SOFTWARE_EN_US_H
12 changes: 11 additions & 1 deletion Software/src/constants/copy/fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ static const LanguageStruct fr = {
"La profondeur des coups augmente à chaque cycle ; la sensation définit le nombre.",
"Pauses entre les coups ; la sensation ajuste la longueur.",
"Modifie la longueur, maintient la vitesse ; la sensation influe sur la direction.",
}};
},
.StrokeEngineNames = {
"Simple Stroke",
"Teasing Pounding",
"Robo Stroke",
"Half'n'Half",
"Deeper",
"Stop'n'Go",
"Insist",
}
};


#endif // OSSM_SOFTWARE_FR_H
17 changes: 11 additions & 6 deletions Software/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ void setup() {
display.setBusClock(400000);
display.begin();

ossm = new OSSM(display, encoder);

ossm = new OSSM(display, encoder, stepper);
// link functions to be called on events.
button.attachClick([]() { ossm->sm->process_event(ButtonPress{}); });
button.attachDoubleClick([]() { ossm->sm->process_event(DoublePress{}); });
button.attachLongPressStart([]() { ossm->sm->process_event(LongPress{}); });

xTaskCreate(
[](void* pvParameters) {
while (true) {
button.tick();
vTaskDelay(10);
}
},
"buttonTask", 4 * configMINIMAL_STACK_SIZE, nullptr, 1, nullptr);
};

void loop() {
button.tick();
ossm->wm.process();
};
void loop() { vTaskDelete(nullptr); };
14 changes: 7 additions & 7 deletions Software/src/ossm/OSSM.PatternControls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
size_t numberOfDescriptions =
sizeof(UserConfig::language.StrokeEngineDescriptions) /
sizeof(UserConfig::language.StrokeEngineDescriptions[0]);
size_t numberOfPatterns = sizeof(patternTable) / sizeof(patternTable[0]);
size_t numberOfPatterns = 7;

void OSSM::drawPatternControlsTask(void *pvParameters) {
// parse ossm from the parameters
Expand All @@ -19,9 +19,9 @@ void OSSM::drawPatternControlsTask(void *pvParameters) {
return ossm->sm->is("strokeEngine.pattern"_s);
};

int nextPattern = ossm->setting.pattern;
int nextPattern = (int)ossm->setting.pattern;
bool shouldUpdateDisplay = true;
String patternName = patternTable[nextPattern]->getName();
String patternName = "nextPattern";
String patternDescription =
UserConfig::language.StrokeEngineDescriptions[nextPattern];

Expand All @@ -33,13 +33,13 @@ void OSSM::drawPatternControlsTask(void *pvParameters) {
while (isInCorrectState(ossm)) {
nextPattern = ossm->encoder.readEncoder() / 3;
shouldUpdateDisplay =
shouldUpdateDisplay || ossm->setting.pattern != nextPattern;
shouldUpdateDisplay || (int)ossm->setting.pattern != nextPattern;
if (!shouldUpdateDisplay) {
vTaskDelay(100);
continue;
}

patternName = patternTable[nextPattern]->getName();
patternName = UserConfig::language.StrokeEngineNames[nextPattern];

if (nextPattern < numberOfDescriptions) {
patternDescription =
Expand All @@ -48,15 +48,15 @@ void OSSM::drawPatternControlsTask(void *pvParameters) {
patternDescription = "No description available";
}

ossm->setting.pattern = nextPattern;
ossm->setting.pattern = (StrokePatterns)nextPattern;

displayMutex.lock();
ossm->display.clearBuffer();

// Draw the title
drawStr::title(patternName);
drawStr::multiLine(0, 20, patternDescription);
drawShape::scroll(100* nextPattern / numberOfPatterns);
drawShape::scroll(100 * nextPattern / numberOfPatterns);

ossm->display.sendBuffer();
displayMutex.unlock();
Expand Down
2 changes: 1 addition & 1 deletion Software/src/ossm/OSSM.SimplePenetration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void OSSM::startSimplePenetrationTask(void *pvParameters) {
}

void OSSM::startSimplePenetration() {
int stackSize = 30 * configMINIMAL_STACK_SIZE;
int stackSize = 10 * configMINIMAL_STACK_SIZE;

xTaskCreatePinnedToCore(startSimplePenetrationTask,
"startSimplePenetrationTask", stackSize, this,
Expand Down
40 changes: 33 additions & 7 deletions Software/src/ossm/OSSM.StrokeEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "OSSM.h"

#include "../../lib/StrokeEngine/src/StrokeEngine.h"
#include "services/stepper.h"

void OSSM::startStrokeEngineTask(void *pvParameters) {
OSSM *ossm = (OSSM *)pvParameters;
Expand All @@ -11,13 +11,11 @@ void OSSM::startStrokeEngineTask(void *pvParameters) {
.keepoutBoundary = 6.0};
SettingPercents lastSetting = ossm->setting;

class StrokeEngine Stroker;

Stroker.begin(&strokingMachine, &servoMotor, ossm->engine, ossm->stepper);
Stroker.begin(&strokingMachine, &servoMotor, ossm->stepper);
Stroker.thisIsHome();

Stroker.setSensation(calculateSensation(ossm->setting.sensation), true);
Stroker.setPattern(int(ossm->setting.pattern), true);

Stroker.setDepth(0.01f * ossm->setting.depth * abs(measuredStrokeMm), true);
Stroker.setStroke(0.01f * ossm->setting.stroke * abs(measuredStrokeMm),
true);
Expand Down Expand Up @@ -70,7 +68,35 @@ void OSSM::startStrokeEngineTask(void *pvParameters) {

if (lastSetting.pattern != ossm->setting.pattern) {
ESP_LOGD("UTILS", "change pattern: %d", ossm->setting.pattern);
Stroker.setPattern(ossm->setting.pattern, false);

switch (ossm->setting.pattern) {
case StrokePatterns::SimpleStroke:
Stroker.setPattern(new SimpleStroke("Simple Stroke"),
false);
break;
case StrokePatterns::TeasingPounding:
Stroker.setPattern(new TeasingPounding("Teasing Pounding"),
false);
break;
case StrokePatterns::RoboStroke:
Stroker.setPattern(new RoboStroke("Robo Stroke"), false);
break;
case StrokePatterns::HalfnHalf:
Stroker.setPattern(new HalfnHalf("Half'n'Half"), false);
break;
case StrokePatterns::Deeper:
Stroker.setPattern(new Deeper("Deeper"), false);
break;
case StrokePatterns::StopNGo:
Stroker.setPattern(new StopNGo("Stop'n'Go"), false);
break;
case StrokePatterns::Insist:
Stroker.setPattern(new Insist("Insist"), false);
break;
default:
break;
}

lastSetting.pattern = ossm->setting.pattern;
}

Expand All @@ -83,7 +109,7 @@ void OSSM::startStrokeEngineTask(void *pvParameters) {
}

void OSSM::startStrokeEngine() {
int stackSize = 15 * configMINIMAL_STACK_SIZE;
int stackSize = 10 * configMINIMAL_STACK_SIZE;

xTaskCreatePinnedToCore(startStrokeEngineTask, "startStrokeEngineTask",
stackSize, this, configMAX_PRIORITIES - 1,
Expand Down
17 changes: 2 additions & 15 deletions Software/src/ossm/OSSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,13 @@ using namespace sml;
// Now we can define the OSSM constructor since OSSMStateMachine::operator() is
// fully defined
OSSM::OSSM(U8G2_SSD1306_128X64_NONAME_F_HW_I2C &display,
AiEsp32RotaryEncoder &encoder)
AiEsp32RotaryEncoder &encoder, FastAccelStepper *stepper)
: display(display),
encoder(encoder),
stepper(stepper),
sm(std::make_unique<
sml::sm<OSSMStateMachine, sml::thread_safe<ESP32RecursiveMutex>,
sml::logger<StateLogger>>>(logger, *this)) {
engine.init();
stepper = engine.stepperConnectToPin(Pins::Driver::motorStepPin);
if (stepper) {
stepper->setDirectionPin(Pins::Driver::motorDirectionPin, false);
stepper->setEnablePin(Pins::Driver::motorEnablePin, true);
stepper->setAutoEnable(false);
}

// disable motor briefly in case we are against a hard stop.
digitalWrite(Pins::Driver::motorEnablePin, HIGH);
delay(600);
digitalWrite(Pins::Driver::motorEnablePin, LOW);
delay(100);

// NOTE: This is a hack to get the wifi credentials loaded early.
wm.setConfigPortalBlocking(false);
wm.startConfigPortal();
Expand Down
16 changes: 8 additions & 8 deletions Software/src/ossm/OSSM.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef OSSM_SOFTWARE_OSSM_H
#define OSSM_SOFTWARE_OSSM_H

#include <memory>

#include "Actions.h"
#include "AiEsp32RotaryEncoder.h"
#include "Events.h"
Expand Down Expand Up @@ -229,9 +227,7 @@ class OSSM {
* ////
* ///////////////////////////////////////////
*/
FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = nullptr;

FastAccelStepper *stepper;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C &display;
StateLogger logger;
AiEsp32RotaryEncoder &encoder;
Expand All @@ -253,8 +249,11 @@ class OSSM {
Menu menuOption;
String errorMessage = "";

SettingPercents setting = {
.speed = 0, .stroke = 0, .sensation = 50, .depth = 50, .pattern = 0};
SettingPercents setting = {.speed = 0,
.stroke = 0,
.sensation = 50,
.depth = 50,
.pattern = StrokePatterns::SimpleStroke};

unsigned long sessionStartTime = 0;
int sessionStrokeCount = 0;
Expand Down Expand Up @@ -324,7 +323,8 @@ class OSSM {

public:
explicit OSSM(U8G2_SSD1306_128X64_NONAME_F_HW_I2C &display,
AiEsp32RotaryEncoder &rotaryEncoder);
AiEsp32RotaryEncoder &rotaryEncoder,
FastAccelStepper *stepper);

std::unique_ptr<
sml::sm<OSSMStateMachine, sml::thread_safe<ESP32RecursiveMutex>,
Expand Down
8 changes: 7 additions & 1 deletion Software/src/services/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#include <Arduino.h>

#include "constants/Pins.h"
#include "services/display.h"
#include "services/encoder.h"
#include "services/stepper.h"

/**
* This file changes the configuration of the board.
*/
void initBoard() {
static void initBoard() {
Serial.begin(115200);

pinMode(Pins::Remote::encoderSwitch,
Expand All @@ -26,6 +29,9 @@ void initBoard() {
analogReadResolution(12);
analogSetAttenuation(ADC_11db); // allows us to read almost full 3.3V range

initEncoder();
initDisplay();
initStepper();
}

#endif // OSSM_SOFTWARE_BOARD_H
6 changes: 6 additions & 0 deletions Software/src/services/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ static U8G2_SSD1306_128X64_NONAME_F_HW_I2C display(U8G2_R0,
Pins::Remote::displayClock,
Pins::Remote::displayData);

static void initDisplay() {
// Display
display.setBusClock(400000);
display.begin();
}

#endif // OSSM_SOFTWARE_DISPLAY_H
Loading