% cruise.m - generate plots for cruise controller
% RMM, 26 Aug 05

aminit;					% initialize MATALB

% Generate the torque curve
beta = 0.4;
Tmax = 190;
omegamax = 420;
omega = 0:1:700;
Tomega = Tmax * (1  - beta * (omega/omegamax - 1).^2);

figure(1); clf;
subplot(211); plot(omega, Tomega);
axis([0 700 0 200]);
% a=gca; set(a,'FontSize',16); 
grid on;
xlabel('omega');
ylabel('T', 'Rotation', 90);
%amprint('torque.eps');

%
% PI controller simulation
%

[t, x] = ode45('drivetrain', [0 20], [20 -0.25]);
vd = 20 * ones(length(t));
vert = [18; 22];			% vertical line

figure(2); clf; subplot(221);
xy = plot(t, x(:,1), 'b-');
set(xy, 'LineWidth', AM_data_linewidth);
hold on; xy = plot(t, vd, 'k-', [5, 5], vert, 'k-');
set(xy, 'LineWidth', AM_ref_linewidth);
axis([0 20 18 22]);

% a=gca; set(a,'FontSize',16);
xlabel('time (sec)');
ylabel('velocity (m/s)');
% Controller dynamics
%kp = 1; ki = 0.5;			% controller gains
kp = 0.3; ki = 0.1;			% controller gains
vref = 20;				% reference speed
e = x(:,1) - vref;			% speed error
u = -(kp * e + ki * x(:,2));		% engine input
subplot(222)
plot(t,u,'-');


%amprint('cruisepi.eps');

