plugins/canfestival/config_utils.py
author greg
Thu, 13 Mar 2008 10:24:58 +0100
changeset 134 517db40c30b7
parent 80 d7eb6e22b02e
child 150 204d515df3dd
permissions -rw-r--r--
docutils is now used for displaying pdf and html
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     3
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     6
#
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     8
#
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    10
#
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    15
#
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    19
#General Public License for more details.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    20
#
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    24
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    25
from types import *
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    26
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    27
# Translation between IEC types and Can Open types
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    28
IECToCOType = {"BOOL":0x01, "SINT":0x02, "INT":0x03,"DINT":0x04,"LINT":0x10,
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    29
               "USINT":0x05,"UINT":0x06,"UDINT":0x07,"ULINT":0x1B,"REAL":0x08,
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    30
               "LREAL":0x11,"STRING":0x09,"BYTE":0x05,"WORD":0x06,"DWORD":0x07,
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    31
               "LWORD":0x1B,"WSTRING":0x0B}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    32
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    33
# Constants for PDO types 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    34
RPDO = 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    35
TPDO = 2
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    36
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    37
SlavePDOType = {"I" : TPDO, "Q" : RPDO}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    38
InvertPDOType = {RPDO : TPDO, TPDO : RPDO}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    39
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    40
VariableIncrement = 0x100
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    41
VariableStartIndex = {TPDO : 0x2000, RPDO : 0x4000}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    42
VariableDirText = {TPDO : "__I", RPDO : "__Q"}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    43
VariableTypeOffset = dict(zip(["","X","B","W","D","L"], range(6)))
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    44
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    45
TrashVariables = [(1, 0x01), (8, 0x05), (16, 0x06), (32, 0x07), (64, 0x1B)]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    46
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    47
def LE_to_BE(value, size):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    48
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    49
    Convert Little Endian to Big Endian
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    50
    @param value: value expressed in integer
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    51
    @param size: number of bytes generated
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    52
    @return: a string containing the value converted
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    53
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    54
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    55
    data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    56
    list_car = [data[i:i+2] for i in xrange(0, len(data), 2)]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    57
    list_car.reverse()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    58
    return "".join([chr(int(car, 16)) for car in list_car])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    59
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    60
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    61
def GetNodePDOIndexes(node, type, parameters = False):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    62
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    63
    Find the PDO indexes of a node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    64
    @param node: node 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    65
    @param type: type of PDO searched (RPDO or TPDO or both)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    66
    @param parameters: indicate which indexes are expected (PDO paramaters : True or PDO mappings : False)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    67
    @return: a list of indexes found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    68
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    69
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    70
    indexes = []
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    71
    if type & RPDO:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    72
        indexes.extend([idx for idx in node.GetIndexes() if 0x1400 <= idx <= 0x15FF])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    73
    if type & TPDO:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    74
        indexes.extend([idx for idx in node.GetIndexes() if 0x1800 <= idx <= 0x19FF])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    75
    if not parameters:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    76
        return [idx + 0x200 for idx in indexes]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    77
    else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    78
        return indexes
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    79
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    80
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    81
def SearchNodePDOMapping(loc_infos, node):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    82
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    83
    Find the PDO indexes of a node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    84
    @param node: node 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    85
    @param type: type of PDO searched (RPDO or TPDO or both)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    86
    @param parameters: indicate which indexes are expected (PDO paramaters : True or PDO mappings : False)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    87
    @return: a list of indexes found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    88
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    89
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    90
    typeinfos = node.GetEntryInfos(loc_infos["type"])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    91
    model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + typeinfos["size"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    92
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    93
    for PDOidx in GetNodePDOIndexes(node, loc_infos["pdotype"]):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    94
        values = node.GetEntry(PDOidx)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    95
        if values != None:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    96
            for subindex, mapping in enumerate(values):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    97
                if subindex != 0 and mapping == model:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    98
                    return PDOidx, subindex
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    99
    return None
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   100
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   101
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   102
def GeneratePDOMappingDCF(idx, cobid, transmittype, pdomapping):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   103
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   104
    Build concise DCF value for configuring a PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   105
    @param idx: index of PDO parameters
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   106
    @param cobid: PDO generated COB ID
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   107
    @param transmittype : PDO transmit type
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   108
    @param pdomapping: list of PDO mappings
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   109
    @return: a tuple of value and number of parameters to add to DCF 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   110
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   111
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   112
    # Create entry for RPDO or TPDO parameters and Disable PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   113
    dcfdata = LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE((0x80000000 + cobid), 4)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   114
    # Set Transmit type synchrone
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   115
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(transmittype, 1)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   116
    # Re-Enable PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   117
    #         ---- INDEX -----   --- SUBINDEX ----   ----- SIZE ------   ------ DATA ------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   118
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x00000000 + cobid, 4)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   119
    nbparams = 3
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   120
    # Map Variables
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   121
    for subindex, (name, loc_infos) in enumerate(pdomapping):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   122
        value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   123
        dcfdata += LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(0x04, 4) + LE_to_BE(value, 4)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   124
        nbparams += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   125
    return dcfdata, nbparams
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   126
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   127
class ConciseDCFGenerator:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   128
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   129
    def __init__(self, nodelist, nodename):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   130
        # Dictionary of location informations classed by name
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   131
        self.IECLocations = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   132
        # Dictionary of location that have not been mapped yet
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   133
        self.LocationsNotMapped = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   134
        # Dictionary of location informations classed by name
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   135
        self.MasterMapping = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   136
        # List of COB IDs available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   137
        self.ListCobIDAvailable = range(0x180, 0x580)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   138
        self.SlavesPdoNumber = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   139
        # Dictionary of mapping value where unexpected variables are stored
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   140
        self.TrashVariables = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   141
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   142
        self.NodeList = nodelist
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   143
        self.Manager = self.NodeList.Manager
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   144
        self.MasterNode = self.Manager.GetCurrentNodeCopy()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   145
        self.MasterNode.SetNodeName(nodename)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   146
        self.PrepareMasterNode()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   147
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   148
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   149
    def RemoveUsedNodeCobId(self, node):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   150
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   151
        Remove all PDO COB ID used by the given node from the list of available COB ID
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   152
        @param node: node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   153
        @return: a tuple of number of RPDO and TPDO for the node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   154
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   155
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   156
        # Get list of all node TPDO and RPDO indexes
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   157
        nodeRpdoIndexes = GetNodePDOIndexes(node, RPDO, True)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   158
        nodeTpdoIndexes = GetNodePDOIndexes(node, TPDO, True)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   159
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   160
        # Mark all the COB ID of the node already mapped PDO as not available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   161
        for PdoIdx in nodeRpdoIndexes + nodeTpdoIndexes:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   162
            pdo_cobid = node.GetEntry(PdoIdx, 0x01)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   163
            # Extract COB ID, if PDO isn't active
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   164
            if pdo_cobid > 0x600 :
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   165
                pdo_cobid -= 0x80000000
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   166
            # Remove COB ID from the list of available COB ID
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   167
            if pdo_cobid in self.ListCobIDAvailable:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   168
                self.ListCobIDAvailable.remove(pdo_cobid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   169
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   170
        return len(nodeRpdoIndexes), len(nodeTpdoIndexes)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   171
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   172
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   173
    def PrepareMasterNode(self):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   174
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   175
        Add mandatory entries for DCF generation into MasterNode.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   176
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   177
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   178
        # Adding DCF entry into Master node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   179
        if not self.MasterNode.IsEntry(0x1F22):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   180
            self.MasterNode.AddEntry(0x1F22, 1, "")
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   181
        self.Manager.AddSubentriesToCurrent(0x1F22, 127, self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   182
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   183
        # Adding trash mappable variables for unused mapped datas
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   184
        idxTrashVariables = 0x2000 + self.MasterNode.GetNodeID()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   185
        # Add an entry for storing unexpected all variable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   186
        self.Manager.AddMapVariableToCurrent(idxTrashVariables, self.MasterNode.GetNodeName()+"_trashvariables", 3, len(TrashVariables), self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   187
        for subidx, (size, typeidx) in enumerate(TrashVariables):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   188
            # Add a subentry for storing unexpected variable of this size
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   189
            self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, "TRASH%d" % size, "name", None, self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   190
            self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None, self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   191
            # Store the mapping value for this entry
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   192
            self.TrashVariables[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   193
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   194
        RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   195
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   196
        # Store the indexes of the first RPDO and TPDO available for MasterNode
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   197
        self.CurrentPDOParamsIdx = {RPDO : 0x1400 + RPDOnumber, TPDO : 0x1800 + TPDOnumber}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   198
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   199
        # Prepare MasterNode with all nodelist slaves
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   200
        for idx, (nodeid, nodeinfos) in enumerate(self.NodeList.SlaveNodes.items()):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   201
            node = nodeinfos["Node"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   202
            node.SetNodeID(nodeid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   203
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   204
            RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(node)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   205
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   206
            # Store the number of TPDO and RPDO for this node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   207
            self.SlavesPdoNumber[nodeid] = {RPDO : RPDOnumber, TPDO : TPDOnumber}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   208
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   209
            # Get Slave's default SDO server parameters
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   210
            RSDO_cobid = node.GetEntry(0x1200,0x01)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   211
            if not RSDO_cobid:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   212
                RSDO_cobid = 0x600 + nodeid
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   213
            TSDO_cobid = node.GetEntry(0x1200,0x02)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   214
            if not TSDO_cobid:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   215
                TSDO_cobid = 0x580 + nodeid
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   216
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   217
            # Configure Master's SDO parameters entries
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   218
            self.Manager.ManageEntriesOfCurrent([0x1280 + idx], [], self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   219
            self.MasterNode.SetEntry(0x1280 + idx, 0x01, RSDO_cobid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   220
            self.MasterNode.SetEntry(0x1280 + idx, 0x02, TSDO_cobid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   221
            self.MasterNode.SetEntry(0x1280 + idx, 0x03, nodeid)        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   222
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   223
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   224
    def GetMasterNode(self):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   225
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   226
        Return MasterNode.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   227
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   228
        return self.MasterNode
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   229
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   230
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   231
    def GetNewCobID(self, nodeid, type):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   232
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   233
        Select a COB ID from the list of those available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   234
        @param nodeid: id of the slave (int)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   235
        @param type: type of PDO (RPDO or TPDO)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   236
        @return: a tuple of the COD ID and PDO index or None
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   237
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   238
        # Verify that there is still some cobid available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   239
        if len(self.ListCobIDAvailable) == 0:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   240
            return None
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   241
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   242
        # Get the number of PDO of the type given for the node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   243
        nbSlavePDO = self.SlavesPdoNumber[nodeid][type]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   244
        if type == RPDO:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   245
            if nbSlavePDO < 4:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   246
                # For the four first RPDO -> cobid = 0x200 + ( numPdo parameters * 0x100) + nodeid
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   247
                newcobid = (0x200 + nbSlavePDO * 0x100 + nodeid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   248
                # Return calculated cobid if it's still available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   249
                if newcobid in self.ListCobIDAvailable:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   250
                    self.ListCobIDAvailable.remove(newcobid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   251
                    return newcobid, 0x1400 + nbSlavePDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   252
            # Return the first cobid available if no cobid found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   253
            return self.ListCobIDAvailable.pop(0), 0x1400 + nbSlavePDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   254
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   255
        elif type == TPDO:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   256
            if nbSlavePDO < 4:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   257
                # For the four first TPDO -> cobid = 0x180 + ( numPdo parameters * 0x100) + nodeid
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   258
                newcobid = (0x180 + nbSlavePDO * 0x100 + nodeid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   259
                # Return calculated cobid if it's still available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   260
                if newcobid in self.ListCobIDAvailable:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   261
                    self.ListCobIDAvailable.remove(newcobid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   262
                    return newcobid, 0x1800 + nbSlavePDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   263
            # Return the first cobid available if no cobid found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   264
            return self.ListCobIDAvailable.pop(0), 0x1800 + nbSlavePDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   265
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   266
        return None
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   267
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   268
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   269
    def AddParamsToDCF(self, nodeid, data, nbparams):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   270
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   271
        Select a COB ID from the list of those available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   272
        @param nodeid: id of the slave (int)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   273
        @param data: data to add to slave DCF (string)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   274
        @param nbparams: number of params added to slave DCF (int)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   275
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   276
        # Get current DCF for slave
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   277
        nodeDCF = self.MasterNode.GetEntry(0x1F22, nodeid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   278
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   279
        # Extract data and number of params in current DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   280
        if nodeDCF != None and nodeDCF != '':
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   281
            tmpnbparams = [i for i in nodeDCF[:4]]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   282
            tmpnbparams.reverse()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   283
            nbparams += int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   284
            data = nodeDCF[4:] + data
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   285
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   286
        # Build new DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   287
        dcf = LE_to_BE(nbparams, 0x04) + data
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   288
        # Set new DCF for slave
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   289
        self.MasterNode.SetEntry(0x1F22, nodeid, dcf)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   290
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   291
    def AddPDOMapping(self, nodeid, pdotype, pdomapping, sync_TPDOs):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   292
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   293
        Select a COB ID from the list of those available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   294
        @param nodeid: id of the slave (int)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   295
        @param pdotype: type of PDO to generated (RPDO or TPDO)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   296
        @param pdomapping: list od variables to map with PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   297
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   298
        # Get a new cob id
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   299
        result = self.GetNewCobID(nodeid, pdotype)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   300
        if result:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   301
            new_cobid, new_idx = result
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   302
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   303
            # Increment the number of PDO of this type for node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   304
            self.SlavesPdoNumber[nodeid][pdotype] += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   305
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   306
            # Add an entry to MasterMapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   307
            self.MasterMapping[new_cobid] = {"type" : InvertPDOType[pdotype], 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   308
                "mapping" : [None] + [(loc_infos["type"], name) for name, loc_infos in pdomapping]}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   309
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   310
            # Return the data to add to DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   311
            if sync_TPDOs:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   312
                return GeneratePDOMappingDCF(new_idx, new_cobid, 0x01, pdomapping)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   313
            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   314
                return GeneratePDOMappingDCF(new_idx, new_cobid, 0xFF, pdomapping)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   315
        return 0, ""
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   316
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   317
    def GenerateDCF(self, locations, current_location, sync_TPDOs):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   318
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   319
        Generate Concise DCF of MasterNode for the locations list given
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   320
        @param locations: list of locations to be mapped
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   321
        @param current_location: tuple of the located prefixes not to be considered
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   322
        @param sync_TPDOs: indicate if TPDO must be synchronous
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   323
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   324
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   325
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   326
        #               Verify that locations correspond to real slave variables
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   327
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   328
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   329
        # Get list of locations check if exists and mappables -> put them in IECLocations
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   330
        for location in locations:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   331
            COlocationtype = IECToCOType[location["IEC_TYPE"]]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   332
            name = location["NAME"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   333
            if name in self.IECLocations:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   334
                if self.IECLocations[name]["type"] != COlocationtype:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   335
                    raise ValueError, "Conflict type for location \"%s\"" % name 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   336
            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   337
                # Get only the part of the location that concern this node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   338
                loc = location["LOC"][len(current_location):]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   339
                # loc correspond to (ID, INDEX, SUBINDEX [,BIT])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   340
                if len(loc) not in (3, 4):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   341
                    raise ValueError, "Bad location size : %s"%str(loc)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   342
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   343
                direction = location["DIR"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   344
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   345
                sizelocation = location["SIZE"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   346
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   347
                # Extract and check nodeid
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   348
                nodeid, index, subindex = loc[:3]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   349
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   350
                # Check Id is in slave node list
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   351
                if nodeid not in self.NodeList.SlaveNodes.keys():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   352
                    raise ValueError, "Non existing node ID : %d (variable %s)" % (nodeid,name)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   353
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   354
                # Get the model for this node (made from EDS)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   355
                node = self.NodeList.SlaveNodes[nodeid]["Node"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   356
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   357
                # Extract and check index and subindex
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   358
                if not node.IsEntry(index, subindex):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   359
                    raise ValueError, "No such index/subindex (%x,%x) in ID : %d (variable %s)" % (index,subindex,nodeid,name)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   360
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   361
                # Get the entry info
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   362
                subentry_infos = node.GetSubentryInfos(index, subindex)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   363
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   364
                # If a PDO mappable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   365
                if subentry_infos and subentry_infos["pdo"]:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   366
                    if sizelocation == "X" and len(loc) > 3:
61
a20fb174f46e Bug on bit locations generation fixed
lbessard
parents: 59
diff changeset
   367
                        numbit = loc[3]
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   368
                    elif sizelocation != "X" and len(loc) > 3:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   369
                        raise ValueError, "Cannot set bit offset for non bool '%s' variable (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   370
                    else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   371
                        numbit = None
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   372
                    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   373
                    entryinfos = node.GetSubentryInfos(index, subindex)
61
a20fb174f46e Bug on bit locations generation fixed
lbessard
parents: 59
diff changeset
   374
                    if location["IEC_TYPE"] != "BOOL" and entryinfos["type"] != COlocationtype:
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   375
                        raise ValueError, "Invalid type \"%s\"-> %d != %d  for location\"%s\"" % (location["IEC_TYPE"], COlocationtype, entryinfos["type"] , name)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   376
                    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   377
                    typeinfos = node.GetEntryInfos(COlocationtype)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   378
                    self.IECLocations[name] = {"type":COlocationtype, "pdotype":SlavePDOType[direction],
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   379
                                                "nodeid": nodeid, "index": index,"subindex": subindex,
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   380
                                                "bit": numbit, "size": typeinfos["size"], "sizelocation": sizelocation}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   381
                else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   382
                    raise ValueError, "Not PDO mappable variable : '%s' (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   383
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   384
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   385
        #                         Search for locations already mapped
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   386
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   387
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   388
        for name, locationinfos in self.IECLocations.items():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   389
            node = self.NodeList.SlaveNodes[locationinfos["nodeid"]]["Node"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   390
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   391
            # Search if slave has a PDO mapping this locations
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   392
            result = SearchNodePDOMapping(locationinfos, node)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   393
            if result != None:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   394
                index, subindex = result
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   395
                # Get COB ID of the PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   396
                cobid = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   397
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   398
                # Add PDO to MasterMapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   399
                if cobid not in self.MasterMapping.keys():
80
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   400
                    # Verify that PDO transmit type is conform to sync_TPDOs
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   401
                    transmittype = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 2)
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   402
                    if sync_TPDOs and transmittype != 0x01 or transmittype != 0xFF:
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   403
                        if sync_TPDOs:
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   404
                            # Change TransmitType to SYNCHRONE
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   405
                            data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0x01, [])
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   406
                        else:
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   407
                            # Change TransmitType to ASYCHRONE
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   408
                            data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0xFF, [])
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   409
                        
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   410
                        # Add entry to slave dcf to change transmit type of 
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   411
                        self.AddParamsToDCF(locationinfos["nodeid"], data, nbparams)
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   412
                                    
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   413
                    mapping = [None]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   414
                    values = node.GetEntry(index)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   415
                    # Store the size of each entry mapped in PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   416
                    for value in values[1:]:
78
274a343d49d0 Bug on Master config generation fixed
lbessard
parents: 61
diff changeset
   417
                        if value != 0:
274a343d49d0 Bug on Master config generation fixed
lbessard
parents: 61
diff changeset
   418
                            mapping.append(value % 0x100)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   419
                    self.MasterMapping[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   420
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   421
                # Indicate that this PDO entry must be saved
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   422
                self.MasterMapping[cobid]["mapping"][subindex] = (locationinfos["type"], name)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   423
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   424
            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   425
                # Add location to those that haven't been mapped yet
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   426
                if locationinfos["nodeid"] not in self.LocationsNotMapped.keys():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   427
                    self.LocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   428
                self.LocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   429
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   430
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   431
        #                         Build concise DCF for the others locations
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   432
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   433
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   434
        for nodeid, locations in self.LocationsNotMapped.items():
61
a20fb174f46e Bug on bit locations generation fixed
lbessard
parents: 59
diff changeset
   435
            node = self.NodeList.SlaveNodes[nodeid]["Node"]
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   436
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   437
            # Initialize number of params and data to add to node DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   438
            nbparams = 0
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   439
            dataparams = ""
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   440
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   441
            # Generate the best PDO mapping for each type of PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   442
            for pdotype in (TPDO, RPDO):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   443
                pdosize = 0
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   444
                pdomapping = []
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   445
                for name, loc_infos in locations[pdotype]:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   446
                    pdosize += loc_infos["size"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   447
                    # If pdo's size > 64 bits
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   448
                    if pdosize > 64:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   449
                        # Generate a new PDO Mapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   450
                        data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdomapping, sync_TPDOs)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   451
                        dataparams += data
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   452
                        nbparams += nbaddedparams
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   453
                        pdosize = loc_infos["size"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   454
                        pdomapping = [(name, loc_infos)]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   455
                    else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   456
                        pdomapping.append((name, loc_infos))
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   457
                # If there isn't locations yet but there is still a PDO to generate
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   458
                if len(pdomapping) > 0:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   459
                    # Generate a new PDO Mapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   460
                    data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdomapping, sync_TPDOs)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   461
                    dataparams += data
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   462
                    nbparams += nbaddedparams
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   463
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   464
            # Add number of params and data to node DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   465
            self.AddParamsToDCF(nodeid, dataparams, nbparams)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   466
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   467
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   468
        #                         Master Node Configuration
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   469
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   470
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   471
        # Generate Master's Configuration from informations stored in MasterMapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   472
        for cobid, pdo_infos in self.MasterMapping.items():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   473
            # Get next PDO index in MasterNode for this PDO type
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   474
            current_idx = self.CurrentPDOParamsIdx[pdo_infos["type"]]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   475
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   476
            # Search if there is already a PDO in MasterNode with this cob id
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   477
            for idx in GetNodePDOIndexes(self.MasterNode, pdo_infos["type"], True):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   478
                if self.MasterNode.GetEntry(idx, 1) == cobid:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   479
                    current_idx = idx
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   480
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   481
            # Add a PDO to MasterNode if not PDO have been found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   482
            if current_idx == self.CurrentPDOParamsIdx[pdo_infos["type"]]:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   483
                addinglist = [current_idx, current_idx + 0x200]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   484
                self.Manager.ManageEntriesOfCurrent(addinglist, [], self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   485
                self.MasterNode.SetEntry(current_idx, 0x01, cobid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   486
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   487
                # Increment the number of PDO for this PDO type
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   488
                self.CurrentPDOParamsIdx[pdo_infos["type"]] += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   489
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   490
            # Change the transmit type of the PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   491
            if sync_TPDOs:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   492
                self.MasterNode.SetEntry(current_idx, 0x02, 0x01)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   493
            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   494
                self.MasterNode.SetEntry(current_idx, 0x02, 0xFF)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   495
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   496
            # Add some subentries to PDO mapping if there is not enough
59
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 58
diff changeset
   497
            if len(pdo_infos["mapping"]) > 1:
b6ff896ff58b Enhancements in CanFestival plugin. Now PLC with CanFestival plugin compiles and run, exchanging CanOpen PDOs
etisserant
parents: 58
diff changeset
   498
                self.Manager.AddSubentriesToCurrent(current_idx + 0x200, len(pdo_infos["mapping"]) - 1, self.MasterNode)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   499
            
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   500
            # Generate MasterNode's PDO mapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   501
            for subindex, variable in enumerate(pdo_infos["mapping"]):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   502
                if subindex == 0:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   503
                    continue
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   504
                new_index = False
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   505
                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   506
                if type(variable) == IntType:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   507
                    # If variable is an integer then variable is unexpected
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   508
                    self.MasterNode.SetEntry(current_idx + 0x200, subindex, self.TrashVariables[variable])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   509
                else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   510
                    typeidx, varname = variable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   511
                    variable_infos = self.IECLocations[varname]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   512
                    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   513
                    # Calculate base index for storing variable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   514
                    mapvariableidx = VariableStartIndex[variable_infos["pdotype"]] + \
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   515
                                     VariableTypeOffset[variable_infos["sizelocation"]] * VariableIncrement + \
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   516
                                     variable_infos["nodeid"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   517
                    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   518
                    # Search for an entry that has an empty subindex 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   519
                    while mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   520
                        # Entry doesn't exist
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   521
                        if not self.MasterNode.IsEntry(mapvariableidx):    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   522
                            # Generate entry name
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   523
                            indexname = "%s%s%s_%d"%(VariableDirText[variable_infos["pdotype"]],
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   524
                                                     variable_infos["sizelocation"],
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   525
                                                     '_'.join(map(str,current_location)),
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   526
                                                     variable_infos["nodeid"])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   527
                            # Add entry to MasterNode
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   528
                            self.Manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1, self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   529
                            new_index = True
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   530
                            nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   531
                        else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   532
                            # Get Number of subentries already defined
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   533
                            nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   534
                            # if entry is full, go to next entry possible or stop now
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   535
                            if nbsubentries == 0xFF:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   536
                                mapvariableidx += 8 * VariableIncrement
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   537
                            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   538
                                break
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   539
                                
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   540
                    # Verify that a not full entry has been found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   541
                    if mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   542
                        # Generate subentry name
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   543
                        if variable_infos["bit"] != None:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   544
                            subindexname = "%(index)d_%(subindex)d_%(bit)d"%variable_infos
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   545
                        else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   546
                            subindexname = "%(index)d_%(subindex)d"%variable_infos
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   547
                        # If entry have just been created, no subentry have to be added
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   548
                        if not new_index:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   549
                            self.Manager.AddSubentriesToCurrent(mapvariableidx, 1, self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   550
                            nbsubentries += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   551
                        # Add informations to the new subentry created
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   552
                        self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name" : subindexname})
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   553
                        self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type" : typeidx})
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   554
                        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   555
                        # Set value of the PDO mapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   556
                        typeinfos = self.Manager.GetEntryInfos(typeidx)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   557
                        if typeinfos != None:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   558
                            value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   559
                            self.MasterNode.SetEntry(current_idx + 0x200, subindex, value)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   560
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   561
def GenerateConciseDCF(locations, current_location, nodelist, sync_TPDOs, nodename):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   562
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   563
    Fills a CanFestival network editor model, with DCF with requested PDO mappings.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   564
    @param locations: List of complete variables locations \
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   565
        [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   566
        "NAME" : name of the variable (generally "__IW0_1_2" style)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   567
        "DIR" : direction "Q","I" or "M"
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   568
        "SIZE" : size "X", "B", "W", "D", "L"
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   569
        "LOC" : tuple of interger for IEC location (0,1,2,...)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   570
        }, ...]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   571
    @param nodelist: CanFestival network editor model
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   572
    @return: a modified copy of the given CanFestival network editor model
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   573
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   574
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   575
    dcfgenerator = ConciseDCFGenerator(nodelist, nodename)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   576
    dcfgenerator.GenerateDCF(locations, current_location, sync_TPDOs)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   577
    return dcfgenerator.GetMasterNode()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   578
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   579
if __name__ == "__main__":
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   580
    import os, sys, getopt
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   581
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   582
    def usage():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   583
        print """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   584
Usage of config_utils.py test :
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   585
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   586
    %s [options]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   587
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   588
Options:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   589
    --help  (-h)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   590
            Displays help informations for config_utils
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   591
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   592
    --reset (-r)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   593
            Reset the reference result of config_utils test.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   594
            Use with caution. Be sure that config_utils
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   595
            is currently working properly.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   596
"""%sys.argv[0]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   597
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   598
    # Boolean that indicate if reference result must be redefined
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   599
    reset = False
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   600
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   601
    # Extract command options
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   602
    try:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   603
        opts, args = getopt.getopt(sys.argv[1:], "hr", ["help","reset"])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   604
    except getopt.GetoptError:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   605
        # print help information and exit:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   606
        usage()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   607
        sys.exit(2)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   608
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   609
    # Test each option
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   610
    for o, a in opts:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   611
        if o in ("-h", "--help"):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   612
            usage()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   613
            sys.exit()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   614
        elif o in ("-r", "--reset"):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   615
            reset = True
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   616
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   617
    # Extract workspace base folder
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   618
    base_folder = sys.path[0]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   619
    for i in xrange(3):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   620
        base_folder = os.path.split(base_folder)[0]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   621
    # Add CanFestival folder to search pathes
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   622
    sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen"))
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   623
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   624
    from nodemanager import *
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   625
    from nodelist import *
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   626
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   627
    # Open the test nodelist contained into test_config folder
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   628
    manager = NodeManager()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   629
    nodelist = NodeList(manager)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   630
    result = nodelist.LoadProject("test_config")
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   631
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   632
    # List of locations, we try to map for test
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   633
    locations = [{"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24576_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24576,1)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   634
                 {"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_2","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,2)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   635
                 {"IEC_TYPE":"INT","NAME":"__IW0_1_64_25601_3","DIR":"I","SIZE":"W","LOC":(0,1,64,25601,3)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   636
                 {"IEC_TYPE":"INT","NAME":"__QW0_1_64_25617_2","DIR":"Q","SIZE":"W","LOC":(0,1,64,25617,1)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   637
                 {"IEC_TYPE":"BYTE","NAME":"__IB0_1_64_24578_1","DIR":"I","SIZE":"B","LOC":(0,1,64,24578,1)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   638
                 {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_1","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,1)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   639
                 {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_2","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,2)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   640
                 {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_3","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,3)},
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   641
                 {"IEC_TYPE":"UDINT","NAME":"__ID0_1_64_25638_4","DIR":"I","SIZE":"D","LOC":(0,1,64,25638,4)}]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   642
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   643
    # Generate MasterNode configuration
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   644
    try:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   645
        masternode = GenerateConciseDCF(locations, (0, 1), nodelist, True)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   646
    except ValueError, message:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   647
        print "%s\nTest Failed!"%message
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   648
        sys.exit()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   649
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   650
    # Get Text corresponding to MasterNode 
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   651
    result = masternode.PrintString()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   652
    
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   653
    # If reset has been choosen
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   654
    if reset:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   655
        # Write Text into reference result file
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   656
        file = open("test_config/result.txt", "w")
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   657
        file.write(result)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   658
        file.close()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   659
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   660
        print "Reset Successful!"
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   661
    else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   662
        # Test each line of the result with the reference result
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   663
        test = [line.rstrip() for line in result.splitlines()]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   664
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   665
        file = open("test_config/result.txt", "r")
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   666
        model = [line.rstrip() for line in file.readlines() if line.rstrip()]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   667
        file.close()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   668
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   669
        errors = 0
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   670
        for i, line in enumerate(model):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   671
            if i >= len(test):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   672
                errors += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   673
                print "Line %d disappear :\n%s\n"%(i + 1, line)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   674
            elif line != test[i]:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   675
                errors += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   676
                print "Error on line %d :\n%s\nInstead of :\n%s\n"%(i + 1, test[i], line)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   677
        for i in xrange(len(model), len(test)):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   678
            errors += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   679
            print "Line %d appear :\n%s\n"%(i + 1, test[i])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   680
        
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   681
        if errors > 0:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   682
            print "%d errors found.\nTest Failed!"%errors
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   683
        else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   684
            print "Test Successful!"