refactor(Code Cleanup): Modified code structure
This commit is contained in:
parent
b541cc620f
commit
adaa34b1ae
|
@ -3,10 +3,10 @@
|
||||||
|
|
||||||
/* ADC Configuration */
|
/* ADC Configuration */
|
||||||
|
|
||||||
#define LINE_SENSOR_READ_DELAY ( 100 )
|
#define LINE_SENSOR_READ_DELAY (100)
|
||||||
|
|
||||||
#define LEFT_SENSOR_PIN ( 26 )
|
#define LEFT_SENSOR_PIN (26)
|
||||||
#define RIGHT_SENSOR_PIN ( 27 )
|
#define RIGHT_SENSOR_PIN (27)
|
||||||
#define BARCODE_SENSOR_PIN ( 22 )
|
#define BARCODE_SENSOR_PIN (22)
|
||||||
|
|
||||||
#endif //CONFIG_H
|
#endif // CONFIG_H
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#include "line_sensor_init.h"
|
#include "line_sensor_init.h"
|
||||||
#include "car_config.h"
|
#include "car_config.h"
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(void)
|
main(void)
|
||||||
{
|
{
|
||||||
|
@ -10,7 +9,7 @@ main(void)
|
||||||
|
|
||||||
obs_t obs;
|
obs_t obs;
|
||||||
|
|
||||||
car_struct_t car_struct = {.obs = &obs};
|
car_struct_t car_struct = { .obs = &obs };
|
||||||
|
|
||||||
sleep_ms(2000);
|
sleep_ms(2000);
|
||||||
|
|
||||||
|
|
|
@ -9,11 +9,15 @@
|
||||||
* The yaw is calculated using the magnetometer and accelerometer data
|
* The yaw is calculated using the magnetometer and accelerometer data
|
||||||
* The roll, pitch and yaw are combined to calculate the direction
|
* The roll, pitch and yaw are combined to calculate the direction
|
||||||
* of the car with a complementary filter and compensating for the
|
* of the car with a complementary filter and compensating for the
|
||||||
* temperature.
|
* temperature. (existing)
|
||||||
*
|
*
|
||||||
* The complementary filter is used to combine the accelerometer
|
* The complementary filter is used to combine the accelerometer
|
||||||
* and magnetometer data (yaw) to calculate the direction of the car
|
* and magnetometer data (yaw) to calculate the direction of the car
|
||||||
*
|
*
|
||||||
|
* Complementary Filter was used previously, but it was not accurate
|
||||||
|
* enough. The yaw calculated from the magnetometer data was used
|
||||||
|
* instead.
|
||||||
|
*
|
||||||
* Source:
|
* Source:
|
||||||
* https://www.nxp.com/docs/en/application-note/AN3461.pdf
|
* https://www.nxp.com/docs/en/application-note/AN3461.pdf
|
||||||
* https://ahrs.readthedocs.io/en/latest/filters/complementary.html
|
* https://ahrs.readthedocs.io/en/latest/filters/complementary.html
|
||||||
|
@ -80,7 +84,8 @@ calculate_yaw_magnetometer(int16_t magnetometer[3])
|
||||||
* @return Compensated Yaw
|
* @return Compensated Yaw
|
||||||
*/
|
*/
|
||||||
float
|
float
|
||||||
compensate_magnetometer(float yaw_mag, int16_t temperature) {
|
compensate_magnetometer(float yaw_mag, int16_t temperature)
|
||||||
|
{
|
||||||
// Calculate temperature difference from the reference temperature
|
// Calculate temperature difference from the reference temperature
|
||||||
uint delta_temp = temperature - TEMPERATURE_OFFSET;
|
uint delta_temp = temperature - TEMPERATURE_OFFSET;
|
||||||
|
|
||||||
|
@ -301,7 +306,7 @@ print_roll_and_pitch(angle_t roll_angle, angle_t pitch_angle)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
updateDirection(volatile direction_t * g_direction)
|
updateDirection(volatile direction_t *g_direction)
|
||||||
{
|
{
|
||||||
int16_t magnetometer[3];
|
int16_t magnetometer[3];
|
||||||
int16_t accelerometer[3];
|
int16_t accelerometer[3];
|
||||||
|
@ -323,7 +328,7 @@ void
|
||||||
monitor_direction_task(void *pvParameters)
|
monitor_direction_task(void *pvParameters)
|
||||||
{
|
{
|
||||||
volatile direction_t *p_direction = NULL;
|
volatile direction_t *p_direction = NULL;
|
||||||
p_direction = (direction_t *) pvParameters;
|
p_direction = (direction_t *)pvParameters;
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
|
@ -339,7 +344,7 @@ magnetometer_tasks_init(car_struct_t *car_struct)
|
||||||
xTaskCreate(monitor_direction_task,
|
xTaskCreate(monitor_direction_task,
|
||||||
"Direction Task",
|
"Direction Task",
|
||||||
configMINIMAL_STACK_SIZE,
|
configMINIMAL_STACK_SIZE,
|
||||||
(void *) car_struct->p_direction,
|
(void *)car_struct->p_direction,
|
||||||
PRIO,
|
PRIO,
|
||||||
&h_direction_task);
|
&h_direction_task);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,13 +21,13 @@ launch()
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (void)
|
main(void)
|
||||||
{
|
{
|
||||||
stdio_usb_init();
|
stdio_usb_init();
|
||||||
|
|
||||||
direction_t direction;
|
direction_t direction;
|
||||||
|
|
||||||
car_struct_t car_struct = {.p_direction = &direction};
|
car_struct_t car_struct = { .p_direction = &direction };
|
||||||
|
|
||||||
int grid_rows = 10; // Define the number of rows in your grid
|
int grid_rows = 10; // Define the number of rows in your grid
|
||||||
int grid_cols = 10; // Define the number of columns in your grid
|
int grid_cols = 10; // Define the number of columns in your grid
|
||||||
|
@ -39,13 +39,13 @@ main (void)
|
||||||
|
|
||||||
magnetometer_init(&car_struct);
|
magnetometer_init(&car_struct);
|
||||||
|
|
||||||
// printf("Magnetometer initialized!\n");
|
// printf("Magnetometer initialized!\n");
|
||||||
|
|
||||||
magnetometer_tasks_init(&car_struct);
|
magnetometer_tasks_init(&car_struct);
|
||||||
|
|
||||||
vTaskStartScheduler();
|
vTaskStartScheduler();
|
||||||
|
|
||||||
// launch();
|
// launch();
|
||||||
|
|
||||||
return(0);
|
return (0);
|
||||||
}
|
}
|
|
@ -9,9 +9,9 @@
|
||||||
#ifndef TEST_PROJECT_MAP_H
|
#ifndef TEST_PROJECT_MAP_H
|
||||||
#define TEST_PROJECT_MAP_H
|
#define TEST_PROJECT_MAP_H
|
||||||
|
|
||||||
|
|
||||||
// Define the grid structure
|
// Define the grid structure
|
||||||
typedef struct {
|
typedef struct
|
||||||
|
{
|
||||||
bool **data; // 2D array to represent the grid
|
bool **data; // 2D array to represent the grid
|
||||||
int rows; // Number of rows in the grid
|
int rows; // Number of rows in the grid
|
||||||
int cols; // Number of columns in the grid
|
int cols; // Number of columns in the grid
|
||||||
|
@ -21,16 +21,20 @@ typedef struct {
|
||||||
Grid *car_path_grid;
|
Grid *car_path_grid;
|
||||||
|
|
||||||
// Function to create and initialize a grid
|
// Function to create and initialize a grid
|
||||||
Grid *create_grid(int rows, int cols) {
|
Grid *
|
||||||
Grid *grid = (Grid *) malloc(sizeof(Grid));
|
create_grid(int rows, int cols)
|
||||||
|
{
|
||||||
|
Grid *grid = (Grid *)malloc(sizeof(Grid));
|
||||||
grid->rows = rows;
|
grid->rows = rows;
|
||||||
grid->cols = cols;
|
grid->cols = cols;
|
||||||
|
|
||||||
// Allocate memory for the 2D array
|
// Allocate memory for the 2D array
|
||||||
grid->data = (bool **) malloc(rows * sizeof(bool *));
|
grid->data = (bool **)malloc(rows * sizeof(bool *));
|
||||||
for (int i = 0; i < rows; i++) {
|
for (int i = 0; i < rows; i++)
|
||||||
grid->data[i] = (bool *) malloc(cols * sizeof(bool));
|
{
|
||||||
for (int j = 0; j < cols; j++) {
|
grid->data[i] = (bool *)malloc(cols * sizeof(bool));
|
||||||
|
for (int j = 0; j < cols; j++)
|
||||||
|
{
|
||||||
grid->data[i][j] = false; // Initialize to 'false' (unvisited)
|
grid->data[i][j] = false; // Initialize to 'false' (unvisited)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,23 +43,32 @@ Grid *create_grid(int rows, int cols) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to mark a cell as visited
|
// Function to mark a cell as visited
|
||||||
void mark_cell(Grid *grid, int row, int col) {
|
void
|
||||||
if (row >= 0 && row < grid->rows && col >= 0 && col < grid->cols) {
|
mark_cell(Grid *grid, int row, int col)
|
||||||
|
{
|
||||||
|
if (row >= 0 && row < grid->rows && col >= 0 && col < grid->cols)
|
||||||
|
{
|
||||||
grid->data[row][col] = true;
|
grid->data[row][col] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to check if a cell has been visited
|
// Function to check if a cell has been visited
|
||||||
bool is_cell_visited(Grid *grid, int row, int col) {
|
bool
|
||||||
if (row >= 0 && row < grid->rows && col >= 0 && col < grid->cols) {
|
is_cell_visited(Grid *grid, int row, int col)
|
||||||
|
{
|
||||||
|
if (row >= 0 && row < grid->rows && col >= 0 && col < grid->cols)
|
||||||
|
{
|
||||||
return grid->data[row][col];
|
return grid->data[row][col];
|
||||||
}
|
}
|
||||||
return false; // Consider out-of-bounds as unvisited
|
return false; // Consider out-of-bounds as unvisited
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to destroy the grid and free memory
|
// Function to destroy the grid and free memory
|
||||||
void destroy_grid(Grid *grid) {
|
void
|
||||||
for (int i = 0; i < grid->rows; i++) {
|
destroy_grid(Grid *grid)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < grid->rows; i++)
|
||||||
|
{
|
||||||
free(grid->data[i]);
|
free(grid->data[i]);
|
||||||
}
|
}
|
||||||
free(grid->data);
|
free(grid->data);
|
||||||
|
@ -64,12 +77,15 @@ void destroy_grid(Grid *grid) {
|
||||||
|
|
||||||
// Function to update the map based on car's current orientation
|
// Function to update the map based on car's current orientation
|
||||||
// Function to update the map based on car's current orientation and position
|
// Function to update the map based on car's current orientation and position
|
||||||
void update_map(int orientation, int cur_x, int cur_y) {
|
void
|
||||||
|
update_map(int orientation, int cur_x, int cur_y)
|
||||||
|
{
|
||||||
// Define offsets for different orientations
|
// Define offsets for different orientations
|
||||||
int offset_x = 0;
|
int offset_x = 0;
|
||||||
int offset_y = 0;
|
int offset_y = 0;
|
||||||
|
|
||||||
switch (orientation) {
|
switch (orientation)
|
||||||
|
{
|
||||||
case NORTH:
|
case NORTH:
|
||||||
offset_y = 1;
|
offset_y = 1;
|
||||||
break;
|
break;
|
||||||
|
@ -105,24 +121,27 @@ void update_map(int orientation, int cur_x, int cur_y) {
|
||||||
mark_cell(car_path_grid, cur_x + offset_x, cur_y + offset_y);
|
mark_cell(car_path_grid, cur_x + offset_x, cur_y + offset_y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Function to print the map
|
// Function to print the map
|
||||||
void print_map() {
|
void
|
||||||
|
print_map()
|
||||||
|
{
|
||||||
// Invert the map, 0,0 is at the Middle
|
// Invert the map, 0,0 is at the Middle
|
||||||
// Print 1 for visited cells and 0 for unvisited cells
|
// Print 1 for visited cells and 0 for unvisited cells
|
||||||
for (int i = car_path_grid->rows - 1; i >= 0; i--) {
|
for (int i = car_path_grid->rows - 1; i >= 0; i--)
|
||||||
for (int j = 0; j < car_path_grid->cols; j++) {
|
{
|
||||||
|
for (int j = 0; j < car_path_grid->cols; j++)
|
||||||
|
{
|
||||||
(car_path_grid->data[j][i]) ? printf("1 ") : printf("0 ");
|
(car_path_grid->data[j][i]) ? printf("1 ") : printf("0 ");
|
||||||
// case false:
|
// case false:
|
||||||
// printf("0 ");
|
// printf("0 ");
|
||||||
// break;
|
// break;
|
||||||
// case true:
|
// case true:
|
||||||
// printf("1 ");
|
// printf("1 ");
|
||||||
// break;
|
// break;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //TEST_PROJECT_MAP_H
|
#endif // TEST_PROJECT_MAP_H
|
||||||
|
|
Loading…
Reference in New Issue