canfestival/config_utils.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Tue, 15 Aug 2017 15:50:30 +0300
changeset 1740 b789b695b5c6
parent 1739 ec153828ded2
child 1741 dd94b9a68c61
permissions -rw-r--r--
clean-up: fix PEP8 E231 missing whitespace after ':' or ','
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
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     6
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
     8
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
     9
# See COPYING file for copyrights details.
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    10
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    14
# of the License, or (at your option) any later version.
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    15
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    19
# GNU General Public License for more details.
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    20
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1278
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
58
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
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    28
IECToCOType = {
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    29
    "BOOL":    0x01,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    30
    "SINT":    0x02,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    31
    "INT":     0x03,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    32
    "DINT":    0x04,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    33
    "LINT":    0x10,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    34
    "USINT":   0x05,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    35
    "UINT":    0x06,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    36
    "UDINT":   0x07,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    37
    "ULINT":   0x1B,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    38
    "REAL":    0x08,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    39
    "LREAL":   0x11,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    40
    "STRING":  0x09,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    41
    "BYTE":    0x05,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    42
    "WORD":    0x06,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    43
    "DWORD":   0x07,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    44
    "LWORD":   0x1B,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    45
    "WSTRING": 0x0B
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    46
}
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    47
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
    48
# Constants for PDO types
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    49
RPDO = 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    50
TPDO = 2
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    51
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    52
SlavePDOType = {"I": TPDO, "Q": RPDO}
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    53
InvertPDOType = {RPDO: TPDO, TPDO: RPDO}
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    54
PDOTypeBaseIndex = {RPDO: 0x1400, TPDO: 0x1800}
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    55
PDOTypeBaseCobId = {RPDO: 0x200, TPDO: 0x180}
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    56
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    57
VariableIncrement = 0x100
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    58
VariableStartIndex = {TPDO: 0x2000, RPDO: 0x4000}
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    59
VariableDirText = {TPDO: "__I", RPDO: "__Q"}
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    60
VariableTypeOffset = dict(zip(["", "X", "B", "W", "D", "L"], range(6)))
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    61
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    62
TrashVariables = [(1, 0x01), (8, 0x05), (16, 0x06), (32, 0x07), (64, 0x1B)]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    63
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    64
#-------------------------------------------------------------------------------
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    65
#                  Specific exception for PDO mapping errors
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    66
#-------------------------------------------------------------------------------
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    67
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    68
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    69
class PDOmappingException(Exception):
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    70
    pass
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    71
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
    72
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    73
def LE_to_BE(value, size):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    74
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    75
    Convert Little Endian to Big Endian
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    76
    @param value: value expressed in integer
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    77
    @param size: number of bytes generated
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    78
    @return: a string containing the value converted
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    79
    """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
    80
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    81
    data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    82
    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
    83
    list_car.reverse()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    84
    return "".join([chr(int(car, 16)) for car in list_car])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    85
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    86
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    87
def GetNodePDOIndexes(node, type, parameters = False):
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
    Find the PDO indexes of a node
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
    90
    @param node: node
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    91
    @param type: type of PDO searched (RPDO or TPDO or both)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    92
    @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
    93
    @return: a list of indexes found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    94
    """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
    95
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    96
    indexes = []
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    97
    if type & RPDO:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
    98
        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
    99
    if type & TPDO:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   100
        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
   101
    if not parameters:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   102
        return [idx + 0x200 for idx in indexes]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   103
    else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   104
        return indexes
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   105
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   106
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   107
def SearchNodePDOMapping(loc_infos, node):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   108
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   109
    Find the PDO indexes of a node
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   110
    @param node: node
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   111
    @param type: type of PDO searched (RPDO or TPDO or both)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   112
    @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
   113
    @return: a list of indexes found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   114
    """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   115
270
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   116
    model = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   117
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   118
    for PDOidx in GetNodePDOIndexes(node, loc_infos["pdotype"]):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   119
        values = node.GetEntry(PDOidx)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   120
        if values != None:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   121
            for subindex, mapping in enumerate(values):
270
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   122
                if subindex != 0 and mapping & 0xFFFFFF00 == model:
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   123
                    return PDOidx, subindex
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   124
    return None
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   125
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
def GeneratePDOMappingDCF(idx, cobid, transmittype, pdomapping):
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
    Build concise DCF value for configuring a PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   130
    @param idx: index of PDO parameters
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   131
    @param cobid: PDO generated COB ID
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   132
    @param transmittype : PDO transmit type
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   133
    @param pdomapping: list of PDO mappings
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   134
    @return: a tuple of value and number of parameters to add to DCF
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   135
    """
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   136
1278
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   137
    dcfdata=[]
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   138
    # Create entry for RPDO or TPDO parameters and Disable PDO
1278
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   139
    #           ---- INDEX -----   --- SUBINDEX ----   ----- SIZE ------   ------ DATA ------
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   140
    dcfdata += [LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(0x80000000 + cobid, 4)]
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   141
    # Set Transmit type
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   142
    dcfdata += [LE_to_BE(idx, 2) + LE_to_BE(0x02, 1) + LE_to_BE(0x01, 4) + LE_to_BE(transmittype, 1)]
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   143
    if len(pdomapping) > 0:
