Create and Add Custom Traffic in Bluetooth Network Simulation
Traffic models define how data flows between nodes, providing a framework to simulate different types of network usage and application behaviors. With Wireless Network Toolbox™, you can create custom traffic models to meet your simulation needs. This example demonstrates how to create a traffic model from the wnet.Traffic base class and how to integrate it into your Bluetooth® Network simulation.
Implement Custom Traffic
To create a custom traffic model using the wnet.Traffic base class of Wireless Network Toolbox, follow these steps:
Inherit from the
wnet.Trafficbase class. The class definition must have this format, wherecustomTrafficis the name of your custom traffic class.Implement the base class public method
generate. Also, implement any supporting methods and properties.
classdef customTraffic < wnet.Traffic properties ... end methods function obj = customTraffic(varargin) % Constructor ... end function [dt,packetSize,packet] = generate(trafficObject,elapsedTime) ... end end % Supporting methods ... end
Save the class definition in a
.mfile inside your directory.In a simulation script, create an object of the
customTrafficclass. Plug this custom traffic object into Bluetooth® LE nodes by using theaddTrafficSourceobject function.
This example implements a periodic traffic model, hTrafficPeriodic, which generates packets of a specified size at a specified interval. The model is attached to this example as a supporting file. For more information on the implementation of the model, see Supporting File.
Simulate Network with Custom Traffic
Set the seed for the random number generator to 1 to ensure repeatability of results.
rng(1,"combRecursive");Initialize the wireless network simulator.
networkSimulator = wirelessNetworkSimulator.init;
Create Bluetooth Central and Peripheral nodes.
centralNode = bluetoothLENode("central",Name="Central1"); peripheralNode = bluetoothLENode("Peripheral",Name="Peripheral1",Position=[1 0 0]);
Create and configure the connection.
cfgConnection = bluetoothLEConnectionConfig; configureConnection(cfgConnection,centralNode,peripheralNode);
Create a periodic traffic pattern object using hTrafficPeriodic helper object. Specify the period (in seconds) and packet size (in bytes).
traffic = hTrafficPeriodic(Period=0.01,PacketSize=200);
Add traffic from the Central to Peripheral node.
addTrafficSource(centralNode,traffic,DestinationNode=peripheralNode)
Add the Bluetooth nodes to the simulator.
addNodes(networkSimulator,[centralNode peripheralNode]);
If an event log file exists, delete it.
if exist("btEventLog.mat","file") delete("btEventLog.mat"); end
Create an event tracer object to log events to a MAT file during simulation runtime.
eventTracer = wirelessNetworkEventTracer(FileName="btEventLog.mat");Add the Central and Peripheral nodes to the event tracer.
addNodes(eventTracer,centralNode) % Logs the events "TransmissionStarted", "ReceptionEnded", "AppPacketGenerated", and "AppPacketReceived" addNodes(eventTracer,peripheralNode) % Logs the events "TransmissionStarted", "ReceptionEnded", "AppPacketGenerated", and "AppPacketReceived"
Run the simulation for 0.3 seconds.
run(networkSimulator,0.3);
Read only the "AppPacketGenerated" events from node "Central1" from the event tracer.
events = read(eventTracer,EventName="AppPacketGenerated",NodeName="Central1"); disp(events);
1×31 struct array with fields:
EventName
NodeName
NodeID
Timestamp
TechnologyType
EventData
Extract the timestamps for "AppPacketGenerated" events of the Central node from the event log. The timestamps verify that the output is periodic, with an event occurring every 0.01 seconds.
generationTimes = [events.Timestamp]
generationTimes = 1×31
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900 0.3000
Supporting File:
hTrafficPeriodic.m — Implements a periodic traffic model
classdef hTrafficPeriodic < wnet.Traffic properties % Period between generated packets, in seconds (must be a positive numeric scalar) Period (1,1) {mustBeNumeric,mustBePositive} = 1 % Packet size in bytes (must be a positive integer scalar) PacketSize (1,1) {mustBeNumeric,mustBeInteger,mustBePositive} = 1500 % Application data to be included in each packet (It must be column vector of integers between 0 and 255) ApplicationData (:,1) {mustBeNumeric,mustBeInteger,mustBeInRange(ApplicationData,0,255)} = ones(1500,1) end properties (Access = private) % Actual packet data after adjusting for packet size (column vector) pAppData % Timer (in ms) until the generation of the next packet pNextInvokeTime = 0 end methods function trafficPeriodic = hTrafficPeriodic(varargin) % hTrafficPeriodic Constructor for the periodic traffic class % To enable support for configurable properties through name-value arguments, % call the constructor of base class by using the name-value arguments as variable inputs trafficPeriodic@wnet.Traffic(varargin{:}); % Initialize the packet data (pAppData) updatePacketData(trafficPeriodic); end function set.PacketSize(trafficPeriodic,value) % Set method for PacketSize property trafficPeriodic.PacketSize = value; updatePacketData(trafficPeriodic); end function set.ApplicationData(trafficPeriodic,value) % Set method for ApplicationData property trafficPeriodic.ApplicationData = value; updatePacketData(trafficPeriodic); end function [dt,packetSize,packet] = generate(trafficPeriodic,elapsedTime) % generate Generate the next packet after the specified period has elapsed % [dt,packetSize,packet] = generate(trafficPeriodic,elapsedTime) % - elapsedTime: (optional) time in ms since last call (default: 0) % - dt: time in ms to next packet generation % - packetSize: size of the packet generated in bytes (0 if no packet) % - packet: application data (empty if no packet) % % This method maintains a countdown timer. When the timer reaches % zero, a packet is generated and the timer is reset to the period. arguments trafficPeriodic elapsedTime (1,1) {mustBeNonnegative} = 0 % Time in ms since last call (default: 0) end if nargin == 1 % If no elapsedTime is provided, assume this is the first call % and reset the timer so a packet is generated immediately. trafficPeriodic.pNextInvokeTime = 0; else % Subtract elapsed time from the internal timer trafficPeriodic.pNextInvokeTime = trafficPeriodic.pNextInvokeTime - elapsedTime; % Round to avoid numerical errors trafficPeriodic.pNextInvokeTime = round(trafficPeriodic.pNextInvokeTime*1e6)/1e6; end if trafficPeriodic.pNextInvokeTime <= 0 % Countdown timer has elapsed, so generate the next packet dt = trafficPeriodic.Period * 1000; % Set next packet generation time (in ms) packetSize = trafficPeriodic.PacketSize; % Output the configured packet size trafficPeriodic.pNextInvokeTime = dt; % Reset the countdown timer if nargout == 3 % Return the generated packet if requested packet = trafficPeriodic.pAppData; end else % Packet generation is pending until the countdown timer expires dt = trafficPeriodic.pNextInvokeTime; % Time left until next packet (ms) packetSize = 0; % No packet, so size is zero if nargout == 3 packet = []; % No packet to return end end end end methods (Access = private) function updatePacketData(trafficPeriodic) % updatePacketData Update the size and content of application packet based on PacketSize and ApplicationData properties. n = trafficPeriodic.PacketSize; data = trafficPeriodic.ApplicationData; trafficPeriodic.pAppData = ones(n,1,"like",data); % Pre-fill with ones (default) m = min(n,numel(data)); trafficPeriodic.pAppData(1:m) = data(1:m); % Copy as much as fits end end end
See Also
Objects
Classes
wnet.Traffic(Wireless Network Toolbox)