Code Rev 2
Code Rev 2
# Control Parameters
K = 500
w = 1
Lambda = 3
# Simulation properties
dt = 1e-3
LONG = 100000
l = Int(LONG-1)
# System Parameters
m1 = 1 # mass of the first pendulum
m2 = 1 # mass of the second pendulum
L1 = 1 # length of the first pendulum
L2 = 1 # length of the second pendulum
g = 9.81 # acceleration due to gravity
# Initial conditions
theta1 = 0.1 # initial angle of the first pendulum
theta2 = 0.1 # initial angle of the second pendulum
theta1_dot = 0 # initial angular velocity of the first pendulum
theta2_dot = 0 # initial angular velocity of the second pendulum
# Nominal Trajectories
theta1_nom = pi * ones(l+1) # Fix 1: Create an array of nominal angles
theta2_nom = zeros(l+1) # Fix 1: Create an array of nominal angles
# Initialization
theta1 = theta1_arr[1]
theta2 = theta2_arr[1]
theta1_dot = theta1_dot_arr[1]
theta2_dot = theta2_dot_arr[1]
# Simulation
for i in 1:l
# store data
theta1_arr[i] = theta1
theta2_arr[i] = theta2
theta1_dot_arr[i] = theta1_dot
theta2_dot_arr[i] = theta2_dot
# sliding surface
S1 = Lambda * e1 + 2 * Lambda * e1_dot # Fix 3: Correct the sliding surface
calculation
S2 = Lambda * e2 + 2 * Lambda * e2_dot # Fix 3: Correct the sliding surface
calculation
# system dynamics
theta1_ddot = (m2 * L2 * theta2_dot^2 * sin(theta2-theta1) + (m1+m2) * g *
sin(theta1)) / ((m1 + m2) * L1)
theta2_ddot = (u - m2 * L1 * theta1_dot^2 * sin(theta2-theta1) - m2 * g *
sin(theta2)) / (m2 * L2) # Fix 6: Correct the dynamics equation
# Plotting
t = dt:dt:l*dt
tight_layout()
show()