diff --git a/src/motion/Motion.cpp b/src/motion/Motion.cpp index 049a75d..5244d24 100644 --- a/src/motion/Motion.cpp +++ b/src/motion/Motion.cpp @@ -11,6 +11,10 @@ #include "Motion.h" +TaskHandle_t xMoveTaskHandle = NULL; +TaskHandle_t xClockwiseTaskHandle = NULL; +TaskHandle_t xAntiClockwiseTaskHandle = NULL; + // Constructor Motion::Motion() { @@ -20,31 +24,40 @@ Motion::Motion() { void Motion::begin(void) { } -void moveTask(void * moveForMs) { +void moveTask(void * args) { analogWrite(MOTOR_LEFT_PIN, 128); analogWrite(MOTOR_RIGHT_PIN, 128); - vTaskDelay((uint32_t) moveForMs); + vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); analogWrite(MOTOR_LEFT_PIN, 0); analogWrite(MOTOR_RIGHT_PIN, 0); + vTaskDelete(xMoveTaskHandle); } // Move forward for a certain amount of time. void Motion::move(uint32_t moveForMs) { - xTaskCreate(moveTask, "Move", configMINIMAL_STACK_SIZE, (void*)moveForMs, 1, NULL); - // analogWrite(MOTOR_LEFT_PIN, 128); - // analogWrite(MOTOR_RIGHT_PIN, 128); - // vTaskDelay(moveForMs); - // analogWrite(MOTOR_LEFT_PIN, 0); - // analogWrite(MOTOR_RIGHT_PIN, 0); + xTaskCreate(moveTask, "Move", 4096, (void*)moveForMs, 10, &xMoveTaskHandle); } +void leftMotorTask(void * args) { + analogWrite(MOTOR_LEFT_PIN, 128); + vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); + analogWrite(MOTOR_LEFT_PIN, 0); + vTaskDelete(xClockwiseTaskHandle); +} // Rotate clockwise for a certain amount of time. void Motion::rotateClockwise(uint32_t rotateForMs) { + xTaskCreate(leftMotorTask, "LeftMotor", 4096, (void*)rotateForMs, 10, &xClockwiseTaskHandle); +} +void rightMotorTask(void * args) { + analogWrite(MOTOR_RIGHT_PIN, 128); + vTaskDelay((uint32_t) args / portTICK_PERIOD_MS); + analogWrite(MOTOR_RIGHT_PIN, 0); + vTaskDelete(xAntiClockwiseTaskHandle); } // Rotate anticlockwise for a certain amount of time. void Motion::rotateAnticlockwise(uint32_t rotateForMs) { - + xTaskCreate(rightMotorTask, "RightMotor", 4096, (void*)rotateForMs, 10, &xAntiClockwiseTaskHandle); }