PID和三环控制-以大疆M3508、M2006为例

三环控制和PID在电机的应用

演示视频在文章末尾

前言:

最近用到了大疆的直流无刷(BLDC)减速电机M3508和M2006。做RoboMaster比赛的同学应该对它们很熟悉,这两款电机质量都不错,配套电调C620、C610功能强大,应用场景广泛。当然价格不算低。

我作为第一次接触电机控制的新手,在搜索PID和三环控制资料的时候常常得到的是一些理论论述,而且千篇一律。虽然PID是较为简单的控制算法,新人上手难度还是有些大。那么我就彻彻底底地回顾一下搭建最简单的电机控制算法的流程。提供一个新的理解视角。

本文依托大疆官方M2006电机例程,其与M3508电机配套例程在CAN通信驱动、PID等核心部分基本相似甚至可以直接替换(M2006例程里的一些文件注释写的是3508)。最大的不同就是M3508使用了FreeRTOS实时系统,这一点并不方便我们的学习。而2006就是裸机前后台,非常简单明晰。

例程均是基于Keil 、STM32F429(我使用407)、HAL库

例程下载:M2006例程M3508例程

2021/7/23:
第一次接触这两款电机或者CAN通信的朋友可以看第二篇文章:
大疆M3508、M2006必备CAN总线知识与配置方法
2021/10/12:
需要F1版本的可以看下第三篇文章,记录了移植遇到的玄学问题
M3508电机控制程序的F1移植过程
2022/2/2:
ESP32控制大疆电机,CAN总线使用与蓝牙BLE
ESP32:蓝牙BLE控制M3508

0x00 PID到底怎么部署到电机上?

我们都知道PID应用广泛,效果良好,在网上能搜到大量生动形象的文章。这些文章讲述了比例、积分、微分有什么用处和缺陷,什么是死区,并且配有动态曲线图来展示调参效果。但是举的例子往往是这样:一个水缸要固定水位,加水量是输出,反馈是水位之差。我当时看过好多类似文章之后还是不明白怎么把PID应用于更复杂的系统,比如电机控制。

答案是 拆分 复杂系统,运用多次控制算法层层递进

比如在电机上,常用的就是***三环控制***:电流环、速度环、位置环。

三环层层递进并且具有因果关系

这个关系非常好理解,通过电磁学知识我们知道电机能转是因为有电流产生磁场。所以,电流是电机转动的根本原因,也就是:

