I\'ve attached the Matlab code below for this problem. Please answer each part.
ID: 2080585 • Letter: I
Question
I've attached the Matlab code below for this problem. Please answer 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;
Explanation / Answer
% QUESTION A
clc;
close all;
%Cruise control example
clear all;
m = 2400; % mass of car in kg
%v(1) = 10; % initial velocity in m/s
v(1) = 20; % initial velocity in m/s
u(1) = 0; % initial throttle input
deltaT = 0.05; % time step
%maxT = 45; % max time
maxT = 60; % 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
Ki = 0.1; % integral gain
Kp = 1; % proportional gain
% add any hill rules here
theta = (0<t<20)*0.0 + (t>=20)*(pi/15) %radians or degrees?
figure
plot(t,theta);
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
% 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(i-1), 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.
figure
plot( t, v, 'b' );
hold on;
plot( t, a, 'r' );
legend ('output1', 'output2')
% QUESTION B
clc;
close all;
%Cruise control example
clear all;
m = 900; % mass of car in kg
%v(1) = 10; % initial velocity in m/s
v(1) = 20; % initial velocity in m/s
u(1) = 0; % initial throttle input
deltaT = 0.05; % time step
%maxT = 45; % max time
maxT = 60; % 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
Ki = 0.1; % integral gain
Kp = 1; % proportional gain
% add any hill rules here
theta = (0<t<20)*0.0 + (t>=20)*(pi/15) %radians or degrees?
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
% 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(i-1), 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.
figure
plot( t, v, 'b' );
hold on;
plot( t, a, 'r' );
legend ('output1', 'output2')
clc;
close all;
%Cruise control example
clear all;
m = 1500; % mass of car in kg
%v(1) = 10; % initial velocity in m/s
v(1) = 20; % initial velocity in m/s
u(1) = 0; % initial throttle input
deltaT = 0.05; % time step
%maxT = 45; % max time
maxT = 60; % 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
Ki = 0.1; % integral gain
Kp = 1; % proportional gain
% add any hill rules here
theta = (0<t<20)*0.0 + (t>=20)*(pi/15); %radians or degrees?
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
% 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(i-1), 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.
figure
plot( t, v, 'b' );
hold on;
plot( t, u, 'r' );
legend ('output1', 'output2')
% QUESTION C
clc;
close all;
%Cruise control example
clear all;
m = 1200; % mass of car in kg
%v(1) = 10; % initial velocity in m/s
v(1) = 20; % initial velocity in m/s
u(1) = 0; % initial throttle input
deltaT = 0.05; % time step
%maxT = 45; % max time
maxT = 5; % 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
Ki = 0.01; % integral gain
Kp = 0.5; % proportional gain
%Changed Ki = 0.01 and Kp = 0.5
% add any hill rules here
theta = (0<t<20)*0.0 + (t>=20)*(pi/15); %radians or degrees?
figure
plot(t,theta);
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
% 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(i-1), 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.
figure
plot( t, v, 'b' );
hold on;
plot( t, u, 'r' );
legend ('output1', 'output2')