diff --git a/modules/motor/servo_motor.c b/modules/motor/servo_motor.c index e69de29..80132cc 100644 --- a/modules/motor/servo_motor.c +++ b/modules/motor/servo_motor.c @@ -0,0 +1,122 @@ +#include "servo_motor.h" + +extern TIM_HandleTypeDef htim1; +/*第二版*/ +static ServoInstance *servo_motor[SERVO_MOTOR_CNT] = {NULL}; +static int16_t compare_value[SERVO_MOTOR_CNT]={0}; +static uint8_t servo_idx = 0; // register servo_idx,是该文件的全局舵机索引,在注册时使用 + +// 通过此函数注册一个舵机 +ServoInstance *ServoInit(Servo_Init_Config_s *Servo_Init_Config) +{ + servo_motor[servo_idx] = (ServoInstance *)malloc(sizeof(ServoInstance)); + memset(servo_motor[servo_idx], 0, sizeof(ServoInstance)); + servo_motor[servo_idx]->Servo_type = Servo_Init_Config->Servo_type; + servo_motor[servo_idx]->htim = Servo_Init_Config->htim; + servo_motor[servo_idx]->Channel = Servo_Init_Config->Channel; + HAL_TIM_Base_Start(Servo_Init_Config->htim); + HAL_TIM_PWM_Start(Servo_Init_Config->htim, Servo_Init_Config->Channel); + return servo_motor[servo_idx++]; +} + +/** + * @brief 写入自由角度数值 + * + * @param Servo_Motor 注册的舵机实例 + * @param S_angle 改变自由模式设定的角度 + */ +void Servo_Motor_FreeAngle_Set(ServoInstance *Servo_Motor, int16_t S_angle) +{ + switch (Servo_Motor->Servo_type) + { + case Servo180: + if (S_angle > 180) + S_angle = 180; + break; + case Servo270: + if (S_angle > 270) + S_angle = 270; + break; + case Servo360: + if (S_angle > 100) + S_angle = 100; + break; + default: + break; + } + if (S_angle < 0) + S_angle = 0; + Servo_Motor->Servo_Angle.free_angle = S_angle; +} + +/** + * @brief 写入起始,终止角度数值 + * + * @param Servo_Motor 注册的舵机实例 + * @param Start_angle 起始角度 + * @param Final_angle 终止角度 + */ +void Servo_Motor_StartSTOP_Angle_Set(ServoInstance *Servo_Motor, int16_t Start_angle, int16_t Final_angle) +{ + Servo_Motor->Servo_Angle.Init_angle = Start_angle; + Servo_Motor->Servo_Angle.Final_angle = Final_angle; +} +/** + * @brief 舵机模式选择 + * + * @param Servo_Motor 注册的舵机实例 + * @param mode 需要选择的模式 + */ +void Servo_Motor_Type_Select(ServoInstance *Servo_Motor,int16_t mode) +{ + Servo_Motor->Servo_Angle_Type = mode; +} + + +/** + * @brief 舵机输出控制 + * + */ +void Servo_Motor_Control() +{ + static ServoInstance *Servo_Motor; + static Servo_Type_e Servo_type; + + for (size_t i = 0; i < servo_idx; i++) + { + if (servo_motor[i]) + { + Servo_Motor = servo_motor[i]; + Servo_type = Servo_Motor->Servo_type; + + switch (Servo_type) + { + case Servo180: + if (Servo_Motor->Servo_type == Start_mode) + compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Init_angle * 20000 / 20 / 90; + if (Servo_Motor->Servo_type == Final_mode) + compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Final_angle * 20000 / 20 / 90; + if (Servo_Motor->Servo_type == Free_Angle_mode) + compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.free_angle * 20000 / 20 / 90; + __HAL_TIM_SET_COMPARE(Servo_Motor->htim, Servo_Motor->Channel, compare_value[i]); + break; + case Servo270: + if (Servo_Motor->Servo_type == Start_mode) + compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Init_angle * 20000 / 20 / 135; + if (Servo_Motor->Servo_type == Final_mode) + compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Final_angle * 20000 / 20 / 135; + if (Servo_Motor->Servo_type == Free_Angle_mode) + compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.free_angle * 20000 / 20 / 135; + __HAL_TIM_SET_COMPARE(Servo_Motor->htim, Servo_Motor->Channel, compare_value[i]); + break; + case Servo360: + /*500-2500的占空比 500-1500对应正向转速 1500-2500对于反向转速*/ + compare_value[i] = 500 + 20 * Servo_Motor->Servo_Angle.servo360speed; + __HAL_TIM_SET_COMPARE(Servo_Motor->htim, Servo_Motor->Channel, compare_value[i]); + break; + default: + break; + } + } + } +} diff --git a/modules/motor/servo_motor.h b/modules/motor/servo_motor.h index e69de29..d64180c 100644 --- a/modules/motor/servo_motor.h +++ b/modules/motor/servo_motor.h @@ -0,0 +1,90 @@ +/** + * @file servo_motor.h + * @author panrui + * @brief 舵机控制头文件 + * @version 0.1 + * @date 2022-12-12 + * + * @copyright Copyright (c) 2022 + * + */ + +#ifndef SERVO_MOTOR_H +#define SERVO_MOTOR_H + +#include "main.h" +#include "tim.h" +#include + +#define SERVO_MOTOR_CNT 7 + +/*各种舵机类型*/ +typedef enum +{ + Servo180 = 0, + Servo270 = 1, + Servo360 = 2, +} Servo_Type_e; + +/*舵机模式选择*/ +typedef enum +{ + Free_Angle_mode, // 任意角度模式 + Start_mode, // 起始角度模式 + Final_mode, // 终止角度模式 +} Servo_Angle_Type_e; +/*角度设置*/ +typedef struct +{ + /*起止角度模式设置值*/ + int16_t Init_angle; + int16_t Final_angle; + /*任意角度模式设置值*/ + int16_t free_angle; + /*下述值仅仅适用于360°舵机 + *设定值为0-100 为速度值百分比 + *0-50为正转 速度由快到慢 + *51-100为反转 速度由慢到快 + */ + int16_t servo360speed; +} Servo_Angle_s; + +/* 用于初始化不同舵机的结构体,各类舵机通用 */ +typedef struct +{ + Servo_Type_e Servo_type; + Servo_Angle_Type_e Servo_Angle_Type; + // 使用的定时器类型及通道 + TIM_HandleTypeDef *htim; + /*Channel值设定 + *TIM_CHANNEL_1 + *TIM_CHANNEL_2 + *TIM_CHANNEL_3 + *TIM_CHANNEL_4 + *TIM_CHANNEL_ALL + */ + uint32_t Channel; + +} Servo_Init_Config_s; +typedef struct +{ + Servo_Angle_Type_e Servo_Angle_Type; + Servo_Angle_s Servo_Angle; + Servo_Type_e Servo_type; + // 使用的定时器类型及通道 + TIM_HandleTypeDef *htim; + /*Channel值设定 + *TIM_CHANNEL_1 + *TIM_CHANNEL_2 + *TIM_CHANNEL_3 + *TIM_CHANNEL_4 + *TIM_CHANNEL_ALL + */ + uint32_t Channel; +} ServoInstance; + +ServoInstance *ServoInit(Servo_Init_Config_s *Servo_Init_Config); +void Servo_Motor_FreeAngle_Set(ServoInstance *Servo_Motor, int16_t S_angle); +void Servo_Motor_Type_Select(ServoInstance *Servo_Motor,int16_t mode); +void Servo_Motor_Control(); +#endif // SERVO_MOTOR_H \ No newline at end of file diff --git a/modules/motor/servo_motor.md b/modules/motor/servo_motor.md index e69de29..f585360 100644 --- a/modules/motor/servo_motor.md +++ b/modules/motor/servo_motor.md @@ -0,0 +1,66 @@ +##舵机的使用 +

