INF2004-Project v0.1
 
Loading...
Searching...
No Matches
pid.h
Go to the documentation of this file.
1
11#ifndef PID_H // Include guard.
12#define PID_H
13
14#include "pathfinding/maze.h"
15
16// Definitions.
17// -----------------------------------------------------------------------------
18//
19
27#define PID_ENCODER_STEP_TURN_90_DEG 18
29#define PID_ENCODER_STEP_TURN_180_DEG 36
31#define PID_ENCODER_STEP_MOVE 25
33#define PID_ENCODER_CENTER_OFFSET 5 // End of pid_encoder_constants group.
36
44#define PID_KP 0.01f
46#define PID_KI 0.05f
48#define PID_KD 0.01f
50#define PID_EPSILON 0.01f
52#define PID_RATIO_TO_BEARING -0.035f
54#define PID_ENCODER_PIN 21
56#define PID_BIAS_LEFT_RATIO 1.05f
57 // End of pid_constants group.
60
67#define PID_DEGREES_NORMALISE(x) ((x + 180) % 360 - 180) // Normalise degrees.
68
69// Type definitions.
70// -----------------------------------------------------------------------------
71//
72
77typedef struct pid_params
78{
79 float k_p;
80 float k_i;
81 float k_d;
82 float epsilon;
84 float setpoint;
85 float integral;
86 float prev_error;
90
94typedef struct pid_turn_params
95{
103
104// Public function declarations.
105// -----------------------------------------------------------------------------
106//
107
108void pid_init_error_correction(pid_params_t *p_pid_params);
109void pid_init_structs(pid_turn_params_t *p_turn_params);
110void pid_navigate_turn(pid_turn_params_t *p_turn_params,
111 maze_cardinal_direction_t direction);
112float pid_calculate_correction(float current_bearing,
113 float target_bearing,
114 float current_ratio,
115 pid_params_t *p_pid_params);
116void pid_bearing_correction(float target_bearing,
117 float current_bearing,
118 pid_params_t *p_pid_params);
119
120#endif // PID_H
121
122// End of file pid.h
Header file for the maze data structure and public functions.
maze_cardinal_direction_t
This enum contains the possible directions.
Definition: maze.h:47
void pid_init_structs(pid_turn_params_t *p_turn_params)
Sets up pid struct(s).
Definition: pid.c:20
void pid_navigate_turn(pid_turn_params_t *p_turn_params, maze_cardinal_direction_t direction)
Function to turn the car and move in a given direction.
Definition: pid.c:37
float pid_calculate_correction(float current_bearing, float target_bearing, float current_ratio, pid_params_t *p_pid_params)
Calculates the PID control signal.
Definition: pid.c:144
struct pid_params pid_params_t
Struct for PID parameters.
void pid_bearing_correction(float target_bearing, float current_bearing, pid_params_t *p_pid_params)
Updates the motor differential ratio based on the target bearing and current bearing.
Definition: pid.c:173
void pid_init_error_correction(pid_params_t *p_pid_params)
Sets defaults for PID parameters.
Definition: pid.c:120
struct pid_turn_params pid_turn_params_t
Struct for turn parameters.
Struct for PID parameters.
Definition: pid.h:78
float integral
Integral for PID.
Definition: pid.h:85
float current_ratio
Current ratio for PID.
Definition: pid.h:87
float current_bearing
Current bearing for PID.
Definition: pid.h:88
float epsilon
Small epsilon value to prevent division by 0.
Definition: pid.h:82
float k_d
Gain for derivative term.
Definition: pid.h:81
float prev_error
Previous error for PID.
Definition: pid.h:86
float ratio_to_bearing
Ratio to bearing for PID.
Definition: pid.h:83
float k_p
Gain for proportional term.
Definition: pid.h:79
float setpoint
Setpoint for PID.
Definition: pid.h:84
float k_i
Gain for integral term.
Definition: pid.h:80
Struct for turn parameters.
Definition: pid.h:95
uint b_is_turning
Flag to indicate if the car is turning.
Definition: pid.h:96
uint b_is_centered
Flag to indicate if the car is centered.
Definition: pid.h:99
uint encoder_step_count
Current encoder step count.
Definition: pid.h:97
char turn_direction
Direction to turn.
Definition: pid.h:98
uint b_is_turn_complete
Flag to indicate if the turn is complete.
Definition: pid.h:100
uint b_is_moved_cell
Flag to indicate if the car has moved a cell.
Definition: pid.h:101