This example shows Execution synchronization between target computers using the Precision Time Protocol (PTP) with Raw Ethernet as transport protocol
To run this example, you need to:
Acquire two Speedgoat target computers equipped with the Intel® 82574 Ethernet card from http://www.speedgoat.ch/.
Acquire a PTP transparent clock switch, such as the EDS-405A-PTP from http://www.moxa.com/. (In the next section, see an alternative setup in case you do not have a PTP switch.)
Acquire a standard network switch that has at least 3 ports, such as the LINKSYS® SD2005 5-ports switch.
In this example, the two target computers are named
Connect the network port of the development computer to a port of the LYNKSYS® Ethernet switch.
Connect the network port of each target computer that is dedicated to communicating with the development computer to a port of the LINKSYS® Ethernet switch.
Connect the network port of the Intel® 82574 Ethernet card in
TargetPC2 to ports of the EDS-405A-PTP switch respectively. If you do not have a PTP switch, either connect directly the port of the Intel® 82574 Ethernet card of
TargetPC1 to the port of the same card in
TargetPC2, or connect these two ports to ports of the LYNKSYS® Ethernet switch.
Configure the EDS-405A-PTP switch as an end-to-end transparent clock.
Enable PTP for the ports connected to
Click on the following links to open the two models:
The two models are configured for download on target computers
TargetPC2 respectively. If one of these target computers, for example
TargetPC2, does not exist in the Simulink® Real-Time™ environment configuration on your development computer, you can create and configure it by using Simulink® Real-Time™ Explorer or by typing the following command at the MATLAB® command line:
tg2 = SimulinkRealTime.addTarget('TargetPC2');
For each model, open the mask for the
IEEE 1588 Ethernet block and insert the required values for the PCI bus and slot numbers assigned to the Intel 82574 Ethernet card. For example, model
dPTPSlaveEthernet is configured to run on target computer
TargetPC2. To obtain the bus and slot numbers, type the following commands at the MATLAB® command line and look for the information for the Intel® 82574 Ethernet card:
tg2 = SimulinkRealTime.target('TargetPC2'); getPCIInfo(tg2, 'ethernet');
dPTPMasterEthernet is the PTP Master. It displays status and synchronization errors on target computer scopes and receives data from model
openedMdl = find_system('type', 'block_diagram'); masterMdl = 'dPTPMasterEthernet'; masterMdlOpen = 0; % Check if the model is already opened. If not, open it. if ~any(strcmp(masterMdl, openedMdl)) masterMdlOpen = 1; open_system(masterMdl); end
dPTPSlaveEthernet is configured as PTP Slave node. It displays status and synchronization errors on target scopes and sends three signals to model
Synchronization status: Indicates when the slave PTP clock is synchronized to the master PTP clock within the configured threshold of one microsecond.
PTP time at timer interrupt: Indicates the value in seconds of the PTP time when the real-time interrupt occurs.
Offset error: Indicates the synchronization error between the PTP time and the kernel clock that generates real-time interrupts.
Read PTP time at rising edge subsystem shows how you can log timestamps of an event with PTP. The
IEEE 1588 Read Parameter block in the subsystem is configuread to read the PTP time when the block execution starts.
slaveMdl = 'dPTPSlaveEthernet'; slaveMdlOpen = 0; % Check if the model is already opened. If not, open it. if ~any(strcmp(slaveMdl, openedMdl)) slaveMdlOpen = 1; open_system(slaveMdl); end
% Configure for a non-verbose build. set_param(masterMdl,'RTWVerbose','off'); set_param(slaveMdl,'RTWVerbose','off'); % Build and download the models onto the target computers. rtwbuild(masterMdl); rtwbuild(slaveMdl);
### Starting Simulink Real-Time build procedure for model: dPTPMasterEthernet Warning: This model contains blocks that do not handle sample time changes at runtime. To avoid incorrect results, only change the sample time in the original model, then rebuild the model. ### Successful completion of build procedure for model: dPTPMasterEthernet ### Looking for target: TargetPC1 ### Download model onto target: TargetPC1 ### Starting Simulink Real-Time build procedure for model: dPTPSlaveEthernet Warning: Checking 'Slave only' sets the parameters for the Best Master Clock Algorithm to their max values. Warning: This model contains blocks that do not handle sample time changes at runtime. To avoid incorrect results, only change the sample time in the original model, then rebuild the model. ### Successful completion of build procedure for model: dPTPSlaveEthernet ### Looking for target: TargetPC2 ### Download model onto target: TargetPC2
% Run the two models for 50 seconds tg1.start; tg2.start; pause(50); % Stop the models tg1.stop; tg2.stop;
Take a snapshot of the target computer display.
Scope 1 shows the protocol state and the synchronization status. The protocol state value is 9 for a Slave node. The synchronization status is 1 if the Slave PTP clock is synchronized to the Master PTP clock within the specified threshold of 1 microsecond. Otherwise, it is 0.
Scope 2 displays the PTP time when the
Read PTP time at rising edge triggered subsystem is triggered.
Scope 3 shows the current time offset between the Slave PTP clock and the Master PTP clock in nanoseconds.
Scope 4 shows the current time offset between the real-time interrupt clock and the PTP clock in seconds.
% Display Slave node scopes tg2.viewTargetScreen;
Scope 1 shows the protocol state. The protocol state value is 6 for the Master node.
Scope 2 displays the current PTP time in date format.
Scope 3 shows the current time offset between the real-time interrupt clock and the PTP clock in seconds, for the Master node and the Slave node (received from model
Scope 4 shows the difference between the PTP time when the real-time interrupt occurs on the Master node and the Slave node respectively.
When the Slave PTP clock is synchronized to the Master PTP clock and both the Slave and Master real-time interrupt clocks are synchronized to their respective PTP clocks, the signal on Scope 4 indicates the precision to which the execution of the two models is synchronized.
% Display Master node scopes tg1.viewTargetScreen;
The following figure shows the value displayed on Scope 4 of model
dPTPMasterEthernet when the two nodes have their PTP clocks and kernel clocks synchronized.
% Retrieve logged data for slave nodesdPTPMa logDataMaster = tg1.OutputLog; syncIndex = find(logDataMaster(:,1) ~= 0, 1, 'first'); clockDiff = logDataMaster(syncIndex +100:end,2); figh = findobj('Name', 'ptpexample'); % Does the plot figure exist? if isempty(figh) figh = figure; % No: Create figure set(figh, 'Name', 'ptpexample', 'NumberTitle', 'off'); else figure(figh); % Yes: Make it the current figure end % Plot difference of PTP time at real-time interrupt for the two models plot(clockDiff, '.'); xlabel('Sample'); ylabel('Time difference (seconds)'); title('Execution synchronization precision'); drawnow;
% Close the model if we opened them. if (masterMdlOpen) bdclose(masterMdl); end if (slaveMdlOpen) bdclose(slaveMdl); end