|
8 | 8 | #pragma once |
9 | 9 | #include "shared.h" |
10 | 10 |
|
| 11 | + |
| 12 | +/** |
| 13 | + * @def BASE_PWM_ARR_LEN |
| 14 | + * @brief The length of the base PWM array. |
| 15 | + * |
| 16 | + * This macro defines the length of the base PWM ring buffer array. |
| 17 | + */ |
11 | 18 | #define BASE_PWM_ARR_LEN 16 |
12 | 19 |
|
| 20 | +/** |
| 21 | + * @brief Enumeration for controller operating modes. |
| 22 | + * |
| 23 | + * Defines the two controller operating modes. |
| 24 | + * |
| 25 | + * @enum controller_mode_t |
| 26 | + */ |
13 | 27 | typedef enum { |
14 | | - STARTUP_MODE, |
15 | | - PID_MODE, |
| 28 | + STARTUP_MODE, /**< Controller startup mode state */ |
| 29 | + PID_MODE, /**< Controller pid mode state */ |
16 | 30 | } controller_mode_t; |
17 | 31 |
|
| 32 | +/** |
| 33 | + * @brief Structure for startup controller parameters and variables. |
| 34 | + * |
| 35 | + * This structure defines the parameters and variables used in the startup |
| 36 | + * controller, including the latest PWM level, base pwm level ring buffer array and the respective ring buffer index. |
| 37 | + * |
| 38 | + * @typedef startup_parameters_t |
| 39 | + * @struct startup_parameters_t |
| 40 | + */ |
18 | 41 | typedef struct startup_parameters_t { |
19 | | - uint16_t level; |
20 | | - uint16_t base_pwm_arr[BASE_PWM_ARR_LEN]; |
21 | | - uint16_t base_pwm_arr_i; |
| 42 | + uint16_t level; /**< Latest level */ |
| 43 | + uint16_t base_pwm_arr[BASE_PWM_ARR_LEN]; /**< base pwm ring buffer array */ |
| 44 | + uint16_t base_pwm_arr_i; /**< base pwm ring buffer array index */ |
22 | 45 | }startup_parameters_t; |
23 | 46 |
|
| 47 | +/** |
| 48 | + * @brief Structure for PID controller parameters and variables. |
| 49 | + * |
| 50 | + * This structure defines the parameters and variables used in the PID |
| 51 | + * controller, including gains, error tracking, integrator limits, ... |
| 52 | + * |
| 53 | + * @typedef pid_parameters_t |
| 54 | + * @struct pid_parameters_t |
| 55 | + */ |
24 | 56 | typedef struct pid_parameters_t{ |
25 | 57 | // PID Controller variables |
26 | | - float k_i; // Integral gain |
27 | | - float k_d; // Derivative gain |
28 | | - float t; // Sampling time |
29 | | - float tau; // Low-pass-filter time constant |
30 | | - float ci_0; // (k_i*t)/2 |
31 | | - float cd_0; // (k_d*2)/(2*tau+t) |
32 | | - float cd_1; // (2*tau-t)/(2*tau+t) |
33 | | - float int_lim_max; // max limit for integrator |
34 | | - float int_lim_min; // min limit for integrator |
35 | | - float max_output; // max possible pwm output value |
36 | | - float k_p; // Proportional gain |
37 | | - float e; // Error |
38 | | - float e_prev; // Previous Error |
39 | | - float i_prev; // Previous Integral Value |
40 | | - float d_prev; // Previous Derivative Value |
41 | | - // Variable gain variables |
42 | | - float k_p_x_1_shift; |
43 | | - float k_p_x_1; |
44 | | - float k_p_x_2; |
45 | | - float k_p_y_0; |
46 | | - float k_p_y_1; |
47 | | - float k_p_y_2; |
48 | | - float k_p_m_1; |
49 | | - float k_p_m_2; |
50 | | -} pid_parameter_t; |
| 58 | + float k_i; /**< Integral gain */ |
| 59 | + float k_d; /**< Derivative gain */ |
| 60 | + float t; /**< Sampling time */ |
| 61 | + float tau; /**< Low-pass-filter time constant */ |
| 62 | + float ci_0; /**< (k_i*t)/2 */ |
| 63 | + float cd_0; /**< (k_d*2)/(2*tau+t) */ |
| 64 | + float cd_1; /**< (2*tau-t)/(2*tau+t) */ |
| 65 | + float int_lim_max; /**< max limit for integrator */ |
| 66 | + float int_lim_min; /**< min limit for integrator */ |
| 67 | + float max_output; /**< max possible pwm output value */ |
| 68 | + float k_p; /**< Proportional gain */ |
| 69 | + float e; /**< Error */ |
| 70 | + float e_prev; /**< Previous Error */ |
| 71 | + float i_prev; /**< Previous Integral Value */ |
| 72 | + float d_prev; /**< Previous Derivative Value */ |
| 73 | + // Variable gain variables - See gain scheduling documentation for more details |
| 74 | + float k_p_x_1_shift; /**< Effectively defines where x1 is shifted from 0% of max setpoint to 100% of max setpoint */ |
| 75 | + float k_p_x_1; /**< x1 */ |
| 76 | + float k_p_x_2; /**< x2 */ |
| 77 | + float k_p_y_0; /**< y0 = KP @ x0 */ |
| 78 | + float k_p_y_1; /**< y1 = KP @ x1 */ |
| 79 | + float k_p_y_2; /**< y2 = KP @ x2 */ |
| 80 | + float k_p_m_1; /**< slope from (x0, y0) to (x1, y1) */ |
| 81 | + float k_p_m_2; /**< slope from (x1, y1) to (x2, y2) */ |
| 82 | +} pid_parameters_t; |
51 | 83 |
|
| 84 | +/** |
| 85 | + * @brief Structure for various controller parameters. |
| 86 | + * |
| 87 | + * This structure defines the highest level general controller parameters, including |
| 88 | + * General settings (setpoint, mode), startup controller settings struct, PID controller struct, and measurement data. |
| 89 | + * |
| 90 | + * @typedef controller_parameter_t |
| 91 | + * @struct controller_parameter_t |
| 92 | + */ |
52 | 93 | typedef struct controller_parameter_t{ |
53 | | - /*! |
54 | | - * General Variables |
55 | | - */ |
56 | | - controller_mode_t mode; // Controller mode |
57 | | - float feed_fwd; // Feed forward value |
58 | | - uint32_t setpoint; // Current setpoint |
59 | | - uint16_t speed_table[127]; // Array with setpoint values corresponding to every speed step |
60 | | - |
61 | | - /*! |
62 | | - * Startup controller mode specific variables |
63 | | - */ |
64 | | - startup_parameters_t startup; |
65 | | - |
66 | | - /*! |
67 | | - * PID Controller variables |
68 | | - */ |
69 | | - pid_parameter_t pid; |
70 | | - |
71 | | - /*! |
72 | | - * Measurement Variables |
73 | | - */ |
74 | | - float measurement; // Newest Measurement value |
75 | | - float measurement_prev; // Previous Measurement |
76 | | - float measurement_corrected; // measurement - adc_offset = measurement_corrected |
77 | | - float adc_offset; // ADC offset value |
78 | | - uint8_t msr_delay_in_us; // Delay before VEMF Measurement |
79 | | - uint8_t msr_total_iterations; // Amount of samples |
80 | | - uint8_t l_side_arr_cutoff; // Discarded samples (left side) |
81 | | - uint8_t r_side_arr_cutoff; // Discarded samples (right side) |
82 | | - |
| 94 | + // General controller parameters |
| 95 | + controller_mode_t mode; /**< Current controller mode */ |
| 96 | + float feed_fwd; /**< Current feed forward value set by startup controller */ |
| 97 | + uint32_t setpoint; /**< Current setpoint */ |
| 98 | + uint16_t speed_table[127]; /**< Array with setpoint values corresponding to every speed step */ |
| 99 | + // Startup controller parameters |
| 100 | + startup_parameters_t startup; /**< Struct for startup controller specific variables */ |
| 101 | + // PID controller parameters |
| 102 | + pid_parameters_t pid; /**< Struct for PID controller specific variables */ |
| 103 | + // Measurement variables |
| 104 | + float measurement; /**< Latest measurement value */ |
| 105 | + float measurement_prev; /**< Previous measurement value */ |
| 106 | + float measurement_corrected; /**< Corrected measurement value (measurement - adc_offset = measurement_corrected) */ |
| 107 | + float adc_offset; /**< ADC offset value */ |
| 108 | + uint8_t msr_delay_in_us; /**< Delay before V_EMF is measured */ |
| 109 | + uint8_t msr_total_iterations; /**< Amount of samples */ |
| 110 | + uint8_t l_side_arr_cutoff; /**< Discarded outlier samples (left side) */ |
| 111 | + uint8_t r_side_arr_cutoff; /**< Discarded outlier samples (right side) */ |
83 | 112 | } controller_parameter_t; |
84 | 113 |
|
85 | 114 |
|
|
0 commit comments