#mermaid-svg-OpVI4GfOK4luFGvc .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-OpVI4GfOK4luFGvc .label text{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .node rect,#mermaid-svg-OpVI4GfOK4luFGvc .node circle,#mermaid-svg-OpVI4GfOK4luFGvc .node ellipse,#mermaid-svg-OpVI4GfOK4luFGvc .node polygon,#mermaid-svg-OpVI4GfOK4luFGvc .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-OpVI4GfOK4luFGvc .node .label{text-align:center;fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .node.clickable{cursor:pointer}#mermaid-svg-OpVI4GfOK4luFGvc .arrowheadPath{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-OpVI4GfOK4luFGvc .flowchart-link{stroke:#333;fill:none}#mermaid-svg-OpVI4GfOK4luFGvc .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-OpVI4GfOK4luFGvc .edgeLabel rect{opacity:0.9}#mermaid-svg-OpVI4GfOK4luFGvc .edgeLabel span{color:#333}#mermaid-svg-OpVI4GfOK4luFGvc .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-OpVI4GfOK4luFGvc .cluster text{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-OpVI4GfOK4luFGvc .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-OpVI4GfOK4luFGvc text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-OpVI4GfOK4luFGvc .actor-line{stroke:grey}#mermaid-svg-OpVI4GfOK4luFGvc .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-OpVI4GfOK4luFGvc .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-OpVI4GfOK4luFGvc #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-OpVI4GfOK4luFGvc .sequenceNumber{fill:#fff}#mermaid-svg-OpVI4GfOK4luFGvc #sequencenumber{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc #crosshead path{fill:#333;stroke:#333}#mermaid-svg-OpVI4GfOK4luFGvc .messageText{fill:#333;stroke:#333}#mermaid-svg-OpVI4GfOK4luFGvc .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-OpVI4GfOK4luFGvc .labelText,#mermaid-svg-OpVI4GfOK4luFGvc .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-OpVI4GfOK4luFGvc .loopText,#mermaid-svg-OpVI4GfOK4luFGvc .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-OpVI4GfOK4luFGvc .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-OpVI4GfOK4luFGvc .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-OpVI4GfOK4luFGvc .noteText,#mermaid-svg-OpVI4GfOK4luFGvc .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-OpVI4GfOK4luFGvc .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-OpVI4GfOK4luFGvc .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-OpVI4GfOK4luFGvc .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-OpVI4GfOK4luFGvc .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .section{stroke:none;opacity:0.2}#mermaid-svg-OpVI4GfOK4luFGvc .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-OpVI4GfOK4luFGvc .section2{fill:#fff400}#mermaid-svg-OpVI4GfOK4luFGvc .section1,#mermaid-svg-OpVI4GfOK4luFGvc .section3{fill:#fff;opacity:0.2}#mermaid-svg-OpVI4GfOK4luFGvc .sectionTitle0{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .sectionTitle1{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .sectionTitle2{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .sectionTitle3{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-OpVI4GfOK4luFGvc .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .grid path{stroke-width:0}#mermaid-svg-OpVI4GfOK4luFGvc .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-OpVI4GfOK4luFGvc .task{stroke-width:2}#mermaid-svg-OpVI4GfOK4luFGvc .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .taskText:not([font-size]){font-size:11px}#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-OpVI4GfOK4luFGvc .task.clickable{cursor:pointer}#mermaid-svg-OpVI4GfOK4luFGvc .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-OpVI4GfOK4luFGvc .taskText0,#mermaid-svg-OpVI4GfOK4luFGvc .taskText1,#mermaid-svg-OpVI4GfOK4luFGvc .taskText2,#mermaid-svg-OpVI4GfOK4luFGvc .taskText3{fill:#fff}#mermaid-svg-OpVI4GfOK4luFGvc .task0,#mermaid-svg-OpVI4GfOK4luFGvc .task1,#mermaid-svg-OpVI4GfOK4luFGvc .task2,#mermaid-svg-OpVI4GfOK4luFGvc .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutside0,#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutside2{fill:#000}#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutside1,#mermaid-svg-OpVI4GfOK4luFGvc .taskTextOutside3{fill:#000}#mermaid-svg-OpVI4GfOK4luFGvc .active0,#mermaid-svg-OpVI4GfOK4luFGvc .active1,#mermaid-svg-OpVI4GfOK4luFGvc .active2,#mermaid-svg-OpVI4GfOK4luFGvc .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-OpVI4GfOK4luFGvc .activeText0,#mermaid-svg-OpVI4GfOK4luFGvc .activeText1,#mermaid-svg-OpVI4GfOK4luFGvc .activeText2,#mermaid-svg-OpVI4GfOK4luFGvc .activeText3{fill:#000 !important}#mermaid-svg-OpVI4GfOK4luFGvc .done0,#mermaid-svg-OpVI4GfOK4luFGvc .done1,#mermaid-svg-OpVI4GfOK4luFGvc .done2,#mermaid-svg-OpVI4GfOK4luFGvc .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-OpVI4GfOK4luFGvc .doneText0,#mermaid-svg-OpVI4GfOK4luFGvc .doneText1,#mermaid-svg-OpVI4GfOK4luFGvc .doneText2,#mermaid-svg-OpVI4GfOK4luFGvc .doneText3{fill:#000 !important}#mermaid-svg-OpVI4GfOK4luFGvc .crit0,#mermaid-svg-OpVI4GfOK4luFGvc .crit1,#mermaid-svg-OpVI4GfOK4luFGvc .crit2,#mermaid-svg-OpVI4GfOK4luFGvc .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-OpVI4GfOK4luFGvc .activeCrit0,#mermaid-svg-OpVI4GfOK4luFGvc .activeCrit1,#mermaid-svg-OpVI4GfOK4luFGvc .activeCrit2,#mermaid-svg-OpVI4GfOK4luFGvc .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-OpVI4GfOK4luFGvc .doneCrit0,#mermaid-svg-OpVI4GfOK4luFGvc .doneCrit1,#mermaid-svg-OpVI4GfOK4luFGvc .doneCrit2,#mermaid-svg-OpVI4GfOK4luFGvc .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-OpVI4GfOK4luFGvc .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-OpVI4GfOK4luFGvc .milestoneText{font-style:italic}#mermaid-svg-OpVI4GfOK4luFGvc .doneCritText0,#mermaid-svg-OpVI4GfOK4luFGvc .doneCritText1,#mermaid-svg-OpVI4GfOK4luFGvc .doneCritText2,#mermaid-svg-OpVI4GfOK4luFGvc .doneCritText3{fill:#000 !important}#mermaid-svg-OpVI4GfOK4luFGvc .activeCritText0,#mermaid-svg-OpVI4GfOK4luFGvc .activeCritText1,#mermaid-svg-OpVI4GfOK4luFGvc .activeCritText2,#mermaid-svg-OpVI4GfOK4luFGvc .activeCritText3{fill:#000 !important}#mermaid-svg-OpVI4GfOK4luFGvc .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-OpVI4GfOK4luFGvc g.classGroup text .title{font-weight:bolder}#mermaid-svg-OpVI4GfOK4luFGvc g.clickable{cursor:pointer}#mermaid-svg-OpVI4GfOK4luFGvc g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-OpVI4GfOK4luFGvc g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-OpVI4GfOK4luFGvc .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-OpVI4GfOK4luFGvc .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-OpVI4GfOK4luFGvc .dashed-line{stroke-dasharray:3}#mermaid-svg-OpVI4GfOK4luFGvc #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc .commit-id,#mermaid-svg-OpVI4GfOK4luFGvc .commit-msg,#mermaid-svg-OpVI4GfOK4luFGvc .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-OpVI4GfOK4luFGvc g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-OpVI4GfOK4luFGvc g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-OpVI4GfOK4luFGvc g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-OpVI4GfOK4luFGvc .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-OpVI4GfOK4luFGvc .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-OpVI4GfOK4luFGvc .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-OpVI4GfOK4luFGvc .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-OpVI4GfOK4luFGvc .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-OpVI4GfOK4luFGvc .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-OpVI4GfOK4luFGvc .edgeLabel text{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-OpVI4GfOK4luFGvc .node circle.state-start{fill:black;stroke:black}#mermaid-svg-OpVI4GfOK4luFGvc .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-OpVI4GfOK4luFGvc #statediagram-barbEnd{fill:#9370db}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-state .divider{stroke:#9370db}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-OpVI4GfOK4luFGvc .note-edge{stroke-dasharray:5}#mermaid-svg-OpVI4GfOK4luFGvc .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-OpVI4GfOK4luFGvc .error-icon{fill:#522}#mermaid-svg-OpVI4GfOK4luFGvc .error-text{fill:#522;stroke:#522}#mermaid-svg-OpVI4GfOK4luFGvc .edge-thickness-normal{stroke-width:2px}#mermaid-svg-OpVI4GfOK4luFGvc .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-OpVI4GfOK4luFGvc .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-OpVI4GfOK4luFGvc .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-OpVI4GfOK4luFGvc .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-OpVI4GfOK4luFGvc .marker{fill:#333}#mermaid-svg-OpVI4GfOK4luFGvc .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-OpVI4GfOK4luFGvc {
color: rgba(0, 0, 0, 0.75);
font: ;
}

电流
运动

中学物理也讲过,(角)速度是描述运动的物理量,包括方向、大小。如果希望控制速度,比如定速转动,那就也要控制电流。再进一步,运动能改变物体位置,如果想确定达到某个位置那就控制速度。然而控制速度归根结底是控制电流。得出:

