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 www.speedgoat.com.
Acquire a PTP transparent clock switch, such as the EDS-405A-PTP from 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 TargetPC1 and TargetPC2.
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 TargetPC1 and 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 TargetPC1 and TargetPC2.
Click on the following links to open the two models:
The two models are configured for download on target computers TargetPC1 and 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');
Model dPTPMasterEthernet is the PTP Master. It displays status and synchronization errors on target computer scopes and receives data from model dPTPSlaveEthernet.
openedMdl = find_system('type', 'block_diagram'); masterMdl = fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','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
Models dPTPSlaveEthernet is configured as PTP Slave node. It displays status and synchronization errors on target scopes and sends three signals to model dPTPMasterEthernet:
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.
The 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 = fullfile(matlabroot,'toolbox','rtw','targets','xpc','xpcdemos','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 Ethernet configuration to match target computer settings.
Build and download the models onto the target computers.
set_param(masterMdl,'RTWVerbose','off'); set_param('masterMdl/IEEE 1588 Ethernet','PciBus','5','PciSlot','0'); rtwbuild(masterMdl); tg1 = slrt('TargetPC1'); load(tg1,'masterMdl');
set_param(slaveMdl,'RTWVerbose','off'); set_param('slaveMdl/IEEE 1588 Ethernet','PciBus','8','PciSlot','10'); rtwbuild(slaveMdl); tg2 = slrt('TargetPC1'); load(tg2,'slaveMdl');
Run the two models for 50 seconds
tg1.start; tg2.start; pause(50);
Stop the models
View the target computer display.
For model dPTPSlaveEthernet on TargetPC2, use the command:
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.
For model dPTPMasterEthernet on TargetPC1, use the command:
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 dPTPSlaveEthernet).
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.
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);
Does the plot figure exist?
If no, create figure.
If yes, make it the current figure.
figh = findobj('Name', 'ptpexample'); if isempty(figh) figh = figure; set(figh, 'Name', 'ptpexample', 'NumberTitle', 'off'); else figure(figh); 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');
Close the model if we opened them.
if (masterMdlOpen) bdclose(masterMdl); end if (slaveMdlOpen) bdclose(slaveMdl); end