Main Content

Create and Add Custom Mobility Model to Wireless Nodes

This example demonstrates how to implement a custom mobility model and apply it to wireless nodes.

Accurately evaluating wireless network performance requires mobility models that closely mimic the real-world movement patterns of wireless nodes. With Wireless Network Toolbox™, you can define and implement custom mobility models tailored to your specific scenarios.

In this example, you implement the custom mobility model using the wnet.Mobility base class, apply it to wireless nodes, and then visualize the node mobility using wirelessNetworkViewer.

Implement Custom Mobility Model

To create a custom mobility model using the wnet.Mobility base class of Wireless Network Toolbox, follow these steps:

  • Inherit from the wnet.Mobility base class. The class definition must have this format, where customMobility is the name of your custom mobility class.

 classdef customMobility < wnet.Mobility
   ...
 end
  • Override the public method mobilityInfo of the wnet.Mobility base class. Optionally, you can also override the public method init of wnet.Mobility.

  • Save the class definition in an .m file.

  • In a simulation script, create an object of the customMobility class. Plug this custom mobility object into a wireless node inherited from the wnet.Node base class by using the addMobility method of wnet.Node.

This example implements a mobility model with constant acceleration and deceleration. The model is attached to this example as a supporting file. For more information on the implementation of the model, see Supporting Files.

Add Custom Mobility to Wireless Nodes

Initialize the wireless network simulator.

simulator = wirelessNetworkSimulator.init;

Instantiate two wireless nodes at specified 3-D coordinates using the hWirelessNode helper object, attached to this example as a supporting file. This custom class, derived from wnet.Node, simulates wireless nodes and enables you to assign properties such as the position of the node in three-dimensional space.

wNode1 = hWirelessNode(Position=[0 0 0]);
wNode2 = hWirelessNode(Position=[0 -2 0]);
wNode3 = hWirelessNode(Position=[1 -2 0]);

Add the wireless nodes to the simulator.

addNodes(simulator,[wNode1 wNode2 wNode3])

Initialize the wireless network viewer.

visualizer = wirelessNetworkViewer;

Add the wireless nodes to the wireless network viewer.

addNodes(visualizer,[wNode1 wNode2 wNode3],Name=["Node1","Node2","Node3"]);

Create two custom mobility models using the helper object customConstantAccelerationMobility.

% Mobility model 1: Starts with velocity [2,0,0] m/s and acceleration [1,0.2,0] m/s^2
mob1 = customConstantAccelerationMobility([2 0 0], [1 0.2 0]);
% Mobility model 2: Starts with velocity [2,1,0] m/s and deceleration [-1,-1,0] m/s^2
mob2 = customConstantAccelerationMobility([2 2 0],[-1 -1 0]);

Add the custom mobility mob1 to the first wireless node.

addMobility(wNode1,MobilityModel=mob1)

Add the custom mobility mob2 to the second and third wireless nodes.

addMobility([wNode2 wNode3],MobilityModel=mob2)

Simulate and Visualize

Run the simulation for 2 seconds.

You can observe the movement of the nodes in the wireless network viewer. Node 1 starts at position [0 0 0] with an initial velocity of [2 0 0] m/s and accelerates at [1 0.2 0] m/s². Over the 2-second simulation, Node 1 moves primarily along the x-axis, with a slight increase in velocity along the y-axis due to the nonzero y-acceleration. Nodes 2 and 3 start at [0 -2 0] and [1 -2 0], respectively, both with an initial velocity of [2 1 0] m/s and deceleration of [-1 -1 0] m/s². These nodes move diagonally in the xy-plane, but both their x- and y-velocities decrease over time due to the negative accelerations.

run(simulator,2)

Figure Wireless Network Viewer contains an axes object. The axes object with xlabel X-axis (m), ylabel Y-axis (m) contains 9 objects of type line, text. One or more of the lines displays its values using only markers This object represents hWirelessNode.

Supporting Files

  • customConstantAccelerationMobility.m — Implements a mobility model with constant acceleration and deceleration.

% Custom mobility model with constant acceleration and deceleration for wireless network nodes
classdef  customConstantAccelerationMobility < wnet.Mobility
    properties
        % Initial position of the node [x,y,z] when the mobility model is added to the node
        InitialPosition (1,3)
        % Simulation time when the mobility model is added to the node
        InitialTime (1,1)
        % Initial velocity vector [vx,vy,vz] for node movement
        InitialVelocity (1,3)
        % Constant acceleration vector [ax,ay,az]
        Acceleration (1,3)
    end

    methods
        % Constructor: Set the initial velocity and acceleration for this mobility model
        function obj =  customConstantAccelerationMobility(initialVelocity, acceleration)
            validateattributes(initialVelocity, {'numeric'},{'vector','numel',3,'real','finite'});
            validateattributes(acceleration, {'numeric'},{'vector','numel',3,'real','finite'});
            obj.InitialVelocity = initialVelocity;
            obj.Acceleration = acceleration;
        end

        % Initialize mobility model with the current position and time of the node
        % Note: A subclass of wnet.Node calls the init method when you configure
        % a mobility model using the addMobility method. The addMobility method
        % creates the mobility context and passes it to the init method.
        % The mobility context is a structure containing the position, name,
        % ID, and current simulation time of the node. 

        function init(obj,mobilityContext)
            obj.InitialPosition = mobilityContext.LatestPosition;
            obj.InitialTime = mobilityContext.LastUpdateTime;
        end

        % Compute current position and velocity at a given simulation time
        % Whenever the node queries its position and velocity,
        % the simulation invokes the mobilityInfo method.
        function [pos,vel] = mobilityInfo(obj,currentSimulationTime)
            % Calculate elapsed time since initialization
            elapsedTime = currentSimulationTime - obj.InitialTime;
            % Update position using kinematic equation for constant acceleration
            % Mathematical equation:
            % x = x0 + v0*t + 0.5*a*t^2
            % where:
            % x  = position at time t
            % x0 = initial position
            % v0 = initial velocity
            % a  = acceleration
            % t  = elapsed time
            pos = obj.InitialPosition + ...
                obj.InitialVelocity*elapsedTime + ...
                0.5*obj.Acceleration*elapsedTime^2;
            % Update velocity
            % v = v0 + a*t
            % where:
            % v  = velocity at time t
            % v0 = initial velocity
            vel = obj.InitialVelocity + obj.Acceleration*elapsedTime;
        end
    end
end
  • hWirelessNode.m — Implements a custom wireless node.

See Also

Objects

Classes