6 import gepetto.corbaserver
8 import pinocchio
as pin
11 from numpy.linalg
import norm
as norm
13 sys.path += [os.getcwd() +
"/../exercizes"]
14 import matplotlib.pyplot
as plt
15 import plot_utils
as plut
17 np.set_printoptions(precision=3, linewidth=200, suppress=
True)
20 print(
"".
center(LINE_WIDTH,
"#"))
21 print(
" Test TSID with Quadruped Robot ".
center(LINE_WIDTH,
"#"))
22 print(
"".
center(LINE_WIDTH,
"#"),
"\n")
27 contact_frames = [
"BL_contact",
"BR_contact",
"FL_contact",
"FR_contact"]
28 contactNormal = np.array(
45 filename = str(os.path.dirname(os.path.abspath(__file__)))
46 path = filename +
"/../models" 47 urdf = path +
"/quadruped/urdf/quadruped.urdf" 48 vector = pin.StdVec_StdString()
49 vector.extend(item
for item
in path)
50 robot = tsid.RobotWrapper(urdf, vector, pin.JointModelFreeFlyer(),
False)
53 robot_display = pin.RobotWrapper.BuildFromURDF(
58 pin.JointModelFreeFlyer(),
60 l = subprocess.getstatusoutput(
"ps aux |grep 'gepetto-gui'|grep -v 'grep'|wc -l")
62 os.system(
"gepetto-gui &")
64 cl = gepetto.corbaserver.Client()
66 robot_display.initViewer(loadModel=
True)
73 q = np.zeros(robot.nq)
79 v = np.zeros(robot.nv)
81 robot_display.displayCollisions(
False)
82 robot_display.displayVisuals(
True)
83 robot_display.display(q)
85 assert [robot.model().existFrame(name)
for name
in contact_frames]
89 invdyn.computeProblemData(t, q, v)
93 id_contact = robot_display.model.getFrameId(contact_frames[0])
94 q[2] -= robot.framePosition(data, id_contact).translation[2]
95 robot.computeAllTerms(data, q, v)
98 for i, name
in enumerate(contact_frames):
99 contacts[i] = tsid.ContactPoint(name, robot, name, contactNormal, mu, fMin, fMax)
100 contacts[i].setKp(kp_contact * np.ones(3))
101 contacts[i].setKd(2.0 * np.sqrt(kp_contact) * np.ones(3))
102 H_rf_ref = robot.framePosition(data, robot.model().getFrameId(name))
103 contacts[i].setReference(H_rf_ref)
104 contacts[i].useLocalFrame(
False)
105 invdyn.addRigidContact(contacts[i], w_forceRef, 1.0, 1)
107 comTask = tsid.TaskComEquality(
"task-com", robot)
108 comTask.setKp(kp_com * np.ones(3))
109 comTask.setKd(2.0 * np.sqrt(kp_com) * np.ones(3))
110 invdyn.addMotionTask(comTask, w_com, 1, 0.0)
112 postureTask = tsid.TaskJointPosture(
"task-posture", robot)
113 postureTask.setKp(kp_posture * np.ones(robot.nv - 6))
114 postureTask.setKd(2.0 * np.sqrt(kp_posture) * np.ones(robot.nv - 6))
115 invdyn.addMotionTask(postureTask, w_posture, 1, 0.0)
117 com_ref = robot.com(data)
118 trajCom = tsid.TrajectoryEuclidianConstant(
"traj_com", com_ref)
119 sampleCom = trajCom.computeNext()
122 trajPosture = tsid.TrajectoryEuclidianConstant(
"traj_joint", q_ref)
125 "Create QP solver with ",
131 " inequality constraints",
133 solver = tsid.SolverHQuadProgFast(
"qp solver")
134 solver.resize(invdyn.nVar, invdyn.nEq, invdyn.nIn)
136 com_pos = np.empty((3, N_SIMULATION)) * nan
137 com_vel = np.empty((3, N_SIMULATION)) * nan
138 com_acc = np.empty((3, N_SIMULATION)) * nan
140 com_pos_ref = np.empty((3, N_SIMULATION)) * nan
141 com_vel_ref = np.empty((3, N_SIMULATION)) * nan
142 com_acc_ref = np.empty((3, N_SIMULATION)) * nan
144 np.empty((3, N_SIMULATION)) * nan
147 offset = robot.com(data) + np.array([0.0, 0.0, 0.0])
148 amp = np.array([0.0, 0.03, 0.0])
149 two_pi_f = 2 * np.pi * np.array([0.0, 2.0, 0.7])
150 two_pi_f_amp = np.multiply(two_pi_f, amp)
151 two_pi_f_squared_amp = np.multiply(two_pi_f, two_pi_f_amp)
153 for i
in range(0, N_SIMULATION):
154 time_start = time.time()
156 sampleCom.pos(offset + np.multiply(amp, np.sin(two_pi_f * t)))
157 sampleCom.vel(np.multiply(two_pi_f_amp, np.cos(two_pi_f * t)))
158 sampleCom.acc(np.multiply(two_pi_f_squared_amp, -np.sin(two_pi_f * t)))
160 comTask.setReference(sampleCom)
161 samplePosture = trajPosture.computeNext()
162 postureTask.setReference(samplePosture)
164 HQPData = invdyn.computeProblemData(t, q, v)
168 sol = solver.solve(HQPData)
170 print(
"[%d] QP problem could not be solved! Error code:" % (i), sol.status)
173 tau = invdyn.getActuatorForces(sol)
174 dv = invdyn.getAccelerations(sol)
176 com_pos[:, i] = robot.com(invdyn.data())
177 com_vel[:, i] = robot.com_vel(invdyn.data())
178 com_acc[:, i] = comTask.getAcceleration(dv)
179 com_pos_ref[:, i] = sampleCom.pos()
180 com_vel_ref[:, i] = sampleCom.vel()
181 com_acc_ref[:, i] = sampleCom.acc()
182 com_acc_des[:, i] = comTask.getDesiredAcceleration
185 print(
"Time %.3f" % (t))
186 print(
"\tNormal forces: ", end=
" ")
187 for contact
in contacts:
188 if invdyn.checkContact(contact.name, sol):
189 f = invdyn.getContactForce(contact.name, sol)
190 print(
"%4.1f" % (contact.getNormalForce(f)), end=
" ")
193 "\n\ttracking err %s: %.3f" 194 % (comTask.name.ljust(20,
"."), norm(comTask.position_error, 2))
196 print(
"\t||v||: %.3f\t ||dv||: %.3f" % (norm(v, 2), norm(dv)))
198 v_mean = v + 0.5 * dt * dv
200 q = pin.integrate(robot.model(), q, dt * v_mean)
203 if i % DISPLAY_N == 0:
204 robot_display.display(q)
206 time_spent = time.time() - time_start
208 time.sleep(dt - time_spent)
211 time = np.arange(0.0, N_SIMULATION * dt, dt)
213 (f, ax) = plut.create_empty_figure(3, 1)
215 ax[i].plot(time, com_pos[i, :], label=
"CoM " + str(i))
216 ax[i].plot(time, com_pos_ref[i, :],
"r:", label=
"CoM Ref " + str(i))
217 ax[i].set_xlabel(
"Time [s]")
218 ax[i].set_ylabel(
"CoM [m]")
220 leg.get_frame().set_alpha(0.5)
222 (f, ax) = plut.create_empty_figure(3, 1)
224 ax[i].plot(time, com_vel[i, :], label=
"CoM Vel " + str(i))
225 ax[i].plot(time, com_vel_ref[i, :],
"r:", label=
"CoM Vel Ref " + str(i))
226 ax[i].set_xlabel(
"Time [s]")
227 ax[i].set_ylabel(
"CoM Vel [m/s]")
229 leg.get_frame().set_alpha(0.5)
231 (f, ax) = plut.create_empty_figure(3, 1)
233 ax[i].plot(time, com_acc[i, :], label=
"CoM Acc " + str(i))
234 ax[i].plot(time, com_acc_ref[i, :],
"r:", label=
"CoM Acc Ref " + str(i))
235 ax[i].plot(time, com_acc_des[i, :],
"g--", label=
"CoM Acc Des " + str(i))
236 ax[i].set_xlabel(
"Time [s]")
237 ax[i].set_ylabel(
"CoM Acc [m/s^2]")
239 leg.get_frame().set_alpha(0.5)