Simulation of a Motor and Cart System L01 Lauren Hayes Completed February 15, 2024 Submitted February 15, 2024 1 Summary: The experiment aimed to replicate the lab's cart system manually using state equations and Euler’s method in MATLAB. It involved simulating both nonlinear friction and linear approximations, adjusting step sizes, and varying feedback gain and input amplitudes. Results were plotted to justify sampling time, analyze system responses to step inputs, and compare linear and nonlinear simulations. The report included code documentation for transparency. The experimental setup was based on specified parameters, including Ra, Kt, Kb, and J. Overall, the study provided insights into system dynamics and the impact of feedback gain and input amplitude on cart position in the simulated environment. Block Diagram: Block diagram of the cart system – from the Lab Manual 2 Procedure: From the block diagram above, the state equations describing the system were generated. The equations, along with the known parameters, were transferred into a MATLAB script. When writing the script, one decision to be made was the proper step interval to use (that is, the change in time, dt). A test and compare method was used. The value of 0.005 was settled upon due to its delivery of clean results. This value makes sense as Euler’s method will achieve a cleaner approximation the more values are calculated within a specific time interval. dt = 0.05 dt = 0.025 dt = 0.005 (chosen) 3 Results: It was found that once in steady state, linear and nonlinear did not differ in any of the cases. However, in the time that it took to reach steady state, the linear approximation exhibited a much faster response, especially in cases II and III. The nonlinear did have a lower percent overshoot, as can be seen in cases I and II. A feedback gain of five produced a faster response but with an overshoot, whereas a feedback gain of two produced no overshoot but a slower response time. The highest feedback gain with the lowest input magnitude produced the most identical response between the nonlinear and linear approximations. Only in cases I and II did the nonlinear position ever exceed that of the linear. CASE I 4 CASE II 5 CASE III 6 CASE IV 7 Appendix: %-------------------------------------------------------------------------% This script performs simulations of a linear and a nonlinear system % subject to different feedback gains and step input magnitudes. It tests % different sampling times to find an appropriate time step for the % simulations. The system models a cart with parameters such as moment of % inertia, voltage constant, resistance, torque constant, and feedback gain. %-------------------------------------------------------------------------% Parameters: % - J: Moment of inertia (4 Kg-m^2) % - kb: Voltage constant (volt/rad/sec) % - R: Resistance (Ohms) % - kt: Torque constant (N-m/amp) % - k0: Feedback gain %-------------------------------------------------------------------------% Simulation Time: % - t_start: Start time % - t_end: End time % - step_size: Fixed step size for simulation % - feedback_gains: Feedback gain values to test % - input_magnitudes: Step input magnitudes to test %-------------------------------------------------------------------------% Author: Lauren Hayes % Date: 2/15/2024 %-------------------------------------------------------------------------% Start close all; % Close all existing figures clc; % Clear the command window % Define system parameters J = 8 * 10^-4; % Moment of inertia (4 Kg-m^2) kb = 0.1; % Voltage constant (volt/rad/sec) R = 2; % Resistance (Ohms) kt = 0.1; % Torque constant (N-m/amp) k0 = 4; % Feedback gain % Define simulation time parameters t_start = 0; % Start time t_end = 10; % End time dt = 0.005; % Fixed step size for simulation % Define feedback gains and input magnitudes feedback_gains = [5, 2]; % Feedback gain values to test input_magnitudes = [1, 2]; % Step input magnitudes to test 8 % Loop over different feedback gains for gain_idx = 1:length(feedback_gains) k0 = feedback_gains(gain_idx); % Current feedback gain value % Loop over different step input magnitudes for input_idx = 1:length(input_magnitudes) % Reset initial conditions for each simulation theta = zeros(1, (t_end - t_start) / dt + 1); % Initialize angle array for linear simulation omega = zeros(1, (t_end - t_start) / dt + 1); % Initialize angular velocity array for linear simulation THETA = zeros(1, (t_end - t_start) / dt + 1); % Initialize angle array for nonlinear simulation OMEGA = zeros(1, (t_end - t_start) / dt + 1); % Initialize angular velocity array for nonlinear simulation % Simulate linear system for t = 1:length(theta)-1 Vr = 2 * input_magnitudes(input_idx); % Step input for linear simulation x = 2.53 * (theta(t) / 3); % Calculation for x in linear simulation Vs = 0.4 * x; % Calculation for Vs in linear simulation Vi = k0 * (Vr - Vs); % Calculation for Vi in linear simulation Pa = 2 * Vi; % Calculation for Pa in linear simulation Va = Pa - kb * (omega(t)); % Calculation for Va in linear simulation i = Va / R; % Calculation for current in linear simulation f = omega(t) * 0.01; % Calculation for friction in linear simulation T = (i * kt) - f; % Calculation for torque in linear simulation omegaPrime = T / J; % Calculation for angular acceleration in linear simulation theta(t+1) = theta(t) + dt * omega(t); % Update angle using Euler integration in linear simulation omega(t+1) = omega(t) + dt * omegaPrime; % Update angular velocity using Euler integration in linear simulation end % Simulate nonlinear system for t = 1:length(THETA)-1 VR = 2 * input_magnitudes(input_idx); % Step input for nonlinear simulation X = 2.53 * (THETA(t) / 3); % Calculation for X in nonlinear simulation VS = 0.4 * X; % Calculation for VS in nonlinear simulation VI = k0 * (VR - VS); % Calculation for VI in nonlinear simulation % Apply condition for PA calculation if -5 <= VI && VI <= 5 PA = 2 * VI; % Calculation for PA within range in nonlinear simulation elseif VI < -5 PA = -10; % Calculation for PA when VI is less than -5 in nonlinear simulation 9 else PA = 10; % Calculation for PA when VI is greater than 5 in nonlinear simulation end VA = PA - kb * (OMEGA(t)); % Calculation for VA in nonlinear simulation I = VA / R; % Calculation for current in nonlinear simulation % Apply condition for F calculation if OMEGA(t) < 0 F = OMEGA(t) * 0.008 - 0.02; % Calculation for friction when angular velocity is negative in nonlinear simulation elseif OMEGA(t) > 0 F = OMEGA(t) * 0.008 + 0.02; % Calculation for friction when angular velocity is positive in nonlinear simulation else F = 0; % No friction when angular velocity is zero in nonlinear simulation end T = (I * kt) - F; % Calculation for torque in nonlinear simulation OMEGAPrime = T / J; % Calculation for angular acceleration in nonlinear simulation THETA(t+1) = THETA(t) + dt * OMEGA(t); % Update angle using Euler integration in nonlinear simulation OMEGA(t+1) = OMEGA(t) + dt * OMEGAPrime; % Update angular velocity using Euler integration in nonlinear simulation end % Plot results for linear and nonlinear simulations figure; plot((0:length(theta)-1) * dt, theta, 'b', 'LineWidth', 2); hold on; % Plot linear simulation results plot((0:length(THETA)-1) * dt, THETA, 'r--', 'LineWidth', 2); % Plot nonlinear simulation results title(sprintf('Feedback Gain: %d, Input Magnitude: %d', k0, 2*input_magnitudes(input_idx))); % Set plot title xlabel('Time'); % Set x-axis label ylabel('Position (cm)'); % Set y-axis label legend('Linear', 'Nonlinear'); % Set legend grid on; % Add grid to the plot end end 10 11