objdictgen/config_utils.py
author greg
Fri, 06 Jul 2007 10:53:15 +0200
changeset 239 f45fd4cd3832
permissions -rw-r--r--
add auto-mapping tool (config_utils)
fix little bugs
239
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     1
#!/usr/bin/env python
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     3
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     4
#This file is part of CanFestival, a library implementing CanOpen Stack. 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     5
#
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     6
#Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     7
#
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     8
#See COPYING file for copyrights details.
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
     9
#
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    10
#This library is free software; you can redistribute it and/or
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    11
#modify it under the terms of the GNU Lesser General Public
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    12
#License as published by the Free Software Foundation; either
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    13
#version 2.1 of the License, or (at your option) any later version.
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    14
#
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    15
#This library is distributed in the hope that it will be useful,
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    16
#but WITHOUT ANY WARRANTY; without even the implied warranty of
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    17
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    18
#Lesser General Public License for more details.
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    19
#
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    20
#You should have received a copy of the GNU Lesser General Public
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    21
#License along with this library; if not, write to the Free Software
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    22
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    23
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    24
from types import *
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    25
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    26
DicoTypes = {"BOOL":0x01, "SINT":0x02, "INT":0x03,"DINT":0x04,"LINT":0x10,
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    27
             "USINT":0x05,"UINT":0x06,"UDINT":0x07,"ULINT":0x1B,"REAL":0x08,
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    28
             "LREAL":0x11,"STRING":0x09,"BYTE":0x02,"WORD":0x03,"DWORD":0x04,
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    29
             "LWORD":0x1B,"WSTRING":0x0B}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    30
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    31
DictLocations = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    32
DictCobID = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    33
DictLocationsNotMapped = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    34
ListCobIDAvailable = []
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    35
SlavesPdoNumber = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    36
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    37
# Constants for PDO types 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    38
RPDO = 1
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    39
TPDO = 2
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    40
SlavePDOType = {"I" : TPDO, "Q" : RPDO}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    41
InvertPDOType = {RPDO : TPDO, TPDO : RPDO}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    42
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    43
DefaultTransmitType = 0x01
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    44
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    45
GenerateMasterMapping = lambda x:[None] + [(loc_infos["type"], name) for name, loc_infos in x]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    46
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    47
TrashVariableSizes = {1 : 0x01, 8 : 0x05, 16 : 0x06, 32 : 0x07, 64 : 0x1B}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    48
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    49
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    50
def GetSlavePDOIndexes(slave, type, parameters = False):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    51
    indexes = []
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    52
    if type & RPDO:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    53
        indexes.extend([idx for idx in slave.GetIndexes() if 0x1400 <= idx <= 0x15FF])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    54
    if type & TPDO:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    55
        indexes.extend([idx for idx in slave.GetIndexes() if 0x1800 <= idx <= 0x19FF])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    56
    if not parameters:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    57
        return [idx + 0x200 for idx in indexes]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    58
    else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    59
        return indexes
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    60
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    61
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    62
def LE_to_BE(value, size): # Convert Little Endian to Big Endian
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    63
    data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    64
    list_car = [data[i:i+2] for i in xrange(0, len(data), 2)]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    65
    list_car.reverse()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    66
    return "".join([chr(int(car, 16)) for car in list_car])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    67
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    68
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    69
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    70
def SearchSlavePDOMapping(loc_infos, slave): # Search the TPDO or RPDO mapping where location is defined on the slave
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    71
    typeinfos = slave.GetEntryInfos(loc_infos["type"])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    72
    model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + typeinfos["size"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    73
    slavePDOidxlist = GetSlavePDOIndexes(slave, loc_infos["pdotype"])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    74
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    75
    for PDOidx in slavePDOidxlist:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    76
        values = slave.GetEntry(PDOidx)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    77
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    78
        for subindex, mapping in enumerate(values):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    79
            if subindex != 0 and mapping == model:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    80
                return PDOidx, subindex
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    81
    return None
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    82
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    83
def GenerateMappingDCF(cobid, idx, pdomapping, mapped): # Build concise DCF
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    84
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    85
    # Create entry for RPDO or TPDO parameters and Disable PDO
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    86
    dcfdata = LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE((0x80000000 + cobid), 4)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    87
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    88
    # Set Transmit type synchrone
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    89
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(DefaultTransmitType, 1)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    90
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    91
    # Re-Enable PDO
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    92
    #         ---- INDEX -----   --- SUBINDEX ----   ----- SIZE ------   ------ DATA ------
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    93
    dcfdata += LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x00000000 + cobid, 4)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    94
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    95
    nbparams = 3
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    96
    if mapped == False and pdomapping != None:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    97
    # Map Variables
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    98
        for subindex, (name, loc_infos) in enumerate(pdomapping):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
    99
            value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   100
            dcfdata += LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(loc_infos["size"] >> 3, 4) + LE_to_BE(value, loc_infos["size"] >> 3)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   101
            nbparams += 1
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   102
    return dcfdata, nbparams
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   103
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   104
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   105
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   106
def GetNewCobID(nodeid, type): # Return a cobid not used
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   107
    global ListCobIDAvailable, SlavesPdoNumber
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   108
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   109
    if len(ListCobIDAvailable) == 0:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   110
        return None
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   111
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   112
    nbSlavePDO = SlavesPdoNumber[nodeid][type]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   113
    if type == RPDO:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   114
        if nbSlavePDO < 4:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   115
            # For the fourth PDO -> cobid = 0x200 + ( numPdo parameters * 0x100) + nodeid
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   116
            newcobid = (0x200 + nbSlavePDO * 0x100 + nodeid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   117
            if newcobid in ListCobIDAvailable:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   118
                ListCobIDAvailable.remove(newcobid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   119
                return newcobid, 0x1400 + nbSlavePDO
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   120
        return ListCobIDAvailable.pop(0), 0x1400 + nbSlavePDO
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   121
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   122
    elif type == TPDO:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   123
        if nbSlavePDO < 4:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   124
            # For the fourth PDO -> cobid = 0x180 + (numPdo parameters * 0x100) + nodeid
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   125
            newcobid = (0x180 + nbSlavePDO * 0x100 + nodeid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   126
            if newcobid in ListCobIDAvailable:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   127
                ListCobIDAvailable.remove(newcobid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   128
                return newcobid, 0x1800 + nbSlavePDO
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   129
        return ListCobIDAvailable.pop(0), 0x1800 + nbSlavePDO
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   130
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   131
    for number in xrange(4):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   132
        if type == RPDO:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   133
            # For the fourth PDO -> cobid = 0x200 + ( numPdo * 0x100) + nodeid
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   134
            newcobid = (0x200 + number * 0x100 + nodeid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   135
        elif type == TPDO:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   136
            # For the fourth PDO -> cobid = 0x180 + (numPdo * 0x100) + nodeid
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   137
            newcobid = (0x180 + number * 0x100 + nodeid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   138
        else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   139
            return None
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   140
        if newcobid in ListCobIDAvailable:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   141
            ListCobIDAvailable.remove(newcobid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   142
            return newcobid
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   143
    return ListCobIDAvailable.pop(0)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   144
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   145
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   146
def GenerateConciseDCF(locations, busname, nodelist):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   147
    global DictLocations, DictCobID, DictLocationsNotMapped, ListCobIDAvailable, SlavesPdoNumber
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   148
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   149
    DictLocations = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   150
    DictCobID = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   151
    DictLocationsNotMapped = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   152
    DictSDOparams = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   153
    ListCobIDAvailable = range(0x180, 0x580)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   154
    SlavesPdoNumber = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   155
    DictNameVariable = { "" : 1, "X": 2, "B": 3, "W": 4, "D": 5, "L": 6, "increment": 0x100, 1:("__I", 0x2000), 2:("__Q", 0x4000)}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   156
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   157
    # Master Node initialisation
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   158
    manager = nodelist.Manager
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   159
    manager.AddSubentriesToCurrent(0x1F22, 127)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   160
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   161
    # Adding trash mappable variables for unused mapped datas
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   162
    idxTrashVariables = 0x2000 + manager.GetCurrentNodeID()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   163
    TrashVariableValue = {}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   164
    manager.AddMapVariableToCurrent(idxTrashVariables, "trashvariables", 3, len(TrashVariableSizes))
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   165
    for subidx, (size, typeidx) in enumerate(TrashVariableSizes.items()):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   166
        manager.SetCurrentEntry(idxTrashVariables, subidx + 1, "TRASH%d" % size, "name", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   167
        manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   168
        TrashVariableValue[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   169
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   170
    # Extract Master Node current empty mapping index
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   171
    masternode = manager.GetCurrentNode()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   172
    CurrentPDOParamsIdx = {RPDO : 0x1400 + len(GetSlavePDOIndexes(masternode, RPDO)),
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   173
                           TPDO : 0x1800 + len(GetSlavePDOIndexes(masternode, TPDO))}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   174
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   175
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   176
    # Get list of all Slave's CobID and Slave's default SDO server parameters
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   177
    for nodeid, nodeinfos in nodelist.SlaveNodes.items():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   178
        node = nodeinfos["Node"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   179
        node.SetNodeID(nodeid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   180
        DictSDOparams[nodeid] = {"RSDO" : node.GetEntry(0x1200,0x01), "TSDO" : node.GetEntry(0x1200,0x02)}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   181
        slaveRpdoIndexes = GetSlavePDOIndexes(node, RPDO, True)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   182
        slaveTpdoIndexes = GetSlavePDOIndexes(node, TPDO, True)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   183
        SlavesPdoNumber[nodeid] = {RPDO : len(slaveRpdoIndexes), TPDO : len(slaveTpdoIndexes)}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   184
        for PdoIdx in slaveRpdoIndexes + slaveTpdoIndexes:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   185
            pdo_cobid = node.GetEntry(PdoIdx, 0x01)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   186
            if pdo_cobid > 0x600 :
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   187
                pdo_cobid -= 0x80000000
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   188
            if pdo_cobid in ListCobIDAvailable:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   189
                ListCobIDAvailable.remove(pdo_cobid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   190
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   191
    # Get list of locations check if exists and mappables -> put them in DictLocations
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   192
    for locationtype, name in locations:    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   193
        if name in DictLocations.keys():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   194
            if DictLocations[name]["type"] != DicoTypes[locationtype]:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   195
                raise ValueError, "Conflict type for location \"%s\"" % name 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   196
        else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   197
            loc = [i for i in name.split("_") if len(i) > 0]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   198
            if len(loc) not in (4, 5):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   199
                continue
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   200
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   201
            prefix = loc[0][0]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   202
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   203
            # Extract and check busname
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   204
            if loc[0][1].isdigit():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   205
                sizelocation = ""
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   206
                busnamelocation = int(loc[0][1:])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   207
            else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   208
                sizelocation = loc[0][1]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   209
                busnamelocation = int(loc[0][2:])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   210
            if busnamelocation != busname:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   211
                continue # A ne pas remplacer par un message d'erreur
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   212
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   213
            # Extract and check nodeid
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   214
            nodeid = int(loc[1])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   215
            if nodeid not in nodelist.SlaveNodes.keys():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   216
                continue
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   217
            node = nodelist.SlaveNodes[nodeid]["Node"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   218
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   219
            # Extract and check index and subindex
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   220
            index = int(loc[2])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   221
            subindex = int(loc[3])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   222
            if not node.IsEntry(index, subindex):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   223
                continue
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   224
            subentry_infos = node.GetSubentryInfos(index, subindex)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   225
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   226
            if subentry_infos and subentry_infos["pdo"]:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   227
                if sizelocation == "X" and len(loc) > 4:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   228
                    numbit = loc[4]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   229
                elif sizelocation != "X" and len(loc) > 4:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   230
                    continue
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   231
                else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   232
                    numbit = None
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   233
                
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   234
                locationtype = DicoTypes[locationtype]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   235
                entryinfos = node.GetSubentryInfos(index, subindex)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   236
                if entryinfos["type"] != locationtype:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   237
                    raise ValueError, "Invalid type for location \"%s\"" % name
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   238
                
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   239
                typeinfos = node.GetEntryInfos(locationtype)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   240
                DictLocations[name] = {"type":locationtype, "pdotype":SlavePDOType[prefix],
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   241
                                       "nodeid": nodeid, "index": index,"subindex": subindex, 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   242
                                       "bit": numbit, "size": typeinfos["size"], "busname": busname, "sizelocation": sizelocation}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   243
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   244
    # Create DictCobID with variables already mapped and add them in DictValidLocations
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   245
    for name, locationinfos in DictLocations.items():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   246
        node = nodelist.SlaveNodes[locationinfos["nodeid"]]["Node"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   247
        result = SearchSlavePDOMapping(locationinfos, node)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   248
        if result != None:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   249
            index, subindex = result
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   250
            cobid = nodelist.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   251
            if cobid not in DictCobID.keys():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   252
                DefaultNodeTransmitType = node.GetEntry(index - 0x200, 2)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   253
                if not 1 <= DefaultNodeTransmitType <= 240:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   254
                    result = GenerateMappingDCF(cobid, index - 0x200, None, True)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   255
                    data, nbaddedparams = GenerateMappingDCF(cobid, index - 0x200, None, True)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   256
                    nodeDCF = nodelist.GetMasterNodeEntry(0x1F22, locationinfos["nodeid"])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   257
                    if nodeDCF != None and nodeDCF != '':
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   258
                        tmpnbparams = [i for i in nodeDCF[:4]]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   259
                        tmpnbparams.reverse()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   260
                        nbparams = int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   261
                        dataparams = nodeDCF[4:]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   262
                    else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   263
                        nbparams = 0
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   264
                        dataparams = ""
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   265
                    dataparams += data
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   266
                    nbparams += nbaddedparams        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   267
                    dcf = LE_to_BE(nbparams, 0x04) + dataparams
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   268
                    manager.SetCurrentEntry(0x1F22, locationinfos["nodeid"], dcf, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   269
                    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   270
                mapping = [None]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   271
                values = node.GetEntry(index)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   272
                for value in values[1:]:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   273
                    mapping.append(value % 0x100)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   274
                DictCobID[cobid] = {"type" : InvertPDOType[locationinfos["pdotype"]], "mapping" : mapping}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   275
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   276
            DictCobID[cobid]["mapping"][subindex] = (locationinfos["type"], name)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   277
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   278
        else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   279
            if locationinfos["nodeid"] not in DictLocationsNotMapped.keys():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   280
                DictLocationsNotMapped[locationinfos["nodeid"]] = {TPDO : [], RPDO : []}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   281
            DictLocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   282
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   283
    # Check Master Pdo parameters for cobid already used and remove it in ListCobIDAvailable
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   284
    ListPdoParams = [idx for idx in masternode.GetIndexes() if 0x1400 <= idx <= 0x15FF or  0x1800 <= idx <= 0x19FF]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   285
    for idx in ListPdoParams:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   286
        cobid = manager.GetCurrentEntry(idx, 0x01)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   287
        if cobid not in DictCobID.keys():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   288
            ListCobIDAvailable.pop(cobid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   289
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   290
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   291
    #-------------------------------------------------------------------------------
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   292
    #                         Build concise DCF for the others locations
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   293
    #-------------------------------------------------------------------------------
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   294
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   295
    for nodeid, locations in DictLocationsNotMapped.items():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   296
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   297
        # Get current concise DCF
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   298
        node = nodelist.SlaveNodes[nodeid]["Node"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   299
        nodeDCF = nodelist.GetMasterNodeEntry(0x1F22, nodeid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   300
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   301
        if nodeDCF != None and nodeDCF != '':
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   302
            tmpnbparams = [i for i in nodeDCF[:4]]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   303
            tmpnbparams.reverse()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   304
            nbparams = int(''.join(["%2.2x"%ord(i) for i in tmpnbparams]), 16)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   305
            dataparams = nodeDCF[4:]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   306
        else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   307
            nbparams = 0
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   308
            dataparams = ""
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   309
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   310
        for pdotype in (TPDO, RPDO):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   311
            pdosize = 0
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   312
            pdomapping = []
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   313
            for name, loc_infos in locations[pdotype]:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   314
                pdosize += loc_infos["size"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   315
                # If pdo's size > 64 bits
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   316
                if pdosize > 64:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   317
                    result = GetNewCobID(nodeid, pdotype)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   318
                    if result:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   319
                        SlavesPdoNumber[nodeid][pdotype] += 1
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   320
                        new_cobid, new_idx = result
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   321
                        data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   322
                        dataparams += data
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   323
                        nbparams += nbaddedparams
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   324
                        DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   325
                    pdosize = loc_infos["size"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   326
                    pdomapping = [(name, loc_infos)]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   327
                else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   328
                    pdomapping.append((name, loc_infos))
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   329
            if len(pdomapping) > 0:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   330
                result = GetNewCobID(nodeid, pdotype)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   331
                if result:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   332
                    SlavesPdoNumber[nodeid][pdotype] += 1
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   333
                    new_cobid, new_idx = result
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   334
                    data, nbaddedparams = GenerateMappingDCF(new_cobid, new_idx, pdomapping, False)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   335
                    dataparams += data
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   336
                    nbparams += nbaddedparams
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   337
                    DictCobID[new_cobid] = {"type" : InvertPDOType[pdotype], "mapping" : GenerateMasterMapping(pdomapping)}
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   338
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   339
        dcf = LE_to_BE(nbparams, 0x04) + dataparams
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   340
        manager.SetCurrentEntry(0x1F22, nodeid, dcf, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   341
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   342
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   343
    #-------------------------------------------------------------------------------
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   344
    #                         Master Node Configuration
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   345
    #-------------------------------------------------------------------------------
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   346
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   347
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   348
    # Configure Master's SDO parameters entries
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   349
    for nodeid, SDOparams in DictSDOparams.items():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   350
        SdoClient_index = [0x1280 + nodeid]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   351
        manager.ManageEntriesOfCurrent(SdoClient_index,[])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   352
        if SDOparams["RSDO"] != None:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   353
            RSDO_cobid = SDOparams["RSDO"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   354
        else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   355
            RSDO_cobid = 0x600 + nodeid 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   356
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   357
        if SDOparams["TSDO"] != None:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   358
            TSDO_cobid = SDOparams["TSDO"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   359
        else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   360
            TSDO_cobid = 0x580 + nodeid 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   361
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   362
        manager.SetCurrentEntry(SdoClient_index[0], 0x01, RSDO_cobid, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   363
        manager.SetCurrentEntry(SdoClient_index[0], 0x02, TSDO_cobid, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   364
        manager.SetCurrentEntry(SdoClient_index[0], 0x03, nodeid, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   365
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   366
    # Configure Master's PDO parameters entries and set cobid, transmit type
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   367
    for cobid, pdo_infos in DictCobID.items():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   368
        current_idx = CurrentPDOParamsIdx[pdo_infos["type"]]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   369
        addinglist = [current_idx, current_idx + 0x200]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   370
        manager.ManageEntriesOfCurrent(addinglist, [])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   371
        manager.SetCurrentEntry(current_idx, 0x01, cobid, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   372
        manager.SetCurrentEntry(current_idx, 0x02, DefaultTransmitType, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   373
        if len(pdo_infos["mapping"]) > 2:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   374
            manager.AddSubentriesToCurrent(current_idx + 0x200, len(pdo_infos["mapping"]) - 2)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   375
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   376
        # Create Master's PDO mapping
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   377
        for subindex, variable in enumerate(pdo_infos["mapping"]):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   378
            if subindex == 0:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   379
                continue
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   380
            new_index = False
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   381
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   382
            if type(variable) != IntType:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   383
                
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   384
                typeidx, varname = variable
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   385
                indexname = DictNameVariable[DictLocations[variable[1]]["pdotype"]][0] + DictLocations[variable[1]]["sizelocation"] + str(DictLocations[variable[1]]["busname"]) + "_" + str(DictLocations[variable[1]]["nodeid"])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   386
                mapvariableidx = DictNameVariable[DictLocations[variable[1]]["pdotype"]][1] +  DictNameVariable[DictLocations[variable[1]]["sizelocation"]] * DictNameVariable["increment"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   387
                
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   388
                if not manager.IsCurrentEntry(mapvariableidx):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   389
                    manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   390
                    new_index = True
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   391
                    nbsubentries = manager.GetCurrentEntry(mapvariableidx, 0x00)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   392
                else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   393
                    nbsubentries = manager.GetCurrentEntry(mapvariableidx, 0x00)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   394
                    mapvariableidxbase = mapvariableidx 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   395
                    while mapvariableidx < (mapvariableidxbase + 0x1FF) and nbsubentries == 0xFF:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   396
                        mapvariableidx += 0x800
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   397
                        if not manager.IsCurrentEntry(mapvariableidx):
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   398
                            manager.AddMapVariableToCurrent(mapvariableidx, indexname, 3, 1)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   399
                            new_index = True
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   400
                        nbsubentries = manager.GetCurrentEntry(mapvariableidx, 0x00)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   401
                if mapvariableidx < 0x6000:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   402
                    if DictLocations[variable[1]]["bit"] != None:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   403
                        subindexname = "_" + str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"]) + "_" + str(DictLocations[variable[1]]["bit"])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   404
                    else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   405
                        subindexname = "_" + str(DictLocations[variable[1]]["index"]) + "_" + str(DictLocations[variable[1]]["subindex"])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   406
                    if not new_index:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   407
                        manager.AddSubentriesToCurrent(mapvariableidx, 1)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   408
                        nbsubentries += 1
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   409
                    manager.SetCurrentEntry(mapvariableidx, nbsubentries, subindexname, "name", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   410
                    manager.SetCurrentEntry(mapvariableidx, nbsubentries, typeidx, "type", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   411
                    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   412
                    # Map Variable
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   413
                    typeinfos = manager.GetEntryInfos(typeidx)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   414
                    if typeinfos != None:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   415
                        value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   416
                        manager.SetCurrentEntry(current_idx + 0x200, subindex, value, "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   417
            else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   418
                manager.SetCurrentEntry(current_idx + 0x200, subindex, TrashVariableValue[variable], "value", None)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   419
        
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   420
        CurrentPDOParamsIdx[pdo_infos["type"]] += 1
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   421
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   422
if __name__ == "__main__":
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   423
    from nodemanager import *
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   424
    from nodelist import *
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   425
    import sys
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   426
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   427
    manager = NodeManager(sys.path[0])
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   428
    nodelist = NodeList(manager)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   429
    result = nodelist.LoadProject("/home/deobox/Desktop/TestMapping")
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   430
   
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   431
    if result != None:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   432
        print result
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   433
    else:
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   434
        print "MasterNode :"
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   435
        manager.CurrentNode.Print()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   436
        for nodeid, node in nodelist.SlaveNodes.items():
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   437
            print "SlaveNode name=%s id=0x%2.2X :"%(node["Name"], nodeid)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   438
            node["Node"].Print()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   439
            
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   440
    #filepath = "/home/deobox/beremiz/test_nodelist/listlocations.txt"
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   441
    filepath = "/home/deobox/Desktop/TestMapping/listlocations.txt"
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   442
    
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   443
    file = open(filepath,'r')
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   444
    locations = [location.split(' ') for location in [line.strip() for line in file.readlines() if len(line) > 0]] 
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   445
    file.close()
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   446
    GenerateConciseDCF(locations, 32, nodelist)
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   447
    print "MasterNode :"
f45fd4cd3832 add auto-mapping tool (config_utils)
greg
parents:
diff changeset
   448
    manager.GetCurrentNode().Print()