Air Simulation Graphics

Air Simulation Graphics

Deliver a Package by Air Simulation with Graphical User Interface
Deliver a Package by Air. A relief airplane is delivering a food package to a group of people stranded on a very small island. The island is too small for the plane to land on, and the only way to deliver the package is by dropping it.

Part 1 – Write a MATLAB program using Graphical User Interface (GUI) to help the airplane pilot todetermine when to release the package in the air to ensure successful food delivery. Ignore airresistance in the calculation. Assume the island is approximately at sea level.

Show a picture of the food package in GUI when the program starts.

Show a picture of the island in GUI.

The pilot can fly the airplane horizontally at a constant speed while input values into your graphicaluser interface program. The pilot can set the horizontal speed of the airplane in miles per hour(mph). The pilot can also set the altitude of the airplane in meters (m) for calculation.
The program gives the pilot a slider bar to adjust the current constant airplane speed between 0 and1000 miles per hour (mph) in GUI. The program clearly shows the speed value set by the slider barin an Edit Text Box on the Graphical User Interface. The pilot should also be able to manually type inthe airplane speed in the Edit Text Box. The value input in the Edit Text Box should move the sliderlocation.

The program provides the pilot another slider bar to adjust the current airplane altitude between 0to 1000 meters (m). The program clearly shows the altitude value set by the slider bar clearly in anEdit Text Box on the Graphical User Interface. The pilot should be able to manually type in thealtitude in the Edit Text Box as well. The value input in the Edit Text Box should also move the sliderbar.

Provide the pilot a simulation button in GUI. When the simulation button is pressed, the MATLABprogram will compute the distance D (the horizontal distance between the airplane and the island) atwhich the package should be released. The program will then advise the pilot, using a Static TextBox, the angle in degrees when the island appears in the pilot’s line-of-sight that the pilot shouldrelease the food package.

Plot the estimated package projectile motion in the x and y coordinates with 100 equally spacedpoints in GUI from the moment the package is released in the air to the moment the object arrives tothe island. The diameter of the island is approximately 40 meters. Assume the food will lend at thecenter of the island, add a line to represent the island in the plot. Add a point in the plot to markthe estimated location of the airplane.

The example equations to use: http://www.masteringphysicssolutions.net/mastering-physics-solutionsdelivering-a-package-by-air/

Pressing the simulation button should also start the line of sight angle counter, starting from 0degree, incrementing 0.1 degree at a time, in a static text box. The MATLAB built-in pause() functioncan pause the program briefly between the angle increments.

Part 2 – Prepare a GUI button for the pilot to release the food. The pilot can look at the line-of-sightangle counter to decide when to press the GUI button to release the food. It is possible that the pilotcan miss the recommended line-of-sight angle. With the same constant speed and altitude as thepilot input in the edit text boxes or sliders, calculate if the food would land on the island using theline-of-sight angle when the pilot press the button to release the food.

Compute the actual horizontal distance between the airplane and the island (actual x) using theactual angle and h. Compute the delta between D and actual x. Add delta to the estimated xdirection coordinates to obtain the actual x-direction coordinates of the food.

Plot the actual food projectile motion in the same figure generated for Part 1. Add a point in theplot to mark the actual location of the airplane.

Add plot title, legends, and label each axis clearly.

Add an if..else..endstatement in the program to compare if the absolute value of delta is less than orequal to 20 m. If it is, update the picture of the island for successful delivery. Otherwise, update thepicture of the island to let the pilot know that the food is in the sea.

Solution

functionvarargout = food_drop(varargin)

% FOOD_DROP MATLAB code for food_drop.fig

%      FOOD_DROP, by itself, creates a new FOOD_DROP or raises the existing

%      singleton*.

%

%      H = FOOD_DROP returns the handle to a new FOOD_DROP or the handle to

%      the existing singleton*.

%

%      FOOD_DROP(‘CALLBACK’,hObject,eventData,handles,…) calls the local

%      function named CALLBACK in FOOD_DROP.M with the given input arguments.

%

%      FOOD_DROP(‘Property’,’Value’,…) creates a new FOOD_DROP or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before food_drop_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to food_drop_OpeningFcn via varargin.

%

%      *See GUI Options on GUIDE’s Tools menu.  Choose “GUI allows only one

%      instance to run (singleton)”.

%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help food_drop

% Last Modified by GUIDE v2.5 13-Oct-2017 16:51:44

% Begin initialization code – DO NOT EDIT

gui_Singleton = 1;

gui_State = struct(‘gui_Name’,       mfilename, …

‘gui_Singleton’,  gui_Singleton, …

‘gui_OpeningFcn’, @food_drop_OpeningFcn, …

‘gui_OutputFcn’,  @food_drop_OutputFcn, …

‘gui_LayoutFcn’,  [] , …

‘gui_Callback’,   []);

ifnargin&&ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1});

end

ifnargout

