I want to ask a question related to a workflow like this:
1.obtain manipulator equation of multibody plant,
2.re-write it to a form of qdd = f(q,u),
3.calculate next state(x_next),
4.calculate the gradient of x_next w.r.t x and u.
I find the most difficult step is to get the inverse of Inertial matrix which support the gradient calculation in next following step. Drake provide many good tools support double, AutoDiffXd, Symbolic, could we use them to get thing done? Thanks!
Here is is a simple example of double pendulum.( I expect the work flow will be applied to more complicate system.)
def discrete_dynamics(x, u):
dt = 0.05
q = x[0:2]
qd = x[2:4]
plant = MultibodyPlant(time_step=dt)
parser = Parser(plant)
parser.AddModelFromFile("double_pendulum.sdf")
plant.Finalize()
(M, Cv, tauG, B, tauExt) = ManipulatorDynamics(plant.ToAutoDiffXd(), q, qd)
M_inv = inv(M) <------ # ????, how to do it
qdd = np.dot(M_inv,(tauG + np.dot(B ,u) - np.dot(Cv,qd)))
x_d_list = qd.tolist()
qdd_list = qdd.tolist()
x_d_list.extend(qdd_list)
x_d = np.array(x_d_list)
x_next = x+x_d*dt
return x_next
f = discrete_dynamics(x, u)
f_x = ... #calculate gradient of f w.r.t x
f_u = ... #calculate gradient of f w.r.t u