#mermaid-svg-QUbgQxIhLMi9XSEr .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .label text{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .node rect,#mermaid-svg-QUbgQxIhLMi9XSEr .node circle,#mermaid-svg-QUbgQxIhLMi9XSEr .node ellipse,#mermaid-svg-QUbgQxIhLMi9XSEr .node polygon,#mermaid-svg-QUbgQxIhLMi9XSEr .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-QUbgQxIhLMi9XSEr .node .label{text-align:center;fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .node.clickable{cursor:pointer}#mermaid-svg-QUbgQxIhLMi9XSEr .arrowheadPath{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-QUbgQxIhLMi9XSEr .flowchart-link{stroke:#333;fill:none}#mermaid-svg-QUbgQxIhLMi9XSEr .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-QUbgQxIhLMi9XSEr .edgeLabel rect{opacity:0.9}#mermaid-svg-QUbgQxIhLMi9XSEr .edgeLabel span{color:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-QUbgQxIhLMi9XSEr .cluster text{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-QUbgQxIhLMi9XSEr .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-QUbgQxIhLMi9XSEr text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-QUbgQxIhLMi9XSEr .actor-line{stroke:grey}#mermaid-svg-QUbgQxIhLMi9XSEr .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-QUbgQxIhLMi9XSEr #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .sequenceNumber{fill:#fff}#mermaid-svg-QUbgQxIhLMi9XSEr #sequencenumber{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr #crosshead path{fill:#333;stroke:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .messageText{fill:#333;stroke:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-QUbgQxIhLMi9XSEr .labelText,#mermaid-svg-QUbgQxIhLMi9XSEr .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-QUbgQxIhLMi9XSEr .loopText,#mermaid-svg-QUbgQxIhLMi9XSEr .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-QUbgQxIhLMi9XSEr .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-QUbgQxIhLMi9XSEr .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-QUbgQxIhLMi9XSEr .noteText,#mermaid-svg-QUbgQxIhLMi9XSEr .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-QUbgQxIhLMi9XSEr .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-QUbgQxIhLMi9XSEr .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-QUbgQxIhLMi9XSEr .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-QUbgQxIhLMi9XSEr .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .section{stroke:none;opacity:0.2}#mermaid-svg-QUbgQxIhLMi9XSEr .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-QUbgQxIhLMi9XSEr .section2{fill:#fff400}#mermaid-svg-QUbgQxIhLMi9XSEr .section1,#mermaid-svg-QUbgQxIhLMi9XSEr .section3{fill:#fff;opacity:0.2}#mermaid-svg-QUbgQxIhLMi9XSEr .sectionTitle0{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .sectionTitle1{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .sectionTitle2{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .sectionTitle3{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-QUbgQxIhLMi9XSEr .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .grid path{stroke-width:0}#mermaid-svg-QUbgQxIhLMi9XSEr .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-QUbgQxIhLMi9XSEr .task{stroke-width:2}#mermaid-svg-QUbgQxIhLMi9XSEr .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .taskText:not([font-size]){font-size:11px}#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-QUbgQxIhLMi9XSEr .task.clickable{cursor:pointer}#mermaid-svg-QUbgQxIhLMi9XSEr .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-QUbgQxIhLMi9XSEr .taskText0,#mermaid-svg-QUbgQxIhLMi9XSEr .taskText1,#mermaid-svg-QUbgQxIhLMi9XSEr .taskText2,#mermaid-svg-QUbgQxIhLMi9XSEr .taskText3{fill:#fff}#mermaid-svg-QUbgQxIhLMi9XSEr .task0,#mermaid-svg-QUbgQxIhLMi9XSEr .task1,#mermaid-svg-QUbgQxIhLMi9XSEr .task2,#mermaid-svg-QUbgQxIhLMi9XSEr .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutside0,#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutside2{fill:#000}#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutside1,#mermaid-svg-QUbgQxIhLMi9XSEr .taskTextOutside3{fill:#000}#mermaid-svg-QUbgQxIhLMi9XSEr .active0,#mermaid-svg-QUbgQxIhLMi9XSEr .active1,#mermaid-svg-QUbgQxIhLMi9XSEr .active2,#mermaid-svg-QUbgQxIhLMi9XSEr .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-QUbgQxIhLMi9XSEr .activeText0,#mermaid-svg-QUbgQxIhLMi9XSEr .activeText1,#mermaid-svg-QUbgQxIhLMi9XSEr .activeText2,#mermaid-svg-QUbgQxIhLMi9XSEr .activeText3{fill:#000 !important}#mermaid-svg-QUbgQxIhLMi9XSEr .done0,#mermaid-svg-QUbgQxIhLMi9XSEr .done1,#mermaid-svg-QUbgQxIhLMi9XSEr .done2,#mermaid-svg-QUbgQxIhLMi9XSEr .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-QUbgQxIhLMi9XSEr .doneText0,#mermaid-svg-QUbgQxIhLMi9XSEr .doneText1,#mermaid-svg-QUbgQxIhLMi9XSEr .doneText2,#mermaid-svg-QUbgQxIhLMi9XSEr .doneText3{fill:#000 !important}#mermaid-svg-QUbgQxIhLMi9XSEr .crit0,#mermaid-svg-QUbgQxIhLMi9XSEr .crit1,#mermaid-svg-QUbgQxIhLMi9XSEr .crit2,#mermaid-svg-QUbgQxIhLMi9XSEr .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-QUbgQxIhLMi9XSEr .activeCrit0,#mermaid-svg-QUbgQxIhLMi9XSEr .activeCrit1,#mermaid-svg-QUbgQxIhLMi9XSEr .activeCrit2,#mermaid-svg-QUbgQxIhLMi9XSEr .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-QUbgQxIhLMi9XSEr .doneCrit0,#mermaid-svg-QUbgQxIhLMi9XSEr .doneCrit1,#mermaid-svg-QUbgQxIhLMi9XSEr .doneCrit2,#mermaid-svg-QUbgQxIhLMi9XSEr .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-QUbgQxIhLMi9XSEr .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-QUbgQxIhLMi9XSEr .milestoneText{font-style:italic}#mermaid-svg-QUbgQxIhLMi9XSEr .doneCritText0,#mermaid-svg-QUbgQxIhLMi9XSEr .doneCritText1,#mermaid-svg-QUbgQxIhLMi9XSEr .doneCritText2,#mermaid-svg-QUbgQxIhLMi9XSEr .doneCritText3{fill:#000 !important}#mermaid-svg-QUbgQxIhLMi9XSEr .activeCritText0,#mermaid-svg-QUbgQxIhLMi9XSEr .activeCritText1,#mermaid-svg-QUbgQxIhLMi9XSEr .activeCritText2,#mermaid-svg-QUbgQxIhLMi9XSEr .activeCritText3{fill:#000 !important}#mermaid-svg-QUbgQxIhLMi9XSEr .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-QUbgQxIhLMi9XSEr g.classGroup text .title{font-weight:bolder}#mermaid-svg-QUbgQxIhLMi9XSEr g.clickable{cursor:pointer}#mermaid-svg-QUbgQxIhLMi9XSEr g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-QUbgQxIhLMi9XSEr g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-QUbgQxIhLMi9XSEr .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-QUbgQxIhLMi9XSEr .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-QUbgQxIhLMi9XSEr .dashed-line{stroke-dasharray:3}#mermaid-svg-QUbgQxIhLMi9XSEr #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr .commit-id,#mermaid-svg-QUbgQxIhLMi9XSEr .commit-msg,#mermaid-svg-QUbgQxIhLMi9XSEr .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-QUbgQxIhLMi9XSEr g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-QUbgQxIhLMi9XSEr g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-QUbgQxIhLMi9XSEr g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-QUbgQxIhLMi9XSEr .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-QUbgQxIhLMi9XSEr .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-QUbgQxIhLMi9XSEr .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-QUbgQxIhLMi9XSEr .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-QUbgQxIhLMi9XSEr .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-QUbgQxIhLMi9XSEr .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-QUbgQxIhLMi9XSEr .edgeLabel text{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-QUbgQxIhLMi9XSEr .node circle.state-start{fill:black;stroke:black}#mermaid-svg-QUbgQxIhLMi9XSEr .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-QUbgQxIhLMi9XSEr #statediagram-barbEnd{fill:#9370db}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-state .divider{stroke:#9370db}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-QUbgQxIhLMi9XSEr .note-edge{stroke-dasharray:5}#mermaid-svg-QUbgQxIhLMi9XSEr .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-QUbgQxIhLMi9XSEr .error-icon{fill:#522}#mermaid-svg-QUbgQxIhLMi9XSEr .error-text{fill:#522;stroke:#522}#mermaid-svg-QUbgQxIhLMi9XSEr .edge-thickness-normal{stroke-width:2px}#mermaid-svg-QUbgQxIhLMi9XSEr .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-QUbgQxIhLMi9XSEr .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-QUbgQxIhLMi9XSEr .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-QUbgQxIhLMi9XSEr .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-QUbgQxIhLMi9XSEr .marker{fill:#333}#mermaid-svg-QUbgQxIhLMi9XSEr .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-QUbgQxIhLMi9XSEr {
color: rgba(0, 0, 0, 0.75);
font: ;
}

