etherlab/CommonEtherCATFunction.py
changeset 2358 8e5a9830867e
parent 2356 c26e0c66d8d5
child 2360 2a3d022a7dac
equal deleted inserted replaced
2357:7c67286cddbe 2358:8e5a9830867e
   214         """
   214         """
   215         Set slave state to the specified one using "ethercat states -p %d %s" command.
   215         Set slave state to the specified one using "ethercat states -p %d %s" command.
   216         Command example : "ethercat states -p 0 PREOP" (target slave position and target state are given.)
   216         Command example : "ethercat states -p 0 PREOP" (target slave position and target state are given.)
   217         @param command : target slave state
   217         @param command : target slave state
   218         """
   218         """
   219         error, return_val = self.Controler.RemoteExec(SLAVE_STATE%(self.Controler.GetSlavePos(), command), return_val = None)
   219         error, return_val = self.Controler.RemoteExec(SLAVE_STATE % (self.Controler.GetSlavePos(), command), return_val = None)
   220 
   220 
   221     def GetSlaveStateFromSlave(self):
   221     def GetSlaveStateFromSlave(self):
   222         """
   222         """
   223         Get slave information using "ethercat slaves" command and store the information into internal data structure
   223         Get slave information using "ethercat slaves" command and store the information into internal data structure
   224         (self.SlaveState) for "Slave State"
   224         (self.SlaveState) for "Slave State"
   235         """
   235         """
   236         Get SDO objects information of current slave using "ethercat sdos -p %d" command.
   236         Get SDO objects information of current slave using "ethercat sdos -p %d" command.
   237         Command example : "ethercat sdos -p 0"
   237         Command example : "ethercat sdos -p 0"
   238         @return return_val : execution results of "ethercat sdos" command (need to be parsed later)
   238         @return return_val : execution results of "ethercat sdos" command (need to be parsed later)
   239         """
   239         """
   240         error, return_val = self.Controler.RemoteExec(SLAVE_SDO%(self.Controler.GetSlavePos()), return_val = None)
   240         error, return_val = self.Controler.RemoteExec(SLAVE_SDO % (self.Controler.GetSlavePos()), return_val = None)
   241         return return_val
   241         return return_val
   242 
   242 
   243     def SDODownload(self, data_type, idx, sub_idx, value):
   243     def SDODownload(self, data_type, idx, sub_idx, value):
   244         """
   244         """
   245         Set an SDO object value to user-specified value using "ethercat download" command.
   245         Set an SDO object value to user-specified value using "ethercat download" command.
   247         @param data_type : data type of SDO entry
   247         @param data_type : data type of SDO entry
   248         @param idx : index of the SDO entry
   248         @param idx : index of the SDO entry
   249         @param sub_idx : subindex of the SDO entry
   249         @param sub_idx : subindex of the SDO entry
   250         @param value : value of SDO entry
   250         @param value : value of SDO entry
   251         """
   251         """
   252         error, return_val = self.Controler.RemoteExec(SDO_DOWNLOAD%(data_type, self.Controler.GetSlavePos(), idx, sub_idx, value), return_val = None)
   252         error, return_val = self.Controler.RemoteExec(SDO_DOWNLOAD % (data_type, self.Controler.GetSlavePos(), idx, sub_idx, value), return_val = None)
   253 
   253 
   254     def BackupSDODataSet(self):
   254     def BackupSDODataSet(self):
   255         """
   255         """
   256         Back-up current SDO entry information to restore the SDO data
   256         Back-up current SDO entry information to restore the SDO data
   257          in case that the user cancels SDO update operation.
   257          in case that the user cancels SDO update operation.
   486 
   486 
   487             # get protocol (profile) types supported by mailbox; <Device>-<Mailbox>
   487             # get protocol (profile) types supported by mailbox; <Device>-<Mailbox>
   488             mb = device.getMailbox()
   488             mb = device.getMailbox()
   489             if mb is not None:
   489             if mb is not None:
   490                 for mailbox_protocol in mailbox_protocols:
   490                 for mailbox_protocol in mailbox_protocols:
   491                     if getattr(mb,"get%s"%mailbox_protocol)() is not None:
   491                     if getattr(mb,"get%s" % mailbox_protocol)() is not None:
   492                         smartview_infos["supported_mailbox"] += "%s,  "%mailbox_protocol
   492                         smartview_infos["supported_mailbox"] += "%s,  " % mailbox_protocol
   493             smartview_infos["supported_mailbox"] = smartview_infos["supported_mailbox"].strip(",  ")
   493             smartview_infos["supported_mailbox"] = smartview_infos["supported_mailbox"].strip(",  ")
   494 
   494 
   495             # get standard configuration of mailbox; <Device>-<Sm>
   495             # get standard configuration of mailbox; <Device>-<Sm>
   496             for sm_element in device.getSm():
   496             for sm_element in device.getSm():
   497                 if sm_element.getcontent() == "MBoxOut":
   497                 if sm_element.getcontent() == "MBoxOut":
   552         """
   552         """
   553         Get slave EEPROM contents maintained by master device using "ethercat sii_read -p %d" command.
   553         Get slave EEPROM contents maintained by master device using "ethercat sii_read -p %d" command.
   554         Command example : "ethercat sii_read -p 0"
   554         Command example : "ethercat sii_read -p 0"
   555         @return return_val : result of "ethercat sii_read" (binary data)
   555         @return return_val : result of "ethercat sii_read" (binary data)
   556         """
   556         """
   557         error, return_val = self.Controler.RemoteExec(SII_READ%(self.Controler.GetSlavePos()), return_val = None)
   557         error, return_val = self.Controler.RemoteExec(SII_READ % (self.Controler.GetSlavePos()), return_val = None)
   558         self.SiiData = return_val
   558         self.SiiData = return_val
   559         return return_val
   559         return return_val
   560 
   560 
   561     def SiiWrite(self, binary):
   561     def SiiWrite(self, binary):
   562         """
   562         """
   563         Overwrite slave EEPROM contents using "ethercat sii_write -p %d" command.
   563         Overwrite slave EEPROM contents using "ethercat sii_write -p %d" command.
   564         Command example : "ethercat sii_write -p 0 - (binary contents)"
   564         Command example : "ethercat sii_write -p 0 - (binary contents)"
   565         @param binary : EEPROM contents in binary data format
   565         @param binary : EEPROM contents in binary data format
   566         @return return_val : result of "ethercat sii_write" (If it succeeds, the return value is NULL.)
   566         @return return_val : result of "ethercat sii_write" (If it succeeds, the return value is NULL.)
   567         """
   567         """
   568         error, return_val = self.Controler.RemoteExec(SII_WRITE%(self.Controler.GetSlavePos()), return_val = None, sii_data = binary)
   568         error, return_val = self.Controler.RemoteExec(SII_WRITE % (self.Controler.GetSlavePos()), return_val = None, sii_data = binary)
   569         return return_val
   569         return return_val
   570 
   570 
   571     def LoadData(self):
   571     def LoadData(self):
   572         """
   572         """
   573         Loading data from EEPROM use Sii_Read Method
   573         Loading data from EEPROM use Sii_Read Method
   781             # get supported mailbox protocols for EEPROM offset 0x0038-0x0039;
   781             # get supported mailbox protocols for EEPROM offset 0x0038-0x0039;
   782             data = 0
   782             data = 0
   783             mb = device.getMailbox()
   783             mb = device.getMailbox()
   784             if mb is not None :
   784             if mb is not None :
   785                 for bit,mbprot in enumerate(mailbox_protocols):
   785                 for bit,mbprot in enumerate(mailbox_protocols):
   786                     if getattr(mb,"get%s"%mbprot)() is not None:
   786                     if getattr(mb,"get%s" % mbprot)() is not None:
   787                         data += 1<<bit
   787                         data += 1<<bit
   788             data = "{:0>4x}".format(data)
   788             data = "{:0>4x}".format(data)
   789             eeprom.append(data[2:4])
   789             eeprom.append(data[2:4])
   790             eeprom.append(data[0:2])
   790             eeprom.append(data[0:2])
   791 
   791 
  1117         #  category header
  1117         #  category header
  1118         eeprom.append("0a")
  1118         eeprom.append("0a")
  1119         eeprom.append("00")
  1119         eeprom.append("00")
  1120         #  category length (word); 1 word is 4 bytes. "+2" is the length of string's total number
  1120         #  category length (word); 1 word is 4 bytes. "+2" is the length of string's total number
  1121         length = len(vendor_specific_data + dc_related_elements + input_elements + output_elements) + 2
  1121         length = len(vendor_specific_data + dc_related_elements + input_elements + output_elements) + 2
  1122         if length%4 == 0:
  1122         if length % 4 == 0:
  1123             pass
  1123             pass
  1124         else:
  1124         else:
  1125             length +=length%4
  1125             length +=length % 4
  1126             padflag = True
  1126             padflag = True
  1127         eeprom.append("{:0>4x}".format(length/4)[2:4])
  1127         eeprom.append("{:0>4x}".format(length/4)[2:4])
  1128         eeprom.append("{:0>4x}".format(length/4)[0:2])
  1128         eeprom.append("{:0>4x}".format(length/4)[0:2])
  1129         #  total numbers of strings
  1129         #  total numbers of strings
  1130         eeprom.append("{:0>2x}".format(count))
  1130         eeprom.append("{:0>2x}".format(count))
  1178         if mb is not None :
  1178         if mb is not None :
  1179             coe = mb.getCoE()
  1179             coe = mb.getCoE()
  1180             if coe is not None:
  1180             if coe is not None:
  1181                 for bit,flag in enumerate(["SdoInfo", "PdoAssign", "PdoConfig",
  1181                 for bit,flag in enumerate(["SdoInfo", "PdoAssign", "PdoConfig",
  1182                                            "PdoUpload", "CompleteAccess"]):
  1182                                            "PdoUpload", "CompleteAccess"]):
  1183                     if getattr(coe,"get%s"%flag)() is not None:
  1183                     if getattr(coe,"get%s" % flag)() is not None:
  1184                         coe_details += 1<<bit
  1184                         coe_details += 1<<bit
  1185         eeprom.append("{:0>2x}".format(coe_details))
  1185         eeprom.append("{:0>2x}".format(coe_details))
  1186 
  1186 
  1187         # word 4 : FoE Details and EoE Details
  1187         # word 4 : FoE Details and EoE Details
  1188         #  FoE Details; <EtherCATInfo>-<Descriptions>-<Devices>-<Device>-<Mailbox>-<FoE>
  1188         #  FoE Details; <EtherCATInfo>-<Descriptions>-<Devices>-<Device>-<Mailbox>-<FoE>
  1274         if data is not "":
  1274         if data is not "":
  1275             #  category header
  1275             #  category header
  1276             eeprom.append("28")
  1276             eeprom.append("28")
  1277             eeprom.append("00")
  1277             eeprom.append("00")
  1278             #  category length
  1278             #  category length
  1279             if count%2 == 1:
  1279             if count % 2 == 1:
  1280                 padflag = True
  1280                 padflag = True
  1281                 eeprom.append("{:0>4x}".format((count+1)/2)[2:4])
  1281                 eeprom.append("{:0>4x}".format((count+1)/2)[2:4])
  1282                 eeprom.append("{:0>4x}".format((count+1)/2)[0:2])
  1282                 eeprom.append("{:0>4x}".format((count+1)/2)[0:2])
  1283             else:
  1283             else:
  1284                 eeprom.append("{:0>4x}".format((count)/2)[2:4])
  1284                 eeprom.append("{:0>4x}".format((count)/2)[2:4])
  1348         count = 0
  1348         count = 0
  1349         en_fixed = False
  1349         en_fixed = False
  1350         en_mandatory = False
  1350         en_mandatory = False
  1351         en_virtual = False
  1351         en_virtual = False
  1352 
  1352 
  1353         for element in eval("device.get%s()"%pdotype):
  1353         for element in eval("device.get%s()" % pdotype):
  1354             #  PDO Index
  1354             #  PDO Index
  1355             data += "{:0>4x}".format(ExtractHexDecValue(element.getIndex().getcontent()))[2:4]
  1355             data += "{:0>4x}".format(ExtractHexDecValue(element.getIndex().getcontent()))[2:4]
  1356             data += "{:0>4x}".format(ExtractHexDecValue(element.getIndex().getcontent()))[0:2]
  1356             data += "{:0>4x}".format(ExtractHexDecValue(element.getIndex().getcontent()))[0:2]
  1357             #  Number of Entries
  1357             #  Number of Entries
  1358             data += "{:0>2x}".format(len(element.getEntry()))
  1358             data += "{:0>2x}".format(len(element.getEntry()))
  1528         Command example : "ethercat reg_read -p 0 0x0c00 0x0400"
  1528         Command example : "ethercat reg_read -p 0 0x0c00 0x0400"
  1529         @param offset : register address
  1529         @param offset : register address
  1530         @param length : register length
  1530         @param length : register length
  1531         @return return_val : register data
  1531         @return return_val : register data
  1532         """
  1532         """
  1533         error, return_val = self.Controler.RemoteExec(REG_READ%(self.Controler.GetSlavePos(), offset, length), return_val = None)
  1533         error, return_val = self.Controler.RemoteExec(REG_READ % (self.Controler.GetSlavePos(), offset, length), return_val = None)
  1534         return return_val
  1534         return return_val
  1535 
  1535 
  1536     def RegWrite(self, address, data):
  1536     def RegWrite(self, address, data):
  1537         """
  1537         """
  1538         Write data to slave ESC register using "ethercat reg_write -p %d %s %s" command.
  1538         Write data to slave ESC register using "ethercat reg_write -p %d %s %s" command.
  1539         Command example : "ethercat reg_write -p 0 0x0c04 0x0001"
  1539         Command example : "ethercat reg_write -p 0 0x0c04 0x0001"
  1540         @param address : register address
  1540         @param address : register address
  1541         @param data : data to write
  1541         @param data : data to write
  1542         @return return_val : the execution result of "ethercat reg_write" (for error check)
  1542         @return return_val : the execution result of "ethercat reg_write" (for error check)
  1543         """
  1543         """
  1544         error, return_val = self.Controler.RemoteExec(REG_WRITE%(self.Controler.GetSlavePos(), address, data), return_val = None)
  1544         error, return_val = self.Controler.RemoteExec(REG_WRITE % (self.Controler.GetSlavePos(), address, data), return_val = None)
  1545         return return_val
  1545         return return_val
  1546 
  1546 
  1547     def Rescan(self):
  1547     def Rescan(self):
  1548         """
  1548         """
  1549         Synchronize EEPROM data in master controller with the data in slave device after EEPROM write.
  1549         Synchronize EEPROM data in master controller with the data in slave device after EEPROM write.
  1550         Command example : "ethercat rescan -p 0"
  1550         Command example : "ethercat rescan -p 0"
  1551         """
  1551         """
  1552         error, return_val = self.Controler.RemoteExec(RESCAN%(self.Controler.GetSlavePos()), return_val = None)
  1552         error, return_val = self.Controler.RemoteExec(RESCAN % (self.Controler.GetSlavePos()), return_val = None)
  1553 
  1553 
  1554     # -------------------------------------------------------------------------------
  1554     # -------------------------------------------------------------------------------
  1555     #                        Common Use Methods
  1555     #                        Common Use Methods
  1556     # -------------------------------------------------------------------------------
  1556     # -------------------------------------------------------------------------------
  1557     def CheckConnect(self, cyclic_flag):
  1557     def CheckConnect(self, cyclic_flag):
  1561         @return True or False
  1561         @return True or False
  1562         """
  1562         """
  1563         if self.Controler.GetCTRoot()._connector is not None:
  1563         if self.Controler.GetCTRoot()._connector is not None:
  1564             # Check connection between the master and the slave.
  1564             # Check connection between the master and the slave.
  1565             # Command example : "ethercat xml -p 0"
  1565             # Command example : "ethercat xml -p 0"
  1566             error, return_val = self.Controler.RemoteExec(SLAVE_XML%(self.Controler.GetSlavePos()), return_val = None)
  1566             error, return_val = self.Controler.RemoteExec(SLAVE_XML % (self.Controler.GetSlavePos()), return_val = None)
  1567             number_of_lines = return_val.split("\n")
  1567             number_of_lines = return_val.split("\n")
  1568             if len(number_of_lines) <= 2 :  # No slave connected to the master controller
  1568             if len(number_of_lines) <= 2 :  # No slave connected to the master controller
  1569                 if not cyclic_flag :
  1569                 if not cyclic_flag :
  1570                     self.CreateErrorDialog('No connected slaves')
  1570                     self.CreateErrorDialog('No connected slaves')
  1571                 return False
  1571                 return False