Skip to content

Commit 636ab31

Browse files
committed
fix up calls to analytic Jacobian, generalize "torque" to "Q" for forward dynamics
1 parent 2d52378 commit 636ab31

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

roboticstoolbox/robot/Dynamics.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def fdyn(
186186
self,
187187
T,
188188
q0,
189+
Q=None,
190+
Q_args={},
189191
torque=None,
190192
torque_args={},
191193
qd0=None,
@@ -203,11 +205,11 @@ def fdyn(
203205
:type q0: array_like
204206
:param qd0: initial joint velocities, assumed zero if not given
205207
:type qd0: array_like
206-
:param torque: a function that computes torque as a function of time
208+
:param Q: a function that computes generalized joint force as a function of time
207209
and/or state
208-
:type torque: callable
209-
:param torque_args: positional arguments passed to ``torque``
210-
:type torque_args: dict
210+
:type Q: callable
211+
:param Q_args: positional arguments passed to ``Q``
212+
:type Q_args: dict
211213
:type solver: name of scipy solver to use, RK45 is the default
212214
:param solver: str
213215
:type solver_args: arguments passed to the solver
@@ -295,16 +297,19 @@ def myfunc(robot, t, q, qd, qstar, P, D):
295297

296298
n = self.n
297299

300+
if torque is not None:
301+
warnings.warn("torque option is deprecated, use Q and Q_args", DeprecationWarning)
302+
298303
if not isscalar(T):
299304
raise ValueError("T must be a scalar")
300305
q0 = getvector(q0, n)
301306
if qd0 is None:
302307
qd0 = np.zeros((n,))
303308
else:
304309
qd0 = getvector(qd0, n)
305-
if torque is not None:
306-
if not callable(torque):
307-
raise ValueError("torque function must be callable")
310+
if Q is not None:
311+
if not callable(Q):
312+
raise ValueError("generalized joint torque function must be callable")
308313

309314
# concatenate q and qd into the initial state vector
310315
x0 = np.r_[q0, qd0]
@@ -313,7 +318,7 @@ def myfunc(robot, t, q, qd, qstar, P, D):
313318
scipy_integrator = integrate.__dict__[solver]
314319

315320
integrator = scipy_integrator(
316-
lambda t, y: self._fdyn(t, y, torque, torque_args),
321+
lambda t, y: self._fdyn(t, y, Q, Q_args),
317322
t0=0.0,
318323
y0=x0,
319324
t_bound=T,
@@ -362,19 +367,19 @@ def myfunc(robot, t, q, qd, qstar, P, D):
362367
else:
363368
return namedtuple("fdyn", "t q qd")(tarray, xarray[:, :n], xarray[:, n:])
364369

365-
def _fdyn(self, t, x, torqfun, targs):
370+
def _fdyn(self, t, x, Qfunc, Qargs):
366371
"""
367372
Private function called by fdyn
368373
369374
:param t: current time
370375
:type t: float
371376
:param x: current state [q, qd]
372377
:type x: numpy array (2n,)
373-
:param torqfun: a function that computes torque as a function of time
378+
:param Qfunc: a function that computes torque as a function of time
374379
and/or state
375-
:type torqfun: callable
376-
:param targs: argumments passed to ``torqfun``
377-
:type targs: dict
380+
:type Qfunc: callable
381+
:param Qargs: argumments passed to ``torqfun``
382+
:type Qargs: dict
378383
379384
:return: derivative of current state [qd, qdd]
380385
:rtype: numpy array (2n,)
@@ -388,10 +393,10 @@ def _fdyn(self, t, x, torqfun, targs):
388393
qd = x[n:]
389394

390395
# evaluate the torque function if one is given
391-
if torqfun is None:
396+
if Qfunc is None:
392397
tau = np.zeros((n,))
393398
else:
394-
tau = torqfun(self, t, q, qd, **targs)
399+
tau = Qfunc(self, t, q, qd, **Qargs)
395400
if len(tau) != n or not all(np.isreal(tau)):
396401
raise RuntimeError(
397402
"torque function must return vector with N real elements"
@@ -1136,7 +1141,7 @@ def gravload_x(
11361141
if q.shape[0] == 1:
11371142
# single q case
11381143
if Ji is None:
1139-
Ja = self.jacob0(q[0, :], analytical=representation)
1144+
Ja = self.jacob0_analytical(q[0, :], representation=representation)
11401145
if pinv:
11411146
Ji = np.linalg.pinv(Ja)
11421147
else:
@@ -1150,7 +1155,7 @@ def gravload_x(
11501155
# z = np.zeros(self.n)
11511156

11521157
for k, qk in enumerate(q):
1153-
Ja = self.jacob0(qk, analytical=representation)
1158+
Ja = self.jacob0_analytical(qk, representation=representation)
11541159
G = self.gravload(qk)
11551160
if pinv:
11561161
Ji = np.linalg.pinv(Ja)
@@ -1230,7 +1235,7 @@ def accel_x(
12301235

12311236
for k, (qk, xdk, wk) in enumerate(zip(q, xd, w)):
12321237

1233-
Ja = self.jacob0(qk, analytical=representation)
1238+
Ja = self.jacob0_analytical(qk, representation=representation)
12341239
if pinv:
12351240
Ji = np.linalg.pinv(Ja)
12361241
else:
@@ -1251,7 +1256,7 @@ def accel_x(
12511256
# solve is faster than inv() which is faster than pinv()
12521257
# tau_rne = C(q,qd) + G(q)
12531258
# qdd = M^-1 (tau - C(q,qd) - G(q))
1254-
qdd = np.linalg.solve(M, J.T @ wk - tau_rne)
1259+
qdd = np.linalg.solve(M, Ja.T @ wk - tau_rne)
12551260

12561261
# xd = Ja qd
12571262
# xdd = Jad qd + Ja qdd
@@ -1262,7 +1267,7 @@ def accel_x(
12621267

12631268
# need Jacobian dot
12641269
qdk = Ji @ xdk
1265-
Jd = self.jacob_dot(qk, qdk, J0=J)
1270+
Jd = self.jacob0_dot(qk, qdk, J0=Ja)
12661271

12671272
xdd[k, :] = T @ (Jd @ qdk + J @ qdd)
12681273

0 commit comments

Comments
 (0)