INF2004-Project v0.1
 
Loading...
Searching...
No Matches
pid.h File Reference

Header file for PID control. More...

#include "pathfinding/maze.h"
Include dependency graph for pid.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pid_params
 Struct for PID parameters. More...
 
struct  pid_turn_params
 Struct for turn parameters. More...
 

Macros

#define PID_BIAS_LEFT_RATIO   1.05f
 Bias for left ratio.
 
#define PID_DEGREES_NORMALISE(x)   ((x + 180) % 360 - 180)
 Normalises degrees to be between 0 and 360. More...
 
#define PID_ENCODER_CENTER_OFFSET   5
 Number of encoder steps from the center of a cell.
 
#define PID_ENCODER_PIN   21
 Encoder pin for PID.
 
#define PID_ENCODER_STEP_MOVE   25
 Number of encoder steps to move 1 cell.
 
#define PID_ENCODER_STEP_TURN_180_DEG   36
 Number of encoder steps to turn 180 deg.
 
#define PID_ENCODER_STEP_TURN_90_DEG   18
 Number of encoder steps to turn 90 deg.
 
#define PID_EPSILON   0.01f
 Epsilon for PID. A small value to prevent division by 0.
 
#define PID_KD   0.01f
 Gain for derivative term.
 
#define PID_KI   0.05f
 Gain for integral term.
 
#define PID_KP   0.01f
 Gain for proportional term.
 
#define PID_RATIO_TO_BEARING   -0.035f
 Ratio to bearing for PID.
 

Typedefs

typedef struct pid_params pid_params_t
 Struct for PID parameters. More...
 
typedef struct pid_turn_params pid_turn_params_t
 Struct for turn parameters.
 

Functions

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. More...
 
float pid_calculate_correction (float current_bearing, float target_bearing, float current_ratio, pid_params_t *p_pid_params)
 Calculates the PID control signal. More...
 
void pid_init_error_correction (pid_params_t *p_pid_params)
 Sets defaults for PID parameters. More...
 
void pid_init_structs (pid_turn_params_t *p_turn_params)
 Sets up pid struct(s). More...
 
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. More...
 

Detailed Description

Header file for PID control.

Author
Bryan Seah
Version
0.1
Date
2023-10-29

Macro Definition Documentation

◆ PID_DEGREES_NORMALISE

#define PID_DEGREES_NORMALISE (   x)    ((x + 180) % 360 - 180)

Normalises degrees to be between 0 and 360.

Parameters
[in]xDegrees to normalise.
Returns
Normalised degrees.

Typedef Documentation

◆ pid_params_t

typedef struct pid_params pid_params_t

Struct for PID parameters.

Function Documentation

◆ pid_bearing_correction()

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.

Parameters
[in]target_bearingTarget bearing of the car in degrees.
[out]current_bearingCurrent bearing of the car in degrees.
[in,out]p_pid_paramsPointer to the PID parameters.

References pid_params::current_bearing, pid_params::current_ratio, pid_params::epsilon, magneto_get_curr_bearing(), motor_update_ratio(), PID_BIAS_LEFT_RATIO, pid_calculate_correction(), and pid_params::ratio_to_bearing.

Here is the call graph for this function:

◆ pid_calculate_correction()

float pid_calculate_correction ( float  current_bearing,
float  target_bearing,
float  current_ratio,
pid_params_t p_pid_params 
)

Calculates the PID control signal.

Parameters
[in]current_bearingCurrent bearing of the car in degrees.
[in]target_bearingTarget bearing of the car in degrees.
[in]current_ratioCurrent motor differential ratio.
[in,out]p_pid_paramsPointer to the PID parameters.
Returns
float Control signal.

References pid_params::integral, pid_params::k_d, pid_params::k_i, pid_params::k_p, PID_DEGREES_NORMALISE, and pid_params::prev_error.

Here is the caller graph for this function:

◆ pid_init_error_correction()

void pid_init_error_correction ( pid_params_t p_pid_params)

◆ pid_init_structs()

void pid_init_structs ( pid_turn_params_t p_turn_params)

Sets up pid struct(s).

Parameters
[in,out]p_turn_paramsPointer to the turn parameters.

References pid_turn_params::b_is_centered, pid_turn_params::b_is_moved_cell, pid_turn_params::b_is_turn_complete, pid_turn_params::b_is_turning, pid_turn_params::encoder_step_count, and pid_turn_params::turn_direction.

Here is the caller graph for this function:

◆ pid_navigate_turn()

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.

Parameters
[in,out]p_turn_paramsPointer to the turn parameters.
[in]directionDirection to turn.

References pid_turn_params::b_is_centered, pid_turn_params::b_is_moved_cell, pid_turn_params::b_is_turn_complete, pid_turn_params::b_is_turning, pid_turn_params::encoder_step_count, MAZE_EAST, MAZE_SOUTH, MAZE_WEST, motor_move_forward(), motor_turn_left(), motor_turn_right(), PID_ENCODER_CENTER_OFFSET, PID_ENCODER_STEP_MOVE, PID_ENCODER_STEP_TURN_180_DEG, PID_ENCODER_STEP_TURN_90_DEG, and pid_init_structs().

Here is the call graph for this function:
Here is the caller graph for this function: