Files
QD4C-firmware/app/jj_motion.c

239 lines
7.0 KiB
C
Raw Normal View History

2024-01-16 20:03:21 +08:00
#include "jj_motion.h"
2024-05-05 19:15:51 +08:00
#include <math.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;
2024-05-23 15:53:21 +08:00
PID_TypeDef angle_pid;
2024-03-02 16:05:24 +08:00
PID_TypeDef far_gyro_pid;
PID_TypeDef speed_pid;
2024-06-07 21:55:55 +08:00
// 弯道后面风扇角度环
float an_Kp0 = 51.0f;
2024-05-23 15:53:21 +08:00
float an_Ki0 = 0.0f;
float an_Kd0 = 0.0f;
2024-06-07 21:55:55 +08:00
// 直道后面风扇角度环
float an_Kp1 = 42.0f;
2024-05-23 15:53:21 +08:00
float an_Ki1 = 0.0f;
float an_Kd1 = 0.0f;
2024-06-07 21:55:55 +08:00
// 圆环后面风扇角度环
float yu_Kp0 = 40.0f;
float yu_Ki0 = 0.0f;
float yu_Kd0 = 0.0f;
// 弯道侧面风扇角度环
float cn_Kp1 = 420.0f;
float cn_Ki1 = 0.f;
float cn_Kd1 = 0.0f;
// 直道侧面风扇角度环
float po_Kp1 = 600.0f;
float po_Ki1 = 0.0f;
float po_Kd1 = 0.0f;
// 期望和当前量
2024-03-02 16:05:24 +08:00
float in_angle;
float set_angle = 0.0f;
float out_angle;
2024-06-07 21:55:55 +08:00
float in_pos;
float out_pos;
float set_pos = 0.0f;
// 弯道角速度环
float gy_Kp0 = 2.50f;
2024-03-28 04:24:59 +08:00
float gy_Ki0 = 0.0f;
2024-05-23 15:53:21 +08:00
float gy_Kd0 = 0.0f;
2024-06-07 21:55:55 +08:00
// 直道角速度环
float gy_Kp1 = 4.2f;
2024-03-28 04:24:59 +08:00
float gy_Ki1 = 0.0f;
2024-05-23 15:53:21 +08:00
float gy_Kd1 = 0.0f;
2024-06-07 21:55:55 +08:00
// 圆环角速度环
float ygy_Kp0 = 0.0f;
float ygy_Ki0 = 0.0f;
float ygy_Kd0 = 0.0f;
// 当前和输入量
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-06-07 21:55:55 +08:00
// 速度环
2024-05-23 15:53:21 +08:00
float sp_Kp = 16.0f;
2024-06-07 21:55:55 +08:00
float sp_Ki = 42.f;
2024-03-02 16:05:24 +08:00
float sp_Kd = 0.0f;
2024-06-07 21:55:55 +08:00
2024-03-02 16:05:24 +08:00
float in_speed;
float out_speed;
2024-03-28 04:30:33 +08:00
2024-06-07 21:55:55 +08:00
float set_speed;
float set_speed0 = 520.0f;
float set_speed1 = 900.0f;
float set_speed2 = 550.f;
2024-05-05 19:15:51 +08:00
int cnt1 = 0;
int cnt2 = 0;
int cnt3 = 0;
2024-06-05 20:17:45 +08:00
int cnt4 = 0;
2024-05-05 19:15:51 +08:00
uint8_t cnt3_flag = 0;
uint8_t in_state = 0;
uint8_t in_stop = 0;
uint8_t last_state = 0;
2024-06-07 21:55:55 +08:00
uint8_t stop_flag = 0;
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;
2024-04-16 10:54:44 +08:00
speed_now = ALPHA * (float)encoder_get_count(TIM3_ENCOEDER) + (1.0f - ALPHA) * speed_last;
2024-03-05 19:38:31 +08:00
speed_last = speed_now;
2024-04-16 10:54:44 +08:00
encoder_clear_count(TIM3_ENCOEDER);
2024-03-05 19:38:31 +08:00
return speed_now;
#undef ALPHA
}
2024-06-07 21:55:55 +08:00
void sport_motion()
2024-01-16 20:03:21 +08:00
{
2024-05-05 19:15:51 +08:00
2024-05-23 15:53:21 +08:00
imu660ra_get_gyro();
2024-06-07 21:55:55 +08:00
imu660ra_get_acc();
2024-05-23 15:53:21 +08:00
in_gyro = imu660ra_gyro_z;
// 停车
2024-06-07 21:55:55 +08:00
if (imu660ra_acc_z <= 1000) {
2024-04-16 10:54:44 +08:00
bt_fly_flag = bt_run_flag = 0;
2024-03-30 11:51:14 +08:00
}
2024-06-07 21:55:55 +08:00
if (1 == in_stop) {
stop_flag = 1;
}
if (stop_flag == 1) {
cnt4++;
if (cnt4 >= 200) {
bt_fly_flag = bt_run_flag = 0;
}
}
/* 动力风扇设置 */
if (1 == bt_run_flag) {
cnt1++;
cnt2++;
2024-05-23 15:53:21 +08:00
// pid参数切换
if ((last_state != in_state) && (in_state == 0 || in_state == 2)) { // 直道
cnt3_flag = 0;
cnt3 = 0;
2024-06-05 20:17:45 +08:00
bt_printf("to 直道");
2024-05-23 15:53:21 +08:00
set_speed = set_speed1;
PID_SetTunings(&far_angle_pid, an_Kp1, an_Ki1, an_Kd1);
PID_SetTunings(&far_gyro_pid, gy_Kp1, gy_Ki1, gy_Kd1);
2024-06-07 21:55:55 +08:00
PID_SetTunings(&angle_pid, po_Kp1, po_Ki1, po_Kd1);
2024-05-23 15:53:21 +08:00
}
2024-06-05 20:17:45 +08:00
if ((last_state != in_state) && in_state == 1) { // 弯道
bt_printf("to 入弯");
2024-05-23 15:53:21 +08:00
set_speed = set_speed0;
cnt3_flag = 1;
PID_SetTunings(&far_angle_pid, an_Kp0, an_Ki0, an_Kd0);
PID_SetTunings(&far_gyro_pid, gy_Kp0, gy_Ki0, gy_Kd0);
2024-06-07 21:55:55 +08:00
PID_SetTunings(&angle_pid, cn_Kp1, cn_Ki1, cn_Kd1);
2024-05-23 15:53:21 +08:00
}
2024-06-07 21:55:55 +08:00
if ((last_state != in_state) && in_state == 3) { // 圆环
2024-06-05 20:17:45 +08:00
bt_printf("to 圆环");
set_speed = set_speed2;
cnt3_flag = 1;
PID_SetTunings(&far_angle_pid, an_Kp0, an_Ki0, an_Kd0);
2024-06-07 21:55:55 +08:00
PID_SetTunings(&far_angle_pid, an_Kp0, an_Ki0, an_Kd0);
PID_SetTunings(&angle_pid, cn_Kp1, cn_Ki1, cn_Kd1);
2024-06-05 20:17:45 +08:00
}
2024-05-23 15:53:21 +08:00
last_state = in_state;
2024-06-05 20:17:45 +08:00
if (cnt3_flag == 1 && (in_state == 1 || in_state == 3)) {
2024-05-23 15:53:21 +08:00
cnt3++;
if (cnt3 >= 500) // 200ms
{
2024-06-05 20:17:45 +08:00
bt_printf("to 环内");
2024-05-23 15:53:21 +08:00
cnt3_flag = 2;
cnt3 = 0;
}
}
2024-06-05 20:17:45 +08:00
if (cnt1 >= 2) {
PID_Compute(&far_gyro_pid);
cnt1 = 0;
}
2024-06-05 20:17:45 +08:00
if (cnt2 >= 10) {
in_speed = sport_get_speed();
PID_Compute(&speed_pid);
PID_Compute(&far_angle_pid);
2024-05-23 15:53:21 +08:00
PID_Compute(&angle_pid);
2024-06-07 21:55:55 +08:00
cnt2 = 0;
2024-05-23 15:53:21 +08:00
}
if (in_state == 0 || in_state == 2) {
2024-06-05 20:17:45 +08:00
pwm_duty_ls = (int32_t)myclip_f(-out_pos, 0.0f, 6000.f);
pwm_duty_rs = (int32_t)myclip_f(out_pos, 0.0f, 6000.f);
2024-05-23 15:53:21 +08:00
pwm_duty_lb = (int32_t)myclip_f(out_speed + out_gyro, 0.0f, 6000.f);
pwm_duty_rb = (int32_t)myclip_f(out_speed - out_gyro, 0.0f, 6000.f);
2024-06-07 21:55:55 +08:00
} else if (in_state == 1 || in_state == 3) {
2024-05-23 15:53:21 +08:00
2024-06-07 21:55:55 +08:00
pwm_duty_ls = (int32_t)myclip_f(-out_pos, -6000.0f, 6000.f);
pwm_duty_rs = (int32_t)myclip_f(out_pos, -6000.0f, 6000.f);
2024-06-05 20:17:45 +08:00
pwm_duty_lb = (int32_t)myclip_f(out_speed + out_gyro, -6000.0f, 6000.f);
pwm_duty_rb = (int32_t)myclip_f(out_speed - out_gyro, -6000.0f, 6000.f);
2024-03-10 08:34:33 +08:00
}
2024-04-16 16:09:34 +08:00
by_pwm_power_duty((int32_t)pwm_duty_ls, (int32_t)pwm_duty_rs, (int32_t)pwm_duty_lb, (int32_t)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-05-23 15:53:21 +08:00
if (in_state == 1 && cnt3_flag == 1) {
2024-06-05 20:17:45 +08:00
float tt = 60 * fabs(tanf(myclip_f(in_angle, -60.f, 60.f) / 180 * PI));
by_pwm_update_duty(bt_fly + 500 - tt, bt_fly + 500 - tt);
2024-05-05 19:15:51 +08:00
} else {
2024-05-23 15:53:21 +08:00
by_pwm_update_duty(bt_fly + 500, bt_fly + 500);
2024-05-05 19:15:51 +08:00
}
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-05-23 15:53:21 +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);
2024-05-23 15:53:21 +08:00
PID_SetSampleTime(&far_angle_pid, 10);
2024-03-22 11:35:15 +08:00
PID_SetOutputLimits(&far_angle_pid, -3000.0f, 3000.0f);
2024-05-23 15:53:21 +08:00
2024-06-07 21:55:55 +08:00
PID(&angle_pid, &in_angle, &out_pos, &set_pos, po_Kp1, po_Ki1, po_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
2024-05-23 15:53:21 +08:00
PID_SetMode(&angle_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&angle_pid, 10);
PID_SetOutputLimits(&angle_pid, -6000.0f, 6000.0f);
2024-03-22 11:35:15 +08:00
/* 角速度控制 */
2024-05-23 15:53:21 +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); // m是增量
2024-03-22 11:35:15 +08:00
PID_SetMode(&far_gyro_pid, _PID_MODE_AUTOMATIC);
2024-05-23 15:53:21 +08:00
PID_SetSampleTime(&far_gyro_pid, 10);
2024-06-05 20:17:45 +08:00
PID_SetOutputLimits(&far_gyro_pid, -5000.0f, 5000.0f);
2024-03-22 11:35:15 +08:00
/* 速度控制 */
2024-05-23 15:53:21 +08:00
PID(&speed_pid, &in_speed, &out_speed, &set_speed, sp_Kp, sp_Ki, sp_Kd, _PID_P_ON_M, _PID_CD_DIRECT);
2024-03-22 11:35:15 +08:00
PID_SetMode(&speed_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&speed_pid, 10);
2024-06-05 20:17:45 +08:00
PID_SetOutputLimits(&speed_pid, -4000.0f, 5000.0f);
2024-01-16 20:03:21 +08:00
}