Skip to content
YLING-O1Public

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

RS05 — STM32F4 CAN Bus Motor Driver Library

基于 STM32F407 的 RS05 系列无刷直流电机 CAN 总线驱动库,支持私有协议(29 位扩展帧)和 MIT 协议(11 位标准帧),提供面向对象的电机管理架构。

硬件平台

项目 说明
主控芯片 STM32F407IGHx (Cortex-M4, 168MHz)
通信接口 CAN1 (PD0-RX / PD1-TX)
CAN 波特率 1 Mbps
电机型号 RS05 系列无刷直流伺服电机
开发环境 CLion + STM32CubeMX + GCC (CMake)

项目结构

RS05/
├── Core/                    # STM32CubeMX 生成的 HAL 层代码
│   ├── Inc/                 # 外设头文件 (main.h, can.h, gpio.h)
│   ├── Src/                 # 外设源文件 (main.c, can.c, gpio.c)
│   └── startup_stm32f407xx.s
├── Drivers/                 # STM32F4 HAL 驱动库 + CMSIS
├── HLY/                     # 用户自定义电机驱动层 ★
│   ├── Inc/
│   │   ├── drv_can.h        # CAN 总线抽象层 + 电机管理结构体
│   │   ├── drv_math.h       # 浮点数/定点数转换工具
│   │   ├── dvc_RS05.h       # RS05 私有协议驱动接口
│   │   └── dvc_MIT_RS05.h   # RS05 MIT 协议驱动接口
│   └── Src/
│       ├── drv_can.c        # CAN 收发 + 中断回调 + 电机注册
│       ├── drv_math.c       # 线性映射转换实现
│       ├── dvc_RS05.c       # 私有协议:数据包打包/解析/控制指令
│       └── dvc_MIT_RS05.c   # MIT 协议:数据包打包/解析/控制指令
├── cmake/                   # STM32CubeMX 的 CMake 子模块
├── build/                   # 构建输出目录
├── CMakeLists.txt           # 顶层 CMake 构建配置
├── CMakePresets.json
├── RS05.ioc                 # STM32CubeMX 工程配置文件
└── STM32F407XX_FLASH.ld     # 链接脚本

功能特性

双协议支持

协议 帧类型 帧格式 说明
私有协议 29 位扩展帧 CAN 2.0B RS05 原厂协议,功能完整
MIT 协议 11 位标准帧 CAN 2.0A 兼容 MIT Cheetah 系列电机协议

控制模式

私有协议

  • 运控模式 (Operation Control Mode): 位置/速度/力矩三环闭环控制(上电默认)
  • 电流模式 (Current Mode): 直接 Iq 电流指令控制
  • 速度模式 (Velocity Mode): 速度闭环 + 电流限制
  • 位置模式 CSP (Cyclic Synchronous Position): 循环同步位置控制
  • 位置模式 PP (Profile Position): 轨迹位置控制

控制律:τ_ref = Kd × (ω_set - ω_actual) + Kp × (θ_set - θ_actual) + τ_ff

MIT 协议

  • 运动控制模式 (Motion Control Mode): 动态参数模式(默认)
  • 速度模式 (Velocity Mode): 目标速度 + 电流限制
  • 位置模式 (Position Mode): 目标位置 + 最大速度限制

故障检测

支持完整的故障反馈解析:

  • 堵转过载故障 / 三相过流 / 欠压 / 过压
  • 电机过热 / 编码器未校准 / 磁性编码故障
  • 驱动芯片故障 / 硬件识别故障 / 位置初始化错误
  • A/B/C 相采样过流 / 电机过温警告

高级功能

  • 电机 CAN ID 在线配置
  • 电机参数读写(运行模式、电流限制、速度限制等)
  • 机械零位设定
  • 电机数据保存(掉电保存)
  • 波特率动态修改
  • 主动上报模式(默认 10ms 间隔)
  • 协议切换(私有/CANopen/MIT)

快速开始

