加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
LGPL-3.0
环境:
使用HAL库编程,
Documents:文档
FOC-407-Demo:自制电路板V1.0.0 双路无刷驱动代码
ODrive:ODrive硬件平台,单路驱动代码

芯片资源使用:
PWM:
使用中央对其模式2,先向上计数再向下计数,只在向上计数时产生溢出中断。
使用PWM模式1,向上计数 CNT<CCR为有效电平,有效电平为高。
ODrive:使能通道4的比较中断,通道4的CCR为通道1 2 3中CCR最大值+1us。1us是为了让AD采样更稳定
FOC-407-Demo:使用溢出中断,再中断执行函数里选择上溢中断
其中FOC-407-Demo代码中使用主从定时器来将两个电机的电流环计算时间分开,互不干扰。

ABZ编码器:
采用ABZ正交增量编码器
每次上电需要做电气角度校准
利用Z轴每圈做一次清零

绝对式编码器:
只在第一次上电和拆卸电机的时候做校准
芯片选择TLE5012B1000

电流采样:
电流采样使用单次扫描采样,DMA+中断
在定时器1通道4中断中触发ADC采样


控制流程:
定时器中断,进行一次ADC采样.
ADC采样完成触发DMA中断,在DMA中完成FOC电流环。

FOC控制流程:(FOC.c)
1.获取BC两相电流,求出A相电流
2.获取当前电气角度。
3.通过Clarke变换和Park变换求出实际电流 IQ和ID
4.通过与目标IQ ID对比和PID计算,得出要输出的UQ UD
5.将UQ UD做Park反变化得出Uα和Uβ
6.将U阿尔法和Uβ送入SVPWM生成模块。

SVPWM控制流程:(Svpwm.c.c)
1.获取Uα和Uβ
2.通过U阿尔法和Uβ计算当前所在扇区
4.使用7段式PWM计算每个矢量的作用时常
5.通过矢量作用时长计算出定时器的高电平时间
6.通过定时器每个高电平时间计算出每个通道的CCR值
7.挑选出CCR最大值送给通道4,准备下一次定时器中断

定时器比较中断:
打开ADC进行一次AD采样

DMA中断:
读取AD数据进行FOC控制

编码器Z轴中断:
校准角度值


文件介绍:
所有文件均放置在User目录下
APP:应用程序总入口,实现初始化流程管理,循环执行管理。
Function:放置功能程序,目前没有实现,如放置T型加减速。
MCUDriver :放置芯片外设代码,如SPI,GPIO,TIM,ADC
Framework:放置代码库,将驱动代码和硬件平台剥离出来。
PeripheralsDriver:放置驱动程序,将硬件平台代码和代码库结合起来,实现具体功能,即(MCUDriver + Framework)
RTT :使用Segger RTT打印调试,也可以稍作修改改为串口打印。

已知BUG
1.双路FOC代码中(FOC-407-Demo)CUBE生成的代码初始化顺序会导致ADC2无法进入DMA中断,因此外设初始化顺序要修改成我代码中的那样。
2.无法通过SPI与DRV8301通讯,看了数据手册也没有调试通过,哪位老哥有经验望分享。
3.没有做刹车处理

声明:
1.受硬件平台影响,代码可能不能直接运行,但可以参考。
2.先调试SVPWM再调试电流采样,再闭环,SVPWM即可实现电机旋转。
3.闭环先调电流环再调速度环
4.电流环先调试ID再调试IQ
6.有疑问的地方欢迎骚扰,有错误的地方欢迎批评。

联系:
QQ:965552797@qq.com


代码编写风格:
我们实现一个功能其实是分为三个步骤
1.配置MCU引脚
2.配置传感器逻辑功能,即让传感器运行起来,或是通讯协议,或是电机控制,或是时间控制。
3.根据传感器的功能做一些小的逻辑应用,或是LED闪烁,或是电机转速控制。
其实我们发现1和3是受硬件平台和我们要实现的功能影响,需要不断修改,但步骤2是不变的,针对一个传感器来说无论你使用什么硬件平台步骤2是不需要变化的,举个例子来说TLE5012B编码器的SPi通讯逻辑是不变的。
因此我们将步骤2抽象出来,针对TLE5012B我们用结构体的方式表示这类传感器,我们假设其有SPI传输函数,SPI读取函数,SPI_CS引脚控制函数,微秒延时函数,有了这些函数之后我们就能使用这个传感器了。
但是现在这些函数都是虚拟的,我们使用这些假函数先把传感器的逻辑写出来,然后我们在去MCU那里把真正的SPI通讯实现了,然后把这些真正的函数地址传给TLE5012B结构体即可。


在单路FOC代码中会给人一种感觉,这样编写比较麻烦,累赘。但是在双路FOC中这样编写的优点就体现出来了。


LEDControl举例说明
我们使用板子习惯会先点亮一个LED,但我们会发现每次开发板点亮一个LED都要重新编写函数,如果在加一些闪烁效果就更恶心,为了不影响主循环的实时性我们甚至要开一个定时器中断,
真是苦不堪言,在本次代码中我们声明一个LED结构体:
struct SLEDControl_Struct {
    uint8_t state;//LED运行状态 0:LED常灭 1:LED常亮 2:闪烁
    uint8_t onoff;//当前LED状态
    float cycle;//闪烁周期(单位ms)
    uint8_t onLeave;//点亮电平
    uint32_t startTime;
    void(*SetLEDLeave)(uint8_t leave);//设置LED引脚电平函数
};

针对LED,我们要知道点亮电平,引脚电平控制,因此我们便在结构体中声明这两个函数,利用虚拟函数完成逻辑功能如:
void SetLEDON(PLEDControl_Struct gLED)
{
    gLED->state = LEDState_ON;
    gLED->SetLEDLeave(gLED->onLeave);
}

然后我们再去实现这个引脚控制函数
void SetLedLeave(uint8_t leave)
{
	HAL_GPIO_WritePin(SYS_LED_GPIO_Port, SYS_LED_Pin, leave);
}

最后通过接口把这个SetLedLeave函数地址传给结构体 
LED_EXPORT(gSysLed,1,SetLedLeave);

#define LED_EXPORT(x,xOnLeave,xSetLEDLeave)   	\
LEDControl_Struct x = {              			\
    .state = LEDState_OFF,                		\
    .onoff = 0,                      			\
    .cycle = 0.0,                    			\
    .onLeave = xOnLeave,             			\
	.startTime = 0,                  			\
    .SetLEDLeave = xSetLEDLeave,     			\
};
GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

简介

暂无描述 展开 收起
LGPL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化