add direction control

This commit is contained in:
Richie 2023-10-30 23:20:15 +08:00
parent f176192dce
commit ed516b5ce4
2 changed files with 80 additions and 17 deletions

View File

@ -28,19 +28,46 @@ set_wheel_direction(uint32_t direction)
} }
/*! /*!
* @brief Set the speed of the wheels * @brief Turn the wheel, must set the priority higher than the motor PID task
* @param speed The speed of the wheels, from 0 to 5000 * @param direction The direction of the wheel
* @param direction_after The direction of the wheel after turning
* @param speed_after The speed of the wheel after turning
*/ */
void void
set_wheel_speed(uint32_t speed) turn_wheel(uint32_t direction)
{ {
g_motor_right.pwm.level = speed; set_wheel_speed(0u);
g_motor_left.pwm.level = speed; vTaskDelay(pdMS_TO_TICKS(1000));
float initial_right = g_motor_right.speed.distance_cm;
float initial_left = g_motor_left.speed.distance_cm;
pwm_set_chan_level(g_motor_right.pwm.slice_num, set_wheel_direction(direction);
g_motor_right.pwm.channel, set_wheel_speed(3500u);
g_motor_right.pwm.level);
for (;;)
{
// gap between wheels = 11.3cm, to turn 90 degrees, need to travel
// 11.3 * pi / 4 = 8.9cm
if (g_motor_left.speed.distance_cm - initial_left >= 6.8f)
{
g_motor_left.pwm.level = 0;
pwm_set_chan_level(g_motor_left.pwm.slice_num, pwm_set_chan_level(g_motor_left.pwm.slice_num,
g_motor_left.pwm.channel, g_motor_left.pwm.channel,
g_motor_left.pwm.level); g_motor_left.pwm.level);
}
if (g_motor_right.speed.distance_cm - initial_right >= 6.8f)
{
g_motor_right.pwm.level = 0;
pwm_set_chan_level(g_motor_right.pwm.slice_num,
g_motor_right.pwm.channel,
g_motor_right.pwm.level);
}
if (g_motor_left.pwm.level == 0u && g_motor_right.pwm.level == 0u)
{
break;
}
}
vTaskDelay(pdMS_TO_TICKS(1000));
} }

View File

@ -4,7 +4,37 @@
#include "motor_direction.h" #include "motor_direction.h"
#include "motor_pid.h" #include "motor_pid.h"
#define WHEEL_SPEED_PRIO (tskIDLE_PRIORITY + 1UL) #define WHEEL_SPEED_PRIO (tskIDLE_PRIORITY + 2UL)
#define WHEEL_CONTROL_PRIO (tskIDLE_PRIORITY + 2UL)
#define WHEEL_PID_PRIO (tskIDLE_PRIORITY + 2UL)
static void
motor_control_task(__unused void *p_param)
{
for (;;)
{
set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed(3000u);
distance_to_stop(30);
set_wheel_direction(DIRECTION_BACKWARD);
set_wheel_speed(3000u);
distance_to_stop(30);
turn_wheel(DIRECTION_LEFT);
set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed(3000u);
distance_to_stop(30);
turn_wheel(DIRECTION_RIGHT);
set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed(3000u);
distance_to_stop(30);
set_wheel_speed(0u);
vTaskDelay(pdMS_TO_TICKS(3000));
}
}
void void
launch() launch()
@ -19,8 +49,8 @@ launch()
irq_set_enabled(IO_IRQ_BANK0, true); irq_set_enabled(IO_IRQ_BANK0, true);
// Set wheel speed // set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed(3500); // set_wheel_speed(3000);
// Left wheel // Left wheel
// //
@ -32,7 +62,6 @@ launch()
WHEEL_SPEED_PRIO, WHEEL_SPEED_PRIO,
&h_monitor_left_wheel_speed_task_handle); &h_monitor_left_wheel_speed_task_handle);
// Right wheel // Right wheel
// //
TaskHandle_t h_monitor_right_wheel_speed_task_handle = NULL; TaskHandle_t h_monitor_right_wheel_speed_task_handle = NULL;
@ -48,9 +77,17 @@ launch()
"motor_pid_task", "motor_pid_task",
configMINIMAL_STACK_SIZE, configMINIMAL_STACK_SIZE,
(void *)&g_motor_right, (void *)&g_motor_right,
WHEEL_SPEED_PRIO, WHEEL_PID_PRIO,
&h_motor_pid_right_task_handle); &h_motor_pid_right_task_handle);
// control task
TaskHandle_t h_motor_turning_task_handle = NULL;
xTaskCreate(motor_control_task,
"motor_turning_task",
configMINIMAL_STACK_SIZE,
NULL,
WHEEL_CONTROL_PRIO,
&h_motor_turning_task_handle);
vTaskStartScheduler(); vTaskStartScheduler();
} }
@ -64,7 +101,6 @@ main(void)
printf("Test started!\n"); printf("Test started!\n");
motor_init(); motor_init();
set_wheel_direction(DIRECTION_LEFT_FORWARD | DIRECTION_RIGHT_FORWARD);
launch(); launch();