2 Create a simulation environment for a N-pendulum.
17 import pinocchio
as pin
18 from display
import Display
19 from numpy.linalg
import inv
20 from pinocchio.utils
import rand
25 Class representing one 3D mesh of the robot, to be attached to a joint. The class
27 * the name of the 3D objects inside Gepetto viewer.
28 * the ID of the joint in the kinematic tree to which the body is attached.
29 * the placement of the body with respect to the joint frame.
30 This class is only used in the list Robot.visuals (see below).
33 def __init__(self, name, jointParent, placement):
38 def place(self, display, oMjoint):
39 oMbody = oMjoint * self.
placementplacement
40 display.place(self.
namename, oMbody,
False)
45 Define a class Robot with 7DOF (shoulder=3 + elbow=1 + wrist=3).
46 The configuration is nq=7. The velocity is the same.
47 The members of the class are:
48 * viewer: a display encapsulating a gepetto viewer client to create 3D objects and
50 * model: the kinematic tree of the robot.
51 * data: the temporary variables to be used by the kinematic algorithms.
52 * visuals: the list of all the 'visual' 3D objects to render the robot, each element
53 of the list being an object Visual (see above).
55 See tp1.py for an example of use.
59 """Create a Pinocchio model of a N-pendulum, with N the argument <nbJoint>."""
62 self.
modelmodel = pin.Model()
64 self.
datadata = self.
modelmodel.createData()
66 self.
q0q0 = np.zeros(self.
modelmodel.nq)
75 def createPendulum(self, nbJoint, rootId=0, prefix="", jointPlacement=None):
76 color = [red, green, blue, transparency] = [1, 1, 0.78, 1.0]
77 colorred = [1.0, 0.0, 0.0, 1.0]
81 jointPlacement
if jointPlacement
is not None else pin.SE3.Identity()
85 inertia = pin.Inertia(
87 np.array([0.0, 0.0, length / 2]),
88 mass / 5 * np.diagflat([1e-2, length**2, 1e-2]),
91 for i
in range(nbJoint):
93 name = prefix +
"joint" + istr
94 jointName, _bodyName = [name +
"_joint", name +
"_body"]
95 jointId = self.
modelmodel.addJoint(
96 jointId, pin.JointModelRY(), jointPlacement, jointName
98 self.
modelmodel.appendBodyToJoint(jointId, inertia, pin.SE3.Identity())
100 self.
viewerviewer.viewer.gui.addSphere(
101 "world/" + prefix +
"sphere" + istr, 0.15, colorred
106 Visual(
"world/" + prefix +
"sphere" + istr, jointId, pin.SE3.Identity())
109 self.
viewerviewer.viewer.gui.addCapsule(
110 "world/" + prefix +
"arm" + istr, 0.1, 0.8 * length, color
116 "world/" + prefix +
"arm" + istr,
118 pin.SE3(np.eye(3), np.array([0.0, 0.0, length / 2])),
121 jointPlacement = pin.SE3(np.eye(3), np.array([0.0, 0.0, length]))
123 self.
modelmodel.addFrame(
124 pin.Frame(
"tip", jointId, 0, jointPlacement, pin.FrameType.OP_FRAME)
128 pin.forwardKinematics(self.
modelmodel, self.
datadata, q)
129 for visual
in self.
visualsvisuals:
130 visual.place(self.
viewerviewer, self.
datadata.oMi[visual.jointParent])
131 self.
viewerviewer.viewer.gui.refresh()
135 return self.
modelmodel.nq
139 return self.
modelmodel.nv
143 return self.
nqnq + self.
nvnv
153 def reset(self, x0=None):
155 q0 = np.pi * (rand(self.
nqnq) * 2 - 1)
156 v0 = rand(self.
nvnv) * 2 - 1
157 x0 = np.vstack([q0, v0])
158 assert len(x0) == self.
nxnx
161 return self.
obsobs(self.
xx)
164 assert len(u) == self.
nunu
165 _, self.
rr = self.
dynamicsdynamics(self.
xx, u)
166 return self.
obsobs(self.
xx), self.
rr
171 [np.vstack([np.cos(qi), np.sin(qi)])
for qi
in x[: self.
nqnq]]
178 """Return the altitude of pendulum tip"""
179 pin.framesKinematics(self.
modelmodel, self.
datadata, q)
180 return self.
datadata.oMf[1].translation[2, 0]
184 Dynamic function: x,u -> xnext=f(x,y).
185 Put the result in x (the initial value is destroyed).
186 Also compute the cost of making this step.
187 Return x for convenience along with the cost.
191 return (th + np.pi) % (2 * np.pi) - np.pi
194 return np.sum(np.square(x))
197 q = modulePi(x[: self.
nqnq])
199 u = np.clip(np.reshape(np.array(u), [self.
nunu, 1]), -self.
umaxumax, self.
umaxumax)
201 DT = self.
DTDT / self.
NDTNDT
202 for i
in range(self.
NDTNDT):
203 pin.computeAllTerms(self.
modelmodel, self.
datadata, q, v)
205 b = self.
datadata.nle
207 a = inv(M) * (u - self.
KfKf * v - b)
211 cost += (sumsq(q) + 1e-1 * sumsq(v) + 1e-3 * sumsq(u)) * DT
217 x[: self.
nqnq] = modulePi(q)
218 x[self.
nqnq :] = np.clip(v, -self.
vmaxvmax, self.
vmaxvmax)
223 q = self.
xx[: self.
nqnq]
225 time.sleep(self.
DTDT / 10)
def __init__(self, nbJoint=1)
def dynamics(self, x, u, display=False)
def createPendulum(self, nbJoint, rootId=0, prefix="", jointPlacement=None)