%% Create Environment from a Simulink Model
% This example shows how to use createIntegratedEnv to create an environment object starting from a Simulink model implementing the system that the agent needs to interact with. Such a system is often referred to as plant, open loop system or reference system, while the whole (integrated) system including the agent is often referred to as the closed loop system.
% For this example, use the flying robot model described in Train DDPG Agent to Control a Flying Robot as the reference (open loop) system.
% Open the flying robot model.

open_system('rlFlyingRobotEnv');

% Initialize state variables and sample time.

% initial model state variables
theta0 = 0;
x0 = -15;
y0 = 0;

% sample time
Ts = 0.4;

% Create the Simulink model IntegratedEnv containing the flying robot model connected in a closed loop to the agent block. The function also returns the reinforcement learning environment object env, to be used for training.

env=createIntegratedEnv('rlFlyingRobotEnv','IntegratedEnv')

% The function can also return the block path to the RL Agent block in the new integrated model, as well as the observation and action data specifications for the reference model.

[~,agentBlk,observationInfo,actionInfo]=createIntegratedEnv('rlFlyingRobotEnv','IntegratedEnv')

% This is useful in cases in which you need to modify descriptions, limits or names in observationInfo and actionInfo and later create an environment from the integrated model IntegratedEnv, using the function rlSimulinkEnv.
% Copyright 2019 The MathWorks, Inc.
%