1. 克隆仓库

git clone https://github.com/your-username/RS05.git
cd RS05

2. 使用 CLion 打开

  1. 使用 CLion 打开项目根目录
  2. CLion 会自动识别 CMake 工程
  3. 选择构建配置(Debug/Release)
  4. 编译下载

3. 基本使用

#include "dvc_RS05.h"
#include "drv_can.h"

// 1. 创建电机管理对象,指定电机 CAN ID 和主机 CAN ID
Motor_Manage_Object motor1 = {
    .MOTOR_CAN_ID = 0x02,   // 电机 CAN ID
    .HOST_CAN_ID  = 0xFD    // 主机 CAN ID
};

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_CAN1_Init();

    // 2. 配置 CAN 滤波器 (扩展帧 + 接收全部报文)
    CAN_Filter_Config(&hcan1,
        CAN_FILTER(0) | CAN_FIFO_0 | CAN_EXTID | CAN_DATA_TYPE,
        0x0, 0x0);

    // 3. 初始化 CAN 总线并注册回调
    CAN_Init(&hcan1, CAN_RS05_Call_Back);

    // 4. 注册电机到 CAN 总线
    Motor_Register(&CAN1_Manage_Object, &motor1);

    // 5. 使能电机
    RS05_Motor_Enable(&hcan1, &motor1);

    while (1)
    {
        // 6. 运控模式: 力矩=0.5Nm, 角度=0rad, 角速度=20rad/s, Kp=0.0, Kd=1.0
        Operation_Control_Mode(&hcan1, 0.5f, 0.0f, 20.0f, 0.0f, 1.0f, &motor1);
        HAL_Delay(1);  // 1ms 控制周期
    }
}

切换控制模式示例

// 切换到速度模式
Set_Mode(&hcan1, 2, &motor1);                // runmode: 0-运控 1-PP 2-速度 3-电流 5-CSP
RS05_Motor_Enable(&hcan1, &motor1);          // 使能
Set_limit_cur(&hcan1, 16.0f, &motor1);       // 电流限制 16A
Set_spd_ref(&hcan1, 10.0f, &motor1);         // 目标速度 10rad/s

// 切换到位置模式 (CSP)
Set_Mode(&hcan1, 5, &motor1);
RS05_Motor_Enable(&hcan1, &motor1);
Set_limit_spd(&hcan1, 20.0f, &motor1);       // 速度限制 20rad/s
Set_loc_ref(&hcan1, 3.14f, &motor1);         // 目标位置 π rad

// 停止电机
RS05_Motor_Stop(&hcan1, &motor1);

MIT 协议使用

#include "dvc_MIT_RS05.h"

// 初始化时使用 MIT 协议回调
CAN_Init(&hcan1, MIT_CAN_RS05_Call_Back);

// MIT 运动控制模式
MIT_RS05_Enable(&hcan1, &motor1);
MIT_Motion_Control_Mode(&hcan1, 0.0f, 5.0f, 10.0f, 1.0f, 0.5f, &motor1);
//                            角度   角速度  Kp    Kd    力矩

// MIT 位置模式
MIT_Set_Mode(&hcan1, 1, &motor1);             // 1=位置模式
MIT_RS05_Enable(&hcan1, &motor1);
MIT_Position_Mode(&hcan1, 1.57f, 10.0f, &motor1); // 目标位置, 最大速度

// MIT 速度模式
MIT_Set_Mode(&hcan1, 2, &motor1);             // 2=速度模式
MIT_RS05_Enable(&hcan1, &motor1);
MIT_Speed_Mode(&hcan1, 15.0f, 5.0f, &motor1);     // 目标速度, 电流限制

多电机支持

修改 drv_can.h 中的宏定义增加电机数量:

#define MAX_MOTOR_COUNT 4  // 支持最多 4 个电机

然后注册多个电机:

