Skip to content

Commit 3f7ae35

Browse files
authored
Merge pull request #47 from UBC-Rocket/embedded-software/controls-pid-refactor/stage
Embedded software/controls pid refactor/stage
2 parents 2b33e36 + 120636d commit 3f7ae35

21 files changed

Lines changed: 644 additions & 497 deletions

File tree

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"cmake.sourceDirectory": "/home/j1m1/Personal/Dev/Projects/UBCRocket/thrust_vectoring_consolidated/embedded-software/ground-station",
3+
"C_Cpp.errorSquiggles": "disabled"
4+
}

embedded-software/firmware/libs/controls/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ target_include_directories(controls
1616
target_link_libraries(controls
1717
PUBLIC
1818
state_estimation
19+
utilities
1920
PRIVATE
2021
m
2122
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef FLIGHT_CONTROLLER_INTERNAL_H
2+
#define FLIGHT_CONTROLLER_INTERNAL_H
3+
4+
#include "controls/flight_controller.h"
5+
6+
void compute_axis_angle_err(const quaternion_t *q_ref,
7+
const quaternion_t *q_meas,
8+
float phi[3]);
9+
10+
void update_torque_pid(const float phi[3],
11+
const float dt_s,
12+
float torque_cmd[3]);
13+
14+
void decompose_torque(const float torque_cmd[3],
15+
const float thrust_dir[3],
16+
float torque_gimbal[3],
17+
float *torque_thrust_mag);
18+
19+
void compute_thrust_dir(const flight_controller_gimbal_config_t *gcfg,
20+
const float torque_cmd[3],
21+
const float thrust_dir[3],
22+
const float thrust_mag,
23+
float new_thrust_dir[3]);
24+
25+
void compute_gimbal_angles(const flight_controller_gimbal_config_t *gcfg,
26+
const float thrust_dir[3],
27+
float *theta_x_cmd,
28+
float *theta_y_cmd);
29+
30+
void update_thrust_pid(const flight_controller_thrust_config_t *tcfg,
31+
const float z_ref,
32+
const float vz_ref,
33+
const float z_meas,
34+
const float vz_meas,
35+
const float dt_s,
36+
float *T_cmd);
37+
38+
39+
#endif /* FLIGHT_CONTROLLER_INTERNAL_H */

embedded-software/firmware/libs/controls/include/controls/flight_controller.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ extern "C" {
1818
/** Attitude gains and inertia (Section 3). */
1919
typedef struct {
2020
float Kp[3][3]; /**< Proportional gain: attitude error -> torque [N·m/rad] */
21+
float Ki[3][3];
2122
float Kd[3][3]; /**< Derivative gain: angular rate -> torque [N·m·s/rad] */
2223
float I[3][3]; /**< Inertia matrix body frame [kg·m²] */
24+
// float x_integral_limit;
25+
// float y_integral_limit;
26+
// float z_integral_limit;
27+
// float x_torque_min;
28+
// float x_torque_max;
29+
// float y_torque_min;
30+
// float y_torque_max;
31+
// float z_torque_min;
32+
// float z_torque_max;
2333
} flight_controller_attitude_config_t;
2434

2535
/** Allocation: unit thrust direction in body frame (Section 4). */
2636
typedef struct {
27-
float t_hat[3]; /**< Unit thrust direction (e.g. [0,0,-1] for z-up) */
37+
float thrust_dir[3]; /**< Unit thrust direction (e.g. [0,0,-1] for z-up) */
2838
} flight_controller_allocation_config_t;
2939

3040
/** Gimbal geometry and angle limits (Section 5). */
@@ -83,6 +93,13 @@ typedef struct {
8393
*/
8494
void flight_controller_init(const flight_controller_config_t *config);
8595

96+
// /**
97+
// * @brief Set flight controller internal state.
98+
// * Call to update config (e.g. thrust gains)
99+
// * @param config Controller config (thrust gains used for z-PID).
100+
// */
101+
// void flight_controller_set_config(const flight_controller_config_t *config);
102+
86103
/**
87104
* @brief Run one step: torque → allocation → thrust PID → gimbal angles.
88105
* @param state Current estimated state (from state_exchange).
@@ -97,6 +114,12 @@ void flight_controller_run(const state_t *state,
97114
control_output_t *out,
98115
float dt_s);
99116

117+
/**
118+
* @brief Reset flight controller internal state (e.g. z-PID integral).
119+
*
120+
*/
121+
void flight_controller_reset(void);
122+
100123
#ifdef __cplusplus
101124
}
102125
#endif

embedded-software/firmware/libs/controls/include/controls/pid.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ float pid_compute(pid_controller_t *pid,
114114
float measurement,
115115
float dt);
116116

117+
float pid_compute_pv(pid_controller_t *pid,
118+
float x_ref,
119+
float v_ref,
120+
float x_meas,
121+
float v_meas,
122+
float dt
123+
);
124+
117125
/**
118126
* @brief Reset PID controller state
119127
* @param pid: Pointer to PID controller structure

0 commit comments

Comments
 (0)