From 02f63057bf328d40171eb5456cc07d5059f9395f Mon Sep 17 00:00:00 2001 From: BOTAlex Date: Sat, 25 Jan 2025 01:16:08 +0100 Subject: [PATCH] Joystick controls done --- src/ClawMachineOverhaul.ino | 6 ++++-- src/InputController.h | 31 +++++++++++++++++++++++++++++++ src/StepperController.cpp | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/InputController.h diff --git a/src/ClawMachineOverhaul.ino b/src/ClawMachineOverhaul.ino index 067b53f..b361faa 100644 --- a/src/ClawMachineOverhaul.ino +++ b/src/ClawMachineOverhaul.ino @@ -6,6 +6,7 @@ #include "ZCodeParser.h" #include "Shared/MotorMoveParams.h" #include "Shared/Steppers.h" +#include "InputController.h" // [X, Y, Head] #define NUM_STEPPERS 3 @@ -75,7 +76,7 @@ void anotherFunc(void * params){ { StepperController::SetMotorEnabled('x', true); StepperController::SetMovementEnabled('x', true); - StepperController::QueueMove('x', 2000); + StepperController::QueueMove('x', -2000); Serial.println("Added to queue"); vTaskDelay(pdMS_TO_TICKS(5000)); } @@ -90,7 +91,8 @@ void setup() { StepperController::Init(steppers, NUM_STEPPERS); xTaskCreate(StepperController::MotorTask, "Motors", 1024, NULL, 5, NULL); - xTaskCreate(anotherFunc, "Other loop", 1024, NULL, 5, NULL); + // xTaskCreate(anotherFunc, "Other loop", 1024, NULL, 5, NULL); + xTaskCreate(InputControlTask, "Input", 1024, NULL, 5, NULL); } void loop() { diff --git a/src/InputController.h b/src/InputController.h new file mode 100644 index 0000000..50678e9 --- /dev/null +++ b/src/InputController.h @@ -0,0 +1,31 @@ +#include +#ifndef ESP32 + #include +#endif +#include "StepperController.h" + +#define JOYSTICK_PIN 63 +#define JOY_CRITICAL_POINT 25 + +void InputControlTask(void *params) { + while (true) { + int joystickValueRaw = analogRead(JOYSTICK_PIN); + int joystickValue = map(joystickValueRaw, 0, 1023, 0, 100); + bool leftMovement = (joystickValue > (100 - JOY_CRITICAL_POINT)); + bool rightMovement = (joystickValue < JOY_CRITICAL_POINT); + + if (leftMovement) { + StepperController::SetMotorEnabled('x', true); + StepperController::SetMovementEnabled('x', true); + StepperController::SetMoveQueue('x', 150); + } + + if (rightMovement) { + StepperController::SetMotorEnabled('x', true); + StepperController::SetMovementEnabled('x', true); + StepperController::SetMoveQueue('x', -150); + } + + vTaskDelay(pdMS_TO_TICKS(100)); // Poll every x + } +} \ No newline at end of file diff --git a/src/StepperController.cpp b/src/StepperController.cpp index 8c1fc90..abb63d1 100644 --- a/src/StepperController.cpp +++ b/src/StepperController.cpp @@ -105,7 +105,7 @@ void StepperController::MotorTask(void *params) } StepperController::lastUpdateTime = millis(); - vTaskDelay(pdMS_TO_TICKS(100)); + vTaskDelay(pdMS_TO_TICKS(50)); } }