% Script to read GPS car data collected in Arizona
% Programmer : Toni Trani
% Date: March 3, 2023

clc
clear
close all

% load the GPS data file to WorkSpace.
load ('GPS_Data_run1.m')

% Rename variable to facilitate computations

GPS_Data = GPS_Data_run1;

% columns of the GPS data file
% Column 1 = Time of observation (seconds)
% Column 2 = Distance traveled (m)
% Column 3 = Speed (km/hr)
% Column 4 = Acceleration (m/s-s)

% Read the GPS Data 
% define and obtain the paramenter named speed and time in GPS data file. 
time = GPS_Data(:,1);
distance = GPS_Data(:,2);
speed = GPS_Data(:,3);

% Plot the speed of the car (in y-axis) vs time (x- axis).
figure
plot(time,speed, '^-b','LineWidth',3)
title('Speed (km/hr) vs Time (seconds)')
xlabel('Time (seconds)')
ylabel('Speed (km/hr)')
grid 

% Make a comet plot to animate the car trajectory

% Plot the speed of the car (in y-axis) vs time (x- axis).
figure
comet(time,speed,0.2)
title('Speed (km/hr) vs Time (seconds)')
xlabel('Time (seconds)')
ylabel('Speed (km/hr)')
grid 

% hold on
% convert km/hr to m/s
speed_meter_second = speed .* (1000/3600);
% calculate the acceleration rate of car in m/s2
acceleration = gradient(speed_meter_second,time);

% Plot the distance versus time diagram

figure
plot(time,distance, 'o--r')
title('Time-Space Diagram')
xlabel('Time (seconds)')
ylabel('Distance (m)')
grid 

% Plot the acceleration of the car (in y-axis) vs time (x- axis).
% figure
% plot(time,acceleration, 'o--r')
% title('Acceleration m/s2) vs Time (seconds)')
% xlabel('Time (seconds)')
% ylabel('Acceleration (m/s2)')
% grid 

% Find the Maximum Acceleration 
min_acceleration = min(acceleration); %find the maximum deceleration that is equal to minimum acceleration in acceleration vector.
min_Acceleration_index = find(acceleration == min_acceleration ); %find the index of minimum acceleration.
min_acceleration_time = time(min_Acceleration_index); %find the time at which, vehicle has the maximum deceleration.

% Find the Average Speed of the Car 
%Find the average speed of the car for the complete profile. 
speed_average= mean(speed);

% Find Stopped Time 
%find the number of seconds the car is stopped during the recorded data.
stop_time = length(find(speed == 0));



