EtherCAT入门教程.docx
《EtherCAT入门教程.docx》由会员分享,可在线阅读,更多相关《EtherCAT入门教程.docx(8页珍藏版)》请在冰豆网上搜索。
EtherCAT入门教程
EtherCAT入门教程
本文翻译了kithara官方文档,也加入了一些理解,暂时做得还不够完美,后续补充修正。
2014.06.06
EtherCAT
ThistutorialdescribesthefollowingtopicsusingtheEtherCATAPI
本向导描述了使用EtherCATAPI的功能说明CreatingaEtherCATmasterassignedtoanetworkadapterCreatingslavesassignedtothemasterReal-timeprocessdataexchangewithdatasetsandassignedPDOsReal-timeup/downloadofservicedataobjects(SDO)Real-timemailboxcommunicationStatecontrolformasterandslavesGettinginformationformaster,slaves,dataobjects,variableobjects中文说明创建EtherCAT主站使用的网卡创建连接主站的从站使用dataset和PDO进行实时过程数据交互Masterrelatedfunctions----主站相关功能
Openinganetworkadapter----打开网卡
Beforewecancreateamaster,itisnecessarytoopenanetworkadapterfirst.ThisisdonebyusingthefunctionKS_openAdapter.Asparametersanadaptername,areceiveandasendpoolsizeisneeded.Theadapternameisusuallythenameofanetworkdevice.ThefunctionKS_enumDevicesenumeratesalldevices.ToonlygetnetworkdevicestheparameterdeviceTypeissetto“NET”.
在创建主站之前,必须打开网卡。
使用KS_openAdapter函数打开网卡,需要的参数包括网卡名称,收发数据池大小等。
可以使用KS_enumDevices枚举设备获取网卡名称,使用该函数时参数deviceType应该设置为“NET”
MindthatthefunctionKS_openAdaptermustbecalledwiththeflagKSF_REALTIME_EXEC.
使用KS_openAdapter时需要将标志位flag设置为KSF_REALTIME_EXEC,表明网卡运行在实时模式。
ksError=KS_enumDevices(//枚举设备
"NET",//Searchesfornetworkdevices
deviceIndex,//Count,startingwithzero
pDeviceName,//Bufferfordevicename
0);//Flags
if(ksError)
//...ksError=KS_openAdapter(//打开网卡
&hAdapter,//Adapterhandlepointer
pDeviceName,//HardwareIDoftheController
256,//ReceivePoolLength
256,//SendPoolLength
KSF_REALTIME_EXEC);//Flags
if(ksError)
//...ForfurtherdetailsseeHowtolookupfordevices.
Creatingamaster----创建主站
TocreateamasterthefunctionKS_createEcatMastercanbeused.AsparametersanetworkadapterhandleandapathtodevicedescriptionXMLfilesareneeded.Anameofatopologyfile(*.ktp)isoptional.ThedevicedescriptionXMLfilescanbefoundwithadeveloperinstallationintheXMLdirectoryorcanbedownloadedfromtheslavemanufacturerwebsite.AtopologyfilecanbecreatedwiththeKithara?
MasterMonitor?
.
使用KS_createEcatMaster创建主站,需要传入网卡句柄参数和从站XML设备描述文件路径,也可以加入一个拓扑文件ktp参数。
设备描述文件可以从设备制造商的网站上获取。
拓扑文件可以用MasterMonitor生成。
TheKithara?
MasterMonitor?
isatoolfore.g.configuration,diagnosticsandsavingXMLconfigurationfilesforgraphicallyviewingslaves,objectsandvariablesorcomparingagiventopologywithatargettopology.
MasterMonitor是一个用于配置、调试和保存xml配置文件的图形化工具,可以查看从站、对象、变量和对比拓扑文件。
Closingamaster----关闭主站
TocloseamasterandfreeallitsresourcesthefunctionKS_closeEcatMastermustbeused.
使用KS_closeEcatMaster关闭从站
Statechangeforamaster----主站的状态切换
Toqueryandchangethemaster'sstatethefunctionsKS_queryEcatMasterStateandKS_changeEcatStatecanbeused.
用KS_queryEcatMasterState查询主站状态。
用KS_changeEcatState改变主站状态。
ThefunctionKS_queryEcatMasterStateneedsamasterhandle,apointertothestructureKSEcatMasterStateandflags.BesuretoinitializethestructurememberstructSizetotheactualsizeofthestructureKSEcatMasterStatebeforecallingthefunction.
ThefunctionKS_changeEcatStateneedsamasterhandle,therequestedstateandflagsasparameters.Thestateofallassignedslaveswhichareonlineischanged.SlavesareassignedusingthefunctionKS_createEcatSlaveorKS_createEcatSlaveIndirect.
NearlyeveryEtherCATAPIfunctionhasspecialrequirementsregardingthestate(seetheAPIdocumentationofthespecificfunction).ForfurtherinformationaboutthestatemachineseeTheEtherCATstatemachine.
几乎每一个EtherCAT的API函数都需要主站在特定的状态才能使用。
Slaverelatedfunctions----从站相关函数
Enumeratingallonlineslaves----枚举所有在线从站
Afteramasterwascreated,allonlineslavescanbeenumeratedusingthefunctionKS_enumEcatSlaves.Asparametersamasterhandle,aenumerationindexandapointertothestructureKSEcatSlaveStateareneeded.BesuretoinitializethestructurememberstructSizetotheactualsizeofthestructureKSEcatSlaveStatebeforecallingthefunction.
主站创建之后,则可以使用KS_enumEcatSlaves枚举所有从站.需要的参数包括主站句柄、枚举顺序、一个KSEcatSlaveState结构体的指针。
在使用前应当初始化结构体的成员变量structSize大小。
Iftheenumerationindexisgreaterthanthenumberofonlineslaves,theerrorcodeKSERROR_DEVICE_NOT_FOUNDisreturned.Otherwise,apointertothestructureKSEcatSlaveStateisreturnedwhichcontainsinformationabouttheslave'sstate.
UsingthefunctionKS_enumEcatSlavestheslavesareenumeratedbytheirabsoluteposition.
Creatingaslave----创建从站
Tocreateaslave,oneofthefunctionsKS_createEcatSlaveIndirectorKS_createEcatSlavecanbeused.ThefunctionKS_createEcatSlaveIndirectmakesuseofthestructureKSEcatSlaveStatewhichcanbeobtainedusingthefunctionKS_enumEcatSlaves.
ThefunctionKS_createEcatSlaveneedsadditionalparameterslikeslaveid,position,vendoridandrevisionnumber.Withtheparameterslaveidarelativepositioncanbeprovided,too.
Theparametersareuniquetoaslavetype.Thisinformationcanbefoundintheslave'smanual,usingthemethodabovewiththestructureKSEcatSlaveStateorcanbedisplayedwiththeKithara?
MasterMonitor?
.
IfaslaveisalreadyassignedandthefunctionKS_createEcatSlaveiscalledwiththesamevendorID,productID,revisionnumberandposition,anerrorKSERROR_DEVICE_ALREADY_USEDisreturnedtogetherwithaslavehandletothealreadyassignedslave.
Deletingaslave----删除从站
AslavecanbedeletedusingthefunctionKS_deleteEcatSlave.
Gettinginformationfromslaves----获取从站信息
ToqueryinformationfromaslavethefunctionKS_queryEcatSlaveInfocanbeused.ItdeliversapointertothestructureKSEcatSlaveInfo.Ifinformationaboutprocessdataobjects(PDO),servicedataobjects(SDO)orbothareneeded,theflagsKSF_SDOandKSF_PDOcanbepassedtothefunction.Thefunctionistimeconsuming(upto30seconds)andthestatePREOPorhigherisneededforonlineslaves.
WithinthestructureKSEcatSlaveInfothereisanarrayofKSEcatDataObjInfoandwithinthisstructurethereisanarrayofKSEcatDataVarInfo.
IfusinglanguageslikeC#thesestructuredcannotbeaccessed.Alternatively,thefunctionsKS_queryEcatDataObjInfo,KS_queryEcatDataVarInfo,KS_enumEcatDataObjInfoandKS_enumEcatDataVarInfocanbeusedinstead.
Statechangesforslaves----从站状态机改变,与主站类似
Toqueryandchangetheslave'sstatethefunctionsKS_queryEcatSlaveStateandKS_changeEcatStatecanbeused.
查询和改变从站状态可以用以上两个函数,改变状态的函数与改变主站的相同
ThefunctionKS_queryEcatSlaveStateneedsaslavehandle,apointertothestructureKSEcatSlaveStateandflags.BesuretoinitializethestructurememberstructSizetotheactualsizeofthestructureKSEcatSlaveStatebeforecallingthefunction.
查询从站状态函数,需要一个从站句柄、指向KSEcatSlaveState结构体的指针和标志位。
使用前请初始化结构体内部的structSize变量。
ForfurtherinformationconcerningthestatemachineseeTheEtherCATstatemachine.
更多的信息可以查看此章节
Otheroperationsonslaves----从站的其他操作
UsingthefunctionKS_writeEcatSlaveIditispossibletowriteauniqueIDtoanyslave.TheIDiswrittentonon-volatilememoryandwillbepersistent.WithinaconnectedrealtopologytheIDsmustbeunique.MindthattheslavemustbeinINITstate.
使用KS_writeEcatSlaveId函数可以给从站写入唯一的ID。
ID必须是唯一的,注意此时从站的状态必须是INIT
Datasetrelatedfunctions----Dataset相关函数
Adatasetisacollectionofsyncmanagers,asyncmanagercancontainseveralPDOs(processdataobjects)andaPDOcanbemappedtomultipleSDO(servicedataobject)variables.Asyncmanagerisaslaveinternalmechanismtokeepdataconsistent.
dataset是同步管理器的一个数据收集,可以包含多个PDO,一个PDO可以映射多个SDO。
Itisnecessarytocreateatleastonedatasettoestablishtheprocessdataexchange.Dataiscyclicallytransferredbetweenthemasterandtheslaves.
建立过程数据通信至少需要一个dataset,数据是在主站和从站之间循环传送的。
Creatingandassigningadataset----创建和分配一个dataset
TopreparetheprocessdataexchangeadatasetcanbecreatedwiththefunctionKS_createEcatDataSet.Asparametersamasterhandleisneeded.Optionalparametersareanapplicationandasystempointertoaccesssharedmemoryofthedatasetdirectly.Iftheprogramwascalledfromring3thentheapplicationpointercanbeusedandonring0thesystempointer.
Beforetheprocessdataexchangestarts,aPDOshouldbeassignedusingthefunctionKS_assignEcatDataSet.Asparametersadatasethandle,aslavehandle,anindexofasyncmanagerandaplacementisneeded.
在数据交互之前,应该分配PDO给sync管理器,使用KS_assignEcatDataSet。
需要输入dataset句柄,从站句柄,sync管理器序号
Aslavecanhaveseveralsyncmanagers.Therearereadable/writeablesyncmanagersandsyncmanagersforthemailbox(ifexisting).AsyncmanagercontainsoneoremorePDOs.Itispossibletoselectaspecificsyncmanagerwiththeindexparameterorselectgroupsofsyncmanagers(e.g.withKS_ECAT_SYNC_ALL,KS_ECAT_SYNC_INPUT).
一个从站可以有多个sync管理器。
有可读写sync和邮箱式sync。
一个sync拥有一个或多个PDO。
通过index参数,可以选择一个或一组特定的sync管理器
SeveralobjectscanbeassignedtoadatasetwhilenoKSERROR_NOT_ENOUGH_MEMORYisreturned.Perdefaultadatasethasasizeof4KBandcanhaveupto64KB.MindthattheassignedslavemustbeinstatePREOP.Withtheparameterplacementanoffsetcanbespecifiedwhileassigningseveralobjects.
一个dataset可以被分配多个对象,默认的dataset大小为4kb,最大为64kb。
操作时的从站必须位于PREOP状态。
placement参数可以指定一个对象在pdo中的位置。
Itisfurtherpossibletocreatemorethanonedataset.WithmultipledatasetseachonecanbeupdatedwithdifferentfrequenciesorgroupPDOswithmultipledatasetstologicalunits.
也可以创建多个dataset。
多个dataset中,每一个dataset可以被更新不同频率和不同组PDO更新。
Deletingadataset----删除dataset
TodeleteadatasetandfreeallitsresourcesthefunctionKS_deleteEcatDataSetmustbeused.
Postingandreadingprocessdata----发送和读取pdo数据
TopostandreadprocessdatathefunctionsKS_postEcatDataSetandKS_readEcatDataSetcanbeused.
Theprocess