140 lines
4.3 KiB
C
140 lines
4.3 KiB
C
/***
|
|
* @Author: shenghao.xu
|
|
* @Date: 2023-06-13 16:58:57
|
|
* @LastEditors: shenghao.xu
|
|
* @LastEditTime: 2023-06-13 16:59:13
|
|
* @Description:电机驱动模块
|
|
* @email:545403892@qq.com
|
|
* @Copyright (c) 2023 by shenghao.xu, All Rights Reserved.
|
|
*/
|
|
|
|
#ifndef __MOTOR_H
|
|
#define __MOTOR_H
|
|
#include "lib.h"
|
|
#include "gpio.h"
|
|
#include "tim.h"
|
|
|
|
/* 步进电机参数相关宏 */
|
|
#define PULSE_REV 400.0 /* 每圈脉冲数 */
|
|
#define MAX_STEP_ANGLE (360 / PULSE_REV) /* 最小步距(360/PULSE_REV) */
|
|
|
|
typedef enum
|
|
{
|
|
PWM_CHANNEL_1 = TIM_CHANNEL_1,
|
|
PWM_CHANNEL_2 = TIM_CHANNEL_2,
|
|
PWM_CHANNEL_3 = TIM_CHANNEL_3,
|
|
PWM_CHANNEL_4 = TIM_CHANNEL_4,
|
|
} pwm_channel_e;
|
|
|
|
typedef enum
|
|
{
|
|
DIR_CCW = 0, /* 逆时针旋转 */
|
|
DIR_CW, /* 顺时针旋转 */
|
|
} dir_e;
|
|
typedef enum
|
|
{
|
|
DIRECT_CURRENT_MOTOR, // 直流电机
|
|
STEP_MOTOR, // 步进电机
|
|
SERVO_MOTOR, // 舵机
|
|
} motor_type_e; // 电机类型枚举定义
|
|
|
|
typedef enum
|
|
{
|
|
DIRECT_CURRENT_MOTOR_MAX,
|
|
} direct_current_motor_number_e; // 直流电机编号枚举定义
|
|
|
|
typedef enum
|
|
{
|
|
STEP_MOTOR_1,
|
|
STEP_MOTOR_MAX,
|
|
} step_motor_number_e; // 步进电机编号枚举定义
|
|
|
|
typedef enum
|
|
{
|
|
SERVO_MOTOR_MAX,
|
|
} servo_motor_number_e; // 舵机编号枚举定义
|
|
|
|
#pragma pack(1)
|
|
typedef struct
|
|
{
|
|
GPIO_TypeDef *GPIOx;
|
|
uint16_t GPIO_Pin;
|
|
} motor_gpio_t;
|
|
#pragma pack()
|
|
|
|
// 接口定义
|
|
typedef struct
|
|
{
|
|
// public:
|
|
void (*init)(step_motor_number_e num, TIM_HandleTypeDef *tim_handle, motor_gpio_t dir_gpio, motor_gpio_t en_gpio, pwm_channel_e pwm_channel); // 初始化
|
|
void (*stop)(step_motor_number_e num); // 停止
|
|
|
|
void (*set_angle)(step_motor_number_e num, uint32_t angle, dir_e dir); // 将角度转换成脉冲个数并运行
|
|
|
|
void (*stop_cb)(step_motor_number_e num); // 停止回调函数,在stop中执行
|
|
|
|
// private:
|
|
void (*run)(step_motor_number_e num, dir_e dir); // 运行
|
|
} step_motor_interface_t;
|
|
|
|
#pragma pack(1)
|
|
|
|
typedef struct
|
|
{
|
|
|
|
} direct_current_motor_t; // 直流电机结构体
|
|
|
|
typedef struct
|
|
{
|
|
int angle; /* 设置需要旋转的角度 */
|
|
dir_e dir; /* 方向 */
|
|
uint8_t en; /* 使能 */
|
|
volatile uint32_t pulse_count; /* 脉冲个数记录 */
|
|
volatile int add_pulse_count; /* 脉冲个数累计 */
|
|
volatile uint32_t step_angle; /* 步距 */
|
|
} step_motor_attribute_t;
|
|
typedef struct
|
|
{
|
|
TIM_HandleTypeDef *tim_handle; /* 定时器句柄 */
|
|
motor_gpio_t dir_gpio; /* 方向引脚 */
|
|
motor_gpio_t en_gpio; /* 脱机引脚 */
|
|
pwm_channel_e pwm_channel; /* pwm通道 */
|
|
|
|
step_motor_attribute_t attribute; /* 步进电机属性 */
|
|
|
|
step_motor_interface_t interface; /* 步进电机接口 */
|
|
|
|
} step_motor_t; // 步进电机结构体
|
|
|
|
typedef struct
|
|
{
|
|
} servo_motor_t; // 舵机结构体
|
|
|
|
typedef struct
|
|
{
|
|
|
|
} motor_t;
|
|
#pragma pack()
|
|
|
|
/*------------------------电机使能引脚控制--------------------------------*/
|
|
#define ST_ENR(x, port, pin) \
|
|
do \
|
|
{ \
|
|
x ? HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET) : HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET); \
|
|
} while (0)
|
|
|
|
/*----------------------- 方向引脚控制 -----------------------------------*/
|
|
#define ST_DIR(x, port, pin) \
|
|
do \
|
|
{ \
|
|
x ? HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET) : HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); \
|
|
} while (0)
|
|
/******************************************************************************************/
|
|
|
|
extern void motor_init(void);
|
|
extern direct_current_motor_t *get_direct_current_motor(direct_current_motor_number_e number);
|
|
extern step_motor_t *get_step_motor(step_motor_number_e number);
|
|
extern servo_motor_t *get_servo_motor(servo_motor_number_e number);
|
|
|
|
#endif
|