This commit is contained in:
Richie 2023-11-28 13:24:33 +08:00
parent f222315003
commit 7da6295adc
5 changed files with 43 additions and 35 deletions

View File

@ -5,7 +5,7 @@
#define LINE_SENSOR_READ_DELAY ( 100 ) #define LINE_SENSOR_READ_DELAY ( 100 )
#define LEFT_SENSOR_PIN ( 26 ) #define LEFT_SENSOR_PIN ( 21 ) //26
#define RIGHT_SENSOR_PIN ( 27 ) #define RIGHT_SENSOR_PIN ( 20 ) //27
#endif //CONFIG_H #endif //CONFIG_H

View File

@ -9,11 +9,11 @@
#define SLICE_NUM 0U #define SLICE_NUM 0U
// IN1, IN2, IN3, IN4 on the L298N // IN1, IN2, IN3, IN4 on the L298N
#define DIRECTION_PIN_RIGHT_IN1 11U #define DIRECTION_PIN_RIGHT_IN1 3U //11
#define DIRECTION_PIN_RIGHT_IN2 12U #define DIRECTION_PIN_RIGHT_IN2 2U //12
#define DIRECTION_PIN_LEFT_IN3 19U #define DIRECTION_PIN_LEFT_IN3 7U //19
#define DIRECTION_PIN_LEFT_IN4 20U #define DIRECTION_PIN_LEFT_IN4 6U //20
// to turn one side // to turn one side
#define DIRECTION_RIGHT_FORWARD (1U << DIRECTION_PIN_RIGHT_IN2) #define DIRECTION_RIGHT_FORWARD (1U << DIRECTION_PIN_RIGHT_IN2)

View File

@ -38,9 +38,9 @@ motor_init(car_struct_t *car_struct)
g_right_sem = xSemaphoreCreateBinary(); g_right_sem = xSemaphoreCreateBinary();
car_struct->p_pid->use_pid = true; car_struct->p_pid->use_pid = true;
car_struct->p_pid->kp_value = 600.f; car_struct->p_pid->kp_value = 12.f; //25
car_struct->p_pid->ki_value = 66.67f; car_struct->p_pid->ki_value = 0.f;
car_struct->p_pid->kd_value = 1350.f; car_struct->p_pid->kd_value = 30.f;
// initialize the car_struct // initialize the car_struct
car_struct->p_left_motor->pwm.level = 0u; car_struct->p_left_motor->pwm.level = 0u;

View File

@ -67,16 +67,16 @@ repeating_pid_handler(struct repeating_timer *ppp_timer)
if (temp > MAX_PWM_LEVEL) if (temp > MAX_PWM_LEVEL)
{ {
temp = MAX_PWM_LEVEL; temp = 70.0f;
} }
if (temp <= MIN_PWM_LEVEL) if (temp <= MIN_PWM_LEVEL)
{ {
temp = MIN_PWM_LEVEL; temp = 60.0f;
} }
// set_wheel_speed((uint32_t)temp, car_strut->p_right_motor);
set_wheel_speed((uint32_t)temp, car_strut->p_left_motor); set_wheel_speed((uint32_t)temp, car_strut->p_left_motor);
printf("right speed: %f\n", car_strut->p_right_motor->speed.current_cms);
printf("left speed: %f\n", car_strut->p_left_motor->speed.current_cms);
return true; return true;
} }

View File

@ -34,7 +34,7 @@ motor_control_task(void *params)
{ {
car_struct_t *car_struct = (car_struct_t *)params; car_struct_t *car_struct = (car_struct_t *)params;
set_wheel_direction(DIRECTION_FORWARD); set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed_synced(90u, car_struct); set_wheel_speed_synced(62u, car_struct);
for (;;) for (;;)
{ {
@ -44,17 +44,25 @@ motor_control_task(void *params)
default: default:
break; break;
case 0b01: case 0b01:
car_struct->p_right_motor->speed.distance_cm // car_struct->p_right_motor->speed.distance_cm
-= SLOT_DISTANCE_CM; // -= SLOT_DISTANCE_CM;
set_wheel_direction(DIRECTION_LEFT_BACKWARD);
distance_to_stop(car_struct, SLOT_DISTANCE_CM);
set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed_synced(62u, car_struct);
break; break;
case 0b10: case 0b10:
car_struct->p_right_motor->speed.distance_cm // car_struct->p_right_motor->speed.distance_cm
+= SLOT_DISTANCE_CM; // += SLOT_DISTANCE_CM;
set_wheel_direction(DIRECTION_RIGHT_BACKWARD);
distance_to_stop(car_struct, SLOT_DISTANCE_CM);
set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed_synced(62u, car_struct);
break; break;
case 0b11: case 0b11:
turn(DIRECTION_LEFT, 90u, 90u, car_struct); turn(DIRECTION_LEFT, 90u, 62u, car_struct);
set_wheel_direction(DIRECTION_FORWARD); set_wheel_direction(DIRECTION_FORWARD);
set_wheel_speed_synced(90u, car_struct); set_wheel_speed_synced(62u, car_struct);
break; break;
} }
@ -172,13 +180,13 @@ main(void)
sleep_ms(1000u); sleep_ms(1000u);
// control task // control task
// TaskHandle_t h_motor_turning_task_handle = NULL; TaskHandle_t h_motor_turning_task_handle = NULL;
// xTaskCreate(motor_control_task, xTaskCreate(motor_control_task,
// "motor_turning_task", "motor_turning_task",
// configMINIMAL_STACK_SIZE, configMINIMAL_STACK_SIZE,
// (void *)&car_struct, (void *)&car_struct,
// PRIO, PRIO,
// &h_motor_turning_task_handle); &h_motor_turning_task_handle);
// obs task // obs task
// TaskHandle_t h_obs_task_handle = NULL; // TaskHandle_t h_obs_task_handle = NULL;
@ -189,14 +197,14 @@ main(void)
// PRIO, // PRIO,
// &h_obs_task_handle); // &h_obs_task_handle);
// turn task // // turn task
TaskHandle_t h_turn_task_handle = NULL; // TaskHandle_t h_turn_task_handle = NULL;
xTaskCreate(turn_task, // xTaskCreate(turn_task,
"turn_task", // "turn_task",
configMINIMAL_STACK_SIZE, // configMINIMAL_STACK_SIZE,
(void *)&car_struct, // (void *)&car_struct,
PRIO, // PRIO,
&h_turn_task_handle); // &h_turn_task_handle);
// PID timer // PID timer
struct repeating_timer pid_timer; struct repeating_timer pid_timer;