Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve attached the matlab code below for the problem. Please ansnwer each part.

ID: 2080584 • Letter: I

Question

I've attached the matlab code below for the problem. Please ansnwer each part.

mycruisecontrol.m------------

%Cruise control example
clear all;
m = 2400; % mass of car in kg
v(1) = 10; % initial velocity in m/s
u(1) = 0; % initial throttle input

deltaT = 0.05; % time step
maxT = 45; % max time
t = [0 : deltaT : maxT];

vd = 20; % desired velocity in m/s
e(1) = vd - v(1); % initial error in velocity
eI(1) = e(1)*deltaT; % initial error integral

% Controller
Ki = 0.02;               % integral gain
Kp = 1;                   % proportional gain

for i=2:length(t)

%based on current speed, pick a gear (see figure 3.2)
if v(i-1) < 12
gear = 1;
elseif v(i-1) < 18
gear = 2;
elseif v(i-1) < 27
gear = 3;
elseif v(i-1) < 40
gear = 4;
else
gear = 5;
end

% add any hill rules here
theta = 0.0; %radians or degrees?
  
% change desired velocity here
if t(i-1)>20
vd = 17.5;
end
  
%calculate control input
u(i) = Kp*e(i-1) + Ki*eI(i-1); % PI control input
% Saturate the input to the range of 0 to 1
u(i) = min(u(i), 1); u(i) = max(0, u(i));

dv = cruisedyn(v(i-1), u(i), gear, theta, m);
  
%update my states and results
a(i) = dv;
v(i) = v(i-1)+dv*deltaT;
e(i) = vd - v(i); %error in velocity
eI(i) = e(i)*deltaT + eI(i-1); %error in integral
  

end

%plot whatever outputs I am interested in.
plot( t, v, 'b' );
hold on;
plot( t, u, 'r' );
legend ('output1', 'output2')

----------------cruisedyn.m----------

% cruisedyn - dynamic model for cruise control example
%
% Usage: acc = cruisedyn(v, u, gear, theta, m)
%
% This function returns the acceleration of the vehicle given the
% current velocity (in m/s), throttle setting (0 <= u <= 1), gear (1-5),
% road slope (default 0) and car mass (default 1000 kg).

% RMM, 2 Jul 06

function dv = cruisedyn(v, u, gear, theta, m)

% Check the parameters to make sure that they are OK
% MISSING

% Parameters for defining the system dynamics
alpha = [40, 25, 16, 12, 10];       % gear ratios
Tm = 190;               % engine torque constant, Nm
wm = 420;               % peak torque rate, rad/sec
beta = 0.4;               % torque coefficient
Cr = 0.01;               % coefficient of rolling friction
rho = 1.3;               % density of air, kg/m^3
Cd = 0.32;               % drag coefficient
A = 2.4;               % car area, m^2
g = 9.8;               % gravitational constant

% Set defaults if all arguments aren't passed
if (nargin < 3) gear = 1; end;   % gear ratio
if (nargin < 4) theta = 0; end;   % road angle
if (nargin < 5) m = 1000; end;   % mass of the car

% Check to make sure arguments are reasonable
if (gear < 1 || gear > 5)
fprintf(2, 'cruisedyn: gear ratio out of range');
acc = 0; return;
end

if (theta < -pi/4 || theta > pi/4)
fprintf(2, 'cruisedyn: road slope out of range ');
acc = 0; return;
end

if (m < 500 || m > 10000)
fprintf(2, 'cruisedyn: vehicle mass out of range ');
acc = 0; return;
end

% Saturate the input to the range of 0 to 1
u = min(u, 1); u = max(0, u);

% Compute the torque produced by the engine, Tm
omega = alpha(gear) * v;       % engine speed
torque = u * Tm * ( 1 - beta * (omega/wm - 1)^2 );
F = alpha(gear) * torque;

% Check to make sure engine settings are reasonable
if (omega > 700)
fprintf(2, 'cruisedyn: engine rpm too high (%g) ', omega*30/pi);
end

if (torque < 0)
fprintf(2, 'cruisedyn: torque is negative (u=%g, omega=%g) ', u, omega);
end

% Compute the external forces on the vehicle
Fr = m * g * Cr;           % Rolling friction
Fa = 0.5 * rho * Cd * A * v^2;       % Aerodynamic drag
Fg = m * g * sin(theta);       % Road slope force
Fd = Fr + Fa + Fg;           % total deceleration

% Now compute the acceleration
dv = (F - Fd) / m;

Problem 1. Download the following MATLAB scripts in Canvas and put them in one folder: cruise dyn.m myCruiseControl m Go through the scripts and answer the following question (a) How is gear shifted according to speed? (b) In the graph, what does each output mean? (c) How are the control inputs (i.e. desired speed, hill angle, gear) changed during this simulation?

Explanation / Answer

a).

    if v < 12 gear = 1;
    if v < 18 gear = 2;
    if v < 27 gear = 3;
    if v < 40 gear = 4;
    if v > 40 gear = 5;


b)

   output1: v - i.e velocity in m/s
   output2: u - control input

c)
    u(i+1)=Kp*e(i)+Ki*eI(i)       0<u(i+1)<1
  
   u(i+1)=Kp*(vd-v(i))+Ki*(e(i)*dt+eI(i-1))

   u(i+1)=Kp*(vd-v(i-1)-dv*dt)+Ki*(e(i)*dt+eI(i-1))


where    u = control input parameter
   Kp = proportioanl constant
   Ki = integral constant
   vd = desired speed
    dt = hill angle
   v = velocity
   dv = accelaration of the vehical, which is dependent on gear