Joystick controls done

This commit is contained in:
BOTAlex 2025-01-25 01:16:08 +01:00
parent 3e1418257a
commit 02f63057bf
3 changed files with 36 additions and 3 deletions

View File

@ -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() {

31
src/InputController.h Normal file
View File

@ -0,0 +1,31 @@
#include <Arduino.h>
#ifndef ESP32
#include <Arduino_FreeRTOS.h>
#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
}
}

View File

@ -105,7 +105,7 @@ void StepperController::MotorTask(void *params)
}
StepperController::lastUpdateTime = millis();
vTaskDelay(pdMS_TO_TICKS(100));
vTaskDelay(pdMS_TO_TICKS(50));
}
}