描述
<div class="document">
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">* 1、项目功能介绍</h3>
<hr class="horizontal-splitline normal-bold-2">
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="color: #95a5a6; font-size: 14px;">一直想写一套FOC控制电机,就借这个比赛的机会做了一版。虽然网上有很多FOC的开源项目了,但自己还是想学完原理自己写一个FOC驱动库.电机用的KV值小的云台电机,毕竟重点在学习FOC算法上。完成了位置,速度,电流闭环。同时完整推导了,svpwm发波。但工程并不完善,因为时间仓促,我们lcd那块有问题没有修,电机第二路也没有来得及写。然后就是算法上,电机运行起来和我想的有一些出入,这属于是理论欠缺了,后面再好好学一下自控再回头来解决。</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">*2、项目属性</h3>
<hr class="horizontal-splitline normal-bold-2">
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="color: #95a5a6; font-size: 14px;">项目为首次公开。硬件参考了samplefoc。</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">* 3、开源协议</h3>
<hr class="horizontal-splitline normal-bold-2">
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="color: #95a5a6; font-size: 14px;">该项目软硬件完全开源。</span></p>
<p style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><strong><span style="color: #0093e6;">请在竞赛阶段填写 ↓</span></strong></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">*4、硬件部分</h3>
<hr class="horizontal-splitline normal-bold-2">
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">注意:这个lcd屏幕虽然画了,当调的时候没用。因为焊上屏幕,给板子一供电就过流了。这也是这个工程不完整的地方(真的是准备时间不足啊)。</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">驱动板</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/vOhWIi19bbg1AN0QzplssYu8fwLucg5dyqG1q2l6.png" alt=""></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">用这块芯片做主控主要是因为它是QFN封装比较好看,而且比起同性能的来说更加便宜。</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">无线串口用的是逐飞的串口。打过智能车的友友应该都知道。</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">编码器板</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/W9RNrBcZjIdUobK0qdGLROPrA11uBs5RkjYlHACV.png" alt="" width="501" height="338"></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;">用磁编码器检测电机转子位置需要在电机底部加径向磁铁,然后我们是做了3d打印座子(见附件)的,用来固定电机。</p>
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">*5、软件部分</h3>
<hr class="horizontal-splitline normal-bold-2">
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 16px;">核心其实是合成360°任意位置的磁场去控制无刷电机.</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 16px;"><img src="//image.lceda.cn/pullimage/FLU2tHSmyb8RClxKlvTo9RxCGjK6JXyKNcnMIM7F.png" alt="" width="564" height="406"></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">最好的情况就是上面的,合成磁场与转子垂直,这样就是磁场都用来拖动电机了.假如没有垂直,必然在转子上有分量,这个就会使得电机发热.所以说,我们就是需要不停去检测转子的实时位置,然后后生成超前90度的磁场去拖动电机.改变磁场大小就能控制电机的速度了.</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 16px;">那么既然要合成向量就不得不说两个变换,park变换和clark变换.这部分可以参考我之前写的一篇博客.</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 16px;"><a href="https://blog.csdn.net/m0_60917611/article/details/133090282?spm=1001.2014.3001.5501" target="_blank">[电赛电力电子方向]基于DQ坐标系的锁相环-CSDN博客</a></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 24px;">spwm开环</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">首先拿到硬件最重要的就是先给他开环转起来。验证硬件是否有问题。我开始是用三相SPWM波去强拖的,具体用法就是开定时器,在定时器中断里修改pwm的crr来改变占空比。ccr按正弦变化,这个正弦波是每次在中断里更新的,就是你设定角频率欧米伽,中断里每次计算sin(wt)。t是逐渐累加上去的,就是每次进中断t+=Ts(定时器中期).</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/E22wI8awf9Bcgt6zFRcvKuMwnESRB2hqNDdPxcsY.png" alt="" width="875" height="619"></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 24px;">svpwm开环</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 16px;">当然我们FOC肯定是用svpwm波来控制的了.它其实但从生成上来说就是你生成360°任意一个角度,大小的向量.这部分我参考了下面up的视频.</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 16px;"><a href="https://www.bilibili.com/video/BV1ow411U7Za/?spm_id_from=333.1007.top_right_bar_window_custom_collection.content.click&vd_source=482b3d3f9fc536dcbe27733a0c4cd1a4" target="_blank">Simple_FOC教程 一、原理部分_哔哩哔哩_bilibili</a></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;">有了svpwm生成,我们就能开环运行了.还是和上面一样,在定时器中断里去更新时间t,然后wt就是现在的角度了.我们改变w就能调速.至于力矩,也就是期望的q轴电压直接程序去设定一个.改变它可以改变力矩.调的时候会发现,修改速度和力矩可以让电机较好的运行,而且比spwm开环要顺滑很多.</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 24px;">速度闭环</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;">这里测速我们需要磁编码器去读角度,同时svpwm也需要一个角度.这里说一下电机极对数判断,以及计算磁编码器偏置.我们在程序中设定合成向量的角度为0.然后运行电机,电机就会转到0位置,然后旋转电机,它会在几个位置停留.停留几次就是几极对.然后选择一个0位置读出编码器的值就是偏置了。</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;">然后速度闭环的话是pi控制器。pi控制器误差为期望速度与当前速度作差,输出q轴电压,然后送去svpwm发波。</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 24px;">位置闭环</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;">位置闭环是p控制器,运行就是位置误差过p控制器输出速度环参考量,然后就和速度闭环一样了。我调的时候开始是位置环给了i,结果电机会来回震荡,这属于是自控的知识了。后面会深入学习一下。</p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 24px;">电流闭环</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;">首先我们要知道,三相电流正弦波是很难去直接控制的。所以我们就使用那两个变换去把三相正弦转换到dq坐标系下。理论上,d轴为0,q轴有电流,那么我们产生的这个向量就是只控制电机旋转的,而如果d不为0那么久有一部分能量去让电机发热了。这也是闭电流环的一个重要原因。</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;">电流环是pi控制器。就是把三相电流做变换得到dq分量,然后dq分别做误差过pi控制器的到q轴和d轴的电压参考值。(注意,这里pi控制器输出的是电压)。</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 24px;">代码结构介绍</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;"><img src="//image.lceda.cn/pullimage/Bp7eVXEclHX4fsP65NivxL9lnPqKoOSNvgLAn4f6.png" alt="" width="456" height="687"></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;">除了cubemx生成的几个文件夹外,我还加了driver和user文件夹。就是一个放驱动,一个放应用文件。</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;">open_loop里放的是spwm开环的相关函数。isr_callback里放中断回调函数.my_main里是系统初始化,与系统运行函数.就是下面在主函数调用的两个.</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 16px;"><img src="//image.lceda.cn/pullimage/ZP7C5iWtzhCCuUvxKZiAGrBjMYEV4fLKMVNnfUN5.png" alt="" width="637" height="485"></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"><span style="font-size: 24px;">串口通信规范</span></p>
<ul class="ul-list">
<li class="md-list-item md-focus-container">
<p class="md-end-block md-p md-focus"><span class="md-plain">帧头:“$” (u8)</span></p>
</li>
<li class="md-list-item md-focus-container">
<p class="md-end-block md-p md-focus"><span class="md-plain">指令位(发送方地址):2个u8 0-99个指令</span></p>
</li>
<li class="md-list-item">
<p class="md-end-block md-p"><span class="md-plain">数据:任意长度</span></p>
</li>
</ul>
<p class="md-end-block md-p"><span class="md-plain md-expand">帧尾:"\n"(u8)</span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt pap-left-indent-1.6em" style="line-height: 1.8;"> 如:</p>
<p>角度设定$04360\n 04代表这个指令是控制角度的,360就是设定电机转到360度。</p>
<p><span style="font-size: 24px;">上位机</span></p>
<p><span style="font-size: 16px;">我用的VOFA+,软件大家去官网下就行.配置json在下面的附件里.</span></p>
<p><span style="font-size: 16px;"><img src="//image.lceda.cn/pullimage/TIguvu8JsP1UUjnzY08kGePtTyoZksaW5Ddm2c5f.png" alt="" width="688" height="76"></span></p>
<p><img src="//image.lceda.cn/pullimage/NnPXUR4xVF0PDrIvXIX3d7dwBFF6lgnTAhxrFaYo.png" alt="" width="1903" height="840"></p>
<p><img src="//image.lceda.cn/pullimage/MkaFsDIc3QxJccY4JE9a4p7FsXHckj9aLOjCITKh.png" alt="" width="502" height="262"></p>
<p>添加table.json</p>
<p><img src="//image.lceda.cn/pullimage/qBzJpMRWPgROBPRfT5gIUHkuSDZOaR1HSV0IJVUr.png" alt="" width="615" height="996"></p>
<p>在命令区域鼠标右键,添加命令组.起名FOC</p>
<p><img src="//image.lceda.cn/pullimage/9zxI0z19MucKI0YZybekbOU4mWpIUsRETFshvMj1.png" alt="" width="462" height="574"></p>
<p>在命令组里右键载入另一个json.</p>
<p><img src="//image.lceda.cn/pullimage/Za1aH2cKRhdLHWmQoZlTaTZlX8jzROJvByBisHbS.png" alt="" width="1021" height="999"></p>
<p>在命令控件右键绑定命令组.就按如下绑定.</p>
<p> </p>
<p><span style="font-size: 16px;"><img src="//image.lceda.cn/pullimage/IG8cYU4cVxsFNcFiAxacRgCqRns5GX7DuBMjMEVq.png" alt="" width="1712" height="927"></span></p>
<p> </p>
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">*6、BOM清单</h3>
<hr class="horizontal-splitline normal-bold-2">
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="color: #95a5a6;"><span style="font-size: 14px;">编码器</span></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="color: #95a5a6;"><span style="font-size: 14px;"><img src="//image.lceda.cn/pullimage/IAz5X6g6nNmEF8rjNJTOWWxw892FSforSWzTjMCy.png" alt="" width="1565" height="181">驱动</span></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/HR8GRHIIfEB3Pw2t5fGlrE5ktNLxEUa9NVsWGVTi.png" alt="" width="1528" height="517"></p>
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">*7、大赛LOGO验证</h3>
<hr class="horizontal-splitline normal-bold-2">
<p style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><img src="//image.lceda.cn/pullimage/uPE8uze8wkKsLC9untoPxnD0P95chhDvXlArln0D.jpeg" alt="" width="2000" height="2664"></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<h3 class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;">* 8、演示您的项目并录制成视频上传</h3>
<hr class="horizontal-splitline normal-bold-2">
<p style="line-height: 1.8;"><a href="https://www.bilibili.com/video/BV1B34y1M7dG/?spm_id_from=333.999.0.0&vd_source=482b3d3f9fc536dcbe27733a0c4cd1a4" target="_blank">【第八届立创电赛】基于stm32的简易foc_哔哩哔哩_bilibili</a></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"><span style="font-size: 14px;">更多详情:<a href="/posts/15a52db9fd7d40c492eb505280278e45" target="_blank">https://diy.szlcsc.com/posts/15a52db9fd7d40c492eb505280278e45</a></span></p>
<p class="paragraph text-align-type-left pap-line-1.3 pap-line-rule-auto pap-spacing-before-3pt pap-spacing-after-3pt" style="line-height: 1.8;"> </p>
</div>
评论(12)