暂存的更新
This commit is contained in:
104
app/jj_motion.c
104
app/jj_motion.c
@@ -1,5 +1,5 @@
|
||||
#include "jj_motion.h"
|
||||
|
||||
#include <math.h>
|
||||
#include "zf_common_headfile.h"
|
||||
#include "pid.h"
|
||||
|
||||
@@ -51,12 +51,15 @@ float sp_Kd = 0.0f;
|
||||
float in_speed;
|
||||
float out_speed;
|
||||
|
||||
float set_speed0 = 460.0f;
|
||||
float set_speed1 = 460.0f;
|
||||
int cnt1 = 0;
|
||||
int cnt2 = 0;
|
||||
uint8_t in_state = 0;
|
||||
uint8_t in_stop = 0;
|
||||
float set_speed0 = 200.0f;
|
||||
float set_speed1 = 300.0f;
|
||||
int cnt1 = 0;
|
||||
int cnt2 = 0;
|
||||
int cnt3 = 0;
|
||||
uint8_t cnt3_flag = 0;
|
||||
uint8_t in_state = 0;
|
||||
uint8_t in_stop = 0;
|
||||
uint8_t last_state = 0;
|
||||
|
||||
uint32_t pwm_duty_ls;
|
||||
uint32_t pwm_duty_rs;
|
||||
@@ -86,43 +89,70 @@ float sport_get_speed(void)
|
||||
|
||||
void sport_motion(void)
|
||||
{
|
||||
float temp;
|
||||
float tt;
|
||||
if (in_state == 1) {
|
||||
temp = 2.5f;//fabs(tanf(myclip_f(in_angle, -45, 45)/ 180 * PI));
|
||||
tt = myclip_f(fabs(in_angle), 0, 50.f);
|
||||
} else {
|
||||
temp = 1.f;
|
||||
tt = 0;
|
||||
}
|
||||
imu660ra_get_gyro();
|
||||
in_gyro = imu660ra_gyro_z * 0.8f + in_gyro * 0.2f; // 陀螺仪输入
|
||||
|
||||
if (1 == in_stop) {
|
||||
bt_fly_flag = bt_run_flag = 0;
|
||||
}
|
||||
if (0 == in_state || 2 == in_state) {
|
||||
PID_SetTunings(&far_gyro_pid, an_Kp1, an_Ki1, an_Kd1);
|
||||
// pid参数切换
|
||||
if (last_state == 1 && (in_state == 2 || in_state == 0)) { // 直道
|
||||
cnt3_flag = 0;
|
||||
cnt3 = 0;
|
||||
bt_printf("to 0");
|
||||
PID_SetTunings(&far_angle_pid, an_Kp1, an_Ki1, an_Kd1);
|
||||
PID_SetTunings(&near_pos_pid, po_Kp1, po_Ki1, po_Kd1);
|
||||
PID_SetTunings(&far_gyro_pid, gy_Kp1, gy_Ki1, gy_Kd1);
|
||||
} else {
|
||||
PID_SetTunings(&far_gyro_pid, an_Kp0, an_Ki0, an_Kd0);
|
||||
// PID_SetPoints(&speed_pid, &set_speed1);
|
||||
}
|
||||
if (last_state != 1 && in_state == 1) { // 弯道
|
||||
bt_printf("to 1");
|
||||
cnt3_flag = 1;
|
||||
PID_SetTunings(&far_angle_pid, an_Kp0, an_Ki0, an_Kd0);
|
||||
PID_SetTunings(&near_pos_pid, po_Kp0, po_Ki0, po_Kd0);
|
||||
PID_SetTunings(&far_gyro_pid, gy_Kp0, gy_Ki0, gy_Kd0);
|
||||
// PID_SetPoints(&speed_pid, &set_speed0);
|
||||
}
|
||||
last_state = in_state;
|
||||
if (cnt3_flag == 1 && in_state == 1) {
|
||||
cnt3++;
|
||||
if (cnt3 >= 200) // 200ms
|
||||
{
|
||||
bt_printf("to 2");
|
||||
cnt3_flag = 2;
|
||||
cnt3 = 0;
|
||||
}
|
||||
}
|
||||
/* 动力风扇设置 */
|
||||
if (1 == bt_run_flag) {
|
||||
cnt1++;
|
||||
cnt2++;
|
||||
|
||||
imu660ra_get_gyro();
|
||||
in_gyro = imu660ra_gyro_z; // 陀螺仪输入
|
||||
PID_Compute(&far_gyro_pid);
|
||||
|
||||
PID_Compute(&near_pos_pid);
|
||||
if (cnt1 >= 10) {
|
||||
in_speed = -1 * sport_get_speed();
|
||||
in_speed = sport_get_speed();
|
||||
PID_Compute(&speed_pid);
|
||||
|
||||
PID_Compute(&near_pos_pid);
|
||||
|
||||
cnt1 = 0;
|
||||
}
|
||||
if (cnt2 >= 20) {
|
||||
cnt2 = 0;
|
||||
PID_Compute(&far_angle_pid);
|
||||
}
|
||||
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);
|
||||
|
||||
pwm_duty_ls = (int32_t)myclip_f(-1.5f * out_pos + temp * out_gyro, 0.0f, 6000.f);
|
||||
pwm_duty_rs = (int32_t)myclip_f(1.5f * out_pos - temp * out_gyro, 0.0f, 6000.f);
|
||||
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);
|
||||
|
||||
by_pwm_power_duty((int32_t)pwm_duty_ls, (int32_t)pwm_duty_rs, (int32_t)pwm_duty_lb, (int32_t)pwm_duty_rb);
|
||||
} else {
|
||||
@@ -133,7 +163,21 @@ void sport_motion(void)
|
||||
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 (cnt3_flag == 2) // 超时
|
||||
{
|
||||
if (in_angle > 0) {
|
||||
by_pwm_update_duty(bt_fly + 500 - 1.f * tt, bt_fly + 500 - 0.5f * tt);
|
||||
} else {
|
||||
by_pwm_update_duty(bt_fly + 500 - 0.5f * tt, bt_fly + 500 - 1.f * tt);
|
||||
}
|
||||
} else {
|
||||
if (in_angle > 0) {
|
||||
by_pwm_update_duty(bt_fly + 500 - 3.f * tt, bt_fly + 500 - 1.f * tt);
|
||||
} else {
|
||||
by_pwm_update_duty(bt_fly + 500 - 1.f * tt, bt_fly + 500 - 3.f * tt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,30 +188,30 @@ void sport_motion(void)
|
||||
void sport_pid_init()
|
||||
{
|
||||
/* 角度控制 */
|
||||
PID(&far_angle_pid, &in_angle, &out_angle, &set_angle, an_Kp1, an_Ki1, an_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
|
||||
PID(&far_angle_pid, &in_angle, &out_angle, &set_angle, an_Kp1, an_Ki1, an_Kd1, _PID_P_ON_M, _PID_CD_REVERSE);
|
||||
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);
|
||||
|
||||
/* 角速度控制 */
|
||||
PID(&far_gyro_pid, &in_gyro, &out_gyro, &out_angle, gy_Kp1, gy_Ki1, gy_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
|
||||
PID(&far_gyro_pid, &in_gyro, &out_gyro, &out_angle, gy_Kp1, gy_Ki1, gy_Kd1, _PID_P_ON_M, _PID_CD_REVERSE);
|
||||
PID_SetMode(&far_gyro_pid, _PID_MODE_AUTOMATIC);
|
||||
PID_SetSampleTime(&far_gyro_pid, 1);
|
||||
PID_SetOutputLimits(&far_gyro_pid, -4000.0f, 4000.0f);
|
||||
PID_SetOutputLimits(&far_gyro_pid, -3000.0f, 3000.0f);
|
||||
// PID_Init(&far_gyro_pid);
|
||||
|
||||
/* 近点控制 */
|
||||
PID(&near_pos_pid, &in_pos, &out_pos, &set_pos, po_Kp1, po_Ki1, po_Kd1, _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, -4000.0f, 4000.0f);
|
||||
PID_SetSampleTime(&near_pos_pid, 1);
|
||||
PID_SetOutputLimits(&near_pos_pid, -3000.0f, 3000.0f);
|
||||
// PID_Init(&near_pos_pid);
|
||||
|
||||
/* 速度控制 */
|
||||
PID(&speed_pid, &in_speed, &out_speed, &set_speed1, sp_Kp, sp_Ki, sp_Kd, _PID_P_ON_E, _PID_CD_DIRECT);
|
||||
PID(&speed_pid, &in_speed, &out_speed, &set_speed1, sp_Kp, sp_Ki, sp_Kd, _PID_P_ON_M, _PID_CD_DIRECT);
|
||||
PID_SetMode(&speed_pid, _PID_MODE_AUTOMATIC);
|
||||
PID_SetSampleTime(&speed_pid, 10);
|
||||
PID_SetOutputLimits(&speed_pid, 0.0f, 2000.0f);
|
||||
PID_SetOutputLimits(&speed_pid, -2500.0f, 3000.0f);
|
||||
// PID_Init(&speed_pid);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user