[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

gui_mainfcn(gui_State, varargin{:});

end

% End initialization code – DO NOT EDIT

% — Executes just before food_drop is made visible.

functionfood_drop_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject    handle to figure

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% varargin   command line arguments to food_drop (see VARARGIN)

% Choose default command line output for food_drop

handles.output = hObject;

init(handles)

% initialize data

handles.N = 100; % data points for plots

handles.tstep = 0.1; % step between time increments in LOS angle

global D x y release simbutton

% state of push buttons

release = 0;

simbutton = 0;

% plot data

x = zeros(1,handles.N); % horizontal distance data

y = zeros(1,handles.N); % vertical distance data

% horizontal drop distance

D = 0;

handles.txt = {‘Estimated Food Path’, ‘Island’, ‘Estimated Airplane’, …

‘Actual Food Path’, ‘Actual Airplane’}; % legend text

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes food_drop wait for user response (see UIRESUME)

% uiwait(handles.figure1);

% — Outputs from this function are returned to the command line.

functionvarargout = food_drop_OutputFcn(hObject, eventdata, handles)

% varargout  cell array for returning output args (see VARARGOUT);

% hObject    handle to figure

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1} = handles.output;

functionh_Callback(hObject, eventdata, handles)

% hObject    handle to h (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,’String’) returns contents of h as text

%        str2double(get(hObject,’String’)) returns contents of h as a double

height = str2num(get(hObject,’string’));

set(handles.h_slider, ‘value’, height);

handles = reset_params(handles);

guidata(hObject, handles);

% — Executes during object creation, after setting all properties.

functionh_CreateFcn(hObject, eventdata, handles)

