Header file for PID control. More...
#include "pathfinding/maze.h"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... | |
Header file for PID control.
| #define PID_DEGREES_NORMALISE | ( | x | ) | ((x + 180) % 360 - 180) |
Normalises degrees to be between 0 and 360.
| [in] | x | Degrees to normalise. |
| typedef 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.
| [in] | target_bearing | Target bearing of the car in degrees. |
| [out] | current_bearing | Current bearing of the car in degrees. |
| [in,out] | p_pid_params | Pointer 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.
| float pid_calculate_correction | ( | float | current_bearing, |
| float | target_bearing, | ||
| float | current_ratio, | ||
| pid_params_t * | p_pid_params | ||
| ) |
Calculates the PID control signal.
| [in] | current_bearing | Current bearing of the car in degrees. |
| [in] | target_bearing | Target bearing of the car in degrees. |
| [in] | current_ratio | Current motor differential ratio. |
| [in,out] | p_pid_params | Pointer to the PID parameters. |
References pid_params::integral, pid_params::k_d, pid_params::k_i, pid_params::k_p, PID_DEGREES_NORMALISE, and pid_params::prev_error.
| void pid_init_error_correction | ( | pid_params_t * | p_pid_params | ) |
Sets defaults for PID parameters.
| [out] | p_pid_params | Pointer to the PID parameters. |
References pid_params::current_bearing, pid_params::current_ratio, pid_params::epsilon, pid_params::integral, pid_params::k_d, pid_params::k_i, pid_params::k_p, PID_EPSILON, PID_KD, PID_KI, PID_KP, PID_RATIO_TO_BEARING, pid_params::prev_error, pid_params::ratio_to_bearing, and pid_params::setpoint.
| void pid_init_structs | ( | pid_turn_params_t * | p_turn_params | ) |
Sets up pid struct(s).
| [in,out] | p_turn_params | Pointer 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.
| 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.
| [in,out] | p_turn_params | Pointer to the turn parameters. |
| [in] | direction | Direction 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().