137 lines
4.8 KiB
C
137 lines
4.8 KiB
C
//
|
|
// Created by SJQ on 2023/12/30.
|
|
//
|
|
|
|
#include "balance.h"
|
|
|
|
static void mylqr_k(float L0, float K[12]);
|
|
|
|
void Balance_Control_Init(Balance_Control_t* balanceControl,Balance_Init_config_s InitConfig)
|
|
{
|
|
Leg_Init(&balanceControl->leg_right,&InitConfig.legInitConfig);
|
|
Leg_Init(&balanceControl->leg_left,&InitConfig.legInitConfig);
|
|
|
|
LQR_Init(&balanceControl->balance_LQR,InitConfig.LQR_state_num,InitConfig.LQR_control_num);
|
|
PIDInit(&balanceControl->leg_coordination_PID,&InitConfig.leg_cor_PID_config);
|
|
memset(balanceControl->state,0,6);
|
|
memset(balanceControl->target_state,0,6);
|
|
}
|
|
// leg_phi[4] = {phi1_r,phi4_r,phi1_l,phi4_l}
|
|
void Balance_feedback_update(Balance_Control_t* balanceControl,float leg_phi[4],float leg_phi_dot[4],float body_phi,float body_phi_dot,float x,float x_dot)
|
|
{
|
|
Leg_State_update(&balanceControl->leg_right,leg_phi[0],leg_phi[1],leg_phi_dot[0],leg_phi_dot[1]);
|
|
Leg_State_update(&balanceControl->leg_left,leg_phi[2],leg_phi[3],leg_phi_dot[2],leg_phi_dot[3]);
|
|
|
|
float K_matrix[12]={0};
|
|
float L_average = balanceControl->leg_right.legState.L0;
|
|
//(balanceControl->leg_right.legState.L0 + balanceControl->leg_left.legState.L0)/2;
|
|
float alpha = PI/2 - balanceControl->leg_right.legState.phi0;
|
|
|
|
float theta = alpha - body_phi;
|
|
|
|
// static uint32_t dot_cnt = 0;
|
|
// float dot_dt = DWT_GetDeltaT(&dot_cnt);
|
|
// float theta_dot1 = (balanceControl->state[0] - theta) / dot_dt;
|
|
|
|
|
|
float theta_dot = - balanceControl->leg_right.legState.phi0_dot - body_phi_dot;
|
|
|
|
balanceControl->state[0] = theta;
|
|
balanceControl->state[1] = theta_dot;
|
|
balanceControl->state[2] = x; balanceControl->state[3] = x_dot;
|
|
balanceControl->state[4] = body_phi; balanceControl->state[5] = body_phi_dot;
|
|
|
|
mylqr_k(L_average,K_matrix);
|
|
float K_matrix_fix[12];
|
|
for (int i = 0; i < 6; ++i) {
|
|
K_matrix_fix[i] = K_matrix[2*i];
|
|
}
|
|
for (int i = 0; i < 6; ++i) {
|
|
K_matrix_fix[6+i] = K_matrix[2*i+1];
|
|
}
|
|
|
|
LQR_set_K(&balanceControl->balance_LQR,K_matrix_fix);
|
|
}
|
|
|
|
void Balance_Control_Loop(Balance_Control_t* balanceControl)
|
|
{
|
|
LQR_update(&balanceControl->balance_LQR,balanceControl->state,balanceControl->target_state);
|
|
|
|
float Tp = -balanceControl->balance_LQR.control_vector[1]; //髋关节力矩
|
|
float T = balanceControl->balance_LQR.control_vector[0]; //驱动轮力矩
|
|
|
|
LegInstance *leg_r,*leg_l;
|
|
leg_r = &balanceControl->leg_right;
|
|
leg_l = &balanceControl->leg_left;
|
|
|
|
float F_pid_out_r = PIDCalculate(&leg_r->Length_PID,leg_r->legState.L0,leg_r->target_L0);
|
|
float F_pid_out_l = PIDCalculate(&leg_l->Length_PID,leg_l->legState.L0,leg_l->target_L0);
|
|
|
|
float Tp_pid_out_r = PIDCalculate(&leg_r->Phi0_PID,leg_r->legState.phi0,leg_r->target_phi0);
|
|
float Tp_pid_out_l = PIDCalculate(&leg_l->Phi0_PID,leg_l->legState.phi0,leg_l->target_phi0);
|
|
|
|
float coordination_err = leg_r->legState.phi0 - leg_l->legState.phi0;
|
|
|
|
float cor_Tp = PIDCalculate(&balanceControl->leg_coordination_PID,coordination_err,0);
|
|
|
|
Leg_Input_update(&leg_r->legInput,F_pid_out_r+leg_r->F_feedforward,Tp + cor_Tp);
|
|
Leg_Input_update(&leg_l->legInput,F_pid_out_l+leg_l->F_feedforward,Tp - cor_Tp);
|
|
|
|
VMC_getT(leg_r);
|
|
VMC_getT(leg_l);
|
|
}
|
|
|
|
void target_x_set(Balance_Control_t* balanceControl,float x)
|
|
{
|
|
balanceControl->target_state [2] = x;
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* MYLQR_K MATLAB生成
|
|
* K = MYLQR_K(L0)
|
|
*
|
|
* Arguments : float L0
|
|
* float K[12]
|
|
* Return Type : void
|
|
*/
|
|
void mylqr_k(float L0, float K[12])
|
|
{
|
|
float t2;
|
|
float t3;
|
|
/* This function was generated by the Symbolic Math Toolbox version 9.2.
|
|
*/
|
|
/* 10-Jan-2024 15:16:33 */
|
|
t2 = L0 * L0;
|
|
t3 = t2 * L0;
|
|
K[0] = ((L0 * -79.173111F - t2 * 4.61935F) + t3 * 53.7748F) - 1.68270767F;
|
|
K[1] =
|
|
((L0 * 163.861588F - t2 * 454.401855F) + t3 * 423.311035F) + 0.115447611F;
|
|
K[2] = ((L0 * -5.08536911F - t2 * 30.8126068F) + t3 * 29.5321121F) -
|
|
0.590248764F;
|
|
K[3] =
|
|
((L0 * 14.4037905F - t2 * 24.0141125F) + t3 * 13.8384714F) + 0.648175836F;
|
|
K[4] =
|
|
((L0 * 10.8009977F - t2 * 72.3231735F) + t3 * 103.75676F) - 5.87192822F;
|
|
K[5] =
|
|
((L0 * 48.2422218F - t2 * 312.532166F) + t3 * 420.993896F) + 12.3104877F;
|
|
K[6] =
|
|
((L0 * 22.1635437F - t2 * 126.369873F) + t3 * 170.583176F) - 7.8162446F;
|
|
K[7] = ((L0 * 26.8571F - t2 * 228.328079F) + t3 * 316.050446F) + 14.7130146F;
|
|
K[8] =
|
|
((L0 * 15.8044338F - t2 * 196.455856F) + t3 * 276.879242F) + 20.4759655F;
|
|
K[9] =
|
|
((L0 * -80.1292953F + t2 * 698.451477F) - t3 * 1040.30969F) + 51.8307953F;
|
|
K[10] =
|
|
((L0 * -2.62232423F - t2 * 4.29178047F) + t3 * 7.45553255F) + 4.33007193F;
|
|
K[11] =
|
|
((L0 * -20.6260967F + t2 * 144.286728F) - t3 * 203.665939F) + 2.42666435F;
|
|
}
|
|
|
|
/*
|
|
* File trailer for mylqr_k.c
|
|
*
|
|
* [EOF]
|
|
*/
|