[2027] | 1 | %============================================================================== |
---|
| 2 | % Function wl_initNodes() |
---|
| 3 | % |
---|
| 4 | % Input: |
---|
| 5 | % - Single number |
---|
| 6 | % - Array of struct (derived from wl_networkSetup()) |
---|
| 7 | % - Array of wl_nodes; Either optional array of numbers, or array of struct |
---|
| 8 | % |
---|
| 9 | % Output: |
---|
| 10 | % - Array of wl_nodes |
---|
| 11 | % |
---|
| 12 | %============================================================================== |
---|
| 13 | |
---|
[1915] | 14 | function nodes = wl_initNodes(varargin) |
---|
| 15 | |
---|
| 16 | if nargin == 0 |
---|
[2865] | 17 | error('Not enough arguments are provided'); |
---|
[1915] | 18 | elseif nargin == 1 |
---|
[2027] | 19 | switch( class(varargin{1}) ) |
---|
| 20 | case 'wl_node' |
---|
| 21 | nodes = varargin{1}; |
---|
| 22 | numNodes = length(nodes); |
---|
| 23 | nodeIDs = 0:(numNodes-1); % Node IDs default: 0 to (number of nodes in the array) |
---|
| 24 | case 'struct' |
---|
| 25 | numNodes = length(varargin{1}); |
---|
| 26 | nodes(numNodes) = wl_node; |
---|
[2029] | 27 | nodeIDs = varargin{1}; % Node IDs are specified in the input structure |
---|
[2027] | 28 | case 'double' |
---|
| 29 | numNodes = varargin{1}; |
---|
| 30 | nodes(numNodes) = wl_node; |
---|
| 31 | nodeIDs = 0:(numNodes-1); % Node IDs default: 0 to (number of nodes specified) |
---|
| 32 | otherwise |
---|
[2865] | 33 | error('Unknown argument. Argument is of type "%s", need "wl_node", "struct", or "double"', class(varargin{1})); |
---|
[2027] | 34 | end |
---|
| 35 | elseif nargin == 2 |
---|
| 36 | |
---|
| 37 | switch( class(varargin{1}) ) |
---|
| 38 | case 'wl_node' |
---|
| 39 | nodes = varargin{1}; |
---|
| 40 | numNodes = length(nodes); |
---|
| 41 | case 'double' |
---|
| 42 | numNodes = varargin{1}; |
---|
| 43 | nodes(numNodes) = wl_node; |
---|
| 44 | otherwise |
---|
[2865] | 45 | error('Unknown argument. Argument is of type "%s", need "wl_node" or "double"', class(varargin{1})); |
---|
[2027] | 46 | end |
---|
| 47 | |
---|
| 48 | switch( class(varargin{2}) ) |
---|
| 49 | case 'struct' |
---|
[2029] | 50 | nodeIDs = varargin{2}; % Node IDs are specified in the input structure |
---|
[2027] | 51 | case 'double' |
---|
| 52 | nodeIDs = varargin{2}; % Node IDs default: 0 to (number of nodes specified) |
---|
| 53 | otherwise |
---|
[2865] | 54 | error('Unknown argument. Argument is of type "%s", need "struct", or "double"', class(varargin{2})); |
---|
[2027] | 55 | end |
---|
[2001] | 56 | |
---|
[1915] | 57 | if(length(nodeIDs)~=numNodes) |
---|
[2865] | 58 | error('Number of nodes does not match ID vector'); |
---|
[1915] | 59 | end |
---|
[2027] | 60 | else |
---|
[2865] | 61 | error('Too many arguments provided'); |
---|
[1915] | 62 | end |
---|
[2027] | 63 | |
---|
[1915] | 64 | |
---|
[2331] | 65 | gen_error = []; |
---|
| 66 | gen_error_index = 0; |
---|
| 67 | |
---|
[1915] | 68 | for n = numNodes:-1:1 |
---|
[2001] | 69 | currNode = nodes(n); |
---|
[2027] | 70 | |
---|
| 71 | % If we are doing a network setup of the node based on the input structure then create the broadcast packet and send it |
---|
| 72 | % Note: |
---|
| 73 | % - Node must be configured with dip switches 0xF |
---|
| 74 | % - Node will initialize itself to IP address of 10.0.0.0 and wait for a broadcast message to configure itself |
---|
| 75 | % - Node will check against the serial number (only last 5 digits; "W3-a-" is not stored in EEPROM) |
---|
| 76 | if( strcmp( class( nodeIDs ), 'struct') ) |
---|
| 77 | |
---|
| 78 | configFile = which('wl_config.ini'); |
---|
[4818] | 79 | |
---|
[2027] | 80 | if(isempty(configFile)) |
---|
[2865] | 81 | error('cannot find wl_config.ini. please run wl_setup.m'); |
---|
[2027] | 82 | end |
---|
[4818] | 83 | |
---|
[2027] | 84 | readKeys = {'network','','transport',''}; |
---|
| 85 | transport = inifile(configFile,'read',readKeys); |
---|
| 86 | transport = transport{1}; |
---|
| 87 | |
---|
| 88 | switch(transport) |
---|
| 89 | case 'java' |
---|
[2034] | 90 | transport_bcast = wl_transport_eth_udp_java_bcast; |
---|
[2027] | 91 | transport_unicast = wl_transport_eth_udp_java; |
---|
[2149] | 92 | case 'wl_mex_udp' |
---|
[2084] | 93 | transport_bcast = wl_transport_eth_udp_mex_bcast; |
---|
| 94 | transport_unicast = wl_transport_eth_udp_mex; |
---|
[2027] | 95 | end |
---|
| 96 | |
---|
| 97 | % Open a broadcast transport to send the configure command to the node |
---|
| 98 | transport_bcast.open(); |
---|
| 99 | tempNode = wl_node; |
---|
| 100 | |
---|
| 101 | % Send serial number, node ID, and IP address |
---|
[4784] | 102 | myCmd = wl_cmd(tempNode.calcCmd(tempNode.GRP, tempNode.CMD_NODE_CONFIG_SETUP)); |
---|
| 103 | myCmd.addArgs(uint32(sscanf(nodeIDs(n).serialNumber, 'W3-a-%d'))); |
---|
| 104 | myCmd.addArgs(nodeIDs(n).IDUint32); |
---|
| 105 | myCmd.addArgs(nodeIDs(n).ipAddressUint32); |
---|
[2027] | 106 | transport_bcast.send('message',myCmd.serialize()); |
---|
| 107 | end |
---|
| 108 | |
---|
[2036] | 109 | % Error check to make sure that no node in the network has the same |
---|
| 110 | % ID as this host. |
---|
| 111 | configFile = which('wl_config.ini'); |
---|
[4818] | 112 | |
---|
[2036] | 113 | if(isempty(configFile)) |
---|
[2865] | 114 | error('cannot find wl_config.ini. please run wl_setup.m'); |
---|
[2036] | 115 | end |
---|
[4818] | 116 | |
---|
[2036] | 117 | readKeys = {'network','','host_ID',''}; |
---|
| 118 | hostID = inifile(configFile,'read',readKeys); |
---|
| 119 | hostID = hostID{1}; |
---|
| 120 | hostID = sscanf(hostID,'%d'); |
---|
[2331] | 121 | |
---|
| 122 | % Error check to make sure that no node in the network has the same ID as this host. |
---|
| 123 | % |
---|
| 124 | nodeID = 0; |
---|
[2036] | 125 | switch(class( nodeIDs(n) )) |
---|
| 126 | case 'double' |
---|
[2331] | 127 | nodeID = nodeIDs(n); |
---|
| 128 | if( hostID == nodeID ) |
---|
[2865] | 129 | error('Host ID is set to %d and must be unique. No node in the network can share this ID',hostID); |
---|
[2036] | 130 | end |
---|
[2038] | 131 | |
---|
[2036] | 132 | case 'struct' |
---|
[2331] | 133 | nodeID = nodeIDs(n).IDUint32; |
---|
| 134 | if( hostID == nodeID ) |
---|
[2865] | 135 | error('Host ID is set to %d and must be unique. No node in the network can share this ID',hostID); |
---|
[2036] | 136 | end |
---|
| 137 | end |
---|
[2331] | 138 | |
---|
| 139 | % Now that the node has a valid IP address, we can apply the configuration |
---|
| 140 | % |
---|
| 141 | try |
---|
| 142 | currNode.applyConfiguration(nodeIDs(n)); |
---|
| 143 | catch ME |
---|
| 144 | fprintf('\n'); |
---|
| 145 | fprintf('Error in node %d with ID = %d: ', n, nodeID ); |
---|
| 146 | ME |
---|
| 147 | fprintf('Error message follows:\n%s\n', ME.message); |
---|
| 148 | gen_error_index = gen_error_index + 1; |
---|
| 149 | gen_error( gen_error_index ) = nodeID; |
---|
[2865] | 150 | % error('Node with ID = %d is not responding. Please ensure that the node has been configured with the WLAN Exp bitstream.',nodeID) |
---|
[2331] | 151 | fprintf('\n'); |
---|
| 152 | end |
---|
[1915] | 153 | end |
---|
[2331] | 154 | |
---|
| 155 | % Output an error if there was one |
---|
| 156 | if( gen_error_index ~= 0 ) |
---|
| 157 | |
---|
| 158 | error_nodes = sprintf('['); |
---|
| 159 | for n = 1:gen_error_index |
---|
| 160 | error_nodes = sprintf('%s %d', error_nodes, gen_error(n)); |
---|
| 161 | end |
---|
| 162 | error_nodes = sprintf('%s ]', error_nodes); |
---|
| 163 | |
---|
[2865] | 164 | error('The following nodes with IDs = %s are not responding. Please ensure that the nodes have been configured with the WARPLab bitstream.', error_nodes) |
---|
[2331] | 165 | end |
---|
[1915] | 166 | |
---|
| 167 | %Send a test broadcast trigger command |
---|
[2034] | 168 | if(strcmp(class(nodes(1).trigger_manager),'wl_trigger_manager_proc')) |
---|
[2001] | 169 | |
---|
| 170 | configFile = which('wl_config.ini'); |
---|
[4818] | 171 | |
---|
[2001] | 172 | if(isempty(configFile)) |
---|
[2865] | 173 | error('cannot find wl_config.ini. please run wl_setup.m'); |
---|
[2001] | 174 | end |
---|
[4818] | 175 | |
---|
[2001] | 176 | readKeys = {'network','','transport',''}; |
---|
| 177 | transport = inifile(configFile,'read',readKeys); |
---|
| 178 | transport = transport{1}; |
---|
[1935] | 179 | |
---|
[2001] | 180 | switch(transport) |
---|
| 181 | case 'java' |
---|
[2034] | 182 | transport_bcast = wl_transport_eth_udp_java_bcast; |
---|
[2149] | 183 | case 'wl_mex_udp' |
---|
[2084] | 184 | transport_bcast = wl_transport_eth_udp_mex_bcast; |
---|
[2001] | 185 | end |
---|
| 186 | |
---|
| 187 | transport_bcast.open(); |
---|
| 188 | tempNode = wl_node; |
---|
[2017] | 189 | myCmd = wl_cmd(tempNode.calcCmd(wl_trigger_manager_proc.GRP,wl_trigger_manager_proc.CMD_TEST_TRIGGER)); |
---|
[2001] | 190 | bcastTestFlag = uint32(round(2^32 * rand)); |
---|
| 191 | myCmd.addArgs(bcastTestFlag); %Signals to the board that this is writing the received trigger test variable |
---|
| 192 | transport_bcast.send('message',myCmd.serialize()); |
---|
| 193 | |
---|
| 194 | resp = wl_triggerManagerCmd(nodes,'test_trigger'); |
---|
| 195 | |
---|
| 196 | I = find(resp~=bcastTestFlag); |
---|
[4818] | 197 | |
---|
[2001] | 198 | for nodeIndex = I |
---|
[2865] | 199 | warning('Node %d with Serial # %d failed trigger test',nodes(nodeIndex).ID,nodes(nodeIndex).serialNumber) |
---|
[2001] | 200 | end |
---|
| 201 | |
---|
| 202 | if(any(resp~=bcastTestFlag)) |
---|
[2865] | 203 | error('Broadcast triggers are not working. Please verify your ARP table has an entry for the broadcast address on your WARPLab subnet') |
---|
[2001] | 204 | end |
---|
[1935] | 205 | end |
---|
[2060] | 206 | |
---|
| 207 | nodes.wl_nodeCmd('initialize'); |
---|
[1915] | 208 | end |
---|