1278
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   144
        # Disable Mapping
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   145
        dcfdata += [LE_to_BE(idx + 0x200, 2) + LE_to_BE(0x00, 1) + LE_to_BE(0x01, 4) + LE_to_BE(0x00, 1)]
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   146
        # Map Variables
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   147
        for subindex, (name, loc_infos) in enumerate(pdomapping):
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   148
            value = (loc_infos["index"] << 16) + (loc_infos["subindex"] << 8) + loc_infos["size"]
1278
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   149
            dcfdata += [LE_to_BE(idx + 0x200, 2) + LE_to_BE(subindex + 1, 1) + LE_to_BE(0x04, 4) + LE_to_BE(value, 4)]
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   150
        # Re-enable Mapping
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   151
        dcfdata += [LE_to_BE(idx + 0x200, 2) + LE_to_BE(0x00, 1) + LE_to_BE(0x01, 4) + LE_to_BE(len(pdomapping), 1)]
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   152
    # Re-Enable PDO
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   153
    dcfdata += [LE_to_BE(idx, 2) + LE_to_BE(0x01, 1) + LE_to_BE(0x04, 4) + LE_to_BE(cobid, 4)]
74afc7e86d00 CanFestival plugin fills master's DCF to prepare PDO mappings in a way conform to DSP-301
Edouard Tisserant
parents: 721
diff changeset
   154
    return "".join(dcfdata), len(dcfdata)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   155
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   156
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   157
class ConciseDCFGenerator:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   158
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   159
    def __init__(self, nodelist, nodename):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   160
        # Dictionary of location informations classed by name
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   161
        self.IECLocations = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   162
        # Dictionary of location that have not been mapped yet
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   163
        self.LocationsNotMapped = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   164
        # Dictionary of location informations classed by name
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   165
        self.MasterMapping = {}
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   166
        # List of COB IDs available
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   167
        self.ListCobIDAvailable = range(0x180, 0x580)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   168
        # Dictionary of mapping value where unexpected variables are stored
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   169
        self.TrashVariables = {}
163
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   170
        # Dictionary of pointed variables
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   171
        self.PointedVariables = {}
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   172
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   173
        self.NodeList = nodelist
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   174
        self.Manager = self.NodeList.Manager
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   175
        self.MasterNode = self.Manager.GetCurrentNodeCopy()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   176
        self.MasterNode.SetNodeName(nodename)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   177
        self.PrepareMasterNode()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   178
