Dokumen
Dokumen
Dokumen
# https://automaticaddison.com
Np.set_printoptions(precision=3,suppress=True)
# Optional Variables
“””
:param yaw: The yaw angle (rotation angle around the z axis) in radians
“””
[np.sin(yaw)*deltat, 0],
[0, deltat]])
Return B
“””
Calculates the state at time t given the state at time t-1 and
“””
# These next 6 lines of code which place limits on the angular and linear
Control_input_t_minus_1[0] = np.clip(control_input_t_minus_1[0],
-max_linear_velocity,
Max_linear_velocity)
Control_input_t_minus_1[1] = np.clip(control_input_t_minus_1[1],
-max_angular_velocity,
Max_angular_velocity)
Return state_estimate_t
“””
Compute the optimal control inputs given a nonlinear system, cost matrices,
“””
# programming method.
N = 50
P = [None] * (N + 1)
Qf = Q
P[N] = Qf
# For i = N, ..., 1
K = [None] * N
U = [None] * N
# For i = 0, ..., N – 1
For i in range(N):
U_star = u[N-1]
Return u_star
Def main():
Dt = 1.0
# Actual state
# Our robot starts out at the origin (x=0 meters, y=0 meters), and
Actual_state_x = np.array([0,0,0])
Desired_state_xf = np.array([2.000,2.000,np.pi/2])
# A matrix
# Typically a robot on wheels only drives when the wheels are told to turn.
[ 0,1.0, 0],
[ 0, 0, 1.0]])
# R matrix
# motors on the wheels that drive the linear velocity and angular velocity).
# The R matrix has the same number of rows as the number of control
# control inputs.
# This matrix has positive values along the diagonal and 0s elsewhere.
# Q matrix
# Launch the robot, and have it move to the desired goal destination
For i in range(100):
State_error_magnitude = np.linalg.norm(state_error)
B = getB(actual_state_x[2], dt)
Optimal_control_input = lqr(actual_state_x,
Desired_state_xf,
Q, R, A, B, dt)
Optimal_control_input)
# Stop as soon as we reach the goal
Break
Print()
Main()