导致
导致
电流
运动
位置

得出结论:如果要控制位置,那就要控速度,如果要控速度,就必须控制电流。

位置环的输出是速度值,速度环的输出是电流值。而他们的反馈(输入)都是电机的实时数据:实时速度,实时位置。

就这样,最末端的电机^ 1反馈出三个值,依次给最外面的位置,中间的速度,里面的电流。形成了三个闭合的环。如下图

#mermaid-svg-sk4ROz9T234sS2qF .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-sk4ROz9T234sS2qF .label text{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .node rect,#mermaid-svg-sk4ROz9T234sS2qF .node circle,#mermaid-svg-sk4ROz9T234sS2qF .node ellipse,#mermaid-svg-sk4ROz9T234sS2qF .node polygon,#mermaid-svg-sk4ROz9T234sS2qF .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-sk4ROz9T234sS2qF .node .label{text-align:center;fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .node.clickable{cursor:pointer}#mermaid-svg-sk4ROz9T234sS2qF .arrowheadPath{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-sk4ROz9T234sS2qF .flowchart-link{stroke:#333;fill:none}#mermaid-svg-sk4ROz9T234sS2qF .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-sk4ROz9T234sS2qF .edgeLabel rect{opacity:0.9}#mermaid-svg-sk4ROz9T234sS2qF .edgeLabel span{color:#333}#mermaid-svg-sk4ROz9T234sS2qF .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-sk4ROz9T234sS2qF .cluster text{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-sk4ROz9T234sS2qF .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-sk4ROz9T234sS2qF text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-sk4ROz9T234sS2qF .actor-line{stroke:grey}#mermaid-svg-sk4ROz9T234sS2qF .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-sk4ROz9T234sS2qF .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-sk4ROz9T234sS2qF #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-sk4ROz9T234sS2qF .sequenceNumber{fill:#fff}#mermaid-svg-sk4ROz9T234sS2qF #sequencenumber{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF #crosshead path{fill:#333;stroke:#333}#mermaid-svg-sk4ROz9T234sS2qF .messageText{fill:#333;stroke:#333}#mermaid-svg-sk4ROz9T234sS2qF .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-sk4ROz9T234sS2qF .labelText,#mermaid-svg-sk4ROz9T234sS2qF .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-sk4ROz9T234sS2qF .loopText,#mermaid-svg-sk4ROz9T234sS2qF .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-sk4ROz9T234sS2qF .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-sk4ROz9T234sS2qF .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-sk4ROz9T234sS2qF .noteText,#mermaid-svg-sk4ROz9T234sS2qF .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-sk4ROz9T234sS2qF .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-sk4ROz9T234sS2qF .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-sk4ROz9T234sS2qF .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-sk4ROz9T234sS2qF .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .section{stroke:none;opacity:0.2}#mermaid-svg-sk4ROz9T234sS2qF .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-sk4ROz9T234sS2qF .section2{fill:#fff400}#mermaid-svg-sk4ROz9T234sS2qF .section1,#mermaid-svg-sk4ROz9T234sS2qF .section3{fill:#fff;opacity:0.2}#mermaid-svg-sk4ROz9T234sS2qF .sectionTitle0{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .sectionTitle1{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .sectionTitle2{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .sectionTitle3{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-sk4ROz9T234sS2qF .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .grid path{stroke-width:0}#mermaid-svg-sk4ROz9T234sS2qF .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-sk4ROz9T234sS2qF .task{stroke-width:2}#mermaid-svg-sk4ROz9T234sS2qF .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .taskText:not([font-size]){font-size:11px}#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-sk4ROz9T234sS2qF .task.clickable{cursor:pointer}#mermaid-svg-sk4ROz9T234sS2qF .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-sk4ROz9T234sS2qF .taskText0,#mermaid-svg-sk4ROz9T234sS2qF .taskText1,#mermaid-svg-sk4ROz9T234sS2qF .taskText2,#mermaid-svg-sk4ROz9T234sS2qF .taskText3{fill:#fff}#mermaid-svg-sk4ROz9T234sS2qF .task0,#mermaid-svg-sk4ROz9T234sS2qF .task1,#mermaid-svg-sk4ROz9T234sS2qF .task2,#mermaid-svg-sk4ROz9T234sS2qF .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutside0,#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutside2{fill:#000}#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutside1,#mermaid-svg-sk4ROz9T234sS2qF .taskTextOutside3{fill:#000}#mermaid-svg-sk4ROz9T234sS2qF .active0,#mermaid-svg-sk4ROz9T234sS2qF .active1,#mermaid-svg-sk4ROz9T234sS2qF .active2,#mermaid-svg-sk4ROz9T234sS2qF .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-sk4ROz9T234sS2qF .activeText0,#mermaid-svg-sk4ROz9T234sS2qF .activeText1,#mermaid-svg-sk4ROz9T234sS2qF .activeText2,#mermaid-svg-sk4ROz9T234sS2qF .activeText3{fill:#000 !important}#mermaid-svg-sk4ROz9T234sS2qF .done0,#mermaid-svg-sk4ROz9T234sS2qF .done1,#mermaid-svg-sk4ROz9T234sS2qF .done2,#mermaid-svg-sk4ROz9T234sS2qF .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-sk4ROz9T234sS2qF .doneText0,#mermaid-svg-sk4ROz9T234sS2qF .doneText1,#mermaid-svg-sk4ROz9T234sS2qF .doneText2,#mermaid-svg-sk4ROz9T234sS2qF .doneText3{fill:#000 !important}#mermaid-svg-sk4ROz9T234sS2qF .crit0,#mermaid-svg-sk4ROz9T234sS2qF .crit1,#mermaid-svg-sk4ROz9T234sS2qF .crit2,#mermaid-svg-sk4ROz9T234sS2qF .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-sk4ROz9T234sS2qF .activeCrit0,#mermaid-svg-sk4ROz9T234sS2qF .activeCrit1,#mermaid-svg-sk4ROz9T234sS2qF .activeCrit2,#mermaid-svg-sk4ROz9T234sS2qF .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-sk4ROz9T234sS2qF .doneCrit0,#mermaid-svg-sk4ROz9T234sS2qF .doneCrit1,#mermaid-svg-sk4ROz9T234sS2qF .doneCrit2,#mermaid-svg-sk4ROz9T234sS2qF .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-sk4ROz9T234sS2qF .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-sk4ROz9T234sS2qF .milestoneText{font-style:italic}#mermaid-svg-sk4ROz9T234sS2qF .doneCritText0,#mermaid-svg-sk4ROz9T234sS2qF .doneCritText1,#mermaid-svg-sk4ROz9T234sS2qF .doneCritText2,#mermaid-svg-sk4ROz9T234sS2qF .doneCritText3{fill:#000 !important}#mermaid-svg-sk4ROz9T234sS2qF .activeCritText0,#mermaid-svg-sk4ROz9T234sS2qF .activeCritText1,#mermaid-svg-sk4ROz9T234sS2qF .activeCritText2,#mermaid-svg-sk4ROz9T234sS2qF .activeCritText3{fill:#000 !important}#mermaid-svg-sk4ROz9T234sS2qF .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-sk4ROz9T234sS2qF g.classGroup text .title{font-weight:bolder}#mermaid-svg-sk4ROz9T234sS2qF g.clickable{cursor:pointer}#mermaid-svg-sk4ROz9T234sS2qF g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-sk4ROz9T234sS2qF g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-sk4ROz9T234sS2qF .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-sk4ROz9T234sS2qF .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-sk4ROz9T234sS2qF .dashed-line{stroke-dasharray:3}#mermaid-svg-sk4ROz9T234sS2qF #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF .commit-id,#mermaid-svg-sk4ROz9T234sS2qF .commit-msg,#mermaid-svg-sk4ROz9T234sS2qF .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-sk4ROz9T234sS2qF g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-sk4ROz9T234sS2qF g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-sk4ROz9T234sS2qF g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-sk4ROz9T234sS2qF .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-sk4ROz9T234sS2qF .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-sk4ROz9T234sS2qF .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-sk4ROz9T234sS2qF .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-sk4ROz9T234sS2qF .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-sk4ROz9T234sS2qF .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-sk4ROz9T234sS2qF .edgeLabel text{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-sk4ROz9T234sS2qF .node circle.state-start{fill:black;stroke:black}#mermaid-svg-sk4ROz9T234sS2qF .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-sk4ROz9T234sS2qF #statediagram-barbEnd{fill:#9370db}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-state .divider{stroke:#9370db}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-sk4ROz9T234sS2qF .note-edge{stroke-dasharray:5}#mermaid-svg-sk4ROz9T234sS2qF .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-sk4ROz9T234sS2qF .error-icon{fill:#522}#mermaid-svg-sk4ROz9T234sS2qF .error-text{fill:#522;stroke:#522}#mermaid-svg-sk4ROz9T234sS2qF .edge-thickness-normal{stroke-width:2px}#mermaid-svg-sk4ROz9T234sS2qF .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-sk4ROz9T234sS2qF .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-sk4ROz9T234sS2qF .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-sk4ROz9T234sS2qF .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-sk4ROz9T234sS2qF .marker{fill:#333}#mermaid-svg-sk4ROz9T234sS2qF .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-sk4ROz9T234sS2qF {
color: rgba(0, 0, 0, 0.75);
font: ;
}

