diff --git a/README.md b/README.md index 374217d..1423999 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ Motor module consist of 4 components: ## Line Sensor +The Line sensor package is in `frtos/line_sensor`, and its configuration is in `frtos/config/line_sensor_config.h`. It +contains the drivers and FreeRTOS tasks to read the line sensor data and update the car's obstruction struct. + ## Magnetometer The magnetometer used is the [LSM303DLHC](https://www.st.com/resource/en/datasheet/lsm303dlhc.pdf) from STMicroelectronics. diff --git a/frtos/line_sensor/line_sensor_init.h b/frtos/line_sensor/line_sensor_init.h index 1388f37..dfec0ea 100644 --- a/frtos/line_sensor/line_sensor_init.h +++ b/frtos/line_sensor/line_sensor_init.h @@ -31,7 +31,8 @@ SemaphoreHandle_t g_left_sensor_sem = NULL; static inline void line_sensor_init(car_struct_t *p_car_struct) { - p_car_struct->obs->left_sensor_detected, p_car_struct->obs->left_sensor_detected = false; + p_car_struct->obs->left_sensor_detected, + p_car_struct->obs->left_sensor_detected = false; g_left_sensor_sem = xSemaphoreCreateBinary(); @@ -40,7 +41,6 @@ line_sensor_init(car_struct_t *p_car_struct) // Initialise 3 GPIO pins and set them to input gpio_init_mask(mask); gpio_set_dir_in_masked(mask); - } /** @@ -48,35 +48,37 @@ line_sensor_init(car_struct_t *p_car_struct) * @param rt * @return True (To keep the timer running) */ -bool h_left_sensor_timer_handler(repeating_timer_t *repeatingTimer) { +bool +h_left_sensor_timer_handler(repeating_timer_t *repeatingTimer) +{ BaseType_t xHigherPriorityTaskWoken = pdFALSE; - xSemaphoreGiveFromISR(g_left_sensor_sem, - &xHigherPriorityTaskWoken); + xSemaphoreGiveFromISR(g_left_sensor_sem, &xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken); return true; } - void -monitor_line_sensor_task(void *pvParameters) { +monitor_line_sensor_task(void *pvParameters) +{ volatile obs_t *p_obs = NULL; - p_obs = (obs_t *) pvParameters; + p_obs = (obs_t *)pvParameters; for (;;) { -// if (xSemaphoreTake(g_left_sensor_sem, portMAX_DELAY) == pdTRUE) -// { - // Set the flag to notify the task - p_obs->left_sensor_detected = gpio_get(LEFT_SENSOR_PIN); - p_obs->right_sensor_detected = gpio_get(RIGHT_SENSOR_PIN); - // printf("Left Sensor: %d\n", p_obs->line_detected); - vTaskDelay(pdMS_TO_TICKS(LINE_SENSOR_READ_DELAY)); -// } + // Set the flag to notify the task + p_obs->left_sensor_detected = gpio_get(LEFT_SENSOR_PIN); + p_obs->right_sensor_detected = gpio_get(RIGHT_SENSOR_PIN); + + vTaskDelay(pdMS_TO_TICKS(LINE_SENSOR_READ_DELAY)); } } +/** + * @brief Initialise the tasks for the line sensor + * @param car_struct The car struct + */ void line_tasks_init(car_struct_t *car_struct) { @@ -89,5 +91,4 @@ line_tasks_init(car_struct_t *car_struct) &h_monitor_line_sensor_task); } - #endif /* LINE_SENSOR_INIT_H */ diff --git a/frtos/line_sensor/line_sensor_unused.c b/frtos/line_sensor/line_sensor_unused.c deleted file mode 100644 index 91a2ba1..0000000 --- a/frtos/line_sensor/line_sensor_unused.c +++ /dev/null @@ -1,100 +0,0 @@ -#include -#include "pico/stdlib.h" - -#include "hardware/adc.h" - -#include "FreeRTOS.h" -#include "task.h" -#include "queue.h" -#include "semphr.h" - -#include "line_sensor.h" -#include "string.h" - -//const float conversionFactor = 3.3f / (1 << 12); - -volatile u_int8_t map[MAP_SIZE][MAP_SIZE] = {0}; - - -/** - * @brief Update the map based on the car's state - * - * @param car_state The current car state - */ -static inline void -update_map(car_state_t car_state) { - if (car_state.x >= 0 && car_state.x < MAP_SIZE && - car_state.y >= 0 && car_state.y < MAP_SIZE) { - map[car_state.x][car_state.y] = 1; - } -} - -/** - * @brief Handle forward movement of the car - * - * @param car_state The current car state - */ -static void -handle_forward_movement(car_state_t *car_state) { - printf("FORWARD, "); - // TODO: Check car's actual forward movement - switch (car_state->orientation) { - case NORTH: - printf("NORTH\n"); - car_state->y++; - break; - case EAST: - printf("EAST\n"); - car_state->x++; - break; - case SOUTH: - printf("SOUTH\n"); - car_state->y--; - break; - case WEST: - printf("WEST\n"); - car_state->x--; - break; - } -} - -/** - * @brief Handle a right turn of the car - * - * Note: Bitwise AND with 0x03 to ensure that the orientation - * is always between 0 and 3 - * @param car_state The current car state - */ -static inline void -handle_right_turn(car_state_t *car_state) { - car_state->orientation = (car_state->orientation + 1) & 0x03; -} - -/** - * @brief Handle a left turn of the car - * - * @param car_state The current car state - */ -static inline void -handle_left_turn(car_state_t *car_state) { - car_state->orientation = (car_state->orientation - 1) & 0x03; -} - -/** - * @brief Print the map to the console - * - * This function will print the map to the console - */ -void -print_map() { - // Invert the map, 0,0 is at the bottom left - for (int i = MAP_SIZE - 1; i >= 0; i --) - { - for (int j = 0; j < MAP_SIZE; j ++) - { - printf("%d ", map[j][i]); - } - printf("\n"); - } -} - diff --git a/frtos/rtos_car.c b/frtos/rtos_car.c index fd8a0ab..7eed31d 100644 --- a/frtos/rtos_car.c +++ b/frtos/rtos_car.c @@ -18,6 +18,11 @@ check_line_touch(void *params) | (car_struct->obs->right_sensor_detected); } +/** + * @brief Check if the car is on the line or if there is an obstacle + * @param params The car_struct + * @return 1 if there is an obstacle, 0 otherwise + */ bool check_collision(void *params) {