Main Content

UAV Package Delivery

This example shows through incremental design iterations, learn how to implement a small multicopter simulation to takeoff, fly, and land at a different location in a city environment.

Open the Project

To get started, open the example live script and access the supporting files by either clicking Open Live Script in the documentation or using the openExample function.

openExample('uav/UAVPackageDeliveryExample');

Then, open the Simulink™ project file.

prj = openProject('uavPackageDelivery');

UAV package delivery simulink model, showing ground control station, external sensor lidar and camera, on board computer, and multirotor subsystems

Model Architecture and Conventions

The top model consists of the following subsystems and model references:

  1. Ground Control Station: Used to control and monitor the aircraft while in-flight.

  2. 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.

  3. On Board Computer: Used to implement algorithms meant to run in an on-board computer independent from the Autopilot.

  4. Multirotor: Includes a low-fidelity and mid-fidelity multicopter mode, 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 What Are Variants? (Simulink).

Following Example Steps

Use the Project Shortcuts to step through the example. Each shortcut sets up the required variables for the project.

Project shortcuts

1. Getting Started

Click the Getting Started project shortcut, which sets up the model for a four-waypoint mission using a low-fidelity multirotor plant model. Run the uavPackageDelivery model, which shows the multirotor takeoff, fly, and land in a 3-D plot.

Output of UAV animation block showing that the UAV completes the delivery mission

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.

Implementation of the UAV path manager in the Simulink model which provides input for the Guidance Mode Selector

2. Connecting to a GCS

Once you are able to 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:

  1. Click the Connecting to a GCS project shortcut.

  2. Launch QGroundControl.

  3. In QGroundControl, load the mission plan named shortMission.plan located in /utilities/qgc.

  4. Run the simulation.

  5. 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.

Screenshot of QgroundControl showing the flight plan with 4 waypoints

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.

3. Setting a Cuboid Scenario

Now that aircraft's model can be flow 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.

Image on the left shows the US city block scene as seen from the top. Image on the right is the representation of the scene in the cuboid scenario

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 readings 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:

lidar point cloud in the cuboid simulation

4. 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. A scope appears that shows the closest point to a building in the cuboid environment. 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 aircraft attempts to fly in a straight path between buildings to a drop site but deviates to avoid obstacles along the way. Observe the change in distance to obstacles over time and observe a similar behavior in 3D Obstacle Avoidance mode, with the UAV now also able to adjust altitude to fly over obstacles.

Left image shows the point cloud from the Lidar reading, right image shows the plot of UAV distance to obstacle versus time

5. 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:

Simulink model of UAV photorealistic simulation, showing the blocks for 3D simulation environment, Camera Model, and Lidar Point Cloud

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

First image shows the UAV in US city block scene, second image shows the output of the camera in the UAV, third image shows the lidar point cloud

6. 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:

  1. Launch QGroundControl.

  2. In QGroundControl, load the mission plan named shortMission.plan located in /utilities/qgc.

  3. Run the Simulation.

  4. 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.

Screenshot of QGroundControl showing the delivery mission, overlaid with the camera output and the lidar point cloud

7. 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 the 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:

  1. Launch QGroundControl.

  2. In QGroundControl, load the mission plan named oaMission.plan located in /utilities/qgc.

  3. Run the Simulation.

  4. 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.

UAV flight path in QGroundControl which shows that it avoids obstacles

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

UAVPackageDeliveryUnrealAroundCorner400.gif

8. 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.

High fidelity UAV Simulink 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);