位置环
速度环
电流环
速度值
电流值
转速
转速+位置
位置调节
目标位置值
速度调节
位置
电调
电机
BY:胡小安

不难发现,很多例子像水缸加水和直升机定高,他们都是直接的位置环,输出量是加水的速度和向上飞的升力。没有底层的两环。

接下来我们以M2006例程为例子看一看怎么实现上述过程。

0x01 剖析例程–多少行代码能实现电机控速?

先说明,M3508支持PWM和CAN两种控制方式,其中PWM可以直接控速但是没有数据反馈。M2006仅支持CAN总线。两种电机的总线编码都是从0x200开始,驱动文件bsp_can.ccan.c几乎一样可以替换。

本文仅介绍PID的部署实现,不涉及CAN通信内容。

总结下来,PID就是一个结构体三个函数。

一个结构体PID_TypeDef:

typedef struct _PID_TypeDef
{
	float target;							//目标值
	float kp;								//比例系数
	float ki;								//积分系数
	float kd;								//微分系数
	
	float   measure;						//测量值
	float   err;							//误差
	float   last_err;      	      			//上次误差
	
	float pout;								//比例项
	float iout;								//积分项
	float dout;								//微分项
	
	float output;							//本次输出
	float last_output;						//上次输出
	
	float MaxOutput;						//输出限幅
	float IntegralLimit;					//积分限幅
	float DeadBand;						    //死区(绝对值)
	float  Max_Err;							//最大误差
	

	void (*f_param_init)					//参数初始化				   
	void (*f_pid_reset)						//pid三个参数修改
	float (*f_cal_pid) 						 //pid计算
}PID_TypeDef;

