# multirotor

Guidance model for multirotor UAVs

## Description

A `multirotor`

object represents a reduced-order guidance model
for an unmanned aerial vehicle (UAV). The model approximates the behavior of a closed-loop
system consisting of an autopilot controller and a multirotor kinematic model for 3-D
motion.

For fixed-wing UAVs, see `fixedwing`

.

## Creation

`model = multirotor`

creates a multirotor motion model with
`double`

precision values for inputs, outputs, and configuration parameters
of the guidance model.

`model = multirotor(DataType)`

specifies the data type precision
(`DataType`

property) for the inputs, outputs, and configurations
parameters of the guidance model.

## Properties

`Name`

— Name of UAV

`"Unnamed"`

(default) | string scalar

Name of the UAV, used to differentiate it from other models in the workspace, specified as a string scalar.

**Example: **
`"myUAV1"`

**Data Types: **`string`

`Configuration`

— UAV controller configuration

structure

UAV controller configuration, specified as a structure of parameters. Specify these parameters to tune the internal control behaviour of the UAV. Specify the proportional (P) and derivative (D) gains for the dynamic model and other UAV parameters. For multirotor UAVs, the structure contains these fields with defaults listed:

`'PDRoll'`

-`[3402.97 116.67]`

`'PDPitch'`

-`[3402.97 116.67]`

`'PYawRate'`

-`1950`

`'PThrust'`

-`3900`

`'Mass'`

-`0.1`

(measured in kg)

**Example: **
```
struct('PDRoll',[3402.97,116.67],'PDPitch',[3402.97,
116.67],'PYawRate',1950,'PThrust',3900,'Mass',0.1)
```

**Data Types: **`struct`

`ModelType`

— UAV guidance model type

`'MultirotorGuidance'`

(default)

This property is read-only.

UAV guidance model type, specified as
`'MultirotorGuidance'`

.

`DataType`

— Input and output numeric data types

`'double'`

(default) | `'single'`

Input and output numeric data types, specified as either `'double'`

or `'single'`

. Choose the data type based on possible software or
hardware limitations. Specify `DataType`

when first creating the
object.

## Object Functions

`control` | Control commands for UAV |

`derivative` | Time derivative of UAV states |

`environment` | Environmental inputs for UAV |

`state` | UAV state vector |

## Examples

### Simulate A Multirotor Control Command

This example shows how to use the `multirotor`

guidance model to simulate the change in state of a UAV due to a command input.

Create the multirotor guidance model.

model = multirotor;

Create a state structure. Specify the location in world coordinates.

s = state(model); s(1:3) = [3;2;1];

Specify a control command, `u`

, that specified the roll and thrust of the multirotor.

u = control(model); u.Roll = pi/12; u.Thrust = 1;

Create a default environment without wind.

e = environment(model);

Compute the time derivative of the state given the current state, control command, and environment.

sdot = derivative(model,s,u,e);

Simulate the UAV state using `ode45`

integration. The `y`

field outputs the multirotor UAV states as a 13-by-*n *matrix.

simOut = ode45(@(~,x)derivative(model,x,u,e), [0 3], s); size(simOut.y)

`ans = `*1×2*
13 3536

Plot the change in roll angle based on the simulation output. The roll angle (the X Euler angle) is the 9th row of the `simOut.y`

output.

plot(simOut.y(9,:))

Plot the change in the Y and Z positions. With the specified thrust and roll angle, the multirotor should fly over and lose some altitude. A positive value for Z is expected as positive Z is down.

figure plot(simOut.y(2,:)); hold on plot(simOut.y(3,:)); legend('Y-position','Z-position') hold off

You can also plot the multirotor trajectory using `plotTransforms`

. Create the translation and rotation vectors from the simulated state. Downsample (every 300th element) and transpose the `simOut`

elements, and convert the Euler angles to quaternions. Specify the mesh as the `multirotor.stl`

file and the positive Z-direction as `"down"`