panrui@hnu.edu.cn

+### 舵机基础知识 +已最常见的SG90舵机为例,SG90舵机要求工作在频率为50HZ——周期为20ms的PWM波,且对应信号的高低电平在0.5ms - 2.5ms之间,对应的舵机转动角度如下表所示(当然也可以按照这个线性的对应关系去达到转动自己想要的角度,如想要转动60°,则高电平脉宽为大概为1.2ms,具体能不能转到特定的角度还和舵机的精度有关) + +>0.5ms-------------0度; 2.5% +>1.0ms------------45度; 5.0% +>1.5ms------------90度; 7.5% +>2.0ms-----------135度; 10.0% +>2.5ms-----------180度; 12.5% + +根据`Tout = (PSC+1)* (ARR+1)/Tclk`公式 +则我们需要产生50Hz的PWM波,则预分频的系数为 Prescaler = 168-1,自动重装载值 Counter Period = 20000-1,此时定时器产生的频率为 168Mhz/168/20000 = 50Hz。 当然这个值也可以自己设置,只要满足产生的频率为50Hz即可. +`__HAL_TIM_SET_COMPARE(htim, Channel, compare_value);` +这是设置占空比的函数 +eg:当初始占空比为1200/20000则为6%,根据20*6%=1.2ms (1.2-0.5)/(2.5-0.5)*180=63° 故舵机会转动63° +为了方便通过上述eg我们将所需要的角度与PWM计数值对应关系封装成函数。只需要输入我们所需要的角度和相关定时器参数即可。这样我们就可以设置SG90为参数范围内(0~180°)任意度数。 + +--- + +## 如何注册一个舵机实例 +!!! + +**注意!由于舵机为开环控制,无论选择舵机为何种类型,舵机都能够正常运行,但是运行的角度可能会与设定不同,请务必正确选择舵机型号!且最多添加7个舵机!** +我们可以像这样注册一个舵机实例 +```c + +static ServoInstance *leftservomoto; +//初始化参数 +Servo_Init_Config_s config={ + //舵机安装选择的定时器及通道 + //C板有常用的7路PWM输出:TIM1-1,2,3,4 TIM8-1,2,3 + .htim=&htim1, + .Channel=TIM_CHANNEL_1, + //舵机的初始化模式和类型 + .Servo_Angle_Type=Start_mode, + .Servo_type=Servo180, +}; +// 设置好参数后进行初始化并保留返回的指针 +leftservomoto = ServoInit(&config); +``` +>要控制一个舵机 我们提供了以下三个接口 +```c +//自由模式下,写入自由角度数值 +void Servo_Motor_FreeAngle_Set(ServoInstance *Servo_Motor, int16_t S_angle); +//起止模式下,写入起始,终止角度数值(防止反复写入起始和终止角度) +void Servo_Motor_StartSTOP_Angle_Set(ServoInstance *Servo_Motor, int16_t Start_angle, int16_t Final_angle); +/* + Free_Angle_mode, // 任意角度模式 + Start_mode, // 起始角度模式 + Final_mode, // 终止角度模式 +*/ +void Servo_Motor_Type_Select(ServoInstance *Servo_Motor,int16_t mode); +//比如我们要使用舵机,并更改一个舵机的模式 +void ServoTask() +{ + //更改leftservomoto为Free_Angle_mode模式 + Servo_Motor_Type_Select(leftservomoto,Free_Angle_mode); + //设置转到0角度 + Servo_Motor_FreeAngle_Set(leftservomoto, 0); + //调用函数,控制电机 + Servo_Motor_Control(); +} + + +``` \ No newline at end of file