/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : machine.h * @brief : LK540 Machine Stepper Motor Control Header * 步进电机控制模块头文件 ****************************************************************************** * @attention * * Copyright (c) 2026 STMicroelectronics. * All rights reserved. * ****************************************************************************** */ /* USER CODE END Header */ #ifndef __MACHINE_H #define __MACHINE_H #ifdef __cplusplus extern "C" { #endif /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Exported types ------------------------------------------------------------*/ /** * @brief 步进电机方向枚举 */ typedef enum { MOTOR_DIR_CW = 0, /* 向左移动 (Clockwise) */ MOTOR_DIR_CCW = 1 /* 向右移动 (Counter Clockwise) */ } Motor_DirectionTypeDef; /* Exported constants --------------------------------------------------------*/ /* 电机参数定义 */ #define MOTOR_PULSE_PER_MM 5.33f /* 每毫米脉冲数 */ #define MOTOR_ORIGIN_DISTANCE 215.0f /* 原点距离 (mm) */ #define MOTOR_MATERIAL1_DISTANCE 192.4f /* 1#料距离 (mm) */ #define MOTOR_MATERIAL2_DISTANCE 65.4f /* 2#料距离 (mm) */ #define MOTOR_MATERIAL3_DISTANCE 61.6f /* 3#料距离 (mm) */ #define MOTOR_MATERIAL4_DISTANCE 188.6f /* 4#料距离 (mm) */ /* GPIO引脚定义 */ #define MOTOR_ORIGIN_PIN_PORT GPIOB #define MOTOR_ORIGIN_PIN GPIO_PIN_3 #define MOTOR_LIMIT_PIN_PORT GPIOG #define MOTOR_LIMIT_PIN GPIO_PIN_15 #define MOTOR_DIR_PIN_PORT GPIOD #define MOTOR_DIR_PIN GPIO_PIN_7 #define MOTOR_STEP_PIN_PORT GPIOG #define MOTOR_STEP_PIN GPIO_PIN_9 #define CYLINDER_PIN_PORT GPIOG #define CYLINDER_PIN GPIO_PIN_8 /* Exported macro ------------------------------------------------------------*/ /* Exported functions prototypes ---------------------------------------------*/ /** * @brief 检测步进电机是否在原点 * @retval 1: 在原点, 0: 不在原点 */ uint8_t Machine_IsAtOrigin(void); /** * @brief 检测步进电机是否在临界点(限位传感器) * @retval 1: 在临界点, 0: 不在临界点 */ uint8_t Machine_IsAtLimit(void); /** * @brief 设置步进电机方向 * @param dir: 方向 (MOTOR_DIR_CW: 向左, MOTOR_DIR_CCW: 向右) * @retval None */ void Machine_SetDirection(Motor_DirectionTypeDef dir); /** * @brief 控制气缸状态 * @param state: 状态 (0: 张开, 1: 闭合) * @note PG8为0表示张开,1表示闭合 * @retval None */ void Machine_SetCylinder(uint8_t state); /** * @brief 步进电机移动一步 * @note 产生一个脉冲信号,电机移动一步(1/5.33 mm) * @retval None */ void Machine_Step(void); /** * @brief 步进电机移动到原点 * @note 一直向左移动直到检测到原点传感器(PB3为0) * 如果检测到限位传感器(PG15为0),则停止并返回错误 * @retval 0: 成功到达原点, 1: 到达限位点(错误) */ uint8_t Machine_MoveToOrigin(void); /** * @brief 步进电机从原点移动到中心点 * @note 从原点(传感器位置)向右移动215.0mm到达中心点 * @retval 0: 成功, 1: 失败(到达限位点) */ uint8_t Machine_MoveToCenter(void); /** * @brief 步进电机从中心点移动到1#料位置 * @note 从中心点向左移动192.4mm到达1#料 * @retval 0: 成功, 1: 失败(到达限位点) */ uint8_t Machine_MoveToMaterial1(void); /** * @brief 步进电机从中心点移动到2#料位置 * @note 从中心点向左移动65.4mm到达2#料 * @retval 0: 成功, 1: 失败(到达限位点) */ uint8_t Machine_MoveToMaterial2(void); /** * @brief 步进电机从中心点移动到3#料位置 * @note 从中心点向右移动61.6mm到达3#料 * @retval 0: 成功, 1: 失败(到达限位点) */ uint8_t Machine_MoveToMaterial3(void); /** * @brief 步进电机从中心点移动到4#料位置 * @note 从中心点向右移动188.6mm到达4#料 * @retval 0: 成功, 1: 失败(到达限位点) */ uint8_t Machine_MoveToMaterial4(void); /** * @brief 从中心点移动到1#料位置并取料,然后返回中心点放料 * @note 完整的取料流程:移动到1#料 -> 闭合气缸 -> 返回中心点 -> 张开气缸 * @retval 0: 成功, 1: 失败(移动失败) */ uint8_t Machine_PickMaterial1(void); #ifdef __cplusplus } #endif #endif /* __MACHINE_H */