ex_0_ur5_joint_space_control.py
Go to the documentation of this file.
1 import os
2 import subprocess
3 import time
4 
5 import gepetto.corbaserver
6 import matplotlib.pyplot as plt
7 import numpy as np
8 import pinocchio as pin
9 import plot_utils as plut
10 import tsid
11 import ur5_conf as conf
12 from numpy import nan
13 from numpy.linalg import norm as norm
14 
15 print("".center(conf.LINE_WIDTH, "#"))
16 print(" Joint Space Inverse Dynamics - Manipulator ".center(conf.LINE_WIDTH, "#"))
17 print("".center(conf.LINE_WIDTH, "#"), "\n")
18 
19 PLOT_JOINT_POS = 1
20 PLOT_JOINT_VEL = 1
21 PLOT_JOINT_ACC = 1
22 PLOT_TORQUES = 0
23 USE_VIEWER = 1
24 
25 robot = tsid.RobotWrapper(conf.urdf, [conf.path], False)
26 model = robot.model()
27 
28 formulation = tsid.InverseDynamicsFormulationAccForce("tsid", robot, False)
29 q0 = conf.q0
30 v0 = np.zeros(robot.nv)
31 formulation.computeProblemData(0.0, q0, v0)
32 
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)
37 
38 trajPosture = tsid.TrajectoryEuclidianConstant("traj_joint", q0)
39 postureTask.setReference(trajPosture.computeNext())
40 
41 v_max = conf.v_max_scaling * model.velocityLimit
42 v_min = -v_max
43 # jointBoundsTask = tsid.TaskJointBounds("task-joint-bounds", robot, conf.dt)
44 # jointBoundsTask.setVelocityBounds(v_min, v_max)
45 # formulation.addMotionTask(jointBoundsTask, conf.w_joint_bounds, 0, 0.0)
46 
47 solver = tsid.SolverHQuadProgFast("qp solver")
48 solver.resize(formulation.nVar, formulation.nEq, formulation.nIn)
49 
50 if USE_VIEWER:
51  robot_display = pin.RobotWrapper.BuildFromURDF(
52  conf.urdf,
53  [
54  conf.path,
55  ],
56  )
57  l = subprocess.getstatusoutput("ps aux |grep 'gepetto-gui'|grep -v 'grep'|wc -l")
58  if int(l[1]) == 0:
59  os.system("gepetto-gui &")
60  time.sleep(1)
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)
66 # robot_display.viewer.gui.setCameraTransform(0, conf.CAMERA_TRANSFORM)
67 
68 N = conf.N_SIMULATION
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()
78 
79 amp = np.array([0.2, 0.3, 0.4, 0.0, 0.0, 0.0]) # amplitude
80 phi = np.array([0.0, 0.5 * np.pi, 0.0, 0.0, 0.0, 0.0]) # phase
81 two_pi_f = 2 * np.pi * np.array([1.0, 0.5, 0.3, 0.0, 0.0, 0.0]) # frequency (time 2 PI)
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)
84 
85 t = 0.0
86 dt = conf.dt
87 q[:, 0], v[:, 0] = q0, v0
88 
89 for i in range(0, N):
90  time_start = time.time()
91 
92  # set reference trajectory
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)
100 
101  HQPData = formulation.computeProblemData(t, q[:, i], v[:, i])
102  sol = solver.solve(HQPData)
103  if sol.status != 0:
104  print("Time %.3f QP problem could not be solved! Error code:" % t, sol.status)
105  break
106 
107  tau[:, i] = formulation.getActuatorForces(sol)
108  dv[:, i] = formulation.getAccelerations(sol)
109  dv_des[:, i] = postureTask.getDesiredAcceleration
110 
111  if i % conf.PRINT_N == 0:
112  print("Time %.3f" % (t))
113  print(
114  "\ttracking err %s: %.3f"
115  % (postureTask.name.ljust(20, "."), norm(postureTask.position_error, 2))
116  )
117 
118  # numerical integration
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)
122  t += conf.dt
123 
124  if i % conf.DISPLAY_N == 0:
125  robot_display.display(q[:, i])
126 
127  time_spent = time.time() - time_start
128  if time_spent < conf.dt:
129  time.sleep(conf.dt - time_spent)
130 
131 # PLOT STUFF
132 time = np.arange(0.0, N * conf.dt, conf.dt)
133 
134 if PLOT_JOINT_POS:
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]")
142  leg = ax[i].legend()
143  leg.get_frame().set_alpha(0.5)
144 
145 if PLOT_JOINT_VEL:
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]")
155  leg = ax[i].legend()
156  leg.get_frame().set_alpha(0.5)
157 
158 if PLOT_JOINT_ACC:
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]")
167  leg = ax[i].legend()
168  leg.get_frame().set_alpha(0.5)
169 
170 if PLOT_TORQUES:
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]")
177  leg = ax[i].legend()
178  leg.get_frame().set_alpha(0.5)
179 
180 plt.show()
Vec3f center


tsid
Author(s): Andrea Del Prete, Justin Carpentier
autogenerated on Sun Jul 2 2023 02:21:51