pref: 优化控制pid,将角速度环输出加到侧面风扇上
This commit is contained in:
@@ -42,6 +42,13 @@ float set_speed = 0.0f;
|
||||
|
||||
int cnt1 = 0;
|
||||
int cnt2 = 0;
|
||||
|
||||
static float myclip_f(float x, float low, float up)
|
||||
{
|
||||
return (x > up ? up : x < low ? low
|
||||
: x);
|
||||
}
|
||||
|
||||
float sport_get_speed(void)
|
||||
{
|
||||
#define ALPHA (0.97f)
|
||||
@@ -60,56 +67,51 @@ float sport_get_speed(void)
|
||||
|
||||
void sport_motion(void)
|
||||
{
|
||||
|
||||
cnt1++;
|
||||
cnt2++;
|
||||
|
||||
imu660ra_get_gyro();
|
||||
in_gyro = imu660ra_gyro_z; // 陀螺仪输入
|
||||
// in_angle = 0; // 图像远端输入
|
||||
// in_pos = 0; // 图像近端输入
|
||||
// 清除计数
|
||||
PID_Compute(&far_gyro_pid);
|
||||
|
||||
if (cnt1 >= 10) {
|
||||
in_speed = -1 * sport_get_speed();
|
||||
PID_Compute(&speed_pid);
|
||||
|
||||
PID_Compute(&near_pos_pid);
|
||||
|
||||
cnt1 = 0;
|
||||
}
|
||||
|
||||
if (cnt2 >= 20) {
|
||||
cnt2 = 0;
|
||||
PID_Compute(&far_angle_pid);
|
||||
}
|
||||
|
||||
/* 动力风扇设置 */
|
||||
if (1 == bt_run_flag) {
|
||||
if (out_gyro > 0) {
|
||||
by_pwm_power_duty((int32_t)(500 - 1 * out_pos + 0 * out_gyro),
|
||||
(int32_t)(500 + 1 * out_pos - 0 * out_gyro),
|
||||
(int32_t)(500 + out_speed + out_gyro),
|
||||
(int32_t)(500 + out_speed - out_gyro));
|
||||
cnt1++;
|
||||
cnt2++;
|
||||
|
||||
} else if (out_gyro <= 0) {
|
||||
by_pwm_power_duty((int32_t)(500 - 1 * out_pos + 0 * out_gyro),
|
||||
(int32_t)(500 + 1 * out_pos - 0 * out_gyro),
|
||||
(int32_t)(500 + out_speed + out_gyro),
|
||||
(int32_t)(500 + out_speed - out_gyro));
|
||||
imu660ra_get_gyro();
|
||||
in_gyro = imu660ra_gyro_z; // 陀螺仪输入
|
||||
// in_angle = 0; // 图像远端输入
|
||||
// in_pos = 0; // 图像近端输入
|
||||
// 清除计数
|
||||
PID_Compute(&far_gyro_pid);
|
||||
|
||||
if (cnt1 >= 10) {
|
||||
in_speed = -1 * sport_get_speed();
|
||||
PID_Compute(&speed_pid);
|
||||
|
||||
PID_Compute(&near_pos_pid);
|
||||
|
||||
cnt1 = 0;
|
||||
}
|
||||
|
||||
if (cnt2 >= 20) {
|
||||
cnt2 = 0;
|
||||
PID_Compute(&far_angle_pid);
|
||||
}
|
||||
uint32_t pwm_duty_ls = (uint32_t)myclip_f(-1 * out_pos + 1.5 * out_gyro, 0.0f, 5000.f);
|
||||
uint32_t pwm_duty_rs = (uint32_t)myclip_f(1 * out_pos - 1.5 * out_gyro, 0.0f, 5000.f);
|
||||
uint32_t pwm_duty_lb = (uint32_t)myclip_f(1 * out_speed + out_gyro, 0.0f, 5000.f);
|
||||
uint32_t pwm_duty_rb = (uint32_t)myclip_f(1 * out_speed - out_gyro, 0.0f, 5000.f);
|
||||
by_pwm_power_duty(pwm_duty_ls, pwm_duty_rs, pwm_duty_lb, pwm_duty_rb);
|
||||
|
||||
// by_pwm_power_duty(500+out_gyro, 500-out_gyro, 500+out_gyro , 500-out_gyro);
|
||||
} else {
|
||||
by_pwm_power_duty(500, 500, 500, 500);
|
||||
by_pwm_power_duty(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* 升力风扇设置 */
|
||||
if (bt_fly_flag == 0) {
|
||||
by_pwm_update_duty(0 + 500, 0 + 500);
|
||||
} else {
|
||||
by_pwm_update_duty(bt_fly + 500, bt_fly + 500);
|
||||
if (in_angle > 7.5 || in_angle < -7.5) {
|
||||
by_pwm_update_duty(bt_fly + 450, bt_fly + 450);
|
||||
} else {
|
||||
by_pwm_update_duty(bt_fly + 500, bt_fly + 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +139,7 @@ void sport_pid_init()
|
||||
PID(&near_pos_pid, &in_pos, &out_pos, &set_pos, po_Kp, po_Ki, po_Kd, _PID_P_ON_E, _PID_CD_DIRECT);
|
||||
PID_SetMode(&near_pos_pid, _PID_MODE_AUTOMATIC);
|
||||
PID_SetSampleTime(&near_pos_pid, 10);
|
||||
PID_SetOutputLimits(&near_pos_pid, -3000.0f, 3000.0f);
|
||||
PID_SetOutputLimits(&near_pos_pid, -5000.0f, 5000.0f);
|
||||
// PID_Init(&near_pos_pid);
|
||||
|
||||
/* 速度控制 */
|
||||
|
||||
Reference in New Issue
Block a user