Add cylinder control and material 1 picking function: 1. Add PG8 pin for cylinder control (close=1, open=0) 2. Fix material positions distances 3. Fix material 3 direction (right) 4. Add Machine_PickMaterial1() function 5. Auto execute picking after reaching center in main.c

This commit is contained in:
yankun
2026-02-09 14:50:45 +08:00
parent 069097a0b9
commit 4f62d6e58d
3 changed files with 564 additions and 470 deletions

View File

@@ -56,6 +56,9 @@ typedef enum
#define MOTOR_STEP_PIN_PORT GPIOG #define MOTOR_STEP_PIN_PORT GPIOG
#define MOTOR_STEP_PIN GPIO_PIN_9 #define MOTOR_STEP_PIN GPIO_PIN_9
#define CYLINDER_PIN_PORT GPIOG
#define CYLINDER_PIN GPIO_PIN_8
/* Exported macro ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/
/* Exported functions prototypes ---------------------------------------------*/ /* Exported functions prototypes ---------------------------------------------*/
@@ -78,6 +81,14 @@ uint8_t Machine_IsAtLimit(void);
*/ */
void Machine_SetDirection(Motor_DirectionTypeDef dir); void Machine_SetDirection(Motor_DirectionTypeDef dir);
/**
* @brief 控制气缸状态
* @param state: 状态 (0: 张开, 1: 闭合)
* @note PG8为0表示张开1表示闭合
* @retval None
*/
void Machine_SetCylinder(uint8_t state);
/** /**
* @brief 步进电机移动一步 * @brief 步进电机移动一步
* @note 产生一个脉冲信号电机移动一步1/5.33 mm * @note 产生一个脉冲信号电机移动一步1/5.33 mm
@@ -102,32 +113,39 @@ uint8_t Machine_MoveToCenter(void);
/** /**
* @brief 步进电机从中心点移动到1#料位置 * @brief 步进电机从中心点移动到1#料位置
* @note 从原点215.0mm向左移动22.6mm到达1#料192.4mm * @note 从中心点向左移动192.4mm到达1#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial1(void); uint8_t Machine_MoveToMaterial1(void);
/** /**
* @brief 步进电机从中心点移动到2#料位置 * @brief 步进电机从中心点移动到2#料位置
* @note 从原点215.0mm向左移动149.6mm到达2#料65.4mm * @note 从中心点向左移动65.4mm到达2#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial2(void); uint8_t Machine_MoveToMaterial2(void);
/** /**
* @brief 步进电机从中心点移动到3#料位置 * @brief 步进电机从中心点移动到3#料位置
* @note 从原点215.0mm向右移动153.4mm到达3#料61.6mm * @note 从中心点向右移动61.6mm到达3#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial3(void); uint8_t Machine_MoveToMaterial3(void);
/** /**
* @brief 步进电机从中心点移动到4#料位置 * @brief 步进电机从中心点移动到4#料位置
* @note 从原点215.0mm向右移动26.4mm到达4#料188.6mm * @note 从中心点向右移动188.6mm到达4#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial4(void); uint8_t Machine_MoveToMaterial4(void);
/**
* @brief 从中心点移动到1#料位置并取料,然后返回中心点放料
* @note 完整的取料流程移动到1#料 -> 闭合气缸 -> 返回中心点 -> 张开气缸
* @retval 0: 成功, 1: 失败(移动失败)
*/
uint8_t Machine_PickMaterial1(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -37,11 +37,11 @@
#define MAX_STEPS_TO_ORIGIN 50000 /* 移动到原点的最大步数(防止无限循环) */ #define MAX_STEPS_TO_ORIGIN 50000 /* 移动到原点的最大步数(防止无限循环) */
#define STEP_DELAY_US 100 /* 每步之间的延时(微秒) */ #define STEP_DELAY_US 100 /* 每步之间的延时(微秒) */
/* 各料位相对于点的距离mm */ /* 各料位相对于中心点的距离mm */
#define DISTANCE_TO_MATERIAL1 22.6f /* 215.0 - 192.4 = 22.6mm (向左) */ #define DISTANCE_TO_MATERIAL1 192.4f /* 从中心点向左移动192.4mm */
#define DISTANCE_TO_MATERIAL2 149.6f /* 215.0 - 65.4 = 149.6mm (向左) */ #define DISTANCE_TO_MATERIAL2 65.4f /* 从中心点向左移动65.4mm */
#define DISTANCE_TO_MATERIAL3 153.4f /* 215.0 - 61.6 = 153.4mm (向右) */ #define DISTANCE_TO_MATERIAL3 61.6f /* 从中心点向右移动61.6mm */
#define DISTANCE_TO_MATERIAL4 26.4f /* 215.0 - 188.6 = 26.4mm (向右) */ #define DISTANCE_TO_MATERIAL4 188.6f /* 从中心点向右移动188.6mm */
/* USER CODE END PD */ /* USER CODE END PD */
@@ -181,6 +181,24 @@ void Machine_SetDirection(Motor_DirectionTypeDef dir)
HAL_GPIO_WritePin(MOTOR_DIR_PIN_PORT, MOTOR_DIR_PIN, pinState); HAL_GPIO_WritePin(MOTOR_DIR_PIN_PORT, MOTOR_DIR_PIN, pinState);
} }
/**
* @brief 控制气缸状态
* @param state: 状态 (0: 张开, 1: 闭合)
* @note PG8为0表示张开1表示闭合
* @retval None
*/
void Machine_SetCylinder(uint8_t state)
{
GPIO_PinState pinState;
/* 根据状态设置PG8引脚状态 */
/* 0 = 张开 = GPIO_PIN_RESET, 1 = 闭合 = GPIO_PIN_SET */
pinState = (state == 0) ? GPIO_PIN_RESET : GPIO_PIN_SET;
/* 设置气缸控制引脚 */
HAL_GPIO_WritePin(CYLINDER_PIN_PORT, CYLINDER_PIN, pinState);
}
/** /**
* @brief 步进电机移动一步 * @brief 步进电机移动一步
* @note 产生一个脉冲信号,电机移动一步 * @note 产生一个脉冲信号,电机移动一步
@@ -264,7 +282,7 @@ uint8_t Machine_MoveToCenter(void)
/** /**
* @brief 步进电机从中心点移动到1#料位置 * @brief 步进电机从中心点移动到1#料位置
* @note 从原点215.0mm向左移动22.6mm到达1#料192.4mm * @note 从中心点向左移动192.4mm到达1#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial1(void) uint8_t Machine_MoveToMaterial1(void)
@@ -280,7 +298,7 @@ uint8_t Machine_MoveToMaterial1(void)
/** /**
* @brief 步进电机从中心点移动到2#料位置 * @brief 步进电机从中心点移动到2#料位置
* @note 从原点215.0mm向左移动149.6mm到达2#料65.4mm * @note 从中心点向左移动65.4mm到达2#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial2(void) uint8_t Machine_MoveToMaterial2(void)
@@ -296,7 +314,7 @@ uint8_t Machine_MoveToMaterial2(void)
/** /**
* @brief 步进电机从中心点移动到3#料位置 * @brief 步进电机从中心点移动到3#料位置
* @note 从原点215.0mm向右移动153.4mm到达3#料61.6mm * @note 从中心点向右移动61.6mm到达3#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial3(void) uint8_t Machine_MoveToMaterial3(void)
@@ -312,7 +330,7 @@ uint8_t Machine_MoveToMaterial3(void)
/** /**
* @brief 步进电机从中心点移动到4#料位置 * @brief 步进电机从中心点移动到4#料位置
* @note 从原点215.0mm向右移动26.4mm到达4#料188.6mm * @note 从中心点向右移动188.6mm到达4#料
* @retval 0: 成功, 1: 失败(到达限位点) * @retval 0: 成功, 1: 失败(到达限位点)
*/ */
uint8_t Machine_MoveToMaterial4(void) uint8_t Machine_MoveToMaterial4(void)
@@ -326,6 +344,49 @@ uint8_t Machine_MoveToMaterial4(void)
return Machine_MoveSteps(MOTOR_DIR_CCW, steps); return Machine_MoveSteps(MOTOR_DIR_CCW, steps);
} }
/**
* @brief 从中心点移动到1#料位置并取料,然后返回中心点放料
* @note 1. 从中心点移动到1#料位置
* 2. 闭合气缸取料
* 3. 返回中心点
* 4. 张开气缸放料
* @retval 0: 成功, 1: 失败(移动失败)
*/
uint8_t Machine_PickMaterial1(void)
{
/* 从中心点移动到1#料位置 */
if (Machine_MoveToMaterial1() != 0)
{
return 1; /* 移动失败 */
}
/* 延时,确保到位 */
DelayUs(10000); /* 10ms延时 */
/* 闭合气缸取料 */
Machine_SetCylinder(1);
DelayUs(100000); /* 100ms延时确保气缸闭合完成 */
/* 从1#料位置返回中心点向左移动192.4mm,返回需要向右移动) */
{
uint32_t steps;
steps = Machine_MmToSteps(DISTANCE_TO_MATERIAL1);
if (Machine_MoveSteps(MOTOR_DIR_CCW, steps) != 0)
{
return 1; /* 返回失败 */
}
}
/* 延时,确保到位 */
DelayUs(10000); /* 10ms延时 */
/* 张开气缸放料 */
Machine_SetCylinder(0);
DelayUs(100000); /* 100ms延时确保气缸张开完成 */
return 0; /* 成功 */
}
/* USER CODE BEGIN 1 */ /* USER CODE BEGIN 1 */
/* USER CODE END 1 */ /* USER CODE END 1 */

