Run servo movement on different core

This commit is contained in:
Christoph Hagen 2024-04-20 09:57:15 +02:00
parent 6256b6ef33
commit 4a88d1a380
3 changed files with 40 additions and 11 deletions

View File

@ -65,7 +65,7 @@ public:
/**
* @brief Update the servo state periodically
*
* This function should be periodically called to update the servo state,
* This function will be periodically called to update the servo state,
* specifically to release the button after the opening time has elapsed.
*
* There is no required interval to call this function, but the accuracy of
@ -78,16 +78,14 @@ public:
*/
void pressButton();
/**
* Release the door opener button by moving the servo arm.
*/
void releaseButton();
private:
// Indicator that the door button is pushed
bool buttonIsPressed = false;
// Indicate that the button should be pressed
bool shouldPressButton = false;
uint32_t openDuration = 0;
int pressedValue = 0;
@ -103,4 +101,12 @@ private:
// PWM Module needed for the servo
ESP32PWM pwm;
// The task on core 1 that resets the servo
TaskHandle_t servoResetTask;
/**
* Release the door opener button by moving the servo arm.
*/
void releaseButton();
};

View File

@ -64,7 +64,6 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura
void SesameController::loop(uint32_t millis) {
currentTime = millis;
server.loop(millis);
servo.loop(millis);
}
// MARK: Local

View File

@ -4,7 +4,28 @@
ServoController::ServoController() { }
void performReset(void * pvParameters) {
ServoController* servo = (ServoController *) pvParameters;
for(;;){
servo->loop(millis());
delay(50);
}
}
void ServoController::configure(ServoConfiguration configuration) {
// Create a task that runs on a different core,
// So that it's always executed
xTaskCreatePinnedToCore(
performReset, /* Task function. */
"Servo", /* name of task. */
1000, /* Stack size of task */
this, /* parameter of the task */
1, /* priority of the task */
&servoResetTask,
1); /* pin task to core 1 */
openDuration = configuration.openDuration;
pressedValue = configuration.pressedValue;
releasedValue = configuration.releasedValue;
@ -15,9 +36,7 @@ void ServoController::configure(ServoConfiguration configuration) {
}
void ServoController::pressButton() {
servo.write(pressedValue);
buttonIsPressed = true;
openingEndTime = millis() + openDuration;
shouldPressButton = true;
}
void ServoController::releaseButton() {
@ -26,7 +45,12 @@ void ServoController::releaseButton() {
}
void ServoController::loop(uint32_t millis) {
if (buttonIsPressed && millis > openingEndTime) {
if (shouldPressButton) {
servo.write(pressedValue);
openingEndTime = millis + openDuration;
buttonIsPressed = true;
shouldPressButton = false;
} else if (buttonIsPressed && millis > openingEndTime) {
releaseButton();
}
}