tool/CommandPdos.cpp
branchstable-1.5
changeset 2436 960cc1bb6b4a
parent 2421 bc2d4bf9cbe5
equal deleted inserted replaced
2435:4151f6f4c3e1 2436:960cc1bb6b4a
    27  *
    27  *
    28  ****************************************************************************/
    28  ****************************************************************************/
    29 
    29 
    30 #include <iostream>
    30 #include <iostream>
    31 #include <iomanip>
    31 #include <iomanip>
       
    32 #include <cstring>
    32 using namespace std;
    33 using namespace std;
    33 
    34 
    34 #include "CommandPdos.h"
    35 #include "CommandPdos.h"
    35 #include "MasterDevice.h"
    36 #include "MasterDevice.h"
    36 
    37 
    49 
    50 
    50     str << binaryBaseName << " " << getName() << " [OPTIONS]" << endl
    51     str << binaryBaseName << " " << getName() << " [OPTIONS]" << endl
    51         << endl
    52         << endl
    52         << getBriefDescription() << endl
    53         << getBriefDescription() << endl
    53         << endl
    54         << endl
    54         << "The information is displayed in three layers, which are" << endl
    55         << "For the default skin (see --skin option) the information" << endl
       
    56         << "is displayed in three layers, which are" << endl
    55         << "indented accordingly:" << endl
    57         << "indented accordingly:" << endl
    56         << endl
    58         << endl
    57         << "1) Sync managers - Contains the sync manager information" << endl
    59         << "1) Sync managers - Contains the sync manager information" << endl
    58         << "   from the SII: Index, physical start address, default" << endl
    60         << "   from the SII: Index, physical start address, default" << endl
    59         << "   size, control register and enable word. Example:" << endl
    61         << "   size, control register and enable word. Example:" << endl
    74         << "   PDO entry 0x3101:01, 8 bit, \"Status\"" << endl
    76         << "   PDO entry 0x3101:01, 8 bit, \"Status\"" << endl
    75         << endl
    77         << endl
    76         << "Note, that the displayed PDO assignment and PDO mapping" << endl
    78         << "Note, that the displayed PDO assignment and PDO mapping" << endl
    77         << "information can either originate from the SII or from the" << endl
    79         << "information can either originate from the SII or from the" << endl
    78         << "CoE communication area." << endl
    80         << "CoE communication area." << endl
       
    81         << endl
       
    82         << "The \"etherlab\" skin outputs a template configuration" << endl
       
    83         << "for EtherLab's generic EtherCAT slave block." << endl
    79         << endl
    84         << endl
    80         << "Command-specific options:" << endl
    85         << "Command-specific options:" << endl
    81         << "  --alias    -a <alias>" << endl
    86         << "  --alias    -a <alias>" << endl
    82         << "  --position -p <pos>    Slave selection. See the help of" << endl
    87         << "  --position -p <pos>    Slave selection. See the help of" << endl
    83         << "                         the 'slaves' command." << endl
    88         << "                         the 'slaves' command." << endl
       
    89         << "  --skin     -s <skin>   Choose output skin. Possible values are"
       
    90         << endl
       
    91         << "                         \"default\" and \"etherlab\"." << endl
    84         << endl
    92         << endl
    85         << numericInfo();
    93         << numericInfo();
    86 
    94 
    87     return str.str();
    95     return str.str();
    88 }
    96 }
   100         stringstream err;
   108         stringstream err;
   101         err << "'" << getName() << "' takes no arguments!";
   109         err << "'" << getName() << "' takes no arguments!";
   102         throwInvalidUsageException(err);
   110         throwInvalidUsageException(err);
   103     }
   111     }
   104 
   112 
   105 	masterIndices = getMasterIndices();
   113     if (getSkin().empty() || getSkin() == "default") {
   106     multiMaster = masterIndices.size() > 1;
   114         masterIndices = getMasterIndices();
   107     MasterIndexList::const_iterator mi;
   115         multiMaster = masterIndices.size() > 1;
   108     for (mi = masterIndices.begin();
   116         MasterIndexList::const_iterator mi;
   109             mi != masterIndices.end(); mi++) {
   117         for (mi = masterIndices.begin();
   110         MasterDevice m(*mi);
   118                 mi != masterIndices.end(); mi++) {
   111         m.open(MasterDevice::Read);
   119             MasterDevice m(*mi);
   112         slaves = selectedSlaves(m);
   120             m.open(MasterDevice::Read);
   113         showHeader = multiMaster || slaves.size() > 1;
   121             slaves = selectedSlaves(m);
   114 
   122             showHeader = multiMaster || slaves.size() > 1;
   115         for (si = slaves.begin(); si != slaves.end(); si++) {
   123 
   116             listSlavePdos(m, *si, showHeader);
   124             for (si = slaves.begin(); si != slaves.end(); si++) {
   117         }
   125                 listSlavePdos(m, *si, showHeader);
       
   126             }
       
   127         }
       
   128     }
       
   129     else if (getSkin() == "etherlab") {
       
   130         masterIndices = getMasterIndices();
       
   131         MasterIndexList::const_iterator mi;
       
   132         for (mi = masterIndices.begin();
       
   133                 mi != masterIndices.end(); mi++) {
       
   134             MasterDevice m(*mi);
       
   135             m.open(MasterDevice::Read);
       
   136             slaves = selectedSlaves(m);
       
   137 
       
   138             for (si = slaves.begin(); si != slaves.end(); si++) {
       
   139                 etherlabConfig(m, *si);
       
   140             }
       
   141         }
       
   142     }
       
   143     else {
       
   144         stringstream err;
       
   145         err << "Invalid skin '" << getSkin() << "'!";
       
   146         throwInvalidUsageException(err);
   118     }
   147     }
   119 }
   148 }
   120 
   149 
   121 /****************************************************************************/
   150 /****************************************************************************/
   122 
   151 
   175             }
   204             }
   176         }
   205         }
   177     }
   206     }
   178 }
   207 }
   179 
   208 
       
   209 /****************************************************************************/
       
   210 
       
   211 void CommandPdos::etherlabConfig(
       
   212         MasterDevice &m,
       
   213         const ec_ioctl_slave_t &slave
       
   214         )
       
   215 {
       
   216     ec_ioctl_slave_sync_t sync;
       
   217     ec_ioctl_slave_sync_pdo_t pdo;
       
   218     ec_ioctl_slave_sync_pdo_entry_t entry;
       
   219     unsigned int i, j, k;
       
   220 
       
   221     cout << "%" << endl
       
   222         << "% Master " << m.getIndex() << ", Slave " << slave.position;
       
   223     if (strlen(slave.order)) {
       
   224         cout << ", \"" << slave.order << "\"";
       
   225     }
       
   226     cout << endl
       
   227         << "%" << endl
       
   228         << "function rv = slave" << slave.position << "()" << endl << endl;
       
   229 
       
   230     cout << "% Slave configuration" << endl << endl;
       
   231 
       
   232     cout << "rv.SlaveConfig.vendor = " << slave.vendor_id << ";" << endl
       
   233         << "rv.SlaveConfig.product = hex2dec('"
       
   234         << hex << setfill('0') << setw(8) << slave.product_code << "');"
       
   235         << endl
       
   236         << "rv.SlaveConfig.description = '" << slave.order << "';" << endl
       
   237         << "rv.SlaveConfig.sm = { ..." << endl;
       
   238 
       
   239     /* slave configuration */
       
   240     for (i = 0; i < slave.sync_count; i++) {
       
   241         m.getSync(&sync, slave.position, i);
       
   242 
       
   243         cout << "    {" << dec << i << ", "
       
   244             << (sync.control_register & 0x04 ? 0 : 1) << ", {" << endl;
       
   245 
       
   246         for (j = 0; j < sync.pdo_count; j++) {
       
   247             m.getPdo(&pdo, slave.position, i, j);
       
   248 
       
   249             cout << "        {hex2dec('" <<
       
   250                 hex << setfill('0') << setw(4) << pdo.index << "'), ["
       
   251                 << endl;
       
   252 
       
   253             for (k = 0; k < pdo.entry_count; k++) {
       
   254                 m.getPdoEntry(&entry, slave.position, i, j, k);
       
   255 
       
   256                 cout << "            hex2dec('"
       
   257                     << hex << setfill('0') << setw(4) << entry.index
       
   258                     << "'), hex2dec('"
       
   259                     << setw(2) << (unsigned int) entry.subindex
       
   260                     << "'), " << dec << setfill(' ') << setw(3)
       
   261                     << (unsigned int) entry.bit_length
       
   262                     << "; ..." << endl;
       
   263             }
       
   264 
       
   265             cout << "            ]}, ..." << endl;
       
   266         }
       
   267         cout << "        }}, ..." << endl;
       
   268     }
       
   269 
       
   270     cout << "    };" << endl << endl;
       
   271 
       
   272     cout << "% Port configuration" << endl << endl;
       
   273 
       
   274     unsigned int input = 1, output = 1;
       
   275     for (i = 0; i < slave.sync_count; i++) {
       
   276         m.getSync(&sync, slave.position, i);
       
   277 
       
   278         for (j = 0; j < sync.pdo_count; j++) {
       
   279             m.getPdo(&pdo, slave.position, i, j);
       
   280 
       
   281             for (k = 0; k < pdo.entry_count; k++) {
       
   282                 m.getPdoEntry(&entry, slave.position, i, j, k);
       
   283 
       
   284                 if (!entry.index) {
       
   285                     continue;
       
   286                 }
       
   287 
       
   288                 string dir;
       
   289                 unsigned int port;
       
   290 
       
   291                 if (sync.control_register & 0x04) {
       
   292                     dir = "input";
       
   293                     port = input++;
       
   294                 }
       
   295                 else {
       
   296                     dir = "output";
       
   297                     port = output++;
       
   298                 }
       
   299 
       
   300                 stringstream var;
       
   301                 var << "rv.PortConfig." << dir << "(" << dec << port
       
   302                     << ")";
       
   303 
       
   304                 cout << var.str() << ".pdo = ["
       
   305                     << i << ", " << j << ", " << k << ", 0];" << endl;
       
   306                 cout << var.str() << ".pdo_data_type = "
       
   307                     << 1000 + (unsigned int) entry.bit_length
       
   308                     << ";" << endl << endl;
       
   309             }
       
   310         }
       
   311     }
       
   312 
       
   313     cout << "end" << endl;
       
   314 }
       
   315 
   180 /*****************************************************************************/
   316 /*****************************************************************************/