无刷完赛
This commit is contained in:
@@ -6,18 +6,21 @@
|
||||
#define FAN_LL_PWM_PIN TIM8_PWM_MAP1_CH1_B6 // 左升力风扇
|
||||
#define FAN_RL_PWM_PIN TIM8_PWM_MAP1_CH2_B7 // 右升力风扇
|
||||
// M4
|
||||
#define FAN_RB_PWM_A_PIN TIM10_PWM_MAP3_CH4_D7 // 后
|
||||
#define FAN_RB_PWM_B_PIN TIM10_PWM_MAP3_CH2_D3 // you后
|
||||
#define FAN_RB_PWM_A_PIN TIM10_PWM_MAP3_CH2_D3 // 后
|
||||
#define FAN_RB_PWM_B_PIN TIM10_PWM_MAP3_CH4_D7 // you后
|
||||
// M1
|
||||
#define FAN_LB_PWM_A_PIN TIM4_PWM_MAP1_CH3_D14 // zuo后
|
||||
#define FAN_LB_PWM_B_PIN TIM4_PWM_MAP1_CH1_D12 // 后
|
||||
#define FAN_LB_PWM_A_PIN TIM4_PWM_MAP1_CH1_D12 // zuo后
|
||||
#define FAN_LB_PWM_B_PIN TIM4_PWM_MAP1_CH3_D14// 后
|
||||
// M3
|
||||
#define FAN_RS_PWM_B_PIN TIM10_PWM_MAP3_CH1_D1 // 右侧
|
||||
#define FAN_RS_PWM_A_PIN TIM10_PWM_MAP3_CH3_D5 // you侧
|
||||
// M2
|
||||
#define FAN_LS_PWM_B_PIN TIM4_PWM_MAP1_CH4_D15 // 左侧
|
||||
#define FAN_LS_PWM_A_PIN TIM4_PWM_MAP1_CH2_D13 // 左侧
|
||||
// #define FAN_RS_PWM_B_PIN TIM10_PWM_MAP3_CH1_D1 // 右侧
|
||||
// #define FAN_RS_PWM_A_PIN TIM10_PWM_MAP3_CH3_D5 // you侧
|
||||
// // M2
|
||||
// #define FAN_LS_PWM_B_PIN TIM4_PWM_MAP1_CH4_D15 // 左侧
|
||||
// #define FAN_LS_PWM_A_PIN TIM4_PWM_MAP1_CH2_D13 // 左侧
|
||||
|
||||
//
|
||||
#define FAN_LS_PWM_PIN TIM1_PWM_MAP0_CH4_A11
|
||||
#define FAN_RS_PWM_PIN TIM1_PWM_MAP0_CH1_A8
|
||||
int32_t pwm_duty_ls_g;
|
||||
int32_t pwm_duty_rs_g;
|
||||
int32_t pwm_duty_lb_g;
|
||||
@@ -48,7 +51,8 @@ void by_pwm_init(void)
|
||||
gpio_init(C8, GPO, 1, GPO_PUSH_PULL);
|
||||
pwm_init(FAN_LL_PWM_PIN, 50, 500);
|
||||
pwm_init(FAN_RL_PWM_PIN, 50, 500);
|
||||
|
||||
pwm_init(FAN_LS_PWM_PIN, 50, 500);
|
||||
pwm_init(FAN_RS_PWM_PIN, 50, 500);
|
||||
#if FIX_DRIVE == 1
|
||||
pwm_init(FAN_LS_PWM_A_PIN, 20000, 7000);
|
||||
pwm_init(FAN_LS_PWM_B_PIN, 20000, 7000);
|
||||
@@ -60,24 +64,24 @@ void by_pwm_init(void)
|
||||
pwm_init(FAN_RB_PWM_B_PIN, 20000, 7000);
|
||||
while (1);
|
||||
#elif FIX_DRIVE == 2
|
||||
pwm_init(FAN_LS_PWM_A_PIN, 20000, 1000);
|
||||
pwm_init(FAN_LS_PWM_B_PIN, 20000, 0);
|
||||
pwm_init(FAN_RS_PWM_A_PIN, 20000, 1000);
|
||||
pwm_init(FAN_RS_PWM_B_PIN, 20000, 0);
|
||||
pwm_init(FAN_LB_PWM_A_PIN, 20000,1000);
|
||||
pwm_init(FAN_LB_PWM_B_PIN, 20000, 0);
|
||||
pwm_init(FAN_RB_PWM_A_PIN, 20000, 1000);
|
||||
pwm_init(FAN_RB_PWM_B_PIN, 20000, 0);
|
||||
// pwm_init(FAN_LS_PWM_A_PIN, 30000, 1000);
|
||||
// pwm_init(FAN_LS_PWM_B_PIN, 30000, 0);
|
||||
// pwm_init(FAN_RS_PWM_A_PIN, 30000, 1000);
|
||||
// pwm_init(FAN_RS_PWM_B_PIN, 30000, 0);
|
||||
pwm_init(FAN_LB_PWM_A_PIN, 30000, 1000);
|
||||
pwm_init(FAN_LB_PWM_B_PIN, 30000, 0);
|
||||
pwm_init(FAN_RB_PWM_A_PIN, 30000, 1000);
|
||||
pwm_init(FAN_RB_PWM_B_PIN, 30000, 0);
|
||||
while (1);
|
||||
#endif
|
||||
pwm_init(FAN_LS_PWM_A_PIN, 20000, 0);
|
||||
pwm_init(FAN_LS_PWM_B_PIN, 20000, 0);
|
||||
pwm_init(FAN_RS_PWM_A_PIN, 20000, 0);
|
||||
pwm_init(FAN_RS_PWM_B_PIN, 20000, 0);
|
||||
pwm_init(FAN_LB_PWM_A_PIN, 20000, 0);
|
||||
pwm_init(FAN_LB_PWM_B_PIN, 20000, 0);
|
||||
pwm_init(FAN_RB_PWM_A_PIN, 20000, 0);
|
||||
pwm_init(FAN_RB_PWM_B_PIN, 20000, 0);
|
||||
// pwm_init(FAN_LS_PWM_A_PIN, 40000, 0);
|
||||
// pwm_init(FAN_LS_PWM_B_PIN, 40000, 0);
|
||||
// pwm_init(FAN_RS_PWM_A_PIN, 40000, 0);
|
||||
// pwm_init(FAN_RS_PWM_B_PIN, 40000, 0);
|
||||
pwm_init(FAN_LB_PWM_A_PIN, 30000, 0);
|
||||
pwm_init(FAN_LB_PWM_B_PIN, 30000, 0);
|
||||
pwm_init(FAN_RB_PWM_A_PIN, 30000, 0);
|
||||
pwm_init(FAN_RB_PWM_B_PIN, 30000, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,31 +108,34 @@ 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, 7000);
|
||||
bpwm_duty_rs = clip_s32(bpwm_duty_rs, -0, 7000);
|
||||
bpwm_duty_lb = clip_s32(bpwm_duty_lb, -0, 7000);
|
||||
bpwm_duty_rb = clip_s32(bpwm_duty_rb, -0, 7000);
|
||||
|
||||
bpwm_duty_ls = clip_s32(bpwm_duty_ls, 500, 1000);
|
||||
bpwm_duty_rs = clip_s32(bpwm_duty_rs, 500, 1000);
|
||||
// if(1000==bpwm_duty_ls)bpwm_duty_rs=600;
|
||||
// if(1000==bpwm_duty_rs)bpwm_duty_ls=600;
|
||||
bpwm_duty_lb = clip_s32(bpwm_duty_lb, 0, 7000);
|
||||
bpwm_duty_rb = clip_s32(bpwm_duty_rb, 0, 7000);
|
||||
pwm_set_duty(FAN_LS_PWM_PIN, bpwm_duty_ls);
|
||||
pwm_set_duty(FAN_RS_PWM_PIN, bpwm_duty_rs);
|
||||
// pwm_duty_ls_g = pwm_duty_ls;
|
||||
// pwm_duty_rs_g = pwm_duty_rs;
|
||||
// pwm_duty_lb_g = pwm_duty_lb;
|
||||
// pwm_duty_rb_g = pwm_duty_rb;
|
||||
|
||||
if (bpwm_duty_ls >= 0) {
|
||||
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);
|
||||
}
|
||||
// if (bpwm_duty_ls >= 0) {
|
||||
|
||||
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);
|
||||
}
|
||||
// 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);
|
||||
// }
|
||||
|
||||
// if (bpwm_duty_rs >= 0) {
|
||||
|
||||
// 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);
|
||||
// }
|
||||
|
||||
if (bpwm_duty_lb >= 0) {
|
||||
pwm_set_duty(FAN_LB_PWM_A_PIN, bpwm_duty_lb);
|
||||
|
||||
@@ -273,8 +273,7 @@ void EXTI15_10_IRQHandler(void)
|
||||
void TIM1_UP_IRQHandler(void)
|
||||
{
|
||||
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) {
|
||||
by_buzzer_run();
|
||||
sport_motion();
|
||||
|
||||
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
|
||||
}
|
||||
}
|
||||
@@ -310,7 +309,8 @@ void TIM5_IRQHandler(void)
|
||||
void TIM6_IRQHandler(void)
|
||||
{
|
||||
if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) {
|
||||
// ICM_getEulerianAngles();
|
||||
by_buzzer_run();
|
||||
sport_motion();
|
||||
TIM_ClearITPendingBit(TIM6, TIM_IT_Update);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,8 @@ int cnt3 = 0;
|
||||
int cnt4 = 0;
|
||||
int cnt5 = 0;
|
||||
int cnt6 = 0;
|
||||
|
||||
int cnt7 = 0;
|
||||
int cnt8 = 0;
|
||||
uint8_t cnt3_flag = 0;
|
||||
uint8_t in_state = 0;
|
||||
uint8_t in_stop = 0;
|
||||
@@ -102,7 +103,7 @@ int32_t pwm_duty_ls;
|
||||
int32_t pwm_duty_rs;
|
||||
int32_t pwm_duty_lb;
|
||||
int32_t pwm_duty_rb;
|
||||
|
||||
int stop_have = 0;
|
||||
float qd_down = 0.0f;
|
||||
float last_gy = 0.0f;
|
||||
/*
|
||||
@@ -141,13 +142,20 @@ void sport_motion()
|
||||
imu660ra_get_acc();
|
||||
get_vol();
|
||||
in_gyro = imu660ra_gyro_z;
|
||||
if (imu660ra_acc_z <= 800) {
|
||||
// 抖动停车
|
||||
|
||||
if (imu660ra_acc_z <= 800) {
|
||||
cnt7++;
|
||||
|
||||
} else {
|
||||
cnt7 = 0;
|
||||
}
|
||||
if (cnt7 >= 50) {
|
||||
bt_fly_flag = bt_run_flag = 0;
|
||||
bt_printf("ting");
|
||||
}
|
||||
// 斑马线停车
|
||||
if (1 == in_stop) {
|
||||
if (1 == in_stop && stop_have == 1) {
|
||||
stop_flag = 1;
|
||||
}
|
||||
if (stop_flag == 1) {
|
||||
@@ -161,46 +169,46 @@ void sport_motion()
|
||||
cnt1++;
|
||||
cnt2++;
|
||||
cnt5++;
|
||||
cnt8++;
|
||||
if (cnt8 >= 10000) {
|
||||
stop_have = 1;
|
||||
cnt8=0;
|
||||
}
|
||||
// pid参数切换
|
||||
if ((last_state != in_state) && (in_state == 0 || in_state == 2)) { // 直道
|
||||
cnt3_flag = 1;
|
||||
cnt3 = 0;
|
||||
|
||||
bt_printf("to 直道");
|
||||
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);
|
||||
|
||||
PID_SetTunings(&pos_pid, po_Kp1, po_Ki1, po_Kd1);
|
||||
}
|
||||
if ((last_state != in_state) && in_state == 1) { // 弯道
|
||||
bt_printf("to 入弯");
|
||||
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);
|
||||
|
||||
PID_SetTunings(&pos_pid, cn_Kp1, cn_Ki1, cn_Kd1);
|
||||
}
|
||||
if ((last_state != in_state) && in_state == 3) { // 圆环
|
||||
if ((last_state != in_state) && (in_state == 3 || in_state == 4)) { // 圆环
|
||||
bt_printf("to 圆环");
|
||||
if (in_state == 3)
|
||||
set_speed = set_speed2;
|
||||
|
||||
else
|
||||
set_speed = set_speed3;
|
||||
cnt3_flag = 1;
|
||||
PID_SetTunings(&far_angle_pid, yu_Kp0, yu_Ki0, yu_Kd0);
|
||||
PID_SetTunings(&far_gyro_pid, ygy_Kp0, ygy_Ki0, ygy_Kd0);
|
||||
|
||||
PID_SetTunings(&pos_pid, cn_Kp1, cn_Ki1, cn_Kd1);
|
||||
}
|
||||
if ((last_state != in_state) && in_state == 5) { // 圆环
|
||||
if ((last_state != in_state) && in_state == 5) { //
|
||||
bt_printf("to 障碍");
|
||||
set_speed = set_speed4;
|
||||
|
||||
cnt3_flag = 1;
|
||||
PID_SetTunings(&far_angle_pid, za_Kp, za_Ki, za_Kd);
|
||||
PID_SetTunings(&far_gyro_pid, zg_Kp, zg_Ki, zg_Kd);
|
||||
|
||||
PID_SetTunings(&pos_pid, zp_Kp, zp_Ki, zp_Kd);
|
||||
}
|
||||
last_state = in_state;
|
||||
@@ -220,49 +228,50 @@ void sport_motion()
|
||||
PID_Compute(&speed_pid);
|
||||
cnt5 = 0;
|
||||
}
|
||||
// 并级pid,限幅
|
||||
pwm_duty_ls = (int32_t)myclip_f(300 - out_pos, 100.0f, 500.f);
|
||||
pwm_duty_rs = (int32_t)myclip_f(300 + out_pos, 100.0f, 500.f);
|
||||
pwm_duty_lb = (int32_t)myclip_f(out_speed + out_gyro, 0.0f, 5000.f);
|
||||
pwm_duty_rb = (int32_t)myclip_f(out_speed - out_gyro, 0.0f, 5000.f);
|
||||
|
||||
pwm_duty_ls = (int32_t)myclip_f(-out_pos, 0.0f, 5000.f);
|
||||
pwm_duty_rs = (int32_t)myclip_f(out_pos, 0.0f, 5000.f);
|
||||
pwm_duty_lb = (int32_t)myclip_f(out_speed + out_gyro, 0.0f, 7000.f);
|
||||
pwm_duty_rb = (int32_t)myclip_f(out_speed - out_gyro, 0.0f, 7000.f);
|
||||
|
||||
by_pwm_power_duty(pwm_duty_ls, pwm_duty_rs, pwm_duty_lb, pwm_duty_rb);
|
||||
by_pwm_power_duty(500 + pwm_duty_ls, 500 + pwm_duty_rs, pwm_duty_lb, 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);
|
||||
by_pwm_update_duty(500, 500);
|
||||
} else {
|
||||
if ((in_state == 1 || in_state == 3 || in_state == 5) && cnt3_flag == 1) {
|
||||
|
||||
cnt3++;
|
||||
|
||||
if (in_state == 3) {
|
||||
if (cnt3 >= 250) // 200ms
|
||||
if (cnt3 >= 100) // 200ms
|
||||
{
|
||||
bt_printf("to 环内");
|
||||
cnt3_flag = 2;
|
||||
cnt3 = 0;
|
||||
}
|
||||
if (in_speed > set_speed - 100) {
|
||||
qd_down = 5 * fabs(in_pos) + 75;
|
||||
} else if (in_speed > set_speed - 200) {
|
||||
qd_down = 2.5 * fabs(in_pos) + 30;
|
||||
if (in_speed > set_speed) {
|
||||
qd_down = 50;
|
||||
} else if (in_speed > set_speed - 100) {
|
||||
qd_down = 20;
|
||||
} else {
|
||||
qd_down = 0;
|
||||
}
|
||||
} else if (in_state == 1) {
|
||||
if (cnt3 >= 500) // 200ms
|
||||
if (cnt3 >= 300) // 200ms
|
||||
{
|
||||
bt_printf("to 弯道");
|
||||
cnt3_flag = 2;
|
||||
cnt3 = 0;
|
||||
}
|
||||
if (in_speed > set_speed - 100) {
|
||||
qd_down = 5 * fabs(in_pos) + 50;
|
||||
} else if (in_speed > set_speed - 200) {
|
||||
qd_down = 2.5 * fabs(in_pos) + 10;
|
||||
if (in_speed > set_speed) {
|
||||
qd_down = 4 * fabs(in_pos) + 20;
|
||||
} else if (in_speed > set_speed - 100) {
|
||||
qd_down = 2 * fabs(in_pos) + 10;
|
||||
} else {
|
||||
qd_down = 0;
|
||||
}
|
||||
@@ -285,16 +294,14 @@ void sport_motion()
|
||||
by_pwm_update_duty(bt_fly + 500 - qd_down, bt_fly + 500 - qd_down);
|
||||
} else {
|
||||
if (in_state == 1 ) {
|
||||
qd_down = 15;
|
||||
qd_down = 40;
|
||||
} else if(in_state == 3){
|
||||
qd_down = 70;
|
||||
}else{
|
||||
qd_down=0;
|
||||
}
|
||||
if (in_state == 4) {
|
||||
|
||||
by_pwm_update_duty(bt_fly + 500 - qd_down, bt_fly + 500 - qd_down);
|
||||
set_speed = set_speed3;
|
||||
} else {
|
||||
by_pwm_update_duty(bt_fly + 500 - qd_down, bt_fly + 500 - qd_down);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -309,22 +316,22 @@ 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_SetMode(&far_angle_pid, _PID_MODE_AUTOMATIC);
|
||||
PID_SetSampleTime(&far_angle_pid, 20);
|
||||
PID_SetOutputLimits(&far_angle_pid, -4000.0f, 4000.0f);
|
||||
PID_SetOutputLimits(&far_angle_pid, -3000.0f, 3000.0f);
|
||||
// 侧面位置环
|
||||
PID(&pos_pid, &in_angle, &out_pos, &set_pos, po_Kp1, po_Ki1, po_Kd1, _PID_P_ON_E, _PID_CD_REVERSE);
|
||||
PID_SetMode(&pos_pid, _PID_MODE_AUTOMATIC);
|
||||
PID_SetSampleTime(&pos_pid, 20);
|
||||
PID_SetOutputLimits(&pos_pid, -5000.0f, 5000.0f);
|
||||
PID_SetOutputLimits(&pos_pid, -200.0f, 200.0f);
|
||||
|
||||
/* 角速度控制 */
|
||||
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, 20);
|
||||
PID_SetOutputLimits(&far_gyro_pid, -7000.0f, 7000.0f);
|
||||
PID_SetOutputLimits(&far_gyro_pid, -5000.0f, 5000.0f);
|
||||
|
||||
/* 速度控制 */
|
||||
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, 20);
|
||||
PID_SetOutputLimits(&speed_pid, -0.0f, 7000.0f);
|
||||
PID_SetOutputLimits(&speed_pid, -0.0f, 4500.0f);
|
||||
}
|
||||
|
||||
@@ -46,20 +46,19 @@ int main(void)
|
||||
|
||||
encoder_quad_init(TIM3_ENCOEDER, TIM3_ENCOEDER_MAP3_CH1_C6, TIM3_ENCOEDER_MAP3_CH2_C7);
|
||||
ips200_init(IPS200_TYPE_SPI);
|
||||
while (imu660ra_init())
|
||||
;
|
||||
while (imu660ra_init());
|
||||
jj_param_eeprom_init();
|
||||
jj_bt_init();
|
||||
vol_init();
|
||||
by_rb_init();
|
||||
by_pwm_init();
|
||||
by_buzzer_init();
|
||||
by_frame_init();
|
||||
by_pwm_init();
|
||||
|
||||
Page_Init();
|
||||
sport_pid_init();
|
||||
|
||||
pit_ms_init(TIM1_PIT, 1); // 运动解算,编码器
|
||||
pit_ms_init(TIM6_PIT, 1); // 运动解算,编码器
|
||||
|
||||
bt_printf("ok\r\n");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user