Repository navigation
Expand file tree
/
Copy pathtest_state_space_simple.c
More file actions
203 lines (168 loc) · 5.57 KB
/
Copy pathtest_state_space_simple.c
File metadata and controls
203 lines (168 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @file test_state_space_simple.c
* @brief Simple test for state space control algorithms
*/
#include <stdio.h>
#include <math.h>
#include "cal_pole_placement.h"
#include "cal_lqr.h"
#define EPSILON 0.01
void test_pole_placement() {
printf("\n=== Testing Pole Placement ===\n");
// Simple 2nd order system: double integrator
// x_dot = [0 1; 0 0] * x + [0; 1] * u
cal_real_t A_data[] = {
0.0, 1.0,
0.0, 0.0
};
cal_real_t B_data[] = {
0.0,
1.0
};
// Desired poles: -2, -3 (stable, fast response)
cal_real_t poles[] = {-2.0, -3.0};
// Initialize controller
cal_pole_placement_t pp;
cal_error_t err = cal_pole_placement_init(&pp, 2, 1);
if (err != CAL_OK) {
printf("ERROR: Failed to initialize pole placement controller: %d\n", err);
return;
}
// Set system
err = cal_pole_placement_set_system(&pp, A_data, B_data);
if (err != CAL_OK) {
printf("ERROR: Failed to set system: %d\n", err);
cal_pole_placement_free(&pp);
return;
}
// Set desired poles
err = cal_pole_placement_set_poles(&pp, poles);
if (err != CAL_OK) {
printf("ERROR: Failed to set poles: %d\n", err);
cal_pole_placement_free(&pp);
return;
}
// Compute gain
err = cal_pole_placement_compute_gain(&pp);
if (err != CAL_OK) {
printf("ERROR: Failed to compute gain: %d\n", err);
cal_pole_placement_free(&pp);
return;
}
printf("Computed gain K: [%.4f, %.4f]\n", pp.K.data[0], pp.K.data[1]);
// Expected gain for poles at -2, -3: K = [6, 5]
// (from characteristic equation: s^2 + 5s + 6 = (s+2)(s+3))
cal_real_t expected_K0 = 6.0;
cal_real_t expected_K1 = 5.0;
if (fabs(pp.K.data[0] - expected_K0) < EPSILON &&
fabs(pp.K.data[1] - expected_K1) < EPSILON) {
printf("PASS: Pole placement gain is correct\n");
} else {
printf("FAIL: Expected K = [%.4f, %.4f], got [%.4f, %.4f]\n",
expected_K0, expected_K1, pp.K.data[0], pp.K.data[1]);
}
// Test control computation
cal_real_t state[] = {1.0, 0.5};
cal_real_t control;
err = cal_pole_placement_control(&pp, state, &control);
if (err != CAL_OK) {
printf("ERROR: Failed to compute control: %d\n", err);
} else {
printf("Control for state [%.1f, %.1f]: u = %.4f\n", state[0], state[1], control);
// u = -K*x = -[6, 5]*[1.0, 0.5] = -(6*1.0 + 5*0.5) = -8.5
cal_real_t expected_u = -8.5;
if (fabs(control - expected_u) < EPSILON) {
printf("PASS: Control computation is correct\n");
} else {
printf("FAIL: Expected u = %.4f, got %.4f\n", expected_u, control);
}
}
// Clean up
cal_pole_placement_free(&pp);
}
void test_lqr() {
printf("\n=== Testing LQR ===\n");
// Simple 2nd order system: double integrator
// x_dot = [0 1; 0 0] * x + [0; 1] * u
cal_real_t A_data[] = {
0.0, 1.0,
0.0, 0.0
};
cal_real_t B_data[] = {
0.0,
1.0
};
// Cost matrices
// Q: penalize position and velocity equally
cal_real_t Q_data[] = {
1.0, 0.0,
0.0, 1.0
};
// R: control effort penalty
cal_real_t R_data[] = {1.0};
// Initialize controller
cal_lqr_t lqr;
cal_error_t err = cal_lqr_init(&lqr, 2, 1);
if (err != CAL_OK) {
printf("ERROR: Failed to initialize LQR controller: %d\n", err);
return;
}
// Set system
err = cal_lqr_set_system(&lqr, A_data, B_data);
if (err != CAL_OK) {
printf("ERROR: Failed to set system: %d\n", err);
cal_lqr_free(&lqr);
return;
}
// Set cost matrices
err = cal_lqr_set_cost(&lqr, Q_data, R_data);
if (err != CAL_OK) {
printf("ERROR: Failed to set cost: %d\n", err);
cal_lqr_free(&lqr);
return;
}
// Set solver parameters
err = cal_lqr_set_solver_params(&lqr, 1e-4, 500);
if (err != CAL_OK) {
printf("ERROR: Failed to set solver params: %d\n", err);
cal_lqr_free(&lqr);
return;
}
// Compute gain
printf("Computing LQR gain (this may take a moment)...\n");
err = cal_lqr_compute_gain(&lqr);
if (err != CAL_OK) {
printf("ERROR: Failed to compute gain: %d\n", err);
cal_lqr_free(&lqr);
return;
}
printf("Computed gain K: [%.4f, %.4f]\n", lqr.K.data[0], lqr.K.data[1]);
// For this simple system with Q=I, R=1, the optimal gain should be approximately:
// K ≈ [1.732, 1.414] (from solving ARE)
// We'll just check that the gain is reasonable (positive values)
if (lqr.K.data[0] > 0 && lqr.K.data[1] > 0) {
printf("PASS: LQR gain has reasonable values\n");
} else {
printf("FAIL: LQR gain has unexpected values\n");
}
// Test control computation
cal_real_t state[] = {1.0, 0.5};
cal_real_t control;
err = cal_lqr_control(&lqr, state, &control);
if (err != CAL_OK) {
printf("ERROR: Failed to compute control: %d\n", err);
} else {
printf("Control for state [%.1f, %.1f]: u = %.4f\n", state[0], state[1], control);
printf("PASS: Control computation completed\n");
}
// Clean up
cal_lqr_free(&lqr);
}
int main() {
printf("State Space Control Algorithms Test\n");
printf("====================================\n");
test_pole_placement();
test_lqr();
printf("\n=== All tests completed ===\n");
return 0;
}