Files
QD4C-firmware/app/jj_motion.c

171 lines
4.6 KiB
C
Raw Normal View History

2024-01-16 20:03:21 +08:00
#include "jj_motion.h"
2024-03-22 11:35:15 +08:00
#include "zf_common_headfile.h"
#include "pid.h"
#include "jj_blueteeth.h"
2024-01-16 20:03:21 +08:00
#include "by_fan_control.h"
#include "by_imu.h"
2024-03-02 16:05:24 +08:00
PID_TypeDef far_angle_pid;
PID_TypeDef far_gyro_pid;
PID_TypeDef near_pos_pid;
PID_TypeDef speed_pid;
2024-03-28 04:30:33 +08:00
float an_Kp0 = 80.0f;
float an_Ki0 = 1.4f;
float an_Kd0 = 8.0f;
2024-03-28 04:24:59 +08:00
2024-03-28 04:30:33 +08:00
float an_Kp1 = 80.0f;
float an_Ki1 = 1.4f;
float an_Kd1 = 8.0f;
2024-03-02 16:05:24 +08:00
float in_angle;
float set_angle = 0.0f;
float out_angle;
2024-03-28 04:30:33 +08:00
float gy_Kp0 = 2.0f;
2024-03-28 04:24:59 +08:00
float gy_Ki0 = 0.0f;
2024-03-28 04:30:33 +08:00
float gy_Kd0 = 0.3f;
2024-03-28 04:24:59 +08:00
2024-03-28 04:30:33 +08:00
float gy_Kp1 = 2.0f;
2024-03-28 04:24:59 +08:00
float gy_Ki1 = 0.0f;
2024-03-28 04:30:33 +08:00
float gy_Kd1 = 0.3f;
2024-03-02 16:05:24 +08:00
float in_gyro;
float out_gyro;
2024-03-03 19:23:21 +08:00
// float set_gyro = 0.0f;
2024-03-02 16:05:24 +08:00
2024-03-28 04:30:33 +08:00
float po_Kp0 = 827.0f;
float po_Ki0 = 16.0f;
float po_Kd0 = 13.0f;
2024-03-28 04:24:59 +08:00
2024-03-28 04:30:33 +08:00
float po_Kp1 = 500.0f;
float po_Ki1 = 2.0f;
float po_Kd1 = 1.0f;
2024-03-02 16:05:24 +08:00
float in_pos;
float out_pos;
float set_pos = 0.0f;
2024-03-28 04:30:33 +08:00
float sp_Kp = 19.0f;
float sp_Ki = 0.5f;
2024-03-02 16:05:24 +08:00
float sp_Kd = 0.0f;
float in_speed;
float out_speed;
2024-03-28 04:30:33 +08:00
float set_speed0 = 460.0f;
float set_speed1 = 460.0f;
2024-03-28 04:24:59 +08:00
int cnt1 = 0;
int cnt2 = 0;
uint32_t in_state = 0;
2024-03-02 16:05:24 +08:00
2024-03-28 04:24:59 +08:00
uint32_t pwm_duty_ls;
uint32_t pwm_duty_rs;
uint32_t pwm_duty_lb;
uint32_t pwm_duty_rb;
static float myclip_f(float x, float low, float up)
{
return (x > up ? up : x < low ? low
: x);
}
2024-03-05 19:38:31 +08:00
float sport_get_speed(void)
{
#define ALPHA (0.97f)
static float speed_now = 0;
static float speed_last = 0;
speed_now = ALPHA * (float)encoder_get_count(TIM5_ENCOEDER) + (1.0f - ALPHA) * speed_last;
speed_last = speed_now;
encoder_clear_count(TIM5_ENCOEDER);
return speed_now;
#undef ALPHA
}
2024-03-02 16:05:24 +08:00
void sport_motion(void)
2024-01-16 20:03:21 +08:00
{
2024-03-28 04:24:59 +08:00
if (1 == in_state) {
PID_SetTunings(&far_gyro_pid, an_Kp1, an_Ki1, an_Kd1);
2024-03-28 04:30:33 +08:00
PID_SetTunings(&near_pos_pid, po_Kp1, po_Ki1, po_Kd1);
PID_SetTunings(&far_gyro_pid, gy_Kp1, gy_Ki1, gy_Kd1);
2024-03-28 04:24:59 +08:00
} else {
PID_SetTunings(&far_gyro_pid, an_Kp0, an_Ki0, an_Kd0);
2024-03-28 04:30:33 +08:00
PID_SetTunings(&near_pos_pid, po_Kp0, po_Ki0, po_Kd0);
PID_SetTunings(&far_gyro_pid, gy_Kp0, gy_Ki0, gy_Kd0);
2024-03-28 04:24:59 +08:00
}
/* 动力风扇设置 */
if (1 == bt_run_flag) {
cnt1++;
cnt2++;
2024-03-02 16:05:24 +08:00
imu660ra_get_gyro();
in_gyro = imu660ra_gyro_z; // 陀螺仪输入
PID_Compute(&far_gyro_pid);
2024-03-22 11:35:15 +08:00
if (cnt1 >= 10) {
in_speed = -1 * sport_get_speed();
PID_Compute(&speed_pid);
2024-03-22 11:35:15 +08:00
PID_Compute(&near_pos_pid);
2024-03-22 11:35:15 +08:00
cnt1 = 0;
}
if (cnt2 >= 20) {
cnt2 = 0;
PID_Compute(&far_angle_pid);
2024-03-10 08:34:33 +08:00
}
2024-03-28 04:24:59 +08:00
pwm_duty_ls = (uint32_t)myclip_f(-1.5f * out_pos + out_gyro, 0.0f, 8000.f);
pwm_duty_rs = (uint32_t)myclip_f(1.5f * out_pos - out_gyro, 0.0f, 8000.f);
pwm_duty_lb = (uint32_t)myclip_f(1.f * out_speed + out_gyro, 0.0f, 8000.f);
pwm_duty_rb = (uint32_t)myclip_f(1.f * out_speed - out_gyro, 0.0f, 8000.f);
2024-03-10 08:34:33 +08:00
2024-03-28 04:24:59 +08:00
by_pwm_power_duty(pwm_duty_ls, pwm_duty_rs, pwm_duty_lb, pwm_duty_rb);
2024-01-16 20:03:21 +08:00
} else {
by_pwm_power_duty(0, 0, 0, 0);
2024-01-16 20:03:21 +08:00
}
2024-03-22 11:35:15 +08:00
/* 升力风扇设置 */
2024-03-02 16:05:24 +08:00
if (bt_fly_flag == 0) {
by_pwm_update_duty(0 + 500, 0 + 500);
} else {
2024-03-27 13:26:35 +08:00
by_pwm_update_duty(bt_fly + 500, bt_fly + 500);
2024-03-02 16:05:24 +08:00
}
2024-01-16 20:03:21 +08:00
}
2024-03-05 19:38:31 +08:00
2024-01-16 20:03:21 +08:00
/**
* @brief
*
*/
void sport_pid_init()
{
2024-03-22 11:35:15 +08:00
/* 角度控制 */
2024-03-28 04:24:59 +08:00
PID(&far_angle_pid, &in_angle, &out_angle, &set_angle, an_Kp1, an_Ki1, an_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
2024-03-22 11:35:15 +08:00
PID_SetMode(&far_angle_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&far_angle_pid, 20);
PID_SetOutputLimits(&far_angle_pid, -3000.0f, 3000.0f);
// PID_Init(&far_angle_pid);
/* 角速度控制 */
2024-03-28 04:24:59 +08:00
PID(&far_gyro_pid, &in_gyro, &out_gyro, &out_angle, gy_Kp1, gy_Ki1, gy_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
2024-03-22 11:35:15 +08:00
PID_SetMode(&far_gyro_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&far_gyro_pid, 1);
2024-03-27 13:26:35 +08:00
PID_SetOutputLimits(&far_gyro_pid, -4000.0f, 4000.0f);
2024-03-22 11:35:15 +08:00
// PID_Init(&far_gyro_pid);
/* 近点控制 */
2024-03-28 04:24:59 +08:00
PID(&near_pos_pid, &in_pos, &out_pos, &set_pos, po_Kp1, po_Ki1, po_Kd1, _PID_P_ON_E, _PID_CD_DIRECT);
2024-03-22 11:35:15 +08:00
PID_SetMode(&near_pos_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&near_pos_pid, 10);
2024-03-27 13:26:35 +08:00
PID_SetOutputLimits(&near_pos_pid, -4000.0f, 4000.0f);
2024-03-22 11:35:15 +08:00
// PID_Init(&near_pos_pid);
/* 速度控制 */
2024-03-28 04:24:59 +08:00
PID(&speed_pid, &in_speed, &out_speed, &set_speed1, sp_Kp, sp_Ki, sp_Kd, _PID_P_ON_E, _PID_CD_DIRECT);
2024-03-22 11:35:15 +08:00
PID_SetMode(&speed_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&speed_pid, 10);
2024-03-27 13:26:35 +08:00
PID_SetOutputLimits(&speed_pid, 0.0f, 2000.0f);
2024-03-22 11:35:15 +08:00
// PID_Init(&speed_pid);
2024-01-16 20:03:21 +08:00
}