注意:为了简单明晰,我删去了原文件里一些用不到的参数,最后三个函数指针的参数列表也被删除。其中不乏很重要的计算周期,但是在简单的控制下,时间间隔是可以忽略的。

这个结构体的核心就是三个系数,调参调的也就是这三个。

target目标值和output输出值还有measure反馈值,再就是errlast_err两个误差。

其他的一些限幅,和死区[^2]无非就是防止问题发生的补丁。

三个函数:f_param_initf_pid_resetf_cal_pid

名副其实,分别是结构体的参数初始化,三个系数修改、最重要的输出值计算。

static float pid_calculate(PID_TypeDef* pid, float measure)
{

	pid->measure = measure;						//目标速度

		
	pid->last_err  = pid->err;					//更新前一次误差
	pid->err = pid->target - pid->measure;		 //计算当前误差
    
    
	pid->last_output = pid->output;
    
	
	if((ABS(pid->err) > pid->DeadBand))		//是否进入死区,如果进入则直接跳过,返回上一次的output结果
	{
        
		pid->pout = pid->kp * pid->err;			
		pid->iout += (pid->ki * pid->err);			//注意是加等于
		pid->dout =  pid->kd * (pid->err - pid->last_err); 
		
        
		//积分是否超出限制
		if(pid->iout > pid->IntegralLimit)
			pid->iout = pid->IntegralLimit;
		if(pid->iout < - pid->IntegralLimit)
			pid->iout = - pid->IntegralLimit;
		
        
		//pid输出和
		pid->output = pid->pout + pid->iout + pid->dout;
		

		//限制输出的大小
		if(pid->output>pid->MaxOutput)         
		{
			pid->output = pid->MaxOutput;
		}
		if(pid->output < -(pid->MaxOutput))
		{
			pid->output = -(pid->MaxOutput);
		}
	
	}
	return pid->output;
}

这是计算函数,我们要把实时测量值measure传入函数,用来更新误差,产生新的输出。

measure是电机发来的速度或者位置数据,在CAN中断函数里自动更新。

在这里我们用误差之差代替微分,并且对积分项和输出结果进行了限幅。这些幅度都是自定义的。

得出流程如下:

