原文连接:Raspberry PI + Codesys + EtherCAT步进驱动ECR60 Motion功能测试 -- 个人资料收集 (rtplc.com)
<div class="post_info_wrapper ">
<p class="has-drop-cap">运动控制功能是codesys及EtherCAT通讯的重要功能,接下来对测试EtherCAT的运动控制部分,由于对这一部分功能不熟悉,所以测试程序采用ST语言,采用 case语句,方便测试各个函数的运行功能和状态。</p>
<h2>工具准备:</h2>
<ul><li>树莓派4B,已经按照前述方法设置好。</li><li><a href="http://www.szruitech.com/Detail Page ECR60.html" target="_blank" rel="noreferrer noopener">ECR60 步进驱动器</a>一个</li><li><a href="http://www.szruitech.com/Detail Page 57.html" target="_blank" rel="noreferrer noopener">57A2步进电机</a>一个</li><li>CAT6网线一条</li><li>电源:DC24V</li></ul>
<h2>Step1 树莓派准备</h2>
<p>树莓派通电,有显示器的化,可以看到wifi是否连接成功,记录下此时的ip地址,由于后续Codesys连接。</p>
<h2>Step2 电机及驱动器接线</h2>
<ul><li>将24V电源连接至驱动器的V+、V-端子</li><li>将步进电机的A+、A-、B+、B-连接至驱动器电机接线端子,锐特的57A2电机绕组颜色定义为:红、蓝、绿、黑</li><li>网线连接:CAT6网线一端连接树莓派的网口,另外一端连接ECR60的ECAT IN端口。</li><li>完成上述步骤后,可以开通24V电源</li></ul>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="542" src="http://www.rtplc.com/wp-content/uploads/2020/05/ECR60Connect-1024x542.jpg" alt="" class="wp-image-491" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/ECR60Connect-1024x542.jpg 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/ECR60Connect-300x159.jpg 300w, http://www.rtplc.com/wp-content/uploads/2020/05/ECR60Connect-768x407.jpg 768w, http://www.rtplc.com/wp-content/uploads/2020/05/ECR60Connect-1536x814.jpg 1536w, http://www.rtplc.com/wp-content/uploads/2020/05/ECR60Connect-2048x1085.jpg 2048w" sizes="(max-width: 1024px) 100vw, 1024px"></figure>
<p>此时可见ECR60的PWR的绿色指示灯亮起,表示驱动器供电正常;ECAT IN网口的黄色LED灯亮起,表示网线连接正常。</p>
<h2>Step3 Codesys项目设置</h2>
<p>打开Codesys开发环境,新建一个标准项目,并选择目标板为"Codesys Control for Raspberry PI MC SL"</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="536" height="300" src="http://www.rtplc.com/wp-content/uploads/2020/05/Setup0.png" alt="" class="wp-image-505" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Setup0.png 536w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup0-300x168.png 300w" sizes="(max-width: 536px) 100vw, 536px"><figcaption>选择 MCSL</figcaption></figure>
<p>扫描并选择树莓</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="749" src="http://www.rtplc.com/wp-content/uploads/2020/05/Setup1-1024x749.png" alt="" class="wp-image-506" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Setup1-1024x749.png 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup1-300x219.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup1-768x562.png 768w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup1.png 1246w" sizes="(max-width: 1024px) 100vw, 1024px"><figcaption>扫描树莓派</figcaption></figure>
<p>添加EhterCAT master主站</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="705" src="http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_Addmaster-1024x705.png" alt="" class="wp-image-507" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_Addmaster-1024x705.png 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_Addmaster-300x207.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_Addmaster-768x529.png 768w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_Addmaster.png 1079w" sizes="(max-width: 1024px) 100vw, 1024px"><figcaption>添加EtherCAT Master</figcaption></figure>
<p>设定EtherCAT master 的网口属性,如下图,选择"eth0"</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="555" src="http://www.rtplc.com/wp-content/uploads/2020/05/Master-Set1-1024x555.png" alt="" class="wp-image-406" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Master-Set1-1024x555.png 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/Master-Set1-300x163.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/Master-Set1-768x416.png 768w, http://www.rtplc.com/wp-content/uploads/2020/05/Master-Set1-1536x832.png 1536w, http://www.rtplc.com/wp-content/uploads/2020/05/Master-Set1-1200x650.png 1200w, http://www.rtplc.com/wp-content/uploads/2020/05/Master-Set1.png 1920w" sizes="(max-width: 1024px) 100vw, 1024px"></figure>
<p>接下来添加ECR60步进驱动器,我手上的是最新的V202版本,直接添加即可。</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="858" src="http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddECR60-1024x858.png" alt="" class="wp-image-508" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddECR60-1024x858.png 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddECR60-300x251.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddECR60-768x643.png 768w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddECR60.png 1077w" sizes="(max-width: 1024px) 100vw, 1024px"><figcaption>添加ECR60</figcaption></figure>
<p>接下来添加一个运动控制轴,关联到ECR60驱动器,如下图:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="654" height="733" src="http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddSoftMotion.png" alt="" class="wp-image-509" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddSoftMotion.png 654w, http://www.rtplc.com/wp-content/uploads/2020/05/Setup2_AddSoftMotion-268x300.png 268w" sizes="(max-width: 654px) 100vw, 654px"></figure>
<p>右键"Properties",将轴的名字改为Axis1,如下图:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="812" height="484" src="http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Name.png" alt="" class="wp-image-510" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Name.png 812w, http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Name-300x179.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Name-768x458.png 768w" sizes="(max-width: 812px) 100vw, 812px"></figure>
<p>接下来设定轴的参数。首先设定电机运行的当量,ECR60默认使用10000脉冲/转,对应轴的设置如下图:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="992" height="359" src="http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Microstep.png" alt="" class="wp-image-511" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Microstep.png 992w, http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Microstep-300x109.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/AxisSetup1_Microstep-768x278.png 768w" sizes="(max-width: 992px) 100vw, 992px"></figure>
<h2>Step4 程序编写</h2>
<p>至此,整个系统的设置已经完成,接下来编写程序实现步进电机的控制。默认在新建项目时,设定了一个程序"PLC_PRG"的编写方式为ST语言。首先设定运动相关的变量:</p>
<pre class="wp-block-code"><code>PROGRAM PLC_PRG
VAR
mcp: MC_Power; //使能函数
mcmv: MC_Jog; //点动函数
mcmabs: MC_MoveAbsolute; // 绝对值定位运动
mcminc: MC_MoveRelative; // 增量式运动
MVStop: MC_Stop; // 电机停止
Axis1Home: MC_Home; // 电机回零
Axis1Reset: MC_Reset; // 电机故障复位
Motionstate: INT; // 测试用,控制测试功能
Cnt:INT; // 仅仅用于测试
Jog_Pos: BOOL ; // 点动正 使能
Jog_Neg: BOOL; // 点动负 使能
MovEnable: BOOL; // 定位运行、回零及停止时的函数模块使能
MovIncDistance: LREAL; // 增量运动距离设定
MV_Speed: LREAL; // 运行速度设定
MV_Acc: LREAL; // 运行加速度设定
MV_Dec: LREAL; // 运行减速度设定
MV_Jerk: LREAL; // 运行加加速度设定
MovAbsDistance: LREAL; // 绝对定位时的目标位置
END_VAR</code></pre>
<p>设计一个程序,通过状态跳转,实现对各个函数的测试,相关程序如下:</p>
<pre class="wp-block-code"><code>
CASE Motionstate OF
0:
Cnt := Cnt+1;
MV_Speed:= 10000;
MV_Acc:= 1000000;
MV_Dec:= 1000000;
MV_Jerk:= 100000000;
Jog_Pos:= TRUE;
1: //Enable motor,Power on
mcp(
Axis:= Axis1 ,
Enable:= TRUE ,
bRegulatorOn:= TRUE,
bDriveStart:= TRUE,
Status=> ,
Busy=> ,
Error=> ,
ErrorID=> );
2:// Disable motor,Power off
mcp(
Axis:=Axis1 ,
Enable:= TRUE ,
bRegulatorOn:= FALSE,
bDriveStart:= TRUE,
Status=> ,
Busy=> ,
Error=> ,
ErrorID=> );
3: // JOG+ and JOG-
mcmv(
Axis:=Axis1 ,
JogForward:=Jog_Pos ,
JogBackward:=Jog_Neg ,
Velocity:= MV_Speed,
Acceleration:= MV_Acc,
Deceleration:= MV_Dec,
Jerk:= MV_Jerk,
Busy=> ,
CommandAborted=> ,
Error=> ,
ErrorID=> );
4: // Move relative POS and Neg
mcminc(
Axis:=Axis1 ,
Execute:= MovEnable,
Distance:= MovIncDistance,
Velocity:= MV_Speed,
Acceleration:= MV_Acc,
Deceleration:= MV_Dec,
Jerk:= MV_Jerk,
Done=> ,
Busy=> ,
CommandAborted=> ,
Error=> ,
ErrorID=> );
5: // Move Abs
mcmabs(
Axis:=Axis1 ,
Execute:= MovEnable,
Position:= MovAbsDistance,
Velocity:= MV_Speed,
Acceleration:= MV_Acc,
Deceleration:= MV_Dec,
Jerk:= MV_Jerk,
Done=> ,
Busy=> ,
CommandAborted=> ,
Error=> ,
ErrorID=> );
6: // Stop motor
MVStop(
Axis:=Axis1 ,
Execute:= MovEnable,
Jerk:= MV_Jerk,
Done=> ,
Busy=> ,
Error=> ,
ErrorID=> );
7: // Home
Axis1Home(
Axis:=Axis1 ,
Execute:= MovEnable,
Done=> ,
Busy=> ,
CommandAborted=> ,
Error=> ,
ErrorID=> );
8:
Axis1Reset(
Axis:=Axis1 ,
Execute:= TRUE,
);
END_CASE;
</code></pre>
<p>将"PLC_PRG"添加到EtherCAT_Task任务中</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="677" src="http://www.rtplc.com/wp-content/uploads/2020/05/MotionSetup-1024x677.png" alt="" class="wp-image-512" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/MotionSetup-1024x677.png 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/MotionSetup-300x198.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/MotionSetup-768x508.png 768w, http://www.rtplc.com/wp-content/uploads/2020/05/MotionSetup.png 1087w" sizes="(max-width: 1024px) 100vw, 1024px"></figure>
<p>至此,程序编写完成,任务设置完成,可以进行下载程序测试。</p>
<h2>Step5 运行及测试</h2>
<p>在Codesys环境中,"Online" ---> "Login",下载程序进入到树莓派,然后点击"Debug" ---> "Start"。如果之前都是顺利的化,此时步进驱动器的EtherCAT的RUN绿色指示灯常亮,ECAT IN的绿色数据灯一直闪烁,轴Axis1的图标为橙色双箭头,说明轴已经准备就绪,橙色是因为树莓派没有正式的授权运行codesys,处于demo状态。codesys的项目界面如下图:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="767" src="http://www.rtplc.com/wp-content/uploads/2020/05/PLCRun-1024x767.png" alt="" class="wp-image-513" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/PLCRun-1024x767.png 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/PLCRun-300x225.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/PLCRun-768x575.png 768w, http://www.rtplc.com/wp-content/uploads/2020/05/PLCRun.png 1207w" sizes="(max-width: 1024px) 100vw, 1024px"></figure>
<p>测试程序状态变量"MotionState"为0,Cnt一直增加,程序初始化设置运行的速度、加速度等变量。。需要注意的是ECR60默认电流为3000mA,如果你的电机额定电流远小于该值,需要首先修改电流值。57A2额定电流为4000mA,所以可以直接测试。此时步进电机不锁轴,接下来我们首先使能电机,将"MotionState"值改为1,</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="734" height="283" src="http://www.rtplc.com/wp-content/uploads/2020/05/Enable.png" alt="" class="wp-image-514" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Enable.png 734w, http://www.rtplc.com/wp-content/uploads/2020/05/Enable-300x116.png 300w" sizes="(max-width: 734px) 100vw, 734px"></figure>
<p>接下来我们进行点动测试,按照上述方法,将"MotionState"设置为3,"Ctrl + F7"写入值,此时电机将以1转/秒的速度逆时针运转。接下来将Jog_Pos设置为FALSE,Jog_Neg设置为TRUE,"Ctrl + F7"写入值,此时电机立刻改为顺时针运行。</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="791" height="355" src="http://www.rtplc.com/wp-content/uploads/2020/05/JOG.png" alt="" class="wp-image-516" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/JOG.png 791w, http://www.rtplc.com/wp-content/uploads/2020/05/JOG-300x135.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/JOG-768x345.png 768w" sizes="(max-width: 791px) 100vw, 791px"></figure>
<p>将Jog_Pos和Jog_Neg一起都设置为FALSE,此时电机停止运行。接下来我们测试相对运动。设置"MovIncDistance"变量为10000,我们每次让电机运行1转的距离,并将"MotionState"设置为4,"Ctrl + F7"写入值,此时电机并没有运行。需要将"MovEnable"设置为"TRUE",并写入时,电机将运行以1转/秒的速度逆时针运行1转,然后停止。</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="697" height="476" src="http://www.rtplc.com/wp-content/uploads/2020/05/MVINC.png" alt="" class="wp-image-517" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/MVINC.png 697w, http://www.rtplc.com/wp-content/uploads/2020/05/MVINC-300x205.png 300w" sizes="(max-width: 697px) 100vw, 697px"></figure>
<p>如果要再次运行该模块,则需要先将"MovEnable"设置为FALSE,然后再设置为"TRUE",电机才会继续运行1转。那么如果要让电机反转1转,则只需要将"MovIncDistance"设置为"-10000",重复上述动作即可。</p>
<p>经过刚刚的各种测试,此时电机的位置已经到了一个随机的位置,如下图查看:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="463" src="http://www.rtplc.com/wp-content/uploads/2020/05/Positionread-1024x463.png" alt="" class="wp-image-518" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/Positionread-1024x463.png 1024w, http://www.rtplc.com/wp-content/uploads/2020/05/Positionread-300x136.png 300w, http://www.rtplc.com/wp-content/uploads/2020/05/Positionread-768x347.png 768w, http://www.rtplc.com/wp-content/uploads/2020/05/Positionread.png 1261w" sizes="(max-width: 1024px) 100vw, 1024px"></figure>
<p>接下来我们进行绝对值定位,让电机回到"0"的位置。将"MotionState"设置为5,"MovEnable"设置为"TRUE",写入,此时电机朝着0的位置运行,经过一段时间后,电机回到0位置并停止。</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="691" height="458" src="http://www.rtplc.com/wp-content/uploads/2020/05/GoTo0.png" alt="" class="wp-image-519" srcset="http://www.rtplc.com/wp-content/uploads/2020/05/GoTo0.png 691w, http://www.rtplc.com/wp-content/uploads/2020/05/GoTo0-300x199.png 300w" sizes="(max-width: 691px) 100vw, 691px"></figure>
<p>至此,电机已经能够完成各种动作,接着测试电机的回零功能。将"MotionState"设置为7,"MovEnable"设置为"TRUE",写入即可触发电机回零。默认ECR60采用CIA40的回零模式17。电机此时以默认的回零速度寻找负限位,将COM+连接24V正极,IN4端子连接24V负极,此时电机停止然后反向低速反转,此时去除IN4的连接,则电机回零完成。本次测试的项目如下:</p>
<div class="wp-block-file"><a href="http://www.rtplc.com/wp-content/uploads/2020/05/ECR60MotionTest.zip">ECR60MotionTest</a><a href="http://www.rtplc.com/wp-content/uploads/2020/05/ECR60MotionTest.zip" class="wp-block-file__button" download="">下载</a></div>
<p></p>
<div class="wbp-cbm" wb-share-url="http://www.rtplc.com/?p=503"><a class="wb-btn-dwqr wb-btn-like j-dwqr-like-btn" data-count="11"><svg class="wb-icon wbsico-like"><use xlink:href="#wbsico-dwqr-like"></use></svg><span class="like-count">赞(11)</span></a><a class="wb-btn-dwqr wb-share-poster j-dwqr-poster-btn"><svg class="wb-icon wbsico-poster"><use xlink:href="#wbsico-dwqr-poster"></use></svg><span>微海报</span></a><a class="wb-btn-dwqr wb-btn-share j-dwqr-social-btn"><svg class="wb-icon wbsico-share"><use xlink:href="#wbsico-dwqr-share"></use></svg><span>分享</span></a></div> </div>