A collaborative simulation time synchronization method based on XCOS and NS3
By using the collaborative simulation time synchronization method of XCOS and NS3 between SCILAB and NS3 software, the problem of insufficient simulation accuracy between software in the existing technology is solved, and high-precision networked control system simulation is achieved, which is both open source and low-cost. Advantages.
Patent Information
- Application Number
- CN202110398847.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-04-12
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2041-04-12
AI Technical Summary
It is difficult to achieve collaborative simulation between SCILAB and NS3 software with existing technology, resulting in insufficient simulation accuracy of communication networks and control systems in networked control systems, and it is difficult for individual software to achieve both high accuracy and low cost.
Adopt the co-simulation time synchronization method based on XCOS and NS3, install the co-simulation interface, define the buffer, initialize the simulation script, build the control system model, implement the time synchronization mechanism between software, and use socket technology and TCP protocol to ensure that SCILAB and The simulation time of NS3 software is consistent.
It realizes high-precision collaborative simulation between SCILAB and NS3 software, which is both open source and low-cost, reduces copyright risks, and improves the simulation accuracy of networked control systems.
Smart Images

Figure CN113139281B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of wireless networked control systems, and in particular to a collaborative simulation time synchronization mechanism method of an XCOS simulation module of scientific engineering computing software SCILAB and a network simulation software NS3. Background Art
[0002] With the development of communication technology, sensor technology and embedded computing, networked control systems (NCS) have been widely used in scenarios such as Internet of Vehicles, smart buildings, and Industry 4.0. In the theoretical research and engineering application of NCS, it is indispensable to verify the control algorithm through simulation software.
[0003] A collaborative simulation time synchronization method based on XCOS and NS3 is the key technology to realize the collaborative simulation of NCS by SCILAB and NS3. First, NCS forms a closed-loop control loop through the network. It is difficult for a single scientific computing software or network simulation software to achieve relatively accurate simulation of the communication network and the control system at the same time. The use of scientific computing software and network simulation software for collaborative simulation can meet the simulation requirements of NCS, and the interface between software is the basis for realizing collaborative simulation. Secondly, SCILAB and NS3 are currently the most outstanding open source software for control system and communication network simulation. The collaborative simulation of SCILAB and NS3 can have the advantages of simulation accuracy and low cost and low copyright risk of open source software by designing interfaces. Summary of the invention
[0004] The present invention aims to overcome the above-mentioned shortcomings of the prior art and provide a collaborative simulation time synchronization method based on XCOS and NS3. The present invention uses a time synchronization scheme to realize the collaborative communication simulation of SCILAB and NS3, so as to ensure that the collaborative simulation is carried out correctly and orderly. Based on the XCOS visual control system model simulation and TCP socket technology, a time synchronization scheme is proposed to make the XCOS module in SCILAB consistent with the NS3 software in simulation time, realize the simulation collaboration between the two software, and can be applied in the simulation verification of NCS control algorithm.
[0005] The technical solution adopted by the present invention to solve its technical problem is:
[0006] 1. A collaborative simulation time synchronization method based on XCOS and NS3, comprising the following steps:
[0007] (1) Install the collaborative simulation interface in NS3;
[0008] Step 11: Install the socket server program in NS3. Receive SCILAB's synchronization data through the socket;
[0009] Step 12: Define a buffer to cache SCILAB synchronization data.
[0010] (2) Install the collaborative simulation interface in SCILAB;
[0011] Step 21: Install the socket client C++ program in SCILAB. Use the mexArray instruction to get the data to be sent to the server from SCILAB and send it to NS3 through the socket;
[0012] Step 22: Execute the command ilib_mex_build(["interface name" "C++ program file name") to compile the interface program;
[0013] Step 23: Execute the command exec loader.sce to link the SCILAB dynamic library file. In SCILAB, you can call the interface program through the "interface name".
[0014] (3) Initialize NS3 simulation script;
[0015] Create a new NS3 simulation script, define the NS3 simulation module in the script, define the initialization functions: node setting function, network topology establishment function, channel attribute configuration function, network device creation function, network protocol stack installation function, simulation start and end function, and pass values to the above initialization functions through the NS3 collaborative simulation interface (1) to complete the NS3 configuration;
[0016] Step 31: Define and initialize the step size SimulationStep of the NS3 simulation synchronization cycle;
[0017] The step size in SCILAB means a sampling period, within which SCILAB notifies NS3 of the transmission and reception events between running nodes;
[0018] Step 32: Define the next simulation synchronization time TimeLimit, with the initial value being SimulationStep;
[0019] Step 3: Define and initialize the upper limit of simulation time, i.e., the simulation end time SimulationStopTime;
[0020] Step 34: define the current simulation time CurrentTime = m_events->PeekNext().key.m_ts;
[0021] Step 35: define the node sending time SendTime;
[0022] In the NS3 script, use the GetSendTime() command to obtain the time when the node sends, assign it to SendTime, and send it to SCILAB as the first bit of the data packet content, so that the data packet can carry the sending time information throughout the simulation process.
[0023] (4) Design of NS3 synchronization mechanism;
[0024] Step 41: define the jump time JumpTime=TimeLimit-SimulationStep-CurrentTime;
[0025] Step 42: Find the time of the last event that can be executed in the current synchronization cycle. Execute the command m_events->PeekNext().key.m_ts to get the end time of the next event and determine whether it is less than or equal to TimeLimit. If so, update CurrentTime=m_events->PeekNext().key.m_ts, execute the event, and continue to get the next event. Otherwise, it means that the current event is the last event that can be executed in the current synchronization cycle. At this time, move TimeLimit to the next synchronization cycle point, that is, TimeLimit+=SimulationStep, and execute steps 23 and 24 to synchronize information with SCILAB;
[0026] Step 43: NS3 synchronizes information to SCILAB. NS3 sends the event content in the received event, including timestamp, sensor signal, control quantity, etc., to SCILAB through the co-simulation interface;
[0027] Step 44: Receive the synchronization data from SCILAB, calculate the jump time, and schedule new events. The calculation formula for the jump time JumpTime is JumpTime = TimeLimit - SimulationStep - CurrentTime. After the JumpTime delay, execute Simulator::Schedule(JumpTime, &UdpEchoClientNew::Send To, this) to update the event list, where a series of sending events can be customized in the &UdpEchoClientNew::SendTo() function.
[0028] Step 45: Repeat steps 22 to 24 until TimeLimit>SimulationStopTime and the NS3 simulation ends.
[0029] (5) Construction of XCOS control system model in SCILAB;
[0030] Step 51: Build a controlled object model;
[0031] Use the user-defined module scifunc_block_m in XCOS and create a new script in the workspace to write the module function. Define the control quantity u1, system input state x1, system output state y1, and system output z1 of this module. The function of this module is to use u1 and x1 to obtain the one-step simulation results y1 and z1 of the discrete system. The module function form is [y1, z1] = plant (x1, u1). Taking the linear system as an example, the function is implemented as follows:
[0032] y1=Ax1+Bu1,z1=Cx1+Du1 (1)
[0033] Where A, C∈R n*n , B, D∈R n*m , is a given matrix;
[0034] Step 52: Build the controller model;
[0035] Use the scifunc_block_m module to create a controller module. The module accepts NS3 simulation output data as input. The data content includes the timestamp time_stamp, which is the time when the sensor node sends the data, and the sensor signal, which is recorded as u 2, , the controller module output is y2, and the module function form is [y2] = controller (u2). The function content can be written according to the actual control algorithm, taking linear state feedback as an example:
[0036] y2=[time_stamp,Ku2] (2)
[0037] Where K is the given control gain;
[0038] Step 53: Build the actuator model;
[0039] Define the variable ut_memory to compare the timestamp (the moment when the sensor node sends the data packet) with the value arriving at the actuator. Given the initial timestamp of 0 and the size of the initial value, define the variable ut_memory_update to update the value of ut_memory. The content of the packet arriving at the actuator is the data packet processed by the controller. It will first compare the data packet containing the timestamp time_stamp with the data size stored in the variable ut_memory during initialization, select the data packet with the larger timestamp to execute, and save this packet to ut_memory_update.
[0040] (6) Implementation of time synchronization mechanism between collaborative simulation software;
[0041] Step 61: In SCILAB, the initial value of the variable C_A_data sent by the sensor node to the controller node and the initial value of the variable S_C_data sent by the controller node to the actuator node are given, and the interface program is called to pass the values of the variable C_A_data and the variable S_C_data to NS3, and NS3 will arrange events according to the received information;
[0042] Step 62: NS3 arranges two sending events and two receiving events. After completing the four events, the interface program is called to transmit the simulation results to SCILAB;
[0043] Step 63: SCILAB receives the simulation result of NS3, obtains the current simulation time in the simulation result, and assigns it to the SCILAB current time variable CurrentTime_SCI;
[0044] Step 64: Set the state variable State, assign the value of S_C_NS3 to State, and complete the initialization;
[0045] Step 65: Get the result of NS3 simulation from sensor node to controller node, pass the content of the packet to variable S_C_SCI, get the result of NS3 simulation from controller node to actuator node, pass the content of the packet to variable C_A_SCI;
[0046] Step 66: Run the XCOS control system model to obtain the running result, that is, the arrangement of the next simulation of NS3 is required, that is, the content of the packet sent by the controller node to the actuator node is passed to the variable C_A_NS3, and the content sent by the sensor node to the controller node is passed to the variable S_C_NS3;
[0047] Step 67: Set the loop count variable t, i.e. the number of times the sensor is sampled;
[0048] Step 68: Enter the loop body, pass the result variables C_A_NS3 and S_C_NS3 of the last XCOS run to the interface variables C_A_data and S_C_data of the collaborative simulation interface program, run the interface program, send the events that SCILAB needs to simulate to NS3, and receive the results of NS3 simulation, store the delay information, update the content of the package to the variables S_C_SCI and C_A_SCI, run the XCOS control system model, update the current simulation time CurrentTime_SCI+=SimulationStep, then set t+=1, enter the next loop, until the simulation upper limit time is reached;
[0049] (7) Draw the collaborative simulation results diagram;
[0050] Step 72: Define the control system closed loop delay close_loop_delay, the data calculation formula is as follows:
[0051] close_loop_delay=CurrentTime_SCI-time_stamp; (3)
[0052] Step 71: With the sampling number t as the horizontal coordinate and close_loop_delay as the vertical coordinate, use the command plot(t, close_loop_delay) to draw a delay data graph of the control system transmission.
[0053] Compared with the prior art, the advantages of the technical solution of the present invention are:
[0054] (1) The present invention cooperates with two softwares to perform simulation, which can achieve higher simulation accuracy compared with NCS system simulation using a single software;
[0055] (2) Select open source software and achieve collaborative simulation of two simulation software by designing interfaces, which can have the advantages of open source, low cost and low copyright risk. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 is a schematic diagram of the collaborative simulation synchronization mechanism of the present invention;
[0057] Figure 2 is the XCOS control system model of the present invention;
[0058] Figure 3 is the actuator model in XCOS of the present invention;
[0059] Figure 4 It is the collaborative simulation interface between the sensor S and the controller C of the present invention;
[0060] Figure 5 It is a collaborative simulation interface between the controller C and the actuator A of the present invention;
[0061] Figure 6 It is a closed-loop delay line graph of the wireless network control system of the present invention. DETAILED DESCRIPTION
[0062] In order to facilitate those skilled in the art to understand and implement the present invention, the present invention is further described in detail below with reference to the accompanying drawings and embodiments.
[0063] Embodiment 1:
[0064] In order to verify the reliability of the collaborative simulation time synchronization method between XCOS and NS3, SCILAB is used as the main method to call NS3 software for collaborative simulation. After the simulation is completed, the simulation time of the two software is extracted for comparison to verify the reliability of the synchronization scheme. In this verification of the wireless network control system, the NS3 network type is set to 802.11b. Considering that each time a sample is run (a SCILAB simulation cycle is run), NS3 needs to run a synchronization cycle, and SCILAB will also call XCOS once, that is, the two softwares run alternately. In the actuator module of the XCOS control system model, the A and B matrices are set to second-order unit matrices, C is set to a two-dimensional column vector with all elements being 1, and the controller gives the control gain K as a two-dimensional column vector with all elements being 1. Set the step size of NS3 to 30ms and the total simulation time to , run the collaborative simulation program, store the node sending time SendTime of SCILAB into the variable CurrentTime_SCI, and store the CurrentTime in the NS3 simulation result into the file transfer area CurrentTime_record. After the simulation, use the command loadmatfile("CurrentTime_record.txt") to read the data in the file into SCI LAB and store it in the variable CurrentTime_NS3. Compare the values of CurrentTime_SCI and CurrentTime_NS3 at this time. If they are consistent, it means that the simulation times of the two software are coordinated and consistent. Extract the controller-to-actuator and sensor-to-controller delay data generated by NS3 simulation, run the drawing command plo t, with the sampling period t as the horizontal axis and the delay data as the vertical axis, to obtain random delay data, verify that the collaborative simulation of the two software is orderly and normal, the simulation times of the two software are consistent, and the solution is feasible.
[0065] The collaborative simulation time synchronization method based on XCOS and NS3 of the present invention takes SCILAB as the leading factor, calls NS3 software for collaborative simulation, extracts the simulation time of the two software for comparison after the simulation is completed, and verifies the reliability of the synchronization scheme. First, install the collaborative simulation interface in NS3, install the socket server program in NS3, define the buffer, and cache SCILAB synchronization data; install the collaborative simulation interface in SCILAB; initialize the NS3 simulation script; create a new NS3 simulation script, define the NS3 simulation module in the script, define the initialization function: node setting function, network topology establishment function, channel attribute configuration function, network device creation function, network protocol stack installation function, simulation start and end function, and complete the NS3 configuration through the NS3 collaborative simulation interface initialization function; NS3 synchronization mechanism design; XCOS control system model in SCILA B; build the controlled object model; build the controller model; build the actuator model; realize the time synchronization mechanism between collaborative simulation software; draw the collaborative simulation result diagram; finally realize the high-precision simulation result in the networked control system simulation. The present invention has high simulation accuracy and has the advantages of open source and low cost.
[0066] The contents described in the embodiments of this specification are merely an enumeration of the implementation forms of the inventive concept. The protection scope of the present invention should not be regarded as limited to the specific forms described in the embodiments. The protection scope of the present invention also extends to the equivalent technical means that can be conceived by those skilled in the art based on the inventive concept.
Claims
1. A collaborative simulation time synchronization method based on XCOS and NS3, comprising the following steps: (1) Install the collaborative simulation interface in NS3; Step 11: Install the socket server program in NS3; receive SCILAB synchronization data through the socket; Step 12: Define a buffer to cache SCILAB synchronization data; (2) Install the collaborative simulation interface in SCILAB; Step 21: Install the socket client C++ program in SCILAB; use the mexArray instruction to obtain the data to be sent to the server from SCILAB, and send it to NS3 through the socket; Step 22: Execute the command ilib_mex_build to compile the interface program; Step 23: Execute the command exec loader.sce to link the SCILAB dynamic library file. In SCILAB, you can call the interface program through the "interface name"; (3) Initialize NS3 simulation script; Create a new NS3 simulation script, define the NS3 simulation module in the script, define the initialization functions: node setting function, network topology establishment function, channel attribute configuration function, network device creation function, network protocol stack installation function, simulation start and end function, and pass values to the above initialization functions through the NS3 collaborative simulation interface to complete the NS3 configuration; Step 31: Define and initialize the step size SimulationStep of the NS3 simulation synchronization cycle; The step size in SCILAB means a sampling period, within which SCILAB notifies NS3 of the transmission and reception events between running nodes; Step 32: Define the next simulation synchronization time TimeLimit, with the initial value being SimulationStep; Step 33: Define and initialize the upper limit of simulation time, i.e., the simulation end time SimulationStopTime; Step 34: define the current simulation time CurrentTime = m_events->PeekNext().key.m_ts; Step 35: define the node sending time SendTime; In the NS3 script, use the command GetSendTime() to obtain the time when the node sends, assign it to SendTime, and send it to SCILAB as the first bit of the data packet content, so that the data packet can carry the sending time information throughout the simulation process; (4) Design of NS3 synchronization mechanism; Step 41: define the jump time JumpTime=TimeLimit-SimulationStep-CurrentTime; Step 42: Find the time of the last event executed in the current synchronization cycle; execute the command m_events->PeekNext().key.m_ts to get the end time of the next event, and determine whether it is less than or equal to TimeLimit; if so, update CurrentTime=m_events->PeekNext().key.m_ts, execute the event, and continue to get the next event; otherwise, it means that the current event is the last event executed in the current synchronization cycle. At this time, move TimeLimit to the next synchronization cycle point, that is, TimeLimit+=SimulationStep, and execute steps 43 and 44 to synchronize information with SCILAB; Step 43: NS3 synchronizes information to SCILAB; NS3 sends the event content in the received event, including timestamp, sensor signal, and control quantity, to SCILAB through the co-simulation interface; Step 44: Accept the synchronization data from SCILAB, calculate the jump back time, and arrange new events; The calculation formula of the jump time JumpTime is JumpTime = TimeLimit-SimulationStep-CurrentTime. After the JumpTime delay, execute Simulator::Schedule(JumpTime,&UdpEchoClientNew::Send To,this) to update the event list. A series of sending events can be customized in the &UdpEchoClientNew::SendTo() function. Step 45: Repeat steps 42 to 44 until TimeLimit>SimulationStopTime, and the NS3 simulation ends; (5) Construction of XCOS control system model in SCILAB; Step 51: Build a controlled object model; Use the user-defined module scifunc_block_m in XCOS, and create a new script in the workspace to write the module function; define the control quantity u1, system input state x1, system output state y1, and system output z1 of this module; the function of this module is to use u1 and x1 to obtain the one-step simulation results y1 and z1 of the discrete system; the module function form is [y1, z1] = plant (x1, u1); taking the linear system as an example, the function is implemented as follows: y1=Ax1+Bu1,z1=Cx1+Du1 (1) Where A, C∈R n*n , B, D∈R n*m , is a given matrix; Step 52: Build the controller model; Use the scifunc_block_m module to create a controller module. The module accepts NS3 simulation output data as input. The data content includes the timestamp time_stamp, that is, the time when the sensor node sends, and the sensor signal, recorded as u2. The controller module output is y2, and the module function form is [y2] = controller (u2); the function content can be written according to the actual control algorithm, taking linear state feedback as an example: y2=[time_stamp,Ku2] (2) Where K is the given control gain; Step 53: Build the actuator model; Define the variable ut_memory to compare the timestamp with the value arriving at the actuator, that is, the moment when the sensor node sends the data packet. Given the initial timestamp of 0 and the size of the initial value, define the variable ut_memory_update to update the value of ut_memory. The content of the packet arriving at the actuator is the data packet processed by the controller. It will first compare the data packet containing the timestamp time_stamp with the data size stored in the variable ut_memory during initialization, select the data packet with the larger timestamp to execute, and save this packet to ut_memory_update. (6) Implementation of time synchronization mechanism between collaborative simulation software; Step 61: In SCILAB, the initial value of the variable C_A_data sent by the sensor node to the controller node and the initial value of the variable S_C_data sent by the controller node to the actuator node are given, and the interface program is called to pass the values of the variable C_A_data and the variable S_C_data to NS3, and NS3 will arrange events according to the received information; Step 62: NS3 arranges two sending events and two receiving events. After completing the four events, the interface program is called to transmit the simulation results to SCILAB; Step 63: SCILAB receives the simulation result of NS3, obtains the current simulation time in the simulation result, and assigns it to the SCILAB current time variable CurrentTime_SCI; Step 64: Set the state variable State, assign the value of S_C_NS3 to State, and complete the initialization; Step 65: Get the result of NS3 simulation from sensor node to controller node, pass the content of the packet to variable S_C_SCI, get the result of NS3 simulation from controller node to actuator node, pass the content of the packet to variable C_A_SCI; Step 66: Run the XCOS control system model to obtain the running result, that is, the arrangement of the next simulation of NS3 is required, that is, the content of the packet sent by the controller node to the actuator node is passed to the variable C_A_NS3, and the content sent by the sensor node to the controller node is passed to the variable S_C_NS3; Step 67: Set the loop count variable t, i.e. the number of times the sensor is sampled; Step 68: Enter the loop body, pass the result variables C_A_NS3 and S_C_NS3 of the last XCOS run to the interface variables C_A_data and S_C_data of the collaborative simulation interface program, run the interface program, send the events that SCILAB needs to simulate to NS3, and receive the results of NS3 simulation, store the delay information, update the content of the package to the variables S_C_SCI and C_A_SCI, run the XCOS control system model, update the current simulation time CurrentTime_SCI+=SimulationStep, then set t+=1, enter the next loop, until the simulation upper limit time is reached; (7) Draw the collaborative simulation results diagram; Step 71: Define the control system closed loop delay close_loop_delay, the data calculation formula is as follows: close_loop_delay=CurrentTime_SCI-time_stamp; (3) Step 72: With the sampling number t as the horizontal coordinate and close_loop_delay as the vertical coordinate, use the command plot(t, close_loop_delay) to draw a delay data graph of the control system transmission.
Citation Information
Patent Citations
An NS3 and MATLAB integrated joint simulation interface method based on a TCP protocol
CN109598040A
Network control system co-simulation method based on NS3 and MATLAB
CN110457747A
Method and apparatus for accelerating digital logic simulations
US20030188278A1
