Main Content

Simulate UAV Scenario Using Scenario Blocks

This example shows how to use the UAV scenario blocks to simulate a scenario in Simulink®.

Create Scenario

Initialize your UAV scenario with meshes and uavPlatform objects for the UAV scenario blocks to use.

% Initialize the scenario 
scene = uavScenario(UpdateRate=100,ReferenceLocation=[0 0 0]);

%Create a ground for visualization
addMesh(scene,"polygon",{[-15 -15; 15 -15; 15 15; -15 15] [-0.5 0]},[0.3 0.3 0.3]);

% Add cylinder meshes to scan with lidar sensor
addMesh(scene,"cylinder",{[-5 5 2] [0 12]},[0 1 0]);
addMesh(scene,"cylinder",{[5 5 2] [0 12]},[0 1 0]);
addMesh(scene,"cylinder",{[5 -5 2] [0 12]},[0 1 0]);

Create one UAV platform to be controlled and another to be stationary.

% Platform/UAV initial position and orientation
initpos = [0 0 -5]; % NED Frame
initori = [0 0 0];

% Add UAV Platforms to the Scenario and scale them for easier visualization
platform = uavPlatform("platformUAV",scene,ReferenceFrame="NED", ...
platform2 = uavPlatform("platformUAV2",scene,ReferenceFrame="NED", ...
    InitialPosition=[0 7 -11],InitialOrientation=eul2quat(initori));

updateMesh(platform,"quadrotor",{2},[0 0 0],eul2tform([0 0 pi]));
updateMesh(platform2,"quadrotor",{2},[0 0 0],eul2tform([0 0 pi]));

Create a uavSensor with a lidar sensor model, for use in the Simulink simulation, by using the uavLidarPointCloudGenerator, and attach it to the first UAV platform. Specify the parameters of the lidar in the UAV Scenario Lidar block, because the model does not load them from the object.

LidarModel = uavLidarPointCloudGenerator;
uavSensor("Lidar",platform,LidarModel,MountingLocation=[0 0 1],MountingAngles=[0 0 180]);

Configure the Scenario


The UAV Scenario Configuration block is essential for simulating the UAV scenario in Simulink, and must execute first. Place it in the model and set the MATLAB or model workspace variable name to the name of your scenario. Whenever you make changes to the scenario scene in MATLAB, open this block and click Refresh to reflect those changes in Simulink. Change the sample time and the bus and signal names as necessary.

Write to Motion Bus

The UAV Scenario Motion Write block updates the motion bus of the first platform UAV. This updates the position of the UAV in the figure from the UAV Scenario Scope block. Open the UAV Scenario Motion Write block parameters and click Select to configure the block to update platformUAV. This is the platform UAV that we will be updating with this block.

This example uses a multirotor guidance model with constant inputs to determine the next position to write to the motion bus. However, you can use any kind of controller to update the motion bus of the platform if you can extract inputs to use in the UAV Scenario Motion Write block.

Read from Motion Bus

After the UAV Scenario Motion Write block writes to the motion bus, the UAV Scenario Motion Read block reads the data from the motion bus. A controller can use this data to determine the inputs to your plant. This example does not use a controller, instead reading the outputs into Display blocks. Open UAV Scenario Motion Write block parameters and click Select to again choose platformUAV.

Get Transform

The UAV Scenario Get Transform block gets the transformation matrix between the two UAVs. Open the UAV Scenario Get Transform block parameters and set the Source Frame parameter to platformUAV and the Target Frame parameter to platformUAV2.

In this example, a Display block shows the transfomation matrix. However a controller can have many applications for the transformation data.

View and Simulate Lidar

The UAV Scenario Lidar block enables the use of the created lidar sensor in the scenario. Open the UAV Scenario Lidar block parameters and specify the Sensor name parameter as platformUAV/Lidar by clicking Select. You can edit the settings of the lidar by specifying parameters in the block.

To visualize the lidar readings and the motion of the UAV, connect the UAV Scenario Lidar block to a UAV Scenario Scope block. You must create a port by enabling the visualization toggle for the sensors you want to connect. If you recently added a lidar sensor, but do not see it in the table, open the UAV Scenario Configuration block parameters and click Refresh, and open the UAV Scenario Lidar block parameters and click Refresh sensor table.

Enabling visualization of the sensor enables a port on the UAV Scenario Scope block with the name of the platform and its sensor.

Select Show animation in the UAV Scenario Scope block to view the scenario. Run the model to see the animation.