View File

@@ -22,7 +22,7 @@
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "machine.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
@@ -101,9 +101,14 @@ int main(void)
/* Power-on initialization: Move stepper motor to origin from any position */ /* Power-on initialization: Move stepper motor to origin from any position */
if (Machine_MoveToOrigin() == 0) if (Machine_MoveToOrigin() == 0)
{ {
/* 成功到达原点,移动到中心点(中心点就是原点位置) */ /* 成功到达原点,移动到中心点 */
/* Successfully reached origin, move to center (center is the origin position) */ /* Successfully reached origin, move to center */
Machine_MoveToCenter(); if (Machine_MoveToCenter() == 0)
{
/* 成功到达中心点执行1#料取料流程 */
/* Successfully reached center, execute material 1 picking process */
Machine_PickMaterial1();
}
} }
else else
{ {
@@ -224,6 +229,9 @@ static void MX_GPIO_Init(void)
/*Configure GPIO pin Output Level */ /*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_8, GPIO_PIN_RESET);
/*Configure GPIO pin : PD7 */ /*Configure GPIO pin : PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_7; GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
@@ -238,6 +246,13 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pin : PG8 */
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pin : PG15 */ /*Configure GPIO pin : PG15 */
GPIO_InitStruct.Pin = GPIO_PIN_15; GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;