% hObject    handle to h (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    empty – handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

ifispc&&isequal(get(hObject,’BackgroundColor’), get(0,’defaultUicontrolBackgroundColor’))

set(hObject,’BackgroundColor’,’white’);

end

function v0_Callback(hObject, eventdata, handles)

% hObject    handle to v0 (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,’String’) returns contents of v0 as text

%        str2double(get(hObject,’String’)) returns contents of v0 as a double

vinit = str2num(get(hObject,’string’));

set(handles.v0_slider, ‘value’, vinit);

handles = reset_params(handles);

guidata(hObject, handles);

% — Executes during object creation, after setting all properties.

function v0_CreateFcn(hObject, eventdata, handles)

% hObject    handle to v0 (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    empty – handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

ifispc&&isequal(get(hObject,’BackgroundColor’), get(0,’defaultUicontrolBackgroundColor’))

set(hObject,’BackgroundColor’,’white’);

end

% — Executes on slider movement.

functionh_slider_Callback(hObject, eventdata, handles)

% hObject    handle to h_slider (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,’Value’) returns position of slider

%        get(hObject,’Min’) and get(hObject,’Max’) to determine range of slider

height = get(hObject,’value’);

set(handles.h, ‘string’, num2str(height));

handles = reset_params(handles);

guidata(hObject, handles);

% — Executes during object creation, after setting all properties.

functionh_slider_CreateFcn(hObject, eventdata, handles)

% hObject    handle to h_slider (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    empty – handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.

ifisequal(get(hObject,’BackgroundColor’), get(0,’defaultUicontrolBackgroundColor’))

set(hObject,’BackgroundColor’,[.9 .9 .9]);

end

% — Executes on slider movement.

function v0_slider_Callback(hObject, eventdata, handles)

% hObject    handle to v0_slider (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,’Value’) returns position of slider

%        get(hObject,’Min’) and get(hObject,’Max’) to determine range of slider

vinit = get(hObject,’value’);

set(handles.v0, ‘string’, num2str(vinit));

handles = reset_params(handles);

guidata(hObject, handles);

% — Executes during object creation, after setting all properties.

function v0_slider_CreateFcn(hObject, eventdata, handles)

% hObject    handle to v0_slider (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    empty – handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.

ifisequal(get(hObject,’BackgroundColor’), get(0,’defaultUicontrolBackgroundColor’))

set(hObject,’BackgroundColor’,[.9 .9 .9]);

end

% — Executes on button press in sim_button.

functionsim_button_Callback(hObject, eventdata, handles)

% hObject    handle to sim_button (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global release simbutton D x y

% are all parameters keyed in

if isempty(get(handles.h,’String’))||isempty(get(handles.v0,’String’))

msgbox(‘Make sure height and velocity data are keyed in’)

uiwait

return

end

% reset angle

set(handles.theta,’String’, ‘0.0’)

% toggle handles.simbutton

simbutton = 1;

% has drop been done, if yes, reset images

if release == 1

axes(handles.axes2)

imshow(‘food.jpg’)

axes(handles.axes3)

imshow(‘island1.jpg’)

end

% constants

mi2m = 1609.344;

h2s  = 3600;

g = 9.8;

% data

vinit = get(handles.v0_slider, ‘Value’);

height = get(handles.h_slider, ‘Value’);

% speed to m/s

v0 = vinit*mi2m/h2s;

% time to fall

tf = sqrt(2*height/g);

% horizontal distance

a = 0; % horizontal acceleration in m/s

D = v0*tf + 0.5*a*tf^2;

handles.D = D;

% angle in degrees (depression)

alpha = fix(rad2deg(atan(height/D))*10)/10;

% display

set(handles.r_txt, ‘String’, num2str(alpha), ‘HorizontalAlignment’, …

‘Right’);

% projectile data

t = linspace(0, tf, handles.N);

x = v0*t;

y = height-0.5*g*t.^2;

% island location data

xi = D;

yi = 0;

% plane location data

xp = 0;

yp = height;

% create plot

% first clear

axes(handles.axes1)

axes(handles.axes1),cla handles.axes1 reset

legend(handles.axes1,’hide’) % clear axes

% plot

plot(x*1e-3, y, ‘LineWidth’, 1)

hold on

plot(xi*1e-3, yi, ‘o’, ‘LineWidth’, 3)

plot(xp*1e-3, yp, ‘>’, ‘MarkerSize’, 7, ‘LineWidth’, 1.5)

hold off

grid

grid minor

dx = D*0.1*1e-3;

dy = height*0.1;

xn = x*1e-3;

set(gca,’XLim’, [min(xn)-dx max(xn)+dx], ‘YLim’, [min(y)-dy max(y)+dy], …

‘FontName’, ‘Times New Roman’, ‘FontSize’, 12)

title(‘Deliver a Package by Air Simulation’)

ylabel(‘h (m)’)

xlabel(‘D (km)’)

% start incrementing the LOS angle in degrees

release = 0; % assume the drop has not been done

inc = 0.1; % increments in degrees

dur = handles.tstep; % pause duration in seconds

nxt = ‘0’;

handles.toggle = 0;

while str2num(nxt) < 90 && release == 0

nxt = num2str(str2num(get(handles.theta,’String’)) + inc); % next value

set(handles.theta, ‘String’, nxt);

pause(dur);

end

% if nxt == 90

%     msgbox(‘The drop is no longer possible.’)

%     uiwait

% end

guidata(hObject, handles)

% — Executes on button press in rel_button.

functionrel_button_Callback(hObject, eventdata, handles)

% hObject    handle to rel_button (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

global release simbutton D x y

% has the sim button been pressed?

ifsimbutton == 0

msgbox(‘Press the SIMULATE button first’)

uiwait

return

end

% has the release been done already

if release == 1

msgbox(‘Release Done Already.’)

uiwait

return

end

% flight-island parameters

alpha = str2num(get(handles.theta, ‘String’));

height = str2num(get(handles.h, ‘String’));

% distance horizontal

d = height/tand(alpha);

xn = x + D – d; % new xdata

% plane location

xp = D-d;

yp = height;

% update plot

axes(handles.axes1)

hold on

plot(xn*1e-3, y, ‘LineWidth’, 1)

plot(xp*1e-3, yp, ‘>’, ‘MarkerSize’, 7, ‘LineWidth’, 1.5)

hold off

legend(handles.txt, ‘Location’, ‘SouthWest’)

a = [min(xn) max(xn) min(x) max(x)]*1e-3;

dx = 0.1*(max(a)-min(a));

set(gca,’XLim’, [min(a)-dx max(a)+dx])

% add images depending on scenario

if abs(d-D) <= 20

axes(handles.axes3)

imshow(‘full_plates.jpg’)

else

axes(handles.axes2)

imshow(‘ocean.jpg’)

axes(handles.axes3)

imshow(’empty_plate.jpg’)

end

% drop done, successful or not

release = 1;

guidata(hObject, handles)

functiontheta_Callback(hObject, eventdata, handles)

% hObject    handle to theta (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,’String’) returns contents of theta as text

%        str2double(get(hObject,’String’)) returns contents of theta as a double

% — Executes during object creation, after setting all properties.

functiontheta_CreateFcn(hObject, eventdata, handles)

% hObject    handle to theta (see GCBO)

% eventdata  reserved – to be defined in a future version of MATLAB

% handles    empty – handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

%       See ISPC and COMPUTER.

ifispc&&isequal(get(hObject,’BackgroundColor’), get(0,’defaultUicontrolBackgroundColor’))

set(hObject,’BackgroundColor’,’white’);

end

functioninit(handles)

% set default pictures

% default pictures

axes(handles.axes2)

imshow(‘food.jpg’)

axes(handles.axes3)

imshow(‘island1.jpg’)

% default slider position

set(handles.h_slider, ‘value’, handles.h_slider.Min);

set(handles.v0_slider, ‘value’, handles.v0_slider.Min);

% default angle

set(handles.theta, ‘String’, ‘0.0’)

function handles = reset_params(handles)

% resets some basic parameters

% clear figures

% first clear

axes(handles.axes1),cla handles.axes1 reset

legend(handles.axes1,’hide’) % clear axes

axes(handles.axes2),cla handles.axes2 reset

legend(handles.axes2,’hide’) % clear axes

axes(handles.axes3),cla handles.axes3 reset

legend(handles.axes3,’hide’) % clear axes

% default pictures

axes(handles.axes2)

imshow(‘food.jpg’)

axes(handles.axes3)

imshow(‘island1.jpg’)

% static test

set(handles.theta,’String’, ‘0.0’)

set(handles.r_txt,’String’, ‘Recommend’)

% reset indicators of button press

globalsimbutton release

simbutton = 0;

release = 0;