Main Content

environment

Environmental inputs for UAV

Description

example

envStruct = environment(uavGuidanceModel) returns a structure that captures all the relevant environmental variables for the specified UAV guidance model. Use this function to ensure you have the proper fields for your environmental parameters. Use the environmental inputs as an input to the derivative function to get the state time-derivative of the UAV.

Examples

collapse all

Create a UAV scenario with default properties. Set the duration for 15 seconds.

scene = uavScenario;
duration = 15;

Create a UAV platform with a fixed wing UAV guidance model.

p = uavPlatform("UAV",scene);
uavModel = fixedwing;

Create a UAV state vector and set the initial condition at a height of 30 m and airspeed of 25 m/s.

s = state(uavModel);
s(3:4) = [30 25];

Create a control command structure. Set the UAV to fly at an airspeed of 25 m/s at a height of 30 m.

u = control(uavModel);
u.AirSpeed = 25;
u.Height = 30;

Create environment input structure.

e = environment(uavModel);

Create and add headwind gust starting at 5 second into the flight.

w = uavWindGust(GustAmplitude=[-5 0 0],StartTime=5);
addWind(p, w);

Create empty vectors for storing wind velocity, ground speed, and time.

wv = zeros(scene.UpdateRate*duration, 3);
gs = zeros(scene.UpdateRate*duration, 1);
ts = zeros(scene.UpdateRate*duration, 1);
idx = 1;

Set up the scenario.

setup(scene)

Run the simulation. In each time step, obtain the wind velocity vector and update the environment input structure with the wind velocity.

while scene.CurrentTime <= duration
    % Read the UAV motion vector
    motion = read(p);

    % Obtain wind velocity vector in body frame.
    wv(idx, :) = windVelocity(p);

    % Create transformation matrix.
    quat = motion(10:13);
    rotm = quat2rotm(quat);
    
    % Transform wind velocity to NED frame using the transformation matrix.
    wNED = rotm*wv(idx,:)';

    % Update the environment input structure.
    e.WindNorth = wNED(1);
    e.WindEast = wNED(2);
    e.WindDown = wNED(3);

    % Obtain the time derivative of the UAV states.
    sdot = derivative(uavModel, s, u, e);
    
    % Integrate the state derivative to obtain the next state
    s = s + sdot/scene.UpdateRate;

    % Update the UAV motion vector.
    motion(1:3) = s(1:3);
    motion(4:5) = sdot(1:2);
    motion(6) = -sdot(3);

    % Obtain the UAV ground speed.
    gs(idx) = norm(motion(4:6));
    
    % Obtain the current time.
    ts(idx)=scene.CurrentTime;

    % Move the UAV to next position.
    move(p, motion);
    idx = idx + 1;
    
    % Advance the scenario time step.
    advance(scene);
end

Plot the ground speed of the UAV.

figure();
plot(ts, gs, LineWidth=2);
ylim([0 30])
title("UAV Ground Speed")
xlabel("Time (s)")
ylabel("Ground Speed (m/s)")

Input Arguments

collapse all

UAV guidance model, specified as a fixedwing or multirotor object.

Output Arguments

collapse all

Environmental input parameters, returned as a structure.

For fixed-wing UAVs, the fields of the structure are as follows:

  • WindNorth — Wind velocity in the positive x- direction of the NED frame in m/s

  • WindEast — Wind velocity in the positive y- direction of the NED frame in m/s

  • WindDown — Wind velocity in the positive z- direction of the NED frame in m/s

  • Gravity — Gravitational acceleration in m/s2. The default value is 9.8 m/s2

For multirotor UAVs, the only element of the structure is Gravity.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2018b