Do you see a problem here? I get exact new results for both runs.
MU = 0.5 # Coefficient of friction
T = 1.5 # Total time
T_UB = 2.0 # Upper bound on total time
N_knots = 25 # Number of knot points per segment
M = 74 # Mass of the pendulum (kg)
L = 1.74 # Length of the pendulum (m)
lleg = Link(m=0.093 * M, l=0.246 * L) # Lower leg link
uleg = Link(m=0.200 * M, l=0.245 * L) # Upper leg link
torso = Link(m=0.678 * M, l=0.47 * L) # Torso link
lleg = Link(m=1, l=1) # Link 1
uleg = Link(m=1, l=1) # Link 2
torso = Link(m=1, l=1) # Link 3
SEAT_POS = (-0.4, 0.4) # Seat position
FOOT_X_LIMS = (-0.05, 0.2) # Foot x limits
R_weights_init = (1, 1, 1) # Control weights
R_weights = (1, 3, 1)
U_UB = (None,) * 3 # Upper bound on control inputs
U_LB = (None,) * 3 # Lower bound on control inputs
Define the system
sts = STS(link1=lleg, link2=uleg, link3=torso,
mu=MU, seat_pos=SEAT_POS, foot_x_lims=FOOT_X_LIMS)
sts_optimizer = STSTrajectoryOptimizer(sts,
control_weights=R_weights_init,
)
x0 = (None, None, -0.01, 0, 0, 0) # Initial state
xf = (0, 0, 0, 0, 0, 0)
j1, j2 = solve_ik_optimization(*SEAT_POS, lleg.l, uleg.l)
Adjust the joint angles to match STS convention
j2 = j1 + j2 - np.pi/2
j1 = j1 - np.pi/2
print(j1, j2)
sit_mode_initial_guess_x0 = np.array([j1, j2, -0.01, 0, 0, 0]).reshape(-1, 1)
sit_mode_initial_guess_xf = np.array([j1, j2, -np.pi/6, 0, 0, -0.1]).reshape(-1, 1)
sit_mode_initial_guess = np.hstack((sit_mode_initial_guess_x0, sit_mode_initial_guess_xf))
mode_sit = Mode(
name='sit',
dynamics_fn=sts.sit_dynamics_fn,
n_x=6,
n_u=3,
constraints_fns=[
sts_optimizer.seat_contact_no_slip_constraint_fn,
sts_optimizer.sit_path_constraint_fn,
],
initial_x_guess=sit_mode_initial_guess,
u_lb=U_LB,
u_ub=U_UB,
)
mode_stand = Mode(
name='stand',
dynamics_fn=sts.stand_dynamics_fn,
n_x=6,
n_u=3,
constraints_fns=[
# sts_optimizer.COP_constraint_fn,
sts_optimizer.knee_joint_constraint_fn,
],
u_lb=U_LB,
u_ub=U_UB,
)
transition_sit_stand = Transition(
guard_fn=lambda x, u: sts.sit_contact_force_fn(x, u)[1], # y component of the contact force
reset_map_fn=sts_optimizer.sit_stand_map_fn,
)
hybrid_params = HybridSystemParams(
mode_sequence=[mode_sit, mode_stand],
transitions=[transition_sit_stand],
x0=x0, # Initial state
xf=xf, # Final state
# T = T, # Total time
T_ub = T_UB, # Upper bound on total time
min_time_step = 1E-6,
num_knot_points_per_segment = N_knots, # Number of knot points per segment
)
hybrid_optimizer = HybridTrajectoryOptimizer(
params=hybrid_params,
cost_fn=sts_optimizer.cost_function,
global_constraints_fns=[sts_optimizer.base_contact_no_slip_global_constraint_fn,
sts_optimizer.COP_global_constraint_fn,
sts_optimizer.equal_time_segment_global_constraint_fn,
sts_optimizer.control_effort_vanishes_global_constraint_fn,
],
)
Solve the optimization problem
solver_name = 'ipopt'
solver_options = {
'ipopt.print_level': 5,
'ipopt.max_iter': 3000,
'ipopt.fixed_variable_treatment': 'relax_bounds',
'ipopt.tol': 1E-3,
}
solution = hybrid_optimizer.solve(solver_name=solver_name, opts=solver_options)
Now use the solution to warm start the simulation
DIRTY JOB
sts_optimizer.R = ca.diag(R_weights) # Update the control weights
hybrid_optimizer._setup_objective() # Re-setup the objective function with the new weights
solution = hybrid_optimizer.solve(solver_name, solver_options, warm_start=True)
SEAT_POS_NEW = (None, 0.2) # New seat position
Update the initial guess for the sit mode
mode_sit.initial_x_guess = solution['states'][0]
mode_stand.initial_x_guess = solution['states'][1]
sts_new = STS(link1=lleg, link2=uleg, link3=torso,
mu=MU, seat_pos=SEAT_POS_NEW, foot_x_lims=FOOT_X_LIMS)
sts_optimizer_new = STSTrajectoryOptimizer(sts_new,
control_weights=R_weights_init,
)
hybrid_params_new = HybridSystemParams(
mode_sequence=[mode_sit, mode_stand],
transitions=[transition_sit_stand],
x0=x0, # Initial state
xf=xf, # Final state
# T = T, # Total time
T_ub=T_UB, # Upper bound on total time
min_time_step=1E-6,
num_knot_points_per_segment=N_knots, # Number of knot points per segment
)
Recreate the hybrid optimizer with the new system
hybrid_optimizer_new = HybridTrajectoryOptimizer(
params=hybrid_params_new,
cost_fn=sts_optimizer_new.cost_function,
global_constraints_fns=[sts_optimizer_new.base_contact_no_slip_global_constraint_fn,
sts_optimizer_new.COP_global_constraint_fn,
sts_optimizer_new.equal_time_segment_global_constraint_fn,
sts_optimizer_new.control_effort_vanishes_global_constraint_fn,
],
)
Solve the optimization problem again with the new system
solution = hybrid_optimizer_new.solve(solver_name=solver_name, opts=solver_options)
End of dirty job