. The displayed view shows the UAV translating in the Y-direction and losing altitude.

translations = simOut.y(1:3,1:300:end)'; % xyz position rotations = eul2quat(simOut.y(7:9,1:300:end)'); % ZYX Euler plotTransforms(translations,rotations,... 'MeshFilePath','multirotor.stl','InertialZDirection',"down") view([90.00 -0.60])

## More About

### UAV Coordinate Systems

The UAV Toolbox™ uses the North-East-Down (NED) coordinate system convention, which is also sometimes called the local tangent plane (LTP). The UAV position vector consists of three numbers for position along the northern-axis, eastern-axis, and vertical position. The down element complies with the right-hand rule and results in negative values for altitude gain.

The ground plane, or earth frame (NE plane, D = 0), is assumed to be an inertial plane
that is flat based on the operation region for small UAV control. The earth frame
coordinates are
[*x _{e}*,

*y*,

_{e}*z*]. The body frame of the UAV is attached to the center of mass with coordinates [

_{e}*x*,

_{b}*y*,

_{b}*z*].

_{b}*x*is the preferred forward direction of the UAV, and

_{b}*z*is perpendicular to the plane that points downwards when the UAV travels during perfect horizontal flight.

_{b}The orientation of the UAV (body frame) is specified in ZYX Euler angles. To convert
from the earth frame to the body frame, we first rotate about the
*z _{e}*-axis by the yaw angle,

*ψ*. Then, rotate about the intermediate

*y*-axis by the pitch angle,

*ϴ*. Then, rotate about the intermediate

*x*-axis by the roll angle,

*ϕ*.

The angular velocity of the UAV is represented by
[*p*,*q*,*r*] with
respect to the body axes,
[*x _{b}*,

*y*,

_{b}*z*].

_{b}### UAV Multirotor Guidance Model Equations

For multirotors, the following equations are used to define the guidance
model of the UAV. To calculate the time-derivative of the UAV state using these governing
equations, use the `derivative`

function. Specify the inputs using `state`

,
`control`

,
and `environment`

.

The UAV position in the earth frame is [*x _{e}*,

*y*,

_{e}*z*] with orientation as ZYX Euler angles, [

_{e}*ψ*,

*ϴ*,

*ϕ*] in radians. Angular velocities are [

*p*,

*q*,

*r*] in radians per second.

The UAV body frame uses coordinates as [*x _{b}*,

*y*,

_{b}*z*].

_{b}The rotation matrix that rotates vector from body frame to world frame is:

The cos(*x*) and sin(*x*) are abbreviated as
*c _{x}* and

*s*.

_{x}The acceleration of the UAV center of mass in earth coordinates is governed by:

*m* is the UAV mass, *g* is gravity, and
*F _{thrust}* is the total force created by the
propellers applied to the multirotor along the –

*z*axis (points upwards in a horizontal pose).

_{b}The closed-loop roll-pitch attitude controller is approximated by the behavior of 2 independent PD controllers for the two rotation angles, and 2 independent P controllers for the yaw rate and thrust. The angular velocity, angular acceleration, and thrust are governed by:

This model assumes the autopilot takes in commanded roll, pitch, yaw rate, and a commanded total thrust force,
*F ^{c}_{thrust}*. The
structure to specify these inputs is generated from

`control`

.The P and D gains for the control inputs are specified as
*KP _{α}* and

*KD*, where

_{α}*α*is either the rotation angle or thrust. These gains along with the UAV mass,

*m*, are specified in the

`Configuration`

property of the `multirotor`

object.From these governing equations, the model gives the following variables:

These variables match the output of the `state`

function.

## References

[1] Mellinger, Daniel, and Nathan
Michael. "Trajectory Generation and Control for Precise Aggressive Maneuvers with Quadrotors."
*The International Journal of Robotics Research*. 2012, pp.
664-74.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

## Version History

**Introduced in R2018b**

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)