64 #define SOLVER_MIN 100
65 #define SOLVER_MAX 11000
71 #define SOLVER_KF_TORQUE_EPSILON 0.000000001f
72 #define SOLVER_KF_BIAS_EPSILON 0.000000001f
73 #define SOLVER_LQR_RATE_EPSILON 0.00000001f
74 #define SOLVER_LQR_TORQUE_EPSILON 0.000001f
143 memcpy(P, nP,
sizeof(
float)*9);
202 for (
int i = 0;
i < iterations;
i++) {
227 X0 = nX0 -
K0*(nX0 - signal);
228 X1 = nX1 -
K1*(nX0 - signal);
229 float in2 = input*input;
258 A01 = beta*(tau - tau*expf(-Ts/tau));
259 A02 = beta*(tau - tau*expf(-Ts/tau)) - Ts*beta;
261 A12 = expf(-Ts/tau) - 1;
264 B0 = Ts*beta - beta*(tau - tau*expf(-Ts/tau));
265 B1 = 1 - expf(-Ts/tau);
274 rtkf_t rtkf_create(
float beta,
float tau,
float Ts,
float R,
float q1,
float q2,
float q3,
float biaslim)
278 memset(state, 0,
sizeof(*state));
322 float div = (R + B0B0*
P00 + B1B1*P11 + B0B1*(
P01 +
P10));
324 nP[0][0] = (
Q00*R +
P00*(A00A00*R + B0B0*
Q00) +
325 B1B1*(P11*
Q00 + A00A00*(P00P11 - P01P10)) +
330 float common = A01*(
P00*R + B1B1*(P00P11 - P01P10)) - A11*B0B1*(P00P11 + P01P10);
332 nP[1][0] = (A00*(A11*P10*R + common))
336 nP[0][1] = (A00*(A11*
P01*R + common))
341 A11A11*(P11*R + B0B0*(P00P11 - P01P10)) +
342 B1B1*(P11*
Q11 + A01A01*P00P11 - A01A01*P01P10) +
343 A01*A11*
P01*R + A01*A11*P10*R + B0B1*
P01*
Q11 +
344 B0B1*(P10*
Q11 - 2*A01*A11*P00P11 + 2*A01*A11*P01P10))
355 div = (R + B1*B1*P11 + B0*(B0*
P00 + B1*(
P01 +
P10)));
358 nK[0] = (A00*(B0*
P00 + B1*
P10)) / div;
359 nK[1] = (A01*(B0*
P00 + B1*
P10) + A11*(B0*
P01 + B1*P11)) / div;
412 for (
int i = 0;
i < iterations;
i++) {
445 A01 = -beta*tau*(expf(-Ts/tau) - 1);
449 B0 = Ts*beta + beta*tau*(expf(-Ts/tau) - 1);
450 B1 = 1 - expf(-Ts/tau);
464 memset(state, 0,
sizeof(*state));
468 state->Q11 = q2*expf(beta);
482 lqr->Q11 = q2*expf(lqr->
beta);
524 float xr0 = x_est[0] - setpoint;
526 float u = x_est[2] - lqr->K0 * xr0 - lqr->K1 * x_est[1];
528 else if (u > 1) u = 1;
542 memset(state, 0,
sizeof(*state));
552 *rate = lqg->
rtkf->
X[0];
553 *torque = lqg->
rtkf->
X[1];
554 *bias = lqg->
rtkf->
X[2];
559 lqg->
rtkf->
X[0] = x0;
573 return lqg ? lqg->
rtkf : NULL;
578 return lqg ? lqg->
lqr : NULL;
rtkf_t lqg_get_rtkf(lqg_t lqg)
void rtkf_stabilize_covariance(rtkf_t rtkf, int iterations)
lqg_t lqg_create(rtkf_t rtkf, lqr_t lqr)
#define LQG_SOLVER_RUNNING
void lqr_get_gains(lqr_t lqr, float K[2])
lqr_t lqg_get_lqr(lqg_t lqg)
#define SOLVER_KF_TORQUE_EPSILON
void rtkf_prediction_step(float A[3][3], float B[3], float K[3], float X[3], float signal, float input)
void lqg_set_x0(lqg_t lqg, float x0)
void * PIOS_malloc_no_dma(size_t size)
void lqr_update(lqr_t lqr, float q1, float q2, float r)
bool lqr_calculate_covariance_2x2(float A[2][2], float B[2], float K[2], float P[2][2], float Q[2][2], float R)
void lqr_stabilize_covariance(lqr_t lqr, int iterations)
#define SOLVER_KF_BIAS_EPSILON
#define SOLVER_LQR_TORQUE_EPSILON
bool rtkf_calculate_covariance_3x3(float A[3][3], float K[3], float P[3][3], float Q[3][3], float R)
int rtkf_solver_status(rtkf_t rtkf)
#define SOLVER_LQR_RATE_EPSILON
void lqr_initialize_matrices_int(float A[2][2], float B[2], float beta, float tau, float Ts)
float bound_min_max(float val, float min, float max)
Bound input value between min and max.
float bound_sym(float val, float range)
Bound input value within range (plus or minus)
void lqg_run_covariance(lqg_t lqg, int iter)
void lqg_get_rtkf_state(lqg_t lqg, float *rate, float *torque, float *bias)
#define LQG_SOLVER_FAILED
lqr_t lqr_create(float beta, float tau, float Ts, float q1, float q2, float r)
rtkf_t rtkf_create(float beta, float tau, float Ts, float R, float q1, float q2, float q3, float biaslim)
void rtkf_initialize_matrices_int(float A[3][3], float B[3], float beta, float tau, float Ts)
void rtkf_predict_axis(rtkf_t rtkf, float signal, float input, float Xout[3])
float lqg_controller(lqg_t lqg, float signal, float setpoint)
#define PIOS_Assert(test)
int lqr_solver_status(lqr_t lqr)
int lqg_solver_status(lqg_t lqg)