UAV Package Delivery
This example show you how to implement a small multicopter simulation to takeoff, fly, and land at a different location in a city environment.
Getting Started
The uavPackageDelivery.prj Simulink project file contains the reference application, which consists of the uavPackageDelivery.slx Simulink model and its supporting files, as well as the additional riles and project shortcuts that this example series uses. Open the project file by running this code.
prj = openProject('uavPackageDelivery');Click the Getting Started project shortcut, which sets up the uavPackageDelivery.slx Simulink model for a four-waypoint mission using a low-fidelity multirotor plant model.

Model Overview
Open the uavPackageDelivery.slx Simulink model.
open_system("uavPackageDelivery");
The top model consists of the following subsystems and model references:
Ground Control Station: Controls and monitors the aircraft while in-flight.
External Sensors - Lidar & Camera: Used to connect to previously designed scenario or a Photorealistic simulation environment. These produce lidar readings from the environment as the aircraft flies through it.
On Board Computer: Used to implement algorithms meant to run in an onboard computer independent from the Autopilot.
Multirotor: Includes a low-fidelity and mid-fidelity multicopter model, a flight controller including its guidance logic.
The model's design data is contained in a Simulink data dictionary in the data folder (uavPackageDeliveryDataDict.sldd). Additionally, the model uses variant subsystems to manage different configurations of the model. Variables placed in the base workspace configure these variants without the need to modify the data dictionary. For more details on variant subsystems, see Variant Subsystem (Simulink).
Simulate Waypoint Mission with Low-Fidelity Multirotor Plant
Run the uavPackageDelivery model, which shows the multirotor take off, fly, and land in a 3-D plot.

The model uses UAV Path Manager block to determine which is the active waypoint throughout the flight. The active waypoint is passed into the Guidance Mode Selector Stateflow® chart to generate the necessary inner loop control commands.

Connecting to a GCS
Once you can fly a basic mission, you are ready to integrate your simulation with a ground station software so you can better control the aircraft's mission. For this, you need to download and install QGroundControl Ground Control Station software.
The model uses the UAV Toolbox mavlinkio to establish a connection between Simulink and QGroundControl. The connection is implemented as a MATLAB System Block located in uavPackageDelivery/Ground Control Station/Get Flight Mission/QGC/MAVLink Interface.
To test the connectivity between Simulink and QGroundControl follow these steps:
Click the Connecting to a GCS project shortcut.
Launch QGroundControl.
In QGroundControl, load the mission plan named
shortMission.planlocated in/utilities/qgc.Run the simulation.
When QGroundControl indicates that it is connected to the system, upload the mission.
Once the aircraft takes off, you should see the UAV fly its mission as sent by QGC as shown below.

You can modify the mission by adding waypoints or moving those that are already in the mission. Upload the mission and the aircraft should respond to these changes.
Setting a Cuboid Scenario
Now that aircraft's model can be flown from a ground control station, consider the environment the aircraft flies in. For this example, a few city blocks are modeled in a cuboid scenario using the uavScenario object. The scenario is based on the city block shown in the left figure below.

To safely fly the aircraft in this type of scenario, you need a sensor that provides information about the environment such as a lidar sensor to the model. This example uses a uavLidarPointCloudGenerator object added to the UAV scenario with a uavSensor object. The lidar sensor model generates reading based on the pose of the sensor and the obstacles in the environment.
Click the Setting a Cuboid Scenario shortcut and run the model. As the model runs, a lidar point cloud image is displayed as the aircraft flies through the cuboid environment:

Obstacle Avoidance
To avoid obstacles in the environment, the model must use the available sensor data as the UAV flies the mission in the environment. To modify the model configuration, click the Obstacle Avoidance or 3D Obstacle Avoidance shortcut. Obstacle Avoidance shortcut configures the model to use planar lidar information and Vector Field Histogram (Navigation Toolbox) to avoid obstacles by changing the direction of the UAV in its current x-y plane. 3D Obstacle Avoidance shortcut configures the model to use 3D lidar points and Obstacle Avoidance to avoid obstacles by changing the direction of the UAV in 3D space.
Run the model. In Obstacle Avoidance mode, as the model runs, the UAV attempts to fly in a straight path between buildings to a drop site but deviates to avoid obstacles along the way.
The image on the left shows the point cloud that is generated by the lidar sensor during the simulation. The image on the right shows the plot of closest UAV distance to buildings. Observe that when using 3D Obstacle Avoidance, the UAV will adjust its altitude during the simulation.

Photorealistic Simulation
Up to this point, the environment has been a simple cuboid scenario. To increase the fidelity of the environment, click the Photorealistic Simulation shortcut, which places the aircraft in a more realistic world to fly through. The PhotorealisticQuadrotor variant located at uavPackageDelivery/photorealisticSimulationEngi/SimulationEnvironmentVariant becomes active. This variant contains the necessary blocks to configure the simulation environment and the sensors mounted on the aircraft:

Run the model. The aircraft is set up to fly the same mission from steps 1 and 2. Notice that, as the aircraft flies the mission, the lidar point clouds update and an image from the front-facing camera is shown.

Fly Full Mission in a Photorealistic Simulation Environment
Next, click the Fly full mission shortcut, which sets up the connectivity to QGroundControl from step 2 for uploading the mission inside the photorealistic environment. Follow these steps to run the simulation:
Launch QGroundControl.
In QGroundControl, load the mission plan named
shortMission.planlocated in/utilities/qgc.Run the simulation.
When QGroundControl indicates that it is connected to a system, upload the mission.
As the aircraft starts to fly, you can modify the mission in QGroundControl by adding waypoints or moving those that are already in the mission. Upload the mission and the aircraft should respond to these changes. Throughout the flight you'll see the aircraft flying in the scenario.

Flying Obstacle Avoidance in a Photorealistic Simulation Environment
Next, the goal is to fly a mission by specifying a takeoff and landing point in QGroundControl and using obstacle avoidance to navigate around the obstacles along the path. Click the Fly full Obstacle Avoidance or Fly full 3D Obstacle Avoidance shortcut and follow these steps to run the simulation:
Launch QGroundControl.
In QGroundControl, load the mission plan named
oaMission.planlocated in/utilities/qgc.Run the Simulation.
When QGroundControl indicates that it is connected to a system, upload the mission.
Throughout the flight, watch the aircraft try to follow the commanded path in QGroundControl, while at the same time attempting to avoid colliding with the buildings in the environment. If Fly full 3D Obstacle Avoidance is used, the UAV flies over the lower buildings rather than around it.

At some point during the flight, you will see the UAV fly around building corners.

Adding a 6DOF Plant Model for Higher-Fidelity Simulation
As a final step, click the Adding a High Fidelity Plant shortcut, which activates the high-fidelity variant of the UAV model located at uavPackageDelivery/MultirotorModel/Inner Loop and Plant Model/High-FidelityModel. This variant contains an inner-loop controller and a high-fidelity plant model.

Run the model. There are minor changes in behavior due to the high-fidelity model, but the UAV flies the same mission.
When you are done exploring the models, close the project file.
close(prj);