#mermaid-svg-R78Pdv9ugGljgXUX .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-R78Pdv9ugGljgXUX .label text{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .node rect,#mermaid-svg-R78Pdv9ugGljgXUX .node circle,#mermaid-svg-R78Pdv9ugGljgXUX .node ellipse,#mermaid-svg-R78Pdv9ugGljgXUX .node polygon,#mermaid-svg-R78Pdv9ugGljgXUX .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-R78Pdv9ugGljgXUX .node .label{text-align:center;fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .node.clickable{cursor:pointer}#mermaid-svg-R78Pdv9ugGljgXUX .arrowheadPath{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-R78Pdv9ugGljgXUX .flowchart-link{stroke:#333;fill:none}#mermaid-svg-R78Pdv9ugGljgXUX .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-R78Pdv9ugGljgXUX .edgeLabel rect{opacity:0.9}#mermaid-svg-R78Pdv9ugGljgXUX .edgeLabel span{color:#333}#mermaid-svg-R78Pdv9ugGljgXUX .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-R78Pdv9ugGljgXUX .cluster text{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-R78Pdv9ugGljgXUX .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-R78Pdv9ugGljgXUX text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-R78Pdv9ugGljgXUX .actor-line{stroke:grey}#mermaid-svg-R78Pdv9ugGljgXUX .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-R78Pdv9ugGljgXUX .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-R78Pdv9ugGljgXUX #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-R78Pdv9ugGljgXUX .sequenceNumber{fill:#fff}#mermaid-svg-R78Pdv9ugGljgXUX #sequencenumber{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX #crosshead path{fill:#333;stroke:#333}#mermaid-svg-R78Pdv9ugGljgXUX .messageText{fill:#333;stroke:#333}#mermaid-svg-R78Pdv9ugGljgXUX .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-R78Pdv9ugGljgXUX .labelText,#mermaid-svg-R78Pdv9ugGljgXUX .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-R78Pdv9ugGljgXUX .loopText,#mermaid-svg-R78Pdv9ugGljgXUX .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-R78Pdv9ugGljgXUX .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-R78Pdv9ugGljgXUX .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-R78Pdv9ugGljgXUX .noteText,#mermaid-svg-R78Pdv9ugGljgXUX .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-R78Pdv9ugGljgXUX .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-R78Pdv9ugGljgXUX .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-R78Pdv9ugGljgXUX .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-R78Pdv9ugGljgXUX .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .section{stroke:none;opacity:0.2}#mermaid-svg-R78Pdv9ugGljgXUX .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-R78Pdv9ugGljgXUX .section2{fill:#fff400}#mermaid-svg-R78Pdv9ugGljgXUX .section1,#mermaid-svg-R78Pdv9ugGljgXUX .section3{fill:#fff;opacity:0.2}#mermaid-svg-R78Pdv9ugGljgXUX .sectionTitle0{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .sectionTitle1{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .sectionTitle2{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .sectionTitle3{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-R78Pdv9ugGljgXUX .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .grid path{stroke-width:0}#mermaid-svg-R78Pdv9ugGljgXUX .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-R78Pdv9ugGljgXUX .task{stroke-width:2}#mermaid-svg-R78Pdv9ugGljgXUX .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .taskText:not([font-size]){font-size:11px}#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-R78Pdv9ugGljgXUX .task.clickable{cursor:pointer}#mermaid-svg-R78Pdv9ugGljgXUX .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-R78Pdv9ugGljgXUX .taskText0,#mermaid-svg-R78Pdv9ugGljgXUX .taskText1,#mermaid-svg-R78Pdv9ugGljgXUX .taskText2,#mermaid-svg-R78Pdv9ugGljgXUX .taskText3{fill:#fff}#mermaid-svg-R78Pdv9ugGljgXUX .task0,#mermaid-svg-R78Pdv9ugGljgXUX .task1,#mermaid-svg-R78Pdv9ugGljgXUX .task2,#mermaid-svg-R78Pdv9ugGljgXUX .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutside0,#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutside2{fill:#000}#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutside1,#mermaid-svg-R78Pdv9ugGljgXUX .taskTextOutside3{fill:#000}#mermaid-svg-R78Pdv9ugGljgXUX .active0,#mermaid-svg-R78Pdv9ugGljgXUX .active1,#mermaid-svg-R78Pdv9ugGljgXUX .active2,#mermaid-svg-R78Pdv9ugGljgXUX .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-R78Pdv9ugGljgXUX .activeText0,#mermaid-svg-R78Pdv9ugGljgXUX .activeText1,#mermaid-svg-R78Pdv9ugGljgXUX .activeText2,#mermaid-svg-R78Pdv9ugGljgXUX .activeText3{fill:#000 !important}#mermaid-svg-R78Pdv9ugGljgXUX .done0,#mermaid-svg-R78Pdv9ugGljgXUX .done1,#mermaid-svg-R78Pdv9ugGljgXUX .done2,#mermaid-svg-R78Pdv9ugGljgXUX .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-R78Pdv9ugGljgXUX .doneText0,#mermaid-svg-R78Pdv9ugGljgXUX .doneText1,#mermaid-svg-R78Pdv9ugGljgXUX .doneText2,#mermaid-svg-R78Pdv9ugGljgXUX .doneText3{fill:#000 !important}#mermaid-svg-R78Pdv9ugGljgXUX .crit0,#mermaid-svg-R78Pdv9ugGljgXUX .crit1,#mermaid-svg-R78Pdv9ugGljgXUX .crit2,#mermaid-svg-R78Pdv9ugGljgXUX .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-R78Pdv9ugGljgXUX .activeCrit0,#mermaid-svg-R78Pdv9ugGljgXUX .activeCrit1,#mermaid-svg-R78Pdv9ugGljgXUX .activeCrit2,#mermaid-svg-R78Pdv9ugGljgXUX .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-R78Pdv9ugGljgXUX .doneCrit0,#mermaid-svg-R78Pdv9ugGljgXUX .doneCrit1,#mermaid-svg-R78Pdv9ugGljgXUX .doneCrit2,#mermaid-svg-R78Pdv9ugGljgXUX .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-R78Pdv9ugGljgXUX .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-R78Pdv9ugGljgXUX .milestoneText{font-style:italic}#mermaid-svg-R78Pdv9ugGljgXUX .doneCritText0,#mermaid-svg-R78Pdv9ugGljgXUX .doneCritText1,#mermaid-svg-R78Pdv9ugGljgXUX .doneCritText2,#mermaid-svg-R78Pdv9ugGljgXUX .doneCritText3{fill:#000 !important}#mermaid-svg-R78Pdv9ugGljgXUX .activeCritText0,#mermaid-svg-R78Pdv9ugGljgXUX .activeCritText1,#mermaid-svg-R78Pdv9ugGljgXUX .activeCritText2,#mermaid-svg-R78Pdv9ugGljgXUX .activeCritText3{fill:#000 !important}#mermaid-svg-R78Pdv9ugGljgXUX .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-R78Pdv9ugGljgXUX g.classGroup text .title{font-weight:bolder}#mermaid-svg-R78Pdv9ugGljgXUX g.clickable{cursor:pointer}#mermaid-svg-R78Pdv9ugGljgXUX g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-R78Pdv9ugGljgXUX g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-R78Pdv9ugGljgXUX .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-R78Pdv9ugGljgXUX .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-R78Pdv9ugGljgXUX .dashed-line{stroke-dasharray:3}#mermaid-svg-R78Pdv9ugGljgXUX #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX .commit-id,#mermaid-svg-R78Pdv9ugGljgXUX .commit-msg,#mermaid-svg-R78Pdv9ugGljgXUX .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-R78Pdv9ugGljgXUX g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-R78Pdv9ugGljgXUX g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-R78Pdv9ugGljgXUX g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-R78Pdv9ugGljgXUX .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-R78Pdv9ugGljgXUX .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-R78Pdv9ugGljgXUX .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-R78Pdv9ugGljgXUX .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-R78Pdv9ugGljgXUX .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-R78Pdv9ugGljgXUX .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-R78Pdv9ugGljgXUX .edgeLabel text{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-R78Pdv9ugGljgXUX .node circle.state-start{fill:black;stroke:black}#mermaid-svg-R78Pdv9ugGljgXUX .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-R78Pdv9ugGljgXUX #statediagram-barbEnd{fill:#9370db}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-state .divider{stroke:#9370db}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-R78Pdv9ugGljgXUX .note-edge{stroke-dasharray:5}#mermaid-svg-R78Pdv9ugGljgXUX .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-R78Pdv9ugGljgXUX .error-icon{fill:#522}#mermaid-svg-R78Pdv9ugGljgXUX .error-text{fill:#522;stroke:#522}#mermaid-svg-R78Pdv9ugGljgXUX .edge-thickness-normal{stroke-width:2px}#mermaid-svg-R78Pdv9ugGljgXUX .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-R78Pdv9ugGljgXUX .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-R78Pdv9ugGljgXUX .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-R78Pdv9ugGljgXUX .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-R78Pdv9ugGljgXUX .marker{fill:#333}#mermaid-svg-R78Pdv9ugGljgXUX .marker.cross{stroke:#333}

:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-R78Pdv9ugGljgXUX {
color: rgba(0, 0, 0, 0.75);
font: ;
}

人为更新
实时更新
电流值
set_spd
PID_TypeDef
measure
calc计算
电调

0x02 一个小例子

M2006例程相对M3508虽然简单,但还是包括了一些上位机通信控制的代码。

还是简单明晰的原则。下面是一个最最简单的demo框架。

#include "pid.h"
#define NUM_OF_MOTOR 1



PID_TypeDef moto_pid[NUM_OF_MOTOR];
float set_spd;


int main(){
    
    init_all();
    
    for(int i=0;i<NUM_OF_MOTOR;i++){
    	pid_init(&moto_pid[i]);//把结构体里的函数指针赋值,三个函数	
    	moto_pid[i].f_param_init(&moto_pid[i],PID_Speed,16384,5000,10,0,8000,0,1.5,0.1,0);
        //确定结构体内的参数,幅值,死区大小,PID系数
        
    }
    
    
    for(;;){
        
        get_set_spd_from_USART();//从串口得到设定值set_spd
        
        for(int i=0; i<NUM_OF_MOTOR; i++)
   		 {	
     	 motor_pid[i].target = set_spd;	//更新目标值						
      	 motor_pid[i].f_cal_pid(&motor_pid[i],measure[i]);    //PID计算。measure由CAN中断更新
    	  }
        set_moto_current(&hcan1,motor_pid[0].output,   //将PID的计算结果通过CAN发送到电机
                        motor_pid[1].output,
                        motor_pid[2].output,
                        motor_pid[3].output);
        
        HAL_Delay(10);//延时10ms控制周期
    }
    
    return 0;
}

到此为止,一个简单的PID电机控制就做好了。如果写的紧凑一点,代码可能不超过50行,还是非常简单的。

注意事项:

我选用的是F407,带有FPU浮点运算单元的MCU。尽量选择CM4,这样浮点运算会快很多。

实际使用的时候要把Keil的option里面target一栏里floating point Hardware选成Single Precision

演示视频在微信视频号上:扫码查看微信文章底部的视频文章二维码.png

0x03 总结

以上就是最简单的电机控制部署,本着怎么简单怎么来的原则,希望能帮助朋友们节约一些学习时间。

除了官方例程,我还有自己移植的基于F407的版本,去除例程的无用部分,加入了串口的通信解析,可以比较方便的调参,在线修改速度、pid参数等。有需要的可以加微信公众号直接问我要。

关注嵌入式、电机控制的朋友也可以添加公众号,最近会更新有关上位机通信,CAN通信等电机控制相关内容

小安


第二篇:大疆M3508、M2006必备CAN总线知识与配置方法

第三篇:M3508电机控制程序的F1移植过程

第四篇:ESP32:蓝牙BLE控制M3508


大一技术新人,如果发现文中错误请各位大佬不吝赐教,一定指出,如果有意见或建议同样欢迎。谢谢。


欢迎转载,请注明作者与原文地址:https://blog.csdn.net/qq_28039135/article/details/116379392

版权声明:本文为CSDN博主「HuxiaoA」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_28039135/article/details/116379392

生成海报
点赞 0

HuxiaoA

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

keil5新建工程(图解)

下面以stm32f103c8t6新建工程为例来介绍 工程项目模板下载链接(里面有新建工程所需的文件):http://链接:https://pan.baidu.com/s/1Cve57YaA

STM32程序不运行与MicroLIB讲解

成就更好的自己 目录 引言 什么是MicroLIB 不使用Microlib导致卡死的原理 卡死解决办法: 优化空间测评 引言 先说问题,这几天在使用STM32H750调试程序的过程中出现了一些问题&#xf

打开keil5提示缺少“TOOLS.INI“问题解决方法

之前用快捷方式打开keil5发现突然弹出如图所示提示: 提示我的keil根目录下缺少TOOLS.INI,然后打不开keil5。后来查了一下真的根目录下的真的没有这个配置文件,于是在MDK5目录里面的找