From a609af6ba4e0c08bd892720000831060d343de82 Mon Sep 17 00:00:00 2001
From: ws <1621320660@qq.com>
Date: Sun, 20 Apr 2025 20:38:16 +0800
Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E7=94=A8=E4=B8=89=E6=91=A9=E6=93=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Core/Src/gpio.c | 2 +-
Core/Src/stm32f4xx_it.c | 16 +++---
MDK-ARM/R1-shooter.uvoptx | 43 +++++++++++++---
R1-shooter.ioc | 3 +-
User/module/ball.cpp | 106 +++++++++++++++++++++++++++-----------
User/module/ball.hpp | 17 +++---
User/task/ballTask.cpp | 11 ++--
7 files changed, 138 insertions(+), 60 deletions(-)
diff --git a/Core/Src/gpio.c b/Core/Src/gpio.c
index 8779997..59f14c6 100644
--- a/Core/Src/gpio.c
+++ b/Core/Src/gpio.c
@@ -94,7 +94,7 @@ void MX_GPIO_Init(void)
/*Configure GPIO pin : PtPin */
GPIO_InitStruct.Pin = KEY_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
+ GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(KEY_GPIO_Port, &GPIO_InitStruct);
diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c
index c17f522..f54e09f 100644
--- a/Core/Src/stm32f4xx_it.c
+++ b/Core/Src/stm32f4xx_it.c
@@ -279,16 +279,16 @@ void USART1_IRQHandler(void)
/**
* @brief This function handles USART3 global interrupt.
*/
-//void USART3_IRQHandler(void)
-//{
-// /* USER CODE BEGIN USART3_IRQn 0 */
+// void USART3_IRQHandler(void)
+// {
+// /* USER CODE BEGIN USART3_IRQn 0 */
-// /* USER CODE END USART3_IRQn 0 */
-// HAL_UART_IRQHandler(&huart3);
-// /* USER CODE BEGIN USART3_IRQn 1 */
+// /* USER CODE END USART3_IRQn 0 */
+// HAL_UART_IRQHandler(&huart3);
+// /* USER CODE BEGIN USART3_IRQn 1 */
-// /* USER CODE END USART3_IRQn 1 */
-//}
+// /* USER CODE END USART3_IRQn 1 */
+// }
/**
* @brief This function handles DMA2 stream1 global interrupt.
diff --git a/MDK-ARM/R1-shooter.uvoptx b/MDK-ARM/R1-shooter.uvoptx
index 12e95e5..6ae841a 100644
--- a/MDK-ARM/R1-shooter.uvoptx
+++ b/MDK-ARM/R1-shooter.uvoptx
@@ -188,44 +188,71 @@
7
1
- ball,0x0A
+ cnt1,0x0A
8
1
- cnt1,0x0A
+ cmd_fromnuc
9
1
- cmd_fromnuc
+ nucbuf
10
1
- nucbuf
+ nucData
11
1
- nucData
+ send,0x0A
12
1
- send,0x0A
+ speed1,0x0A
13
1
- speed1,0x0A
+ abc,0x0A
14
1
+ \\R1_shooter\../User/task/ballTask.cpp\ball,0x0A
+
+
+ 15
+ 1
+ speedm,0x0A
+
+
+ 16
+ 1
abc,0x0A
+
+ 17
+ 1
+ flag,0x0A
+
+
+ 18
+ 1
+ ball_state,0x0A
+
+
+
+ 0
+ 2
+ ball,0x0A
+
+
0
@@ -1060,7 +1087,7 @@
User/task
- 0
+ 1
0
0
0
diff --git a/R1-shooter.ioc b/R1-shooter.ioc
index 8787544..2091ec3 100644
--- a/R1-shooter.ioc
+++ b/R1-shooter.ioc
@@ -174,8 +174,9 @@ NVIC.USART1_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.USART3_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.USART6_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
-PA0-WKUP.GPIOParameters=GPIO_PuPd,GPIO_Label
+PA0-WKUP.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_ModeDefaultEXTI
PA0-WKUP.GPIO_Label=KEY
+PA0-WKUP.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
PA0-WKUP.GPIO_PuPd=GPIO_PULLUP
PA0-WKUP.Locked=true
PA0-WKUP.Signal=GPXTI0
diff --git a/User/module/ball.cpp b/User/module/ball.cpp
index 51dd632..c1d6ac8 100644
--- a/User/module/ball.cpp
+++ b/User/module/ball.cpp
@@ -7,12 +7,16 @@ extern RC_ctrl_t rc_ctrl;
extern int key;
// 定义状态机变量
-BallState_t currentBallState = BALL_IDLE;
+BallState_t currentState1 = BALL_IDLE; // 当前状态
+uint32_t startTime = 0; // 用于记录延时的起始时间
+int speedm=0;
#define MOTOR_SPEED 1000
const fp32 Ball:: M3508_speed_PID[3] = {5, 0, 0};
+//PE11 气缸
+
//三摩擦轮运球!!!
Ball ::Ball()
@@ -45,46 +49,90 @@ void Ball ::Spin(float speed)
}
- CAN_cmd_200(result[MOTOR_1],result[MOTOR_2],result[MOTOR_3],0,&hcan1);
- osDelay(1);
-
}
+ int flag =0;
+ int ball_state = 0;
void Ball::ballHadling(void)
{
- if (key > 0) // 检测按键是否被按下
+
+ ball_state =HAL_GPIO_ReadPin(up_ball_GPIO_Port, up_ball_Pin);//有球 0 无球 1
+ switch (currentState1)
{
- if (key % 2 == 1) // key 为奇数,摩擦轮正转
+ case BALL_IDLE:
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET);
+ if (key > 0) // 检测按键是否被按下
{
- Spin(5000); // 正转
+ speedm=-4000;
+ currentState1 = BALL_FORWARD; // 切换到正转状态
}
- else if (key % 2 == 0) // key 为偶数,摩擦轮反转
+ break;
+
+ case BALL_FORWARD:
+
+ if ( hand_Motor[MOTOR_1]->speed_rpm > 3980&&hand_Motor[MOTOR_1]->speed_rpm <= 4010&&
+ hand_Motor[MOTOR_2]->speed_rpm <= -3980&&hand_Motor[MOTOR_2]->speed_rpm >= -4010&&
+ hand_Motor[MOTOR_3]->speed_rpm <= -3980&&hand_Motor[MOTOR_2]->speed_rpm >= -4010 )
{
- Spin(-5000); // 反转
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_SET);// 打开气缸
+ currentState1 = BALL_DROP; // 切换到球下落状态
}
+ break;
+
+ case BALL_DROP:
+
+ if (ball_state == 1)
+ {
+ osDelay(200); // 延时50ms
+ speedm=2500;
+ currentState1 = BALL_REVERSE; // 切换到反转状态
+ }
+
+ break;
+
+ case BALL_REVERSE:
+
+ if (ball_state == 0)
+ {
+
+ flag=2;
+ speedm=0;
+ currentState1 = BALL_CLOSE; // 切换到完成状态
+ }
+
+ break;
+
+ case BALL_CLOSE:
+ osDelay(200); // 延时50ms
+ if (flag == 2)
+ {
+ if(ball_state == 0)
+ {
+ HAL_GPIO_WritePin(CLOSE_GPIO_Port, CLOSE_Pin, GPIO_PIN_RESET);
+ currentState1 = BALL_FINISH; // 切换到完成状态
+ }
+
+ }
+ break;
+
+ case BALL_FINISH:
+ osDelay(200); // 延时50ms
+ key = 0; // 重置按键状态
+ flag=0;
+ speedm=0;
+ currentState1 = BALL_IDLE; // 回到空闲状态
+ break;
+
+ default:
+ currentState1 = BALL_IDLE; // 默认回到空闲状态
+ break;
}
}
-// void Ball::ballHadling(void)
-// {
-// static bool isReversed = false; // 静态变量,记录当前摩擦轮状态,初始为正转
+void Ball::Send_control()
+{
+ CAN_cmd_200(result[MOTOR_1],result[MOTOR_2],result[MOTOR_3],0,&hcan1);
-// if (key > 0) // 检测按键是否被按下
-// {
-// key = 0; // 重置按键状态,防止重复触发
+}
-// if (isReversed)
-// {
-// // 当前为反转,切换为正转
-// Spin(MOTOR_SPEED); // 正转
-// isReversed = false;
-// }
-// else
-// {
-// // 当前为正转,切换为反转
-// Spin(-MOTOR_SPEED); // 反转
-// isReversed = true;
-// }
-// }
-// }
diff --git a/User/module/ball.hpp b/User/module/ball.hpp
index b44dafa..a6f196d 100644
--- a/User/module/ball.hpp
+++ b/User/module/ball.hpp
@@ -9,12 +9,12 @@
// 定义状态枚举
typedef enum {
- BALL_IDLE, // 空闲状态
- BALL_RELEASE, // 下球气缸弹出,爪子张开
- BALL_JOINT_REVERSE, // 关节电机反转置水平位置
- BALL_FALLING, // 球加速落下
- BALL_WAIT_BOUNCE, // 等待光电检测反弹
- BALL_NET_CLOSE // 收网状态
+ BALL_IDLE, // 空闲状态
+ BALL_FORWARD, // 正转状态
+ BALL_DROP, // 球下落状态
+ BALL_REVERSE, // 反转状态
+ BALL_CLOSE, // 关闭状态
+ BALL_FINISH // 完成状态
} BallState_t;
typedef enum
@@ -35,14 +35,15 @@ public:
Ball();
void Spin(float speed);
void ballHadling(void);
+ void Send_control(void);
void ballStateMachine(void);
int16_t result[MOTOR_NUM];
-
+ motor_measure_t *hand_Motor[MOTOR_NUM];
private:
int up_ball_state; //上球状态 0:无球 1:有球
- motor_measure_t *hand_Motor[MOTOR_NUM];
+
//三个摩擦轮
static const float M3508_speed_PID[3];
static const float M3508_angle_PID[3];
diff --git a/User/task/ballTask.cpp b/User/task/ballTask.cpp
index af7afe9..bdc1a75 100644
--- a/User/task/ballTask.cpp
+++ b/User/task/ballTask.cpp
@@ -10,6 +10,7 @@ Ball ball;
int abc=0;
int c30=0;
int speed1=500;
+extern int speedm;
void FunctionBall(void *argument)
{
@@ -17,16 +18,16 @@ void FunctionBall(void *argument)
const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_BALL;
uint32_t tick = osKernelGetTickCount();
-
+
while(1)
{
#ifdef DEBUG
task_struct.stack_water_mark.ball = osThreadGetStackSpace(osThreadGetId());
#endif
- abc=HAL_GPIO_ReadPin(STOP_GPIO_Port, STOP_Pin);
- ball.ballHadling(); // 处理摩擦轮转动
- // ball.Spin(speed1);
- osDelay(3);
+ abc=HAL_GPIO_ReadPin(up_ball_GPIO_Port, up_ball_Pin);
+ ball.ballHadling(); // 处理摩擦轮转动
+ ball.Spin(speedm);
+ ball.Send_control();
tick += delay_tick; /* 计算下一个唤醒时刻 */
osDelayUntil(tick);