mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 19:47:44 +08:00
rm vofa/led,add bluetooth,change servo_motor
This commit is contained in:
@@ -115,14 +115,16 @@ void DMMotorOuterLoop(DMMotorInstance *motor, Closeloop_Type_e type)
|
||||
motor->motor_settings.outer_loop_type = type;
|
||||
}
|
||||
|
||||
|
||||
//@Todo: 目前只实现了力控,更多位控PID等请自行添加
|
||||
void DMMotorTask(void const *argument)
|
||||
{
|
||||
float pid_measure, pid_ref, set;
|
||||
float pid_ref, set;
|
||||
DMMotorInstance *motor = (DMMotorInstance *)argument;
|
||||
DM_Motor_Measure_s *measure = &motor->measure;
|
||||
//DM_Motor_Measure_s *measure = &motor->measure;
|
||||
Motor_Control_Setting_s *setting = &motor->motor_settings;
|
||||
CANInstance *motor_can = motor->motor_can_instace;
|
||||
uint16_t tmp;
|
||||
//CANInstance *motor_can = motor->motor_can_instace;
|
||||
//uint16_t tmp;
|
||||
DMMotor_Send_s motor_send_mailbox;
|
||||
while (1)
|
||||
{
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
|
||||
#define DM_MOTOR_CNT 4
|
||||
|
||||
#define DM_P_MIN -12.5f
|
||||
#define DM_P_MIN (-12.5f)
|
||||
#define DM_P_MAX 12.5f
|
||||
#define DM_V_MIN -45.0f
|
||||
#define DM_V_MIN (-45.0f)
|
||||
#define DM_V_MAX 45.0f
|
||||
#define DM_T_MIN -18.0f
|
||||
#define DM_T_MIN (-18.0f)
|
||||
#define DM_T_MAX 18.0f
|
||||
|
||||
typedef struct
|
||||
|
||||
@@ -21,7 +21,5 @@ void MotorControlTask()
|
||||
// HTMotorControl();
|
||||
// 将所有的CAN设备集中在一处发送,最高反馈频率仅能达到500Hz,为了更好的控制效果,应使用新的HTMotorControlInit()接口
|
||||
|
||||
ServeoMotorControl();
|
||||
|
||||
// StepMotorControl();
|
||||
}
|
||||
|
||||
@@ -1,122 +1,77 @@
|
||||
#include "servo_motor.h"
|
||||
#include "stdlib.h"
|
||||
#include "memory.h"
|
||||
#include "bsp_log.h"
|
||||
uint8_t servo_angle_read[6]={0x55 ,0x55 ,0x04, 0x15 ,0x01 ,0x01 ,};
|
||||
uint8_t servo_angle_write[16]={0x55 ,0x55, 0x08, 0x03, 0x01 ,0xF4 ,0x01 ,0x01 ,0x20 ,0x03,0x55 ,0x55 ,0x04, 0x15 ,0x01 ,0x01 ,};
|
||||
uint8_t servo_unload[6]={0x55,0x55,0x04,0x14,0x01,0x01};
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
/*第二版*/
|
||||
static ServoInstance *servo_motor_instance[SERVO_MOTOR_CNT] = {NULL};
|
||||
static int16_t compare_value[SERVO_MOTOR_CNT] = {0};
|
||||
static ServoInstance *servo_motor_instance[SERVO_MOTOR_CNT];
|
||||
static uint8_t servo_idx = 0; // register servo_idx,是该文件的全局舵机索引,在注册时使用
|
||||
|
||||
static void DecodeServo();
|
||||
// 通过此函数注册一个舵机
|
||||
ServoInstance *ServoInit(Servo_Init_Config_s *Servo_Init_Config)
|
||||
{
|
||||
ServoInstance *servo = (ServoInstance *)malloc(sizeof(ServoInstance));
|
||||
memset(servo, 0, sizeof(ServoInstance));
|
||||
USART_Init_Config_s config;
|
||||
servo->servo_type = Servo_Init_Config->servo_type;
|
||||
switch (Servo_Init_Config->servo_type)
|
||||
{
|
||||
case Bus_Servo:
|
||||
config.module_callback = DecodeServo;
|
||||
config.recv_buff_size = Servo_MAX_BUFF;
|
||||
config.usart_handle = Servo_Init_Config->_handle;
|
||||
servo->usart_instance = USARTRegister(&config);
|
||||
break;
|
||||
case PWM_Servo:
|
||||
servo->pwm_instance = PWMRegister(&Servo_Init_Config->pwm_init_config);
|
||||
break;
|
||||
default:
|
||||
LOGERROR("Servo type error");
|
||||
break;
|
||||
}
|
||||
servo->servo_id = Servo_Init_Config->servo_id;
|
||||
servo_idx++;
|
||||
servo_motor_instance[servo->servo_id] = servo;
|
||||
|
||||
servo->Servo_type = Servo_Init_Config->Servo_type;
|
||||
servo->htim = Servo_Init_Config->htim;
|
||||
servo->Channel = Servo_Init_Config->Channel;
|
||||
|
||||
HAL_TIM_PWM_Start(Servo_Init_Config->htim, Servo_Init_Config->Channel);
|
||||
servo_motor_instance[servo_idx++] = servo;
|
||||
return servo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 写入自由角度数值
|
||||
*
|
||||
* @param Servo_Motor 注册的舵机实例
|
||||
* @param S_angle 改变自由模式设定的角度
|
||||
*/
|
||||
void Servo_Motor_FreeAngle_Set(ServoInstance *Servo_Motor, int16_t S_angle)
|
||||
//@todo PWM舵机的角度设置需要根据相应定时器PWM等参数进行计算(是否需要规范定时器PWM的初始化参数,以便于计算)
|
||||
void ServoSetAngle(ServoInstance *servo, float angle)
|
||||
{
|
||||
switch (Servo_Motor->Servo_type)
|
||||
|
||||
switch (servo->servo_type)
|
||||
{
|
||||
case Servo180:
|
||||
if (S_angle > 180)
|
||||
S_angle = 180;
|
||||
case Bus_Servo:
|
||||
servo_angle_write[8] = (uint16_t)angle&0xff;
|
||||
servo_angle_write[9] = (uint16_t)angle>>8;
|
||||
USARTSend(servo->usart_instance, servo_angle_write, 16, USART_TRANSFER_DMA);
|
||||
// USARTSend(servo->usart_instance, servo_angle_read, 6, USART_TRANSFER_DMA);
|
||||
break;
|
||||
case Servo270:
|
||||
if (S_angle > 270)
|
||||
S_angle = 270;
|
||||
break;
|
||||
case Servo360:
|
||||
if (S_angle > 100)
|
||||
S_angle = 100;
|
||||
case PWM_Servo:
|
||||
servo->angle = angle;
|
||||
PWMSetDutyRatio(servo->pwm_instance, angle);
|
||||
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)
|
||||
//@todo 只读取了角度 还有电压,动作是否完成等 且只支持一个串口
|
||||
static void DecodeServo()
|
||||
{
|
||||
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 ServeoMotorControl()
|
||||
{
|
||||
ServoInstance *Servo_Motor;
|
||||
|
||||
for (size_t i = 0; i < servo_idx; i++)
|
||||
for (uint8_t i = 0; i < servo_idx; i++)
|
||||
{
|
||||
if (servo_motor_instance[i])
|
||||
if (servo_motor_instance[i]->servo_type == Bus_Servo)
|
||||
{
|
||||
Servo_Motor = servo_motor_instance[i];
|
||||
|
||||
switch (Servo_Motor->Servo_type)
|
||||
if (servo_motor_instance[i]->usart_instance->recv_buff[0] == Servo_Frame_First && servo_motor_instance[i]->usart_instance->recv_buff[1] == Servo_Frame_Second)
|
||||
{
|
||||
case Servo180:
|
||||
if (Servo_Motor->Servo_Angle_Type == Start_mode)
|
||||
compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Init_angle * 20000 / 20 / 90;
|
||||
if (Servo_Motor->Servo_Angle_Type == Final_mode)
|
||||
compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Final_angle * 20000 / 20 / 90;
|
||||
if (Servo_Motor->Servo_Angle_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_Angle_Type == Start_mode)
|
||||
compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Init_angle * 20000 / 20 / 135;
|
||||
if (Servo_Motor->Servo_Angle_Type == Final_mode)
|
||||
compare_value[i] = 0.5 * 20000 / 20 + Servo_Motor->Servo_Angle.Final_angle * 20000 / 20 / 135;
|
||||
if (Servo_Motor->Servo_Angle_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;
|
||||
if (servo_motor_instance[i]->usart_instance->recv_buff[3] == 21)
|
||||
{
|
||||
servo_motor_instance[i]->recv_angle = (servo_motor_instance[i]->usart_instance->recv_buff[7] << 8 | servo_motor_instance[i]->usart_instance->recv_buff[6]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,90 +1,45 @@
|
||||
/**
|
||||
* @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 <stdint-gcc.h>
|
||||
#include "bsp_pwm.h"
|
||||
#include "bsp_usart.h"
|
||||
|
||||
#define SERVO_MOTOR_CNT 7
|
||||
|
||||
/*各种舵机类型*/
|
||||
#define Servo_Frame_First 0x55
|
||||
#define Servo_Frame_Second 0x55
|
||||
#define Servo_MAX_BUFF 10
|
||||
#define SERVO_MOVE_CMD 0x03
|
||||
#define SERVO_UNLOAD_CMD 0x14
|
||||
#define SERVO_POS_READ_CMD 0x15
|
||||
typedef enum
|
||||
{
|
||||
Servo180 = 0,
|
||||
Servo270 = 1,
|
||||
Servo360 = 2,
|
||||
} Servo_Type_e;
|
||||
Servo_None_Type = 0,
|
||||
Bus_Servo = 1,
|
||||
PWM_Servo = 2,
|
||||
}ServoType_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;
|
||||
PWM_Init_Config_s pwm_init_config;
|
||||
ServoType_e servo_type;
|
||||
UART_HandleTypeDef *_handle;
|
||||
uint8_t servo_id;
|
||||
}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;
|
||||
{
|
||||
uint8_t servo_id;
|
||||
float angle;
|
||||
uint16_t recv_angle;
|
||||
PWMInstance *pwm_instance;
|
||||
USARTInstance *usart_instance;
|
||||
ServoType_e servo_type;
|
||||
}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 ServeoMotorControl();
|
||||
void ServoSetAngle(ServoInstance *servo, float angle);
|
||||
#endif // SERVO_MOTOR_H
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
<p align='left' >panrui@hnu.edu.cn</p>
|
||||
|
||||
> todo: 由于新增了bsp_pwm的支持,舵机模块需要部分重构
|
||||
> todo: 角度设置等需要规范化
|
||||
### 舵机基础知识
|
||||
|
||||
已最常见的SG90舵机为例,SG90舵机要求工作在频率为50HZ——周期为20ms的PWM波,且对应信号的高低电平在0.5ms - 2.5ms之间,对应的舵机转动角度如下表所示(当然也可以按照这个线性的对应关系去达到转动自己想要的角度,如想要转动60°,则高电平脉宽为大概为1.2ms,具体能不能转到特定的角度还和舵机的精度有关)
|
||||
@@ -21,50 +21,3 @@ eg:当初始占空比为1200/20000则为6%,根据20*6%=1.2ms (1.2-0.5)/(
|
||||
为了方便通过上述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();
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user