163
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   179
    def GetPointedVariables(self):
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   180
        return self.PointedVariables
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   181
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   182
    def RemoveUsedNodeCobId(self, node):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   183
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   184
        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
   185
        @param node: node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   186
        @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
   187
        """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   188
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   189
        # Get list of all node TPDO and RPDO indexes
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   190
        nodeRpdoIndexes = GetNodePDOIndexes(node, RPDO, True)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   191
        nodeTpdoIndexes = GetNodePDOIndexes(node, TPDO, True)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   192
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   193
        # 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
   194
        for PdoIdx in nodeRpdoIndexes + nodeTpdoIndexes:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   195
            pdo_cobid = node.GetEntry(PdoIdx, 0x01)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   196
            # Extract COB ID, if PDO isn't active
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   197
            if pdo_cobid > 0x600:
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   198
                pdo_cobid -= 0x80000000
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   199
            # Remove COB ID from the list of available COB ID
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   200
            if pdo_cobid in self.ListCobIDAvailable:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   201
                self.ListCobIDAvailable.remove(pdo_cobid)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   202
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   203
        return len(nodeRpdoIndexes), len(nodeTpdoIndexes)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   204
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   205
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   206
    def PrepareMasterNode(self):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   207
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   208
        Add mandatory entries for DCF generation into MasterNode.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   209
        """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   210
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   211
        # Adding DCF entry into Master node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   212
        if not self.MasterNode.IsEntry(0x1F22):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   213
            self.MasterNode.AddEntry(0x1F22, 1, "")
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   214
        self.Manager.AddSubentriesToCurrent(0x1F22, 127, self.MasterNode)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   215
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   216
        # Adding trash mappable variables for unused mapped datas
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   217
        idxTrashVariables = 0x2000 + self.MasterNode.GetNodeID()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   218
        # Add an entry for storing unexpected all variable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   219
        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
   220
        for subidx, (size, typeidx) in enumerate(TrashVariables):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   221
            # Add a subentry for storing unexpected variable of this size
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   222
            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
   223
            self.Manager.SetCurrentEntry(idxTrashVariables, subidx + 1, typeidx, "type", None, self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   224
            # Store the mapping value for this entry
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   225
            self.TrashVariables[size] = (idxTrashVariables << 16) + ((subidx + 1) << 8) + size
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   226
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   227
        RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(self.MasterNode)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   228
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   229
        # Store the indexes of the first RPDO and TPDO available for MasterNode
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   230
        self.CurrentPDOParamsIdx = {RPDO: 0x1400 + RPDOnumber, TPDO: 0x1800 + TPDOnumber}
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   231
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   232
        # Prepare MasterNode with all nodelist slaves
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   233
        for idx, (nodeid, nodeinfos) in enumerate(self.NodeList.SlaveNodes.items()):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   234
            node = nodeinfos["Node"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   235
            node.SetNodeID(nodeid)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   236
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   237
            RPDOnumber, TPDOnumber = self.RemoveUsedNodeCobId(node)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   238
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   239
            # Get Slave's default SDO server parameters
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   240
            RSDO_cobid = node.GetEntry(0x1200, 0x01)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   241
            if not RSDO_cobid:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   242
                RSDO_cobid = 0x600 + nodeid
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   243
            TSDO_cobid = node.GetEntry(0x1200, 0x02)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   244
            if not TSDO_cobid:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   245
                TSDO_cobid = 0x580 + nodeid
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   246
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   247
            # Configure Master's SDO parameters entries
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   248
            self.Manager.ManageEntriesOfCurrent([0x1280 + idx], [], self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   249
            self.MasterNode.SetEntry(0x1280 + idx, 0x01, RSDO_cobid)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   250
            self.MasterNode.SetEntry(0x1280 + idx, 0x02, TSDO_cobid)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   251
            self.MasterNode.SetEntry(0x1280 + idx, 0x03, nodeid)
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   252
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   253
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   254
    def GetMasterNode(self):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   255
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   256
        Return MasterNode.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   257
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   258
        return self.MasterNode
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   259
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   260
    def AddParamsToDCF(self, nodeid, data, nbparams):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   261
        """
155
515f3fbd1173 Fixed typos in config_utils comments
etisserant
parents: 150
diff changeset
   262
        Add entry to DCF, for the requested nodeID
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   263
        @param nodeid: id of the slave (int)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   264
        @param data: data to add to slave DCF (string)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   265
        @param nbparams: number of params added to slave DCF (int)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   266
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   267
        # Get current DCF for slave
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   268
        nodeDCF = self.MasterNode.GetEntry(0x1F22, nodeid)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   269
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   270
        # Extract data and number of params in current DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   271
        if nodeDCF != None and nodeDCF != '':
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   272
            tmpnbparams = [i for i in nodeDCF[:4]]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   273
            tmpnbparams.reverse()
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   274
            nbparams += int(''.join(["%2.2x" % ord(i) for i in tmpnbparams]), 16)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   275
            data = nodeDCF[4:] + data
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   276
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   277
        # Build new DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   278
        dcf = LE_to_BE(nbparams, 0x04) + data
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   279
        # Set new DCF for slave
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   280
        self.MasterNode.SetEntry(0x1F22, nodeid, dcf)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   281
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   282
    def GetEmptyPDO(self, nodeid, pdotype, start_index=None):
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   283
        """
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   284
        Search a not configured PDO for a slave
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   285
        @param node: the slave node object
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   286
        @param pdotype: type of PDO to generated (RPDO or TPDO)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   287
        @param start_index: Index where search must start (default: None)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   288
        @return tuple of PDO index, COB ID and number of subindex defined
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   289
        """
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   290
        # If no start_index defined, start with PDOtype base index
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   291
        if start_index is None:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   292
            index = PDOTypeBaseIndex[pdotype]
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   293
        else:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   294
            index = start_index
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   295
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   296
        # Search for all PDO possible index until find a configurable PDO
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   297
        # starting from start_index
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   298
        while index < PDOTypeBaseIndex[pdotype] + 0x200:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   299
            values = self.NodeList.GetSlaveNodeEntry(nodeid, index + 0x200)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   300
            if values != None and values[0] > 0:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   301
                # Check that all subindex upper than 0 equal 0 => configurable PDO
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   302
                if reduce(lambda x, y: x and y, map(lambda x: x == 0, values[1:]), True):
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   303
                    cobid = self.NodeList.GetSlaveNodeEntry(nodeid, index, 1)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   304
                    # If no COB ID defined in PDO, generate a new one (not used)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   305
                    if cobid == 0:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   306
                        if len(self.ListCobIDAvailable) == 0:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   307
                            return None
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   308
                        # Calculate COB ID from standard values
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   309
                        if index < PDOTypeBaseIndex[pdotype] + 4:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   310
                            cobid = PDOTypeBaseCobId[pdotype] + 0x100 * (index - PDOTypeBaseIndex[pdotype]) + nodeid
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   311
                        if cobid not in self.ListCobIDAvailable:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   312
                            cobid = self.ListCobIDAvailable.pop(0)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   313
                    return index, cobid, values[0]
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   314
            index += 1
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   315
        return None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   316
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   317
    def AddPDOMapping(self, nodeid, pdotype, pdoindex, pdocobid, pdomapping, sync_TPDOs):
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   318
        """
155
515f3fbd1173 Fixed typos in config_utils comments
etisserant
parents: 150
diff changeset
   319
        Record a new mapping request for a slave, and add related slave config to the DCF
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   320
        @param nodeid: id of the slave (int)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   321
        @param pdotype: type of PDO to generated (RPDO or TPDO)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   322
        @param pdomapping: list od variables to map with PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   323
        """
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   324
        # Add an entry to MasterMapping
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   325
        self.MasterMapping[pdocobid] = {"type": InvertPDOType[pdotype],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   326
            "mapping": [None] + [(loc_infos["type"], name) for name, loc_infos in pdomapping]}
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   327
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   328
        # Return the data to add to DCF
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   329
        if sync_TPDOs:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   330
            return GeneratePDOMappingDCF(pdoindex, pdocobid, 0x01, pdomapping)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   331
        else:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   332
            return GeneratePDOMappingDCF(pdoindex, pdocobid, 0xFF, pdomapping)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   333
        return 0, ""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   334
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   335
    def GenerateDCF(self, locations, current_location, sync_TPDOs):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   336
        """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   337
        Generate Concise DCF of MasterNode for the locations list given
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   338
        @param locations: list of locations to be mapped
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   339
        @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
   340
        @param sync_TPDOs: indicate if TPDO must be synchronous
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   341
        """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   342
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   343
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   344
        #               Verify that locations correspond to real slave variables
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   345
        #-------------------------------------------------------------------------------
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   346
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   347
        # 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
   348
        for location in locations:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   349
            COlocationtype = IECToCOType[location["IEC_TYPE"]]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   350
            name = location["NAME"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   351
            if name in self.IECLocations:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   352
                if self.IECLocations[name]["type"] != COlocationtype:
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   353
                    raise PDOmappingException, _("Type conflict for location \"%s\"") % name
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   354
            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   355
                # Get only the part of the location that concern this node
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   356
                loc = location["LOC"][len(current_location):]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   357
                # loc correspond to (ID, INDEX, SUBINDEX [,BIT])
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   358
                if len(loc) not in (2, 3, 4):
361
331d698e1118 Adding support for internationalization
laurent
parents: 340
diff changeset
   359
                    raise PDOmappingException, _("Bad location size : %s") % str(loc)
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   360
                elif len(loc) == 2:
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   361
                    continue
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   362
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   363
                direction = location["DIR"]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   364
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   365
                sizelocation = location["SIZE"]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   366
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   367
                # Extract and check nodeid
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   368
                nodeid, index, subindex = loc[:3]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   369
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   370
                # Check Id is in slave node list
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   371
                if nodeid not in self.NodeList.SlaveNodes.keys():
1581
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   372
                    raise PDOmappingException, _("Non existing node ID : {a1} (variable {a2})").format(a1 = nodeid, a2 = name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   373
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   374
                # Get the model for this node (made from EDS)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   375
                node = self.NodeList.SlaveNodes[nodeid]["Node"]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   376
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   377
                # Extract and check index and subindex
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   378
                if not node.IsEntry(index, subindex):
1581
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   379
                    msg = _("No such index/subindex ({a1},{a2}) in ID : {a3} (variable {a4})").\
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   380
                          format(a1 = "%x" % index, a2 ="%x" % subindex, a3 = nodeid, a4 = name)
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   381
                    raise PDOmappingException, msg
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   382
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   383
                # Get the entry info
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   384
                subentry_infos = node.GetSubentryInfos(index, subindex)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   385
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   386
                # If a PDO mappable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   387
                if subentry_infos and subentry_infos["pdo"]:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   388
                    if sizelocation == "X" and len(loc) > 3:
61
a20fb174f46e Bug on bit locations generation fixed
lbessard
parents: 59
diff changeset
   389
                        numbit = loc[3]
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   390
                    elif sizelocation != "X" and len(loc) > 3:
1581
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   391
                        msg = _("Cannot set bit offset for non bool '{a1}' variable (ID:{a2},Idx:{a3},sIdx:{a4}))").\
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   392
                              format(a1 = name, a2 = nodeid, a3 = "%x" % index, a4 = "%x" % subindex)
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   393
                        raise PDOmappingException, msg
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   394
                    else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   395
                        numbit = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   396
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   397
                    if location["IEC_TYPE"] != "BOOL" and subentry_infos["type"] != COlocationtype:
1722
89824afffef2 fix some typos
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   398
                        raise PDOmappingException, _("Invalid type \"{a1}\"-> {a2} != {a3}  for location \"{a4}\"").\
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   399
                            format(a1 = location["IEC_TYPE"], a2 = COlocationtype, a3 = subentry_infos["type"], a4 = name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   400
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   401
                    typeinfos = node.GetEntryInfos(COlocationtype)
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   402
                    self.IECLocations[name] = {
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   403
                        "type":         COlocationtype,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   404
                        "pdotype":      SlavePDOType[direction],
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   405
                        "nodeid":       nodeid,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   406
                        "index":        index,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   407
                        "subindex":     subindex,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   408
                        "bit":          numbit,
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   409
                        "size":         typeinfos["size"],
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   410
                        "sizelocation": sizelocation
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   411
                    }
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   412
                else:
1581
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   413
                    raise PDOmappingException, _("Not PDO mappable variable : '{a1}' (ID:{a2},Idx:{a3},sIdx:{a4}))").\
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   414
                        format(a1 = name, a2 = nodeid, a3 = "%x" % index, a4 = "%x" % subindex)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   415
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   416
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   417
        #                         Search for locations already mapped
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   418
        #-------------------------------------------------------------------------------
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   419
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   420
        for name, locationinfos in self.IECLocations.items():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   421
            node = self.NodeList.SlaveNodes[locationinfos["nodeid"]]["Node"]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   422
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   423
            # Search if slave has a PDO mapping this locations
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   424
            result = SearchNodePDOMapping(locationinfos, node)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   425
            if result != None:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   426
                index, subindex = result
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   427
                # Get COB ID of the PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   428
                cobid = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 1)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   429
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   430
                # Add PDO to MasterMapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   431
                if cobid not in self.MasterMapping.keys():
80
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   432
                    # Verify that PDO transmit type is conform to sync_TPDOs
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   433
                    transmittype = self.NodeList.GetSlaveNodeEntry(locationinfos["nodeid"], index - 0x200, 2)
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   434
                    if sync_TPDOs and transmittype != 0x01 or transmittype != 0xFF:
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   435
                        if sync_TPDOs:
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   436
                            # Change TransmitType to SYNCHRONE
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   437
                            data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0x01, [])
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   438
                        else:
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   439
                            # Change TransmitType to ASYCHRONE
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   440
                            data, nbparams = GeneratePDOMappingDCF(index - 0x200, cobid, 0xFF, [])
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   441
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   442
                        # Add entry to slave dcf to change transmit type of
80
d7eb6e22b02e Bug on Master DCF generation fixed
lbessard
parents: 78
diff changeset
   443
                        self.AddParamsToDCF(locationinfos["nodeid"], data, nbparams)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   444
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   445
                    mapping = [None]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   446
                    values = node.GetEntry(index)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   447
                    # Store the size of each entry mapped in PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   448
                    for value in values[1:]:
78
274a343d49d0 Bug on Master config generation fixed
lbessard
parents: 61
diff changeset
   449
                        if value != 0:
274a343d49d0 Bug on Master config generation fixed
lbessard
parents: 61
diff changeset
   450
                            mapping.append(value % 0x100)
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   451
                    self.MasterMapping[cobid] = {"type": InvertPDOType[locationinfos["pdotype"]], "mapping": mapping}
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   452
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   453
                # Indicate that this PDO entry must be saved
270
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   454
                if locationinfos["bit"] is not None:
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   455
                    if not isinstance(self.MasterMapping[cobid]["mapping"][subindex], ListType):
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   456
                        self.MasterMapping[cobid]["mapping"][subindex] = [1] * self.MasterMapping[cobid]["mapping"][subindex]
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   457
                    if locationinfos["bit"] < len(self.MasterMapping[cobid]["mapping"][subindex]):
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   458
                        self.MasterMapping[cobid]["mapping"][subindex][locationinfos["bit"]] = (locationinfos["type"], name)
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   459
                else:
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   460
                    self.MasterMapping[cobid]["mapping"][subindex] = (locationinfos["type"], name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   461
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   462
            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   463
                # Add location to those that haven't been mapped yet
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   464
                if locationinfos["nodeid"] not in self.LocationsNotMapped.keys():
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   465
                    self.LocationsNotMapped[locationinfos["nodeid"]] = {TPDO: [], RPDO: []}
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   466
                self.LocationsNotMapped[locationinfos["nodeid"]][locationinfos["pdotype"]].append((name, locationinfos))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   467
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   468
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   469
        #                         Build concise DCF for the others locations
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   470
        #-------------------------------------------------------------------------------
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   471
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   472
        for nodeid, locations in self.LocationsNotMapped.items():
61
a20fb174f46e Bug on bit locations generation fixed
lbessard
parents: 59
diff changeset
   473
            node = self.NodeList.SlaveNodes[nodeid]["Node"]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   474
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   475
            # Initialize number of params and data to add to node DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   476
            nbparams = 0
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   477
            dataparams = ""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   478
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   479
            # Generate the best PDO mapping for each type of PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   480
            for pdotype in (TPDO, RPDO):
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   481
                if len(locations[pdotype]) > 0:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   482
                    pdosize = 0
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   483
                    pdomapping = []
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   484
                    result = self.GetEmptyPDO(nodeid, pdotype)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   485
                    if result is None:
415
339fa2542481 improved english spelling and grammar and internationalization updated
laurent
parents: 361
diff changeset
   486
                        raise PDOmappingException, _("Unable to define PDO mapping for node %02x") % nodeid
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   487
                    pdoindex, pdocobid, pdonbparams = result
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   488
                    for name, loc_infos in locations[pdotype]:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   489
                        pdosize += loc_infos["size"]
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   490
                        # If pdo's size > 64 bits
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   491
                        if pdosize > 64 or len(pdomapping) >= pdonbparams:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   492
                            # Generate a new PDO Mapping
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   493
                            data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdoindex, pdocobid, pdomapping, sync_TPDOs)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   494
                            dataparams += data
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   495
                            nbparams += nbaddedparams
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   496
                            pdosize = loc_infos["size"]
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   497
                            pdomapping = [(name, loc_infos)]
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   498
                            result = self.GetEmptyPDO(nodeid, pdotype, pdoindex + 1)
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   499
                            if result is None:
415
339fa2542481 improved english spelling and grammar and internationalization updated
laurent
parents: 361
diff changeset
   500
                                raise PDOmappingException, _("Unable to define PDO mapping for node %02x") % nodeid
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   501
                            pdoindex, pdocobid, pdonbparams = result
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   502
                        else:
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   503
                            pdomapping.append((name, loc_infos))
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   504
                    # If there isn't locations yet but there is still a PDO to generate
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   505
                    if len(pdomapping) > 0:
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   506
                        # Generate a new PDO Mapping
340
651b8fb572e7 Adding support for using only PDO define in EDS file and not configured for adding mapping in node
greg
parents: 307
diff changeset
   507
                        data, nbaddedparams = self.AddPDOMapping(nodeid, pdotype, pdoindex, pdocobid, pdomapping, sync_TPDOs)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   508
                        dataparams += data
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   509
                        nbparams += nbaddedparams
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   510
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   511
            # Add number of params and data to node DCF
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   512
            self.AddParamsToDCF(nodeid, dataparams, nbparams)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   513
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   514
        #-------------------------------------------------------------------------------
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   515
        #                         Master Node Configuration
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   516
        #-------------------------------------------------------------------------------
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   517
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   518
        # Generate Master's Configuration from informations stored in MasterMapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   519
        for cobid, pdo_infos in self.MasterMapping.items():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   520
            # Get next PDO index in MasterNode for this PDO type
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   521
            current_idx = self.CurrentPDOParamsIdx[pdo_infos["type"]]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   522
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   523
            # 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
   524
            for idx in GetNodePDOIndexes(self.MasterNode, pdo_infos["type"], True):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   525
                if self.MasterNode.GetEntry(idx, 1) == cobid:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   526
                    current_idx = idx
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   527
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   528
            # Add a PDO to MasterNode if not PDO have been found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   529
            if current_idx == self.CurrentPDOParamsIdx[pdo_infos["type"]]:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   530
                addinglist = [current_idx, current_idx + 0x200]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   531
                self.Manager.ManageEntriesOfCurrent(addinglist, [], self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   532
                self.MasterNode.SetEntry(current_idx, 0x01, cobid)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   533
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   534
                # Increment the number of PDO for this PDO type
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   535
                self.CurrentPDOParamsIdx[pdo_infos["type"]] += 1
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   536
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   537
            # Change the transmit type of the PDO
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   538
            if sync_TPDOs:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   539
                self.MasterNode.SetEntry(current_idx, 0x02, 0x01)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   540
            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   541
                self.MasterNode.SetEntry(current_idx, 0x02, 0xFF)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   542
270
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   543
            mapping = []
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   544
            for item in pdo_infos["mapping"]:
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   545
                if isinstance(item, ListType):
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   546
                    mapping.extend(item)
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   547
                else:
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   548
                    mapping.append(item)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   549
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   550
            # Add some subentries to PDO mapping if there is not enough
270
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   551
            if len(mapping) > 1:
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   552
                self.Manager.AddSubentriesToCurrent(current_idx + 0x200, len(mapping) - 1, self.MasterNode)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   553
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   554
            # Generate MasterNode's PDO mapping
270
3b8fb275cf7e Bug on bit mapping in CanFestival plugin fixed
lbessard
parents: 225
diff changeset
   555
            for subindex, variable in enumerate(mapping):
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   556
                if subindex == 0:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   557
                    continue
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   558
                new_index = False
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   559
225
aed21ae6658f Some instance type test improved
lbessard
parents: 166
diff changeset
   560
                if isinstance(variable, (IntType, LongType)):
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   561
                    # If variable is an integer then variable is unexpected
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   562
                    self.MasterNode.SetEntry(current_idx + 0x200, subindex, self.TrashVariables[variable])
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   563
                else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   564
                    typeidx, varname = variable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   565
                    variable_infos = self.IECLocations[varname]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   566
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   567
                    # Calculate base index for storing variable
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   568
                    mapvariableidx = VariableStartIndex[variable_infos["pdotype"]] + \
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   569
                                     VariableTypeOffset[variable_infos["sizelocation"]] * VariableIncrement + \
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   570
                                     variable_infos["nodeid"]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   571
163
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   572
                    # Generate entry name
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   573
                    indexname = "%s%s%s_%d" % (VariableDirText[variable_infos["pdotype"]],
163
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   574
                                                 variable_infos["sizelocation"],
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   575
                                                 '_'.join(map(str, current_location)),
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   576
                                                 variable_infos["nodeid"])
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   577
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   578
                    # Search for an entry that has an empty subindex
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   579
                    while mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   580
                        # Entry doesn't exist
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   581
                        if not self.MasterNode.IsEntry(mapvariableidx):
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   582
                            # Add entry to MasterNode
163
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   583
                            self.Manager.AddMapVariableToCurrent(mapvariableidx, "beremiz"+indexname, 3, 1, self.MasterNode)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   584
                            new_index = True
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   585
                            nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   586
                        else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   587
                            # Get Number of subentries already defined
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   588
                            nbsubentries = self.MasterNode.GetEntry(mapvariableidx, 0x00)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   589
                            # 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
   590
                            if nbsubentries == 0xFF:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   591
                                mapvariableidx += 8 * VariableIncrement
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   592
                            else:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   593
                                break
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   594
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   595
                    # Verify that a not full entry has been found
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   596
                    if mapvariableidx < VariableStartIndex[variable_infos["pdotype"]] + 0x2000:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   597
                        # Generate subentry name
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   598
                        if variable_infos["bit"] != None:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   599
                            subindexname = "%(index)d_%(subindex)d_%(bit)d" % variable_infos
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   600
                        else:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   601
                            subindexname = "%(index)d_%(subindex)d" % variable_infos
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   602
                        # 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
   603
                        if not new_index:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   604
                            self.Manager.AddSubentriesToCurrent(mapvariableidx, 1, self.MasterNode)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   605
                            nbsubentries += 1
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   606
                        # Add informations to the new subentry created
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   607
                        self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"name": subindexname})
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   608
                        self.MasterNode.SetMappingEntry(mapvariableidx, nbsubentries, values = {"type": typeidx})
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   609
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   610
                        # Set value of the PDO mapping
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   611
                        typeinfos = self.Manager.GetEntryInfos(typeidx)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   612
                        if typeinfos != None:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   613
                            value = (mapvariableidx << 16) + ((nbsubentries) << 8) + typeinfos["size"]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   614
                            self.MasterNode.SetEntry(current_idx + 0x200, subindex, value)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   615
163
482ca562d414 Support for extern pointer for located variables + Preliminary slave code (broken)
etisserant
parents: 155
diff changeset
   616
                        # Add variable to pointed variables
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   617
                        self.PointedVariables[(mapvariableidx, nbsubentries)] = "%s_%s" % (indexname, subindexname)
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   618
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   619
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   620
def GenerateConciseDCF(locations, current_location, nodelist, sync_TPDOs, nodename):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   621
    """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   622
    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
   623
    @param locations: List of complete variables locations \
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   624
        [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   625
        "NAME" : name of the variable (generally "__IW0_1_2" style)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   626
        "DIR" : direction "Q","I" or "M"
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   627
        "SIZE" : size "X", "B", "W", "D", "L"
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   628
        "LOC" : tuple of interger for IEC location (0,1,2,...)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   629
        }, ...]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   630
    @param nodelist: CanFestival network editor model
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   631
    @return: a modified copy of the given CanFestival network editor model
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   632
    """
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   633
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   634
    dcfgenerator = ConciseDCFGenerator(nodelist, nodename)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   635
    dcfgenerator.GenerateDCF(locations, current_location, sync_TPDOs)
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   636
    masternode, pointers = dcfgenerator.GetMasterNode(), dcfgenerator.GetPointedVariables()
307
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   637
    # allow access to local OD from Master PLC
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   638
    pointers.update(LocalODPointers(locations, current_location, masternode))
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   639
    return masternode, pointers
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   640
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   641
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   642
def LocalODPointers(locations, current_location, slave):
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   643
    IECLocations = {}
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   644
    pointers = {}
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   645
    for location in locations:
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   646
        COlocationtype = IECToCOType[location["IEC_TYPE"]]
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   647
        name = location["NAME"]
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   648
        if name in IECLocations:
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   649
            if IECLocations[name] != COlocationtype:
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   650
                raise PDOmappingException, _("Type conflict for location \"%s\"") % name
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   651
        else:
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   652
            # Get only the part of the location that concern this node
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   653
            loc = location["LOC"][len(current_location):]
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   654
            # loc correspond to (ID, INDEX, SUBINDEX [,BIT])
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   655
            if len(loc) not in (2, 3, 4):
361
331d698e1118 Adding support for internationalization
laurent
parents: 340
diff changeset
   656
                raise PDOmappingException, _("Bad location size : %s") % str(loc)
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   657
            elif len(loc) != 2:
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   658
                continue
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   659
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   660
            # Extract and check nodeid
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   661
            index, subindex = loc[:2]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   662
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   663
            # Extract and check index and subindex
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   664
            if not slave.IsEntry(index, subindex):
1581
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   665
                raise PDOmappingException, _("No such index/subindex ({a1},{a2}) (variable {a3})").\
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   666
                    format(a1 = "%x" % index, a2 = "%x" % subindex, a3 = name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   667
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   668
            # Get the entry info
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   669
            subentry_infos = slave.GetSubentryInfos(index, subindex)
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   670
            if subentry_infos["type"] != COlocationtype:
1581
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
   671
                raise PDOmappingException, _("Invalid type \"{a1}\"-> {a2} != {a3} for location \"{a4}\"").\
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   672
                    format( a1 = location["IEC_TYPE"], a2 = COlocationtype, a3 = subentry_infos["type"], a4 = name)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   673
166
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   674
            IECLocations[name] = COlocationtype
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   675
            pointers[(index, subindex)] = name
121b18748de0 Preliminary CANopen slave generation support
etisserant
parents: 163
diff changeset
   676
    return pointers
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   677
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   678
if __name__ == "__main__":
1732
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   679
    import os
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   680
    import sys
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   681
    import getopt
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   682
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   683
    def usage():
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   684
        print """
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   685
Usage of config_utils.py test :
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   686
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   687
    %s [options]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   688
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   689
Options:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   690
    --help  (-h)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   691
            Displays help informations for config_utils
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   692
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   693
    --reset (-r)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   694
            Reset the reference result of config_utils test.
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   695
            Use with caution. Be sure that config_utils
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   696
            is currently working properly.
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   697
""" % sys.argv[0]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   698
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   699
    # Boolean that indicate if reference result must be redefined
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   700
    reset = False
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   701
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   702
    # Extract command options
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   703
    try:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   704
        opts, args = getopt.getopt(sys.argv[1:], "hr", ["help", "reset"])
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   705
    except getopt.GetoptError:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   706
        # print help information and exit:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   707
        usage()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   708
        sys.exit(2)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   709
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   710
    # Test each option
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   711
    for o, a in opts:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   712
        if o in ("-h", "--help"):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   713
            usage()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   714
            sys.exit()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   715
        elif o in ("-r", "--reset"):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   716
            reset = True
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   717
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   718
    # Extract workspace base folder
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   719
    base_folder = sys.path[0]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   720
    for i in xrange(3):
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   721
        base_folder = os.path.split(base_folder)[0]
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   722
    # Add CanFestival folder to search pathes
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   723
    sys.path.append(os.path.join(base_folder, "CanFestival-3", "objdictgen"))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   724
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   725
    from nodemanager import *
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   726
    from nodelist import *
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   727
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   728
    # Open the test nodelist contained into test_config folder
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   729
    manager = NodeManager()
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   730
    nodelist = NodeList(manager)
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   731
    result = nodelist.LoadProject("test_config")
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   732
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   733
    # List of locations, we try to map for test
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   734
    locations = [
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   735
        {"IEC_TYPE": "BYTE",  "NAME": "__IB0_1_64_24576_1", "DIR": "I", "SIZE": "B", "LOC": (0, 1, 64, 24576, 1)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   736
        {"IEC_TYPE": "INT",   "NAME": "__IW0_1_64_25601_2", "DIR": "I", "SIZE": "W", "LOC": (0, 1, 64, 25601, 2)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   737
        {"IEC_TYPE": "INT",   "NAME": "__IW0_1_64_25601_3", "DIR": "I", "SIZE": "W", "LOC": (0, 1, 64, 25601, 3)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   738
        {"IEC_TYPE": "INT",   "NAME": "__QW0_1_64_25617_2", "DIR": "Q", "SIZE": "W", "LOC": (0, 1, 64, 25617, 1)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   739
        {"IEC_TYPE": "BYTE",  "NAME": "__IB0_1_64_24578_1", "DIR": "I", "SIZE": "B", "LOC": (0, 1, 64, 24578, 1)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   740
        {"IEC_TYPE": "UDINT", "NAME": "__ID0_1_64_25638_1", "DIR": "I", "SIZE": "D", "LOC": (0, 1, 64, 25638, 1)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   741
        {"IEC_TYPE": "UDINT", "NAME": "__ID0_1_64_25638_2", "DIR": "I", "SIZE": "D", "LOC": (0, 1, 64, 25638, 2)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   742
        {"IEC_TYPE": "UDINT", "NAME": "__ID0_1_64_25638_3", "DIR": "I", "SIZE": "D", "LOC": (0, 1, 64, 25638, 3)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   743
        {"IEC_TYPE": "UDINT", "NAME": "__ID0_1_64_25638_4", "DIR": "I", "SIZE": "D", "LOC": (0, 1, 64, 25638, 4)},
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   744
        {"IEC_TYPE": "UDINT", "NAME": "__ID0_1_4096_0",     "DIR": "I", "SIZE": "D", "LOC": (0, 1, 4096, 0)}
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   745
    ]
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   746
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   747
    # Generate MasterNode configuration
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   748
    try:
307
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   749
        masternode, pointedvariables = GenerateConciseDCF(locations, (0, 1), nodelist, True, "TestNode")
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   750
    except ValueError, message:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
   751
        print "%s\nTest Failed!" % message
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   752
        sys.exit()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   753
307
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   754
    import pprint
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   755
    # Get Text corresponding to MasterNode
307
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   756
    result_node = masternode.PrintString()
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   757
    result_vars = pprint.pformat(pointedvariables)
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   758
    result = result_node + "\n********POINTERS*********\n" + result_vars + "\n"
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   759
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   760
    # If reset has been choosen
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   761
    if reset:
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   762
        # Write Text into reference result file
307
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   763
        testfile = open("test_config/result.txt", "w")
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   764
        testfile.write(result)
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   765
        testfile.close()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   766
58
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   767
        print "Reset Successful!"
c0741cc16c99 Basic CANOpen master node test compiles and run.
etisserant
parents:
diff changeset
   768
    else:
150
204d515df3dd Fixed non-regression test of config_utils in canfestival plugin
etisserant
parents: 80
diff changeset
   769
        import os
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   770
307
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   771
        testfile = open("test_config/result_tmp.txt", "w")
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   772
        testfile.write(result)
b80d3a84b8bf Updated config_utils so that command line tests work.
etisserant
parents: 270
diff changeset
   773
        testfile.close()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1722
diff changeset
   774
150
204d515df3dd Fixed non-regression test of config_utils in canfestival plugin
etisserant
parents: 80
diff changeset
   775
        os.system("diff test_config/result.txt test_config/result_tmp.txt")
204d515df3dd Fixed non-regression test of config_utils in canfestival plugin
etisserant
parents: 80
diff changeset
   776
        os.remove("test_config/result_tmp.txt")