1 Star 0 Fork 0

马紫玉666/KLE3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mylunarlander.py 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
from jax import jacfwd, jit
from jax.ops import index, index_add, index_update
import jax.numpy as np
from jax.lax import cond
import scipy
Hz = 10.
@jit
def f(x, u_free):
u = np.tanh(u_free)
x1, x2, th, x1dot, x2dot, thdot = x
xddot = -np.sum(u) * np.sin(th)
yddot = np.sum(u) * np.cos(th) - 1.
thddot = 2.0*(u[0] - u[1])
xdot = np.array([x1dot, x2dot, thdot, xddot, yddot, thddot])
xnew = x + xdot/Hz
return xnew
def wrap2pi(th):
x = np.fmod(th + np.pi, 2.0*np.pi)
x = cond(x < 0, x, lambda x: x+2.0*np.pi, x, lambda x: x)
return x - np.pi
# LQR controller
x_eq, u_eq = np.zeros(6), np.ones(2)*0.5
lqr_config = {
'A' : jacfwd(f)(x_eq, u_eq),
'B' : jacfwd(f, argnums=1)(x_eq, u_eq),
'Q' : np.diag(np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0])),
'R' : np.diag(0.001*np.ones(2))
}
def get_lqr_from_config(config):
A, B = config['A'], config['B']
Q, R = config['Q'], config['R']
P = scipy.linalg.solve_discrete_are(A, B, Q, R)
k_lqr = np.linalg.inv(B.T.dot(P).dot(B) + R).dot(B.T.dot(P).dot(A))
return k_lqr
# equilibrium stability policy
k_lqr = get_lqr_from_config(lqr_config)
@jit
def pi(x):
xmod = index_update(x, 2, wrap2pi(x[2]))
return -np.dot(k_lqr, xmod)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ma-ziyu-666/kle3.git
git@gitee.com:ma-ziyu-666/kle3.git
ma-ziyu-666
kle3
KLE3
master

搜索帮助