Integrate Line and Ultraman

This commit is contained in:
Devoalda 2023-11-09 13:18:21 +08:00
parent 5dabf0edc8
commit 102572c2af
8 changed files with 69 additions and 83 deletions

View File

@ -7,14 +7,5 @@
#include "car_init.h"
static car_state_t initialize_car_state() {
g_car_state.x = MAP_SIZE >> 1;
g_car_state.y = MAP_SIZE >> 1;
g_car_state.current_direction = FORWARD;
g_car_state.orientation = NORTH;
return g_car_state;
}
#endif /* CAR_H */

View File

@ -12,13 +12,5 @@
#include "FreeRTOS.h"
#include "task.h"
#include "line_sensor_config.h"
struct s_obs_struct {
bool line_detected;
bool ultrasonic_detected;
};
#endif /* CAR_INIT_H */

View File

@ -8,16 +8,4 @@
#define LEFT_SENSOR_PIN ( 26 )
#define RIGHT_SENSOR_PIN ( 27 )
typedef struct s_obs_struct {
uint8_t line_detected;
bool ultrasonic_detected;
} obs_t;
typedef struct
{
obs_t *obs;
} line_car_struct_t;
#endif //CONFIG_H

View File

@ -12,6 +12,8 @@ target_link_libraries(
target_include_directories(line_sensor_test
PRIVATE ../config
../car
../ultrasonic_sensor
)
pico_enable_stdio_usb(line_sensor_test 1)

View File

@ -18,6 +18,7 @@
#include "semphr.h"
#include "line_sensor_config.h"
#include "car_config.h"
// Semaphore
SemaphoreHandle_t g_left_sensor_sem = NULL;
@ -28,10 +29,10 @@ SemaphoreHandle_t g_left_sensor_sem = NULL;
* This function will setup the Line Sensor by initializing it as an input
*/
static inline void
line_sensor_init(line_car_struct_t *p_car) {
obs_t obs = {1, 0};
p_car->obs = &obs;
line_sensor_init() {
// obs_t obs = {0, 0};
//
// p_car->obs = &obs;
g_left_sensor_sem = xSemaphoreCreateBinary();
@ -65,12 +66,13 @@ monitor_left_sensor_task(void *pvParameters) {
for (;;)
{
if (xSemaphoreTake(g_left_sensor_sem, portMAX_DELAY) == pdTRUE)
{
// if (xSemaphoreTake(g_left_sensor_sem, portMAX_DELAY) == pdTRUE)
// {
// Set the flag to notify the task
p_obs->line_detected = gpio_get(LEFT_SENSOR_PIN);
printf("Left Sensor: %d\n", p_obs->line_detected);
}
vTaskDelay(pdMS_TO_TICKS(LINE_SENSOR_READ_DELAY));
// }
}
}

View File

@ -1,17 +1,13 @@
#include "line_sensor_init.h"
#include "ultrasonic_sensor.h"
#include "car_config.h"
#define READ_LEFT_SENSOR_PRIO (tskIDLE_PRIORITY + 2UL)
void
launch(line_car_struct_t *car_struct)
launch(car_struct_t *car_struct)
{
struct repeating_timer g_left_sensor_timer;
add_repeating_timer_ms(LINE_SENSOR_READ_DELAY,
h_left_sensor_timer_handler,
NULL,
&g_left_sensor_timer);
TaskHandle_t h_monitor_left_sensor_task = NULL;
xTaskCreate(monitor_left_sensor_task,
"read_left_sensor_task",
@ -20,7 +16,13 @@ launch(line_car_struct_t *car_struct)
READ_LEFT_SENSOR_PRIO,
&h_monitor_left_sensor_task);
vTaskStartScheduler();
TaskHandle_t h_monitor_ultrasonic_task = NULL;
xTaskCreate(check_obstacle,
"read_ultrasonic_task",
configMINIMAL_STACK_SIZE,
(void *)car_struct->obs,
READ_LEFT_SENSOR_PRIO,
&h_monitor_ultrasonic_task);
}
int
@ -30,15 +32,21 @@ main(void)
obs_t obs = { 0, 0 };
line_car_struct_t car_struct = { .obs = &obs };
car_struct_t car_struct = { .obs = &obs };
sleep_ms(2000);
printf("Test started!\n");
line_sensor_init(&car_struct);
line_sensor_init();
printf("Line sensor initialized!\n");
init_ultrasonic();
printf("Ultrasonic sensor initialized!\n");
launch(&car_struct);
vTaskStartScheduler();
return (0);
}

View File

@ -340,7 +340,7 @@ void updateDirection() {
// Temperature in degrees Celsius
// printf("Temperature: %d\n", temperature[0]);
print_orientation_data();
// print_orientation_data();
// printf("Direction: ");

View File

@ -8,7 +8,8 @@
#define ULTRASONIC_SENSOR_H
#include "ultrasonic_init.h"
#include "motor_speed.h"
//#include "motor_speed.h"
#include "car_config.h"
// volatile uint32_t start_time;
// volatile uint32_t end_time;
@ -79,28 +80,30 @@ check_obstacle(void *pvParameters)
// printf("Distance: %.2f cm\n", distance);
// change value of obstacle_detected in ultrasonic_t struct
ultrasonic_t *ultrasonic_sensor = (ultrasonic_t *)pvParameters;
ultrasonic_sensor->obstacle_detected = (distance < 7);
obs_t *ultrasonic_sensor = (obs_t *)pvParameters;
ultrasonic_sensor->ultrasonic_detected = (distance < 7);
printf("Distance: %.2f cm, Obstacle Detected: %d\n",
distance,
ultrasonic_sensor->obstacle_detected);
ultrasonic_sensor->ultrasonic_detected);
vTaskDelay(pdMS_TO_TICKS(ULTRASONIC_SENSOR_READ_DELAY));
}
}
void
check_global(void *pvParameters)
{
while (true)
{
ultrasonic_t *ultrasonic_sensor = (ultrasonic_t *)pvParameters;
printf("Global Obstacle Detected : %d\n",
ultrasonic_sensor->obstacle_detected);
vTaskDelay(pdMS_TO_TICKS(ULTRASONIC_SENSOR_READ_DELAY));
}
}
//void
//check_global(void *pvParameters)
//{
// while (true)
// {
// ultrasonic_t *ultrasonic_sensor = (ultrasonic_t *)pvParameters;
//
// printf("Global Obstacle Detected : %d\n",
// ultrasonic_sensor->obstacle_detected);
// vTaskDelay(pdMS_TO_TICKS(ULTRASONIC_SENSOR_READ_DELAY));
// }
//}
// void
// distance_task(__unused void *params)