Motor_Manage_Object RS05_1 = {.MOTOR_CAN_ID = 0x01, .HOST_CAN_ID = 0xFD};
Motor_Manage_Object RS05_2 = {.MOTOR_CAN_ID = 0x02, .HOST_CAN_ID = 0xFD};
Motor_Manage_Object RS05_3 = {.MOTOR_CAN_ID = 0x03, .HOST_CAN_ID = 0xFD};
Motor_Manage_Object RS05_4 = {.MOTOR_CAN_ID = 0x04, .HOST_CAN_ID = 0xFD};

Motor_Register(&CAN1_Manage_Object, &RS05_1);
Motor_Register(&CAN1_Manage_Object, &RS05_2);
Motor_Register(&CAN1_Manage_Object, &RS05_3);
Motor_Register(&CAN1_Manage_Object, &RS05_4);

API 文档

CAN 总线管理 (drv_can.h)

函数 说明
CAN_Init(hcan, CallBack) 初始化 CAN 总线,启动接收中断
Motor_Register(CAN_Mgr, Motor) 将电机注册到指定 CAN 总线

私有协议 (dvc_RS05.h)

函数 说明
RS05_Motor_Enable(hcan, motor) 电机使能运行
RS05_Motor_Stop(hcan, motor) 电机停止运行
Operation_Control_Mode(hcan, torque, angle, omega, kp, kd, motor) 运控模式指令
Set_Mode(hcan, runmode, motor) 设置运行模式
Set_iq_ref(hcan, iq_ref, motor) 设置 Iq 电流指令
Set_spd_ref(hcan, spd_ref, motor) 设置目标速度
Set_loc_ref(hcan, loc_ref, motor) 设置目标位置
Set_limit_cur(hcan, cur, motor) 设置电流限制
Set_limit_spd(hcan, spd, motor) 设置速度限制
CAN_Filter_Config(hcan, ..., id, mask) 配置扩展帧 CAN 滤波器

MIT 协议 (dvc_MIT_RS05.h)

函数 说明
MIT_RS05_Enable(hcan, motor) MIT 模式电机使能
MIT_Stop(hcan, motor) MIT 模式电机停止
MIT_Motion_Control_Mode(hcan, angle, omega, kp, kd, torque, motor) MIT 运动控制
MIT_Set_Mode(hcan, mode, motor) MIT 模式切换
MIT_Speed_Mode(hcan, speed, limit, motor) MIT 速度模式
MIT_Position_Mode(hcan, pos, speed, motor) MIT 位置模式

电机反馈数据结构

通过 Motor_Manage_Object 结构体可以读取电机实时反馈:

// 电机反馈数据
motor1.Current_Angle;        // 当前角度 (uint16, 映射到 -4π ~ +4π)
motor1.Current_Omega;        // 当前角速度 (uint16, 映射到 -50 ~ +50 rad/s)
motor1.Current_Torque;       // 当前转矩 (uint16, 映射到 -5.5 ~ +5.5 Nm)
motor1.Current_Temperature;  // 当前温度 (°C)

// 故障状态
motor1.Fault_Information;           // 故障总标志
motor1.Overtemperature;             // 过热故障
motor1.Three_Phase_Overcurrent_Fault; // 三相过流
motor1.Undervoltage_Fault;          // 欠压故障
motor1.Mode_Status;                 // 模式状态 (0-复位 1-校准 2-运行)

工具函数 (drv_math.h)

函数 说明
float_to_uint16(x, x_min, x_max, bits) 浮点数 → 16 位定点数映射
float_to_uint32(x, x_min, x_max) 浮点数 → 32 位定点数映射

构建

# 使用 CMake 命令行构建
mkdir build && cd build
cmake ..
make -j4

本项目使用 STM32CubeMX 6.16.0 生成,HAL 固件包版本 STM32Cube FW_F4 V1.28.3。

如需重新生成 CubeMX 代码,请使用 .ioc 文件,工具链选择 CMake。

许可证

MIT License

Copyright (c) 2026

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

相关资源

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages