5 import gepetto.corbaserver
6 import matplotlib.pyplot
as plt
8 import pinocchio
as pin
9 import plot_utils
as plut
11 import ur5_conf
as conf
13 from numpy.linalg
import norm
as norm
15 print(
"".
center(conf.LINE_WIDTH,
"#"))
16 print(
" Joint Space Inverse Dynamics - Manipulator ".
center(conf.LINE_WIDTH,
"#"))
17 print(
"".
center(conf.LINE_WIDTH,
"#"),
"\n")
25 robot = tsid.RobotWrapper(conf.urdf, [conf.path],
False)
30 v0 = np.zeros(robot.nv)
31 formulation.computeProblemData(0.0, q0, v0)
33 postureTask = tsid.TaskJointPosture(
"task-posture", robot)
34 postureTask.setKp(conf.kp_posture * np.ones(robot.nv))
35 postureTask.setKd(2.0 * np.sqrt(conf.kp_posture) * np.ones(robot.nv))
36 formulation.addMotionTask(postureTask, conf.w_posture, 1, 0.0)
38 trajPosture = tsid.TrajectoryEuclidianConstant(
"traj_joint", q0)
39 postureTask.setReference(trajPosture.computeNext())
41 v_max = conf.v_max_scaling * model.velocityLimit
47 solver = tsid.SolverHQuadProgFast(
"qp solver")
48 solver.resize(formulation.nVar, formulation.nEq, formulation.nIn)
51 robot_display = pin.RobotWrapper.BuildFromURDF(
57 l = subprocess.getstatusoutput(
"ps aux |grep 'gepetto-gui'|grep -v 'grep'|wc -l")
59 os.system(
"gepetto-gui &")
61 gepetto.corbaserver.Client()
62 robot_display.initViewer(loadModel=
True)
63 robot_display.displayCollisions(
False)
64 robot_display.displayVisuals(
True)
65 robot_display.display(q0)
69 tau = np.empty((robot.na, N)) * nan
70 q = np.empty((robot.nq, N + 1)) * nan
71 v = np.empty((robot.nv, N + 1)) * nan
72 dv = np.empty((robot.nv, N + 1)) * nan
73 q_ref = np.empty((robot.nq, N)) * nan
74 v_ref = np.empty((robot.nv, N)) * nan
75 dv_ref = np.empty((robot.nv, N)) * nan
76 dv_des = np.empty((robot.nv, N)) * nan
77 samplePosture = trajPosture.computeNext()
79 amp = np.array([0.2, 0.3, 0.4, 0.0, 0.0, 0.0])
80 phi = np.array([0.0, 0.5 * np.pi, 0.0, 0.0, 0.0, 0.0])
81 two_pi_f = 2 * np.pi * np.array([1.0, 0.5, 0.3, 0.0, 0.0, 0.0])
82 two_pi_f_amp = np.multiply(two_pi_f, amp)
83 two_pi_f_squared_amp = np.multiply(two_pi_f, two_pi_f_amp)
87 q[:, 0], v[:, 0] = q0, v0
90 time_start = time.time()
93 q_ref[:, i] = q0 + amp * np.sin(two_pi_f * t + phi)
94 v_ref[:, i] = two_pi_f_amp * np.cos(two_pi_f * t + phi)
95 dv_ref[:, i] = -two_pi_f_squared_amp * np.sin(two_pi_f * t + phi)
96 samplePosture.pos(q_ref[:, i])
97 samplePosture.vel(v_ref[:, i])
98 samplePosture.acc(dv_ref[:, i])
99 postureTask.setReference(samplePosture)
101 HQPData = formulation.computeProblemData(t, q[:, i], v[:, i])
102 sol = solver.solve(HQPData)
104 print(
"Time %.3f QP problem could not be solved! Error code:" % t, sol.status)
107 tau[:, i] = formulation.getActuatorForces(sol)
108 dv[:, i] = formulation.getAccelerations(sol)
109 dv_des[:, i] = postureTask.getDesiredAcceleration
111 if i % conf.PRINT_N == 0:
112 print(
"Time %.3f" % (t))
114 "\ttracking err %s: %.3f" 115 % (postureTask.name.ljust(20,
"."), norm(postureTask.position_error, 2))
119 v_mean = v[:, i] + 0.5 * dt * dv[:, i]
120 v[:, i + 1] = v[:, i] + dt * dv[:, i]
121 q[:, i + 1] = pin.integrate(model, q[:, i], dt * v_mean)
124 if i % conf.DISPLAY_N == 0:
125 robot_display.display(q[:, i])
127 time_spent = time.time() - time_start
128 if time_spent < conf.dt:
129 time.sleep(conf.dt - time_spent)
132 time = np.arange(0.0, N * conf.dt, conf.dt)
135 (f, ax) = plut.create_empty_figure(int(robot.nv / 2), 2)
136 ax = ax.reshape(robot.nv)
137 for i
in range(robot.nv):
138 ax[i].plot(time, q[i, :-1], label=
"q " + str(i))
139 ax[i].plot(time, q_ref[i, :],
"--", label=
"q ref " + str(i))
140 ax[i].set_xlabel(
"Time [s]")
141 ax[i].set_ylabel(
"Q [rad]")
143 leg.get_frame().set_alpha(0.5)
146 (f, ax) = plut.create_empty_figure(int(robot.nv / 2), 2)
147 ax = ax.reshape(robot.nv)
148 for i
in range(robot.nv):
149 ax[i].plot(time, v[i, :-1], label=
"dq " + str(i))
150 ax[i].plot(time, v_ref[i, :],
"--", label=
"dq ref " + str(i))
151 ax[i].plot([time[0], time[-1]], 2 * [v_min[i]],
":")
152 ax[i].plot([time[0], time[-1]], 2 * [v_max[i]],
":")
153 ax[i].set_xlabel(
"Time [s]")
154 ax[i].set_ylabel(
"dq [rad/s]")
156 leg.get_frame().set_alpha(0.5)
159 (f, ax) = plut.create_empty_figure(int(robot.nv / 2), 2)
160 ax = ax.reshape(robot.nv)
161 for i
in range(robot.nv):
162 ax[i].plot(time, dv[i, :-1], label=
"ddq " + str(i))
163 ax[i].plot(time, dv_ref[i, :],
"--", label=
"ddq ref " + str(i))
164 ax[i].plot(time, dv_des[i, :],
":", label=
"ddq des " + str(i))
165 ax[i].set_xlabel(
"Time [s]")
166 ax[i].set_ylabel(
"ddq [rad/s^2]")
168 leg.get_frame().set_alpha(0.5)
171 (f, ax) = plut.create_empty_figure(int(robot.nv / 2), 2)
172 ax = ax.reshape(robot.nv)
173 for i
in range(robot.nv):
174 ax[i].plot(time, tau[i, :], label=
"Torque " + str(i))
175 ax[i].set_xlabel(
"Time [s]")
176 ax[i].set_ylabel(
"Torque [Nm]")
178 leg.get_frame().set_alpha(0.5)