From 4db4582450210464184743d2fc7285170cdfa510 Mon Sep 17 00:00:00 2001
From: ws <1621320660@qq.com>
Date: Sat, 28 Jun 2025 10:27:40 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=86=E8=BF=90=E7=90=83?=
=?UTF-8?q?=E7=9A=84=E5=BB=B6=E6=97=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
MDK-ARM/.vscode/keil-assistant.log | 8 +
MDK-ARM/.vscode/uv4.log | 3 +-
MDK-ARM/.vscode/uv4.log.lock | 2 +-
MDK-ARM/R1.uvoptx | 19 +-
User/bsp/TopDefine.h | 2 +-
User/module/ball.cpp | 339 ++++++++++++++---------------
6 files changed, 172 insertions(+), 201 deletions(-)
diff --git a/MDK-ARM/.vscode/keil-assistant.log b/MDK-ARM/.vscode/keil-assistant.log
index 7ded289..9e0649c 100644
--- a/MDK-ARM/.vscode/keil-assistant.log
+++ b/MDK-ARM/.vscode/keil-assistant.log
@@ -74,3 +74,11 @@
[info] Log at : 2025/6/25|19:23:57|GMT+0800
+[info] Log at : 2025/6/27|11:41:11|GMT+0800
+
+[info] Log at : 2025/6/27|15:32:00|GMT+0800
+
+[info] Log at : 2025/6/27|18:43:18|GMT+0800
+
+[info] Log at : 2025/6/28|01:19:54|GMT+0800
+
diff --git a/MDK-ARM/.vscode/uv4.log b/MDK-ARM/.vscode/uv4.log
index 12c1513..7aa70a2 100644
--- a/MDK-ARM/.vscode/uv4.log
+++ b/MDK-ARM/.vscode/uv4.log
@@ -1,9 +1,8 @@
*** Using Compiler 'V5.06 update 7 (build 960)', folder: 'D:\keil\ARM\ARMCC\Bin'
Build target 'R1'
compiling ball.cpp...
-compiling shoot.cpp...
linking...
-Program Size: Code=31632 RO-data=1832 RW-data=268 ZI-data=32252
+Program Size: Code=31188 RO-data=1832 RW-data=268 ZI-data=32252
FromELF: creating hex file...
"R1\R1.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:00:06
diff --git a/MDK-ARM/.vscode/uv4.log.lock b/MDK-ARM/.vscode/uv4.log.lock
index 87abbbb..f0d3f72 100644
--- a/MDK-ARM/.vscode/uv4.log.lock
+++ b/MDK-ARM/.vscode/uv4.log.lock
@@ -1 +1 @@
-2025/6/26 22:06:06
\ No newline at end of file
+2025/6/28 1:27:37
\ No newline at end of file
diff --git a/MDK-ARM/R1.uvoptx b/MDK-ARM/R1.uvoptx
index 87c4110..4aab291 100644
--- a/MDK-ARM/R1.uvoptx
+++ b/MDK-ARM/R1.uvoptx
@@ -173,22 +173,7 @@
3
1
- ball_exit,0x0A
-
-
- 4
- 1
- nucbuf
-
-
- 5
- 1
- nuc_v
-
-
- 6
- 1
- asd,0x0A
+ nuc_v,0x0A
@@ -957,7 +942,7 @@
User/device
- 1
+ 0
0
0
0
diff --git a/User/bsp/TopDefine.h b/User/bsp/TopDefine.h
index 261faac..61c243d 100644
--- a/User/bsp/TopDefine.h
+++ b/User/bsp/TopDefine.h
@@ -16,7 +16,7 @@
#endif
-#define ONE_CONTROL 1
+#define ONE_CONTROL 0
//是否使用大疆DT7遥控器
#ifndef DT7
diff --git a/User/module/ball.cpp b/User/module/ball.cpp
index 91b5c00..dfc72c6 100644
--- a/User/module/ball.cpp
+++ b/User/module/ball.cpp
@@ -9,105 +9,101 @@
extern RC_ctrl_t rc_ctrl;
extern int ball_exit;
-//伸缩
+// 伸缩
#define I_ANGLE 147
#define O_ANGLE 187
#define WAIT_POS 170
-//PE11 气缸
+// PE11 气缸
Ball ::Ball()
-{
- detect_init();
+{
+ detect_init();
- //小米电机
- feedback=get_CyberGear_point();
+ // 小米电机
+ feedback = get_CyberGear_point();
- //小米电机初始化
- xiaomi.position = I_ANGLE; //
- xiaomi.speed = 25; //
- xiaomi.K_P = 80; // 位置增益
- xiaomi.K_D =20; // 位置阻尼
- xiaomi.K_C = 12 ; // 力矩
- xiaomi.Pmax =1; //好像没啥用
+ // 小米电机初始化
+ xiaomi.position = I_ANGLE; //
+ xiaomi.speed = 25; //
+ xiaomi.K_P = 80; // 位置增益
+ xiaomi.K_D = 50; // 位置阻尼
+ xiaomi.K_C = 12; // 力矩
+ xiaomi.Pmax = 1; // 好像没啥用
// //小米电机初始化
- // xiaomi.position = 0; //
- // xiaomi.speed = 0; //
- // xiaomi.K_P = 0; //
- // xiaomi.K_D =0; //
- // xiaomi.K_C = 0 ;
+ // xiaomi.position = 0; //
+ // xiaomi.speed = 0; //
+ // xiaomi.K_P = 0; //
+ // xiaomi.K_D =0; //
+ // xiaomi.K_C = 0 ;
// xiaomi.Pmax =0;
- //状态机状态初始化
- currentState1= BALL_IDLE;
-
+ // 状态机状态初始化
+ currentState1 = BALL_IDLE;
}
-
-// E键 sw[1] 👆 200 shoot 中 1000 stop sw[2]👇1800
+// E键 sw[1] 👆 200 shoot 中 1000 stop sw[2]👇1800
// G键 sw[6]👆 200 中 1000 👇1800
// sw[5] 👆 200 👇1800
-//左旋 sw[7] 200 --1800
+// 左旋 sw[7] 200 --1800
void Ball::rc_mode()
{
- if(rc_ctrl.sw[6]==200)
+ if (rc_ctrl.sw[6] == 200)
{
- rc_key=UP2;
+ rc_key = UP2;
}
- if(rc_ctrl.sw[6]==1000)
+ if (rc_ctrl.sw[6] == 1000)
{
- rc_key=MIDDLE2;
+ rc_key = MIDDLE2;
}
- if(rc_ctrl.sw[6]==1800)
+ if (rc_ctrl.sw[6] == 1800)
{
- rc_key=DOWN2;
+ rc_key = DOWN2;
}
- if(rc_ctrl.sw[5]==200)
+ if (rc_ctrl.sw[5] == 200)
{
- extern_key=IN;
+ extern_key = IN;
}
- if(rc_ctrl.sw[5]==1800)
+ if (rc_ctrl.sw[5] == 1800)
{
- extern_key=OUT;
+ extern_key = OUT;
}
- if(rc_ctrl.sw[5]==1800)
+ if (rc_ctrl.sw[5] == 1800)
{
- ready_key=SIDE;
+ ready_key = SIDE;
}
- if(rc_ctrl.sw[5]==200)
+ if (rc_ctrl.sw[5] == 200)
{
- ready_key=0; //默认不准备
+ ready_key = 0; // 默认不准备
}
}
void Ball::Send_control()
{
-
- CAN_XiaoMi(1,&xiaomi,&hcan2);
- osDelay(1);
+ CAN_XiaoMi(1, &xiaomi, &hcan2);
+ osDelay(1);
}
-#if ONE_CONTROL==0
+#if ONE_CONTROL == 0
void Ball::ballDown(void)
{
HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_SET); // 打开气缸爪子
HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
-
}
void Ball::Move_Extend()
{
- if(extern_key==IN)
+ if (extern_key == IN)
{
xiaomi.position = I_ANGLE;
}
- if(extern_key==OUT)
+ if (extern_key == OUT)
{
xiaomi.position = O_ANGLE;
}
@@ -119,64 +115,60 @@ void Ball::ball_control()
Move_Extend();
- switch (rc_key){
- case MIDDLE2:
- HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); //确保爪气缸关闭
- HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
+ switch (rc_key)
+ {
+ case MIDDLE2:
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保爪气缸关闭
+ HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
if (currentState1 == BALL_FINISH)
{
currentState1 = BALL_IDLE;
}
- else {
- currentState1 = BALL_IDLE; // 默认回到空闲状态
+ else
+ {
+ currentState1 = BALL_IDLE; // 默认回到空闲状态
}
break;
- case UP2:
+ case UP2:
ballDown();
break;
- case DOWN2:
+ case DOWN2:
ballHadling();
- //test_Handling();
break;
-
}
- Send_control();
-
+ Send_control();
}
-
- int ball_state = 0;
- int last_ball_state = 0; // 上一次的光电状态
+int ball_state = 0;
+int last_ball_state = 0; // 上一次的光电状态
void Ball::ballHadling(void)
{
-
+
switch (currentState1)
{
case BALL_IDLE:
- HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); //确保爪气缸关闭
- HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
- if (rc_key == DOWN2) // 检测按键是否被按下
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保爪气缸关闭
+ HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
+ if (rc_key == DOWN2) // 检测按键是否被按下
{
-
-
- currentState1 = BALL_FORWARD;
+ currentState1 = BALL_FORWARD;
}
break;
case BALL_FORWARD:
HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_SET); // 打开气缸爪子
- osDelay(5);
+ osDelay(5);
HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_SET); // 打开下气缸
- currentState1 = BALL_DROP; // 切换到球下落状态
+ currentState1 = BALL_DROP; // 切换到球下落状态
break;
- case BALL_DROP:
- osDelay(100); // 延时 100ms
+ case BALL_DROP:
+ osDelay(100); // 延时 100ms
HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 关闭下气缸
// 一直检测有球(ball_state == 0),等球离开
if (ball_state == 1 && last_ball_state == 0) // 球离开
@@ -186,7 +178,7 @@ void Ball::ballHadling(void)
last_ball_state = ball_state;
break;
- case BALL_FLAG:
+ case BALL_FLAG:
osDelay(10); // 延时 50ms
// 等待球弹回再次检测到球
if (ball_state == 0 && last_ball_state == 1) // 球弹回
@@ -197,18 +189,17 @@ void Ball::ballHadling(void)
break;
case BALL_CLOSE:
-
- osDelay(25);
- HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 闭合气缸爪子
- currentState1 = BALL_FINISH; // 切换到反转状态
+ osDelay(100);
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 闭合气缸爪子
+ currentState1 = BALL_FINISH; // 切换到反转状态
break;
case BALL_FINISH:
- osDelay(50); // 延时 50ms
+ osDelay(50); // 延时 50ms
HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保气缸爪子闭合
- HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
+ HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
- //currentState1 = BALL_IDLE; // 回到空闲状态
+ // currentState1 = BALL_IDLE; // 回到空闲状态
break;
@@ -216,11 +207,7 @@ void Ball::ballHadling(void)
currentState1 = BALL_IDLE; // 默认回到空闲状态
break;
}
-
-
- }
-
-
+}
#endif
@@ -228,101 +215,99 @@ void Ball::ballHadling(void)
void Ball::ball_control()
{
- hand_thread = osThreadFlagsGet(); // 获取任务通知标志位
+ hand_thread = osThreadFlagsGet(); // 获取任务通知标志位
ball_state = HAL_GPIO_ReadPin(up_ball_GPIO_Port, up_ball_Pin); // 读取光电状态(有球 1,无球 0)
- //进攻
- if(ready_key==SIDE)
+ // 进攻
+ if (ready_key == SIDE)
{
- switch (rc_key){
+ switch (rc_key)
+ {
case MIDDLE2:
- Idle_control();
- break;
+ Idle_control();
+ break;
- case UP2:
- ballDown();
- break;
-
- case DOWN2:
- ballHadling();
- break;
+ case UP2:
+ ballDown();
+ break;
+ case DOWN2:
+ ballHadling();
+ break;
+ }
}
- }
- //防守
+ // 防守
else
{
- xiaomi.position = I_ANGLE; // 保持收回
- HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保爪气缸关闭
- HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
-
+ xiaomi.position = I_ANGLE; // 保持收回
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保爪气缸关闭
+ HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
}
-
- // 发送小米电机控制数据
- Send_control();
+ // 发送小米电机控制数据
+ Send_control();
}
-
-//任务通知来作全过程
+// 任务通知来作全过程
void Ball::ballDown(void)
{
- switch(currentState1)
+ switch (currentState1)
{
- case BALL_IDLE:
- xiaomi.position = I_ANGLE; // 保持收回
- if(feedback->position_deg >= I_ANGLE-0.8 && feedback->position_deg <= I_ANGLE+0.8)
- {
- currentState1 = EXTEND_DOWN;
- }
- break;
+ case BALL_IDLE:
+ xiaomi.position = I_ANGLE; // 保持收回
+ if (feedback->position_deg >= I_ANGLE - 0.8 && feedback->position_deg <= I_ANGLE + 0.8)
+ {
+ currentState1 = EXTEND_DOWN;
+ }
+ break;
- case EXTEND_DOWN:
- HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET);
- // 检测到球自由下落一次就切换状态
- if(ball_state == 1)
- {
- currentState1 = EXTEND_OUT;
- }
- break;
+ case EXTEND_DOWN:
+ osDelay(500); // 不放太快
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_SET);
+ HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET);
+ // 检测到球自由下落一次就切换状态
+ if (ball_state == 1)
+ {
+ currentState1 = EXTEND_OUT;
+ }
+ break;
- case EXTEND_OUT:
- xiaomi.position = O_ANGLE; // 保持伸出
- if(feedback->position_deg >= O_ANGLE-0.2 && feedback->position_deg <= O_ANGLE+0.2)
- {
- osThreadFlagsSet(task_struct.thread.shoot, EXTEND_OK);
- currentState1 = EXTEND_FINISH; // 保持伸出,直到拨杆复位
- }
- break;
+ case EXTEND_OUT:
+ xiaomi.position = O_ANGLE; // 保持伸出
+ if (feedback->position_deg >= O_ANGLE - 0.2 && feedback->position_deg <= O_ANGLE + 0.2)
+ {
- case EXTEND_FINISH:
- xiaomi.position = O_ANGLE; // 一直保持伸出
- // 等待拨杆复位(如切到MIDDLE2),Idle_control会负责回位
- break;
+ osThreadFlagsSet(task_struct.thread.shoot, EXTEND_OK);
+ currentState1 = EXTEND_FINISH; // 保持伸出,直到拨杆复位
+ }
+ break;
- default:
- currentState1 = BALL_IDLE;
- break;
+ case EXTEND_FINISH:
+ xiaomi.position = O_ANGLE; // 一直保持伸出
+ // 等待拨杆复位(如切到MIDDLE2),Idle_control会负责回位
+ break;
+
+ default:
+ currentState1 = BALL_IDLE;
+ break;
}
}
void Ball::Idle_control()
{
- HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保爪气缸关闭
- HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保爪气缸关闭
+ HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
-
osThreadFlagsClear(EXTEND_OK);
- if(ready_key==SIDE) // 检测是否准备好
+ if (ready_key == SIDE) // 检测是否准备好
{
- xiaomi.position = WAIT_POS;
- if(feedback->position_deg >= WAIT_POS-3)
+ xiaomi.position = WAIT_POS;
+ if (feedback->position_deg >= WAIT_POS - 3)
{
// 只在READY_TELL未置位时发送,防止重复
- if((osThreadFlagsGet() & READY_TELL) == 0)
+ if ((osThreadFlagsGet() & READY_TELL) == 0)
{
osThreadFlagsSet(task_struct.thread.shoot, READY_TELL);
}
@@ -332,55 +317,53 @@ void Ball::Idle_control()
{
xiaomi.position = I_ANGLE; // 默认回到收回位置
}
-
+
// 拨杆回到中间挡位时,回位并重置状态机
- if(currentState1==EXTEND_FINISH)//转移后
+ if (currentState1 == EXTEND_FINISH) // 转移后
{
- xiaomi.position = I_ANGLE;
- currentState1 = BALL_IDLE;
+ xiaomi.position = I_ANGLE;
+ currentState1 = BALL_IDLE;
}
- if(currentState1==BALL_FINISH)//运球完成
+ if (currentState1 == BALL_FINISH) // 运球完成
{
- xiaomi.position = O_ANGLE;
- currentState1 = BALL_IDLE;
+ xiaomi.position = O_ANGLE;
+ currentState1 = BALL_IDLE;
}
else
{
- currentState1 = BALL_IDLE;
+ currentState1 = BALL_IDLE;
}
- //xiaomi.position = I_ANGLE;
-
+ // xiaomi.position = I_ANGLE;
}
- int ball_state = 0;
- int last_ball_state = 0; // 上一次的光电状态
+int ball_state = 0;
+int last_ball_state = 0; // 上一次的光电状态
void Ball::ballHadling(void)
{
-
+
switch (currentState1)
{
case BALL_IDLE:
if (rc_key == DOWN2)
- {
- xiaomi.position = O_ANGLE;//外伸
- if(feedback->position_deg>= O_ANGLE-1)// 确保伸缩电机到位
+ {
+ xiaomi.position = O_ANGLE; // 外伸
+ if (feedback->position_deg >= O_ANGLE - 1) // 确保伸缩电机到位
{
- currentState1 = BALL_FORWARD;
+ currentState1 = BALL_FORWARD;
}
-
}
break;
case BALL_FORWARD:
HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_SET); // 打开气缸爪子
- osDelay(5);
+ osDelay(5);
HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_SET); // 打开下气缸
- currentState1 = BALL_DROP; // 切换到球下落状态
+ currentState1 = BALL_DROP; // 切换到球下落状态
break;
- case BALL_DROP:
- osDelay(100); // 延时 100ms
+ case BALL_DROP:
+ osDelay(100); //不要动这里
HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 关闭下气缸
// 一直检测有球(ball_state == 0),等球离开
if (ball_state == 1 && last_ball_state == 0) // 球离开
@@ -390,7 +373,7 @@ void Ball::ballHadling(void)
last_ball_state = ball_state;
break;
- case BALL_FLAG:
+ case BALL_FLAG:
osDelay(10); // 延时 50ms
// 等待球弹回再次检测到球
if (ball_state == 0 && last_ball_state == 1) // 球弹回
@@ -402,26 +385,22 @@ void Ball::ballHadling(void)
case BALL_CLOSE:
- osDelay(25);
- HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 闭合气缸爪子
- currentState1 = BALL_FINISH; // 切换到反转状态
+ osDelay(100);
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 闭合气缸爪子
+ currentState1 = BALL_FINISH; // 切换到反转状态
break;
case BALL_FINISH:
osDelay(50); // 延时 50ms
- //osThreadFlagsSet(task_struct.thread.ball, HANDING_FINISH);
+ // osThreadFlagsSet(task_struct.thread.ball, HANDING_FINISH);
HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET); // 确保气缸爪子闭合
- HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
+ HAL_GPIO_WritePin(DOWN_GPIO_Port, DOWN_Pin, GPIO_PIN_RESET); // 确保下气缸关闭
break;
default:
currentState1 = BALL_IDLE; // 默认回到空闲状态
break;
}
+}
-
- }
-
-
#endif
-