feat: 下位机发车

This commit is contained in:
2024-05-23 15:53:21 +08:00
parent 6b1f345ead
commit 55cc6b31f3
8 changed files with 150 additions and 106 deletions

View File

@@ -122,10 +122,10 @@ void by_pwm_update_duty(uint32_t update_pwm_duty1, uint32_t update_pwm_duty2)
*/
void by_pwm_power_duty(int32_t bpwm_duty_ls, int32_t bpwm_duty_rs, int32_t bpwm_duty_lb, int32_t bpwm_duty_rb)
{
bpwm_duty_ls = clip_s32(bpwm_duty_ls, 0, 5000);
bpwm_duty_rs = clip_s32(bpwm_duty_rs, 0, 5000);
bpwm_duty_lb = clip_s32(bpwm_duty_lb, -3000, 8000);
bpwm_duty_rb = clip_s32(bpwm_duty_rb, -3000, 8000);
// bpwm_duty_ls = clip_s32(bpwm_duty_ls, 0, 5000);
// bpwm_duty_rs = clip_s32(bpwm_duty_rs, 0, 5000);
// bpwm_duty_lb = clip_s32(bpwm_duty_lb, -3000, 8000);
// bpwm_duty_rb = clip_s32(bpwm_duty_rb, -3000, 8000);
// pwm_duty_ls_g = pwm_duty_ls;
// pwm_duty_rs_g = pwm_duty_rs;
@@ -136,19 +136,19 @@ void by_pwm_power_duty(int32_t bpwm_duty_ls, int32_t bpwm_duty_rs, int32_t bpwm_
pwm_set_duty(FAN_LS_PWM_A_PIN, bpwm_duty_ls);
pwm_set_duty(FAN_LS_PWM_B_PIN, 0);
}
// else {
// pwm_set_duty(FAN_LS_PWM_A_PIN, 0);
// pwm_set_duty(FAN_LS_PWM_B_PIN, -1 * pwm_duty_ls);
// }
else {
pwm_set_duty(FAN_LS_PWM_A_PIN, 0);
pwm_set_duty(FAN_LS_PWM_B_PIN, -1 * pwm_duty_ls);
}
if (bpwm_duty_rs >= 0) {
pwm_set_duty(FAN_RS_PWM_A_PIN, bpwm_duty_rs);
pwm_set_duty(FAN_RS_PWM_B_PIN, 0);
}
// else {
// pwm_set_duty(FAN_RS_PWM_A_PIN, 0);
// pwm_set_duty(FAN_RS_PWM_B_PIN, -1 * pwm_duty_rs);
// }
else {
pwm_set_duty(FAN_RS_PWM_A_PIN, 0);
pwm_set_duty(FAN_RS_PWM_B_PIN, -1 * pwm_duty_rs);
}
if (bpwm_duty_lb >= 0) {
pwm_set_duty(FAN_LB_PWM_A_PIN, bpwm_duty_lb);

View File

@@ -19,7 +19,7 @@ void by_frame_init(void)
uart_rx_interrupt(BY_FRAME_UART_INDEX, ENABLE);
}
void by_frame_send(uint32_t *data_array)
void by_frame_send(uint32_t* data_array)
{
uint16_t crc_cal = 0;
const uint8_t data_byte_num = BY_FRAME_DATA_NUM * sizeof(uint32_t);

View File

@@ -8,49 +8,58 @@
#include "by_imu.h"
PID_TypeDef far_angle_pid;
PID_TypeDef angle_pid;
PID_TypeDef far_gyro_pid;
PID_TypeDef near_pos_pid;
PID_TypeDef speed_pid;
float an_Kp0 = 80.0f;
float an_Ki0 = 1.4f;
float an_Kd0 = 8.0f;
float an_Kp0 = 45.0f;
float an_Ki0 = 0.0f;
float an_Kd0 = 0.0f;
float an_Kp1 = 80.0f;
float an_Ki1 = 1.4f;
float an_Kd1 = 8.0f;
float an_Kp1 = 45.0f;
float an_Ki1 = 0.0f;
float an_Kd1 = 0.0f;
float in_angle;
float set_angle = 0.0f;
float out_angle;
float gy_Kp0 = 2.0f;
float gy_Ki0 = 0.0f;
float gy_Kd0 = 0.3f;
float cn_Kp1 = 45.0f;
float cn_Ki1 = 0.f;
float cn_Kd1 = 0.0f;
float gy_Kp1 = 2.0f;
float out_cangle;
float gy_Kp0 = 4.0f;
float gy_Ki0 = 0.0f;
float gy_Kd0 = 0.0f;
float gy_Kp1 = 4.0f;
float gy_Ki1 = 0.0f;
float gy_Kd1 = 0.3f;
float gy_Kd1 = 0.0f;
float in_gyro;
float out_gyro;
// float set_gyro = 0.0f;
float po_Kp0 = 827.0f;
float po_Ki0 = 16.0f;
float po_Kd0 = 13.0f;
float po_Kp0 = 0.0f;
float po_Ki0 = 0.0f;
float po_Kd0 = 0.0f;
float po_Kp1 = 500.0f;
float po_Kp1 = 200.0f;
float po_Ki1 = 2.0f;
float po_Kd1 = 1.0f;
float po_Kd1 = 0.0f;
float in_pos;
float out_pos;
float set_pos = 0.0f;
float sp_Kp = 19.0f;
float sp_Ki = 0.5f;
float sp_Kp = 16.0f;
float sp_Ki = 10.f;
float sp_Kd = 0.0f;
float in_speed;
float out_speed;
float temp_data = 0;
float set_speed = 100.0f;
float set_speed0 = 200.0f;
float set_speed1 = 300.0f;
int cnt1 = 0;
@@ -89,54 +98,43 @@ 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; // 陀螺仪输入
imu660ra_get_gyro();
in_gyro = imu660ra_gyro_z;
// 停车
if (1 == in_stop) {
bt_fly_flag = bt_run_flag = 0;
}
// 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);
// 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++;
// pid参数切换
if ((last_state != in_state) && (in_state == 0 || in_state == 2)) { // 直道
cnt3_flag = 0;
cnt3 = 0;
bt_printf("to 0");
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);
}
if ((last_state != in_state) && (in_state == 1 || in_state == 3)) { // 弯道
bt_printf("to 1");
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);
}
last_state = in_state;
if (cnt3_flag == 1 && in_state == 1) {
cnt3++;
if (cnt3 >= 500) // 200ms
{
bt_printf("to 2");
cnt3_flag = 2;
cnt3 = 0;
}
}
PID_Compute(&far_gyro_pid);
PID_Compute(&near_pos_pid);
if (cnt1 >= 10) {
@@ -147,36 +145,48 @@ void sport_motion(void)
if (cnt2 >= 20) {
cnt2 = 0;
PID_Compute(&far_angle_pid);
PID_Compute(&angle_pid);
}
if (in_state == 0 || in_state == 2) {
pwm_duty_ls = (int32_t)myclip_f(-out_pos, 0.0f, 7000.f);
pwm_duty_rs = (int32_t)myclip_f(out_pos, 0.0f, 7000.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);
} else if (in_state == 1) {
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);
pwm_duty_ls = (int32_t)myclip_f(-out_cangle, -7000.0f, 7000.f);
pwm_duty_rs = (int32_t)myclip_f(out_cangle, -7000.0f, 7000.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);
} else if (in_state == 3) {
if (out_gyro > 0) {
pwm_duty_ls = (int32_t)myclip_f(-out_cangle, -7000.0f, 7000.f);
pwm_duty_rs = (int32_t)myclip_f(out_cangle, -7000.0f, 7000.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, 0.0f, 6000.f);
} else {
pwm_duty_ls = (int32_t)myclip_f(-out_cangle, -7000.0f, 7000.f);
pwm_duty_rs = (int32_t)myclip_f(out_cangle, -7000.0f, 7000.f);
pwm_duty_lb = (int32_t)myclip_f(out_speed, 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 {
by_pwm_power_duty(0, 0, 0, 0);
}
/* 升力风扇设置 */
if (bt_fly_flag == 0) {
by_pwm_update_duty(0 + 500, 0 + 500);
} else {
if (in_state == 1 && cnt3_flag == 1) {
float tt = 150 * fabs(tanf(myclip_f(in_angle, -45.f, 45.f) / 180 * PI));
by_pwm_update_duty(bt_fly + 500-tt, bt_fly + 500-tt);
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);
}
by_pwm_update_duty(bt_fly + 500, bt_fly + 500);
}
}
}
@@ -188,30 +198,33 @@ 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_M, _PID_CD_REVERSE);
PID(&far_angle_pid, &in_angle, &out_angle, &set_angle, an_Kp1, an_Ki1, an_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
PID_SetMode(&far_angle_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&far_angle_pid, 20);
PID_SetSampleTime(&far_angle_pid, 10);
PID_SetOutputLimits(&far_angle_pid, -3000.0f, 3000.0f);
// PID_Init(&far_angle_pid);
PID(&angle_pid, &in_angle, &out_cangle, &set_angle, cn_Kp1, cn_Ki1, cn_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
PID_SetMode(&angle_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&angle_pid, 10);
PID_SetOutputLimits(&angle_pid, -6000.0f, 6000.0f);
/* 角速度控制 */
PID(&far_gyro_pid, &in_gyro, &out_gyro, &out_angle, gy_Kp1, gy_Ki1, gy_Kd1, _PID_P_ON_M, _PID_CD_REVERSE);
PID(&far_gyro_pid, &in_gyro, &out_gyro, &out_angle, gy_Kp1, gy_Ki1, gy_Kd1, _PID_P_ON_E, _PID_CD_REVERSE); // m是增量
PID_SetMode(&far_gyro_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&far_gyro_pid, 1);
PID_SetOutputLimits(&far_gyro_pid, -3000.0f, 3000.0f);
// PID_Init(&far_gyro_pid);
PID_SetSampleTime(&far_gyro_pid, 10);
PID_SetOutputLimits(&far_gyro_pid, -4000.0f, 4000.0f);
/* 近点控制 */
PID(&near_pos_pid, &in_pos, &out_pos, &set_pos, po_Kp1, po_Ki1, po_Kd1, _PID_P_ON_E, _PID_CD_DIRECT);
PID(&near_pos_pid, &in_angle, &out_pos, &set_pos, po_Kp1, po_Ki1, po_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
PID_SetMode(&near_pos_pid, _PID_MODE_AUTOMATIC);
PID_SetSampleTime(&near_pos_pid, 1);
PID_SetOutputLimits(&near_pos_pid, -3000.0f, 3000.0f);
PID_SetSampleTime(&near_pos_pid, 10);
PID_SetOutputLimits(&near_pos_pid, -5000.0f, 5000.0f);
// PID_Init(&near_pos_pid);
/* 速度控制 */
PID(&speed_pid, &in_speed, &out_speed, &set_speed1, sp_Kp, sp_Ki, sp_Kd, _PID_P_ON_M, _PID_CD_DIRECT);
PID(&speed_pid, &in_speed, &out_speed, &set_speed, 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, -2500.0f, 3000.0f);
PID_SetOutputLimits(&speed_pid, -4000.0f, 6000.0f);
// PID_Init(&speed_pid);
}

View File

@@ -12,7 +12,9 @@ extern float an_Kd1;
extern float in_angle;
extern float set_angle;
extern float out_angle;
extern float cn_Kp1;
extern float cn_Ki1;
extern float cn_Kd1;
extern float gy_Kp1;
extern float gy_Ki1;
extern float gy_Kd1;
@@ -40,6 +42,7 @@ extern float in_speed;
extern float out_speed;
extern float set_speed0;
extern float set_speed1;
extern float set_speed ;
extern uint32_t pwm_duty_ls;
extern uint32_t pwm_duty_rs;
extern uint32_t pwm_duty_lb;

View File

@@ -46,9 +46,13 @@ void jj_param_eeprom_init(void)
PARAM_REG(pos_Kp1, &po_Kp1, EFLOAT, 1, "po_P1:"); // 注冊
PARAM_REG(pos_Ki1, &po_Ki1, EFLOAT, 1, "po_I1:"); // 注冊
PARAM_REG(pos_Kd1, &po_Kd1, EFLOAT, 1, "po_D1:");
PARAM_REG(can_Kp1, &cn_Kp1, EFLOAT, 1, "cn_P1:"); // 注冊
PARAM_REG(can_Ki1, &cn_Ki1, EFLOAT, 1, "cn_I1:"); // 注冊
PARAM_REG(can_Kd1, &cn_Kd1, EFLOAT, 1, "cn_D1:");
PARAM_REG(param_set_speed1, &set_speed1, EFLOAT, 1, "rate1:");
jj_param_read(); // 注冊
set_speed=set_speed1;
}
/**
* @brief 参数写入

View File

@@ -42,6 +42,9 @@ typedef enum {
pos_Kp1,
pos_Ki1,
pos_Kd1,
can_Kp1,
can_Ki1,
can_Kd1,
param_set_speed1,
DATA_IN_FLASH_NUM,

View File

@@ -1,14 +1,18 @@
#include "zf_common_headfile.h"
#include "page_ui_widget.h"
#include "page.h"
#include "by_frame.h"
#include "jj_blueteeth.h"
#define LINE_HEAD 1
#define LINE_END 7
#define LINE_END 5
static char Text[] = "Menu";
static int8_t Curser = LINE_HEAD; // 定义光标位置
static int8_t Curser_Last = LINE_HEAD; // 定义光标位置
uint32_t cnt = 0;
uint32_t reset[1];
static void Print_Menu_p(void);
/***************************************************************************************
*
@@ -25,7 +29,8 @@ static void Setup()
{
ips200_clear();
Print_Menu_p();
Print_Curser(Curser, Curser_Last,RGB565_PURPLE);
Print_Curser(Curser, Curser_Last, RGB565_PURPLE);
reset[0]=0x01;
}
/**
@@ -64,6 +69,22 @@ static void Event(page_event event)
if (page_max > Curser && page_menu < Curser) {
Page_Shift(Curser); // 切换到光标选中的页面
}
} else if (page_event_press_long == event) {
if(cnt==0)
{
by_frame_send(reset);
cnt=1;
}
else if(cnt==1)
{
cnt=2;
bt_fly_flag=1;
}else if(cnt==2)
{
cnt=0;
bt_run_flag=1;
}
}
if (Curser < LINE_HEAD) {
@@ -72,7 +93,7 @@ static void Event(page_event event)
Curser = LINE_HEAD;
}
Print_Curser(Curser, Curser_Last,RGB565_PURPLE);
Print_Curser(Curser, Curser_Last, RGB565_PURPLE);
}
/**

View File

@@ -26,7 +26,7 @@ static void Setup()
{
ips200_clear();
Print_Curser(Curser, Curser_Last, RGB565_PURPLE);
for (int16 i = 0; i < 10; i++) {
for (int16 i = 0; i < 13; i++) {
ips200_show_string(0, i * 18 + 2, Param_Data[i + 13].text);
if (Param_Data[i + 13].type == EINT32)
ips200_show_int(50, i * 18 + 2, *((int32 *)(Param_Data[i + 13].p_data)), 5);