|
| 1 | +""" |
| 2 | +Simple demo of usage of ContactTwoFramePositions in TSID |
| 3 | +Make the Talos gripper model works with closed kinematic chains |
| 4 | +(c) MIPT |
| 5 | +""" |
| 6 | +import os |
| 7 | +import time |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import pinocchio as pin |
| 11 | +import tsid |
| 12 | +from numpy import nan |
| 13 | +from numpy.linalg import norm as norm |
| 14 | + |
| 15 | +np.set_printoptions(precision=3, linewidth=200, suppress=True) |
| 16 | + |
| 17 | +LINE_WIDTH = 60 |
| 18 | +print("".center(LINE_WIDTH, "#")) |
| 19 | +print( |
| 20 | + " Demo of TSID with Closed Kinematic Chains via ContactTwoFramePositions".center( |
| 21 | + LINE_WIDTH, "#" |
| 22 | + ) |
| 23 | +) |
| 24 | +print("".center(LINE_WIDTH, "#"), "\n") |
| 25 | + |
| 26 | +w_ee = 1.0 # weight of end effector task |
| 27 | +w_posture = 1e-3 # weight of joint posture task |
| 28 | + |
| 29 | +kp_ee = 10.0 # proportional gain of end effector task |
| 30 | +kp_posture = 10.0 # proportional gain of joint posture task |
| 31 | + |
| 32 | +dt = 0.001 # controller time step |
| 33 | +PRINT_N = 500 # print every PRINT_N time steps |
| 34 | +DISPLAY_N = 25 # update robot configuration in viwewer every DISPLAY_N time steps |
| 35 | +N_SIMULATION = 6000 # number of time steps simulated |
| 36 | + |
| 37 | +# Loading Talos gripper model modified with extra links to mark the position of contact creation |
| 38 | +# Talos gripepr model (c) 2016, PAL Robotics, S.L. |
| 39 | +# Please use https://github.com/egordv/tsid_demo_closed_kinematic_chain repo for the model files |
| 40 | + |
| 41 | +filename = str(os.path.dirname(os.path.abspath(__file__))) |
| 42 | +path = filename + "../../tsid_demo_closed_kinematic_chain/models/talos_gripper" |
| 43 | +urdf = path + "../../tsid_demo_closed_kinematic_chain/urdf/talos_gripper_half.urdf" |
| 44 | +vector = pin.StdVec_StdString() |
| 45 | +vector.extend(item for item in path) |
| 46 | + |
| 47 | +robot = tsid.RobotWrapper(urdf, vector, False) # Load with fixed base |
| 48 | + |
| 49 | +# for viewer |
| 50 | +robot_display = pin.RobotWrapper.BuildFromURDF(urdf, [path]) |
| 51 | +robot_display.initViewer(loadModel=True) |
| 52 | + |
| 53 | +model = robot.model() |
| 54 | +q = np.zeros(robot.nq) |
| 55 | +v = np.zeros(robot.nv) |
| 56 | + |
| 57 | +viz = pin.visualize.MeshcatVisualizer( |
| 58 | + robot_display.model, |
| 59 | + robot_display.collision_model, |
| 60 | + robot_display.visual_model, |
| 61 | +) |
| 62 | +viz.initViewer(loadModel=True, open=True) |
| 63 | + |
| 64 | +t = 0.0 # time |
| 65 | +invdyn = tsid.InverseDynamicsFormulationAccForce("tsid", robot, False) |
| 66 | +invdyn.computeProblemData(t, q, v) |
| 67 | +data = invdyn.data() |
| 68 | + |
| 69 | + |
| 70 | +fingertip_name = "gripper_left_fingertip_3_link" |
| 71 | +H_fingertip_ref = robot.framePosition(invdyn.data(), model.getFrameId(fingertip_name)) |
| 72 | + |
| 73 | +fingertipPositionTask = tsid.TaskSE3Equality( |
| 74 | + "task-fingertip-position", robot, fingertip_name |
| 75 | +) |
| 76 | +fingertipPositionTask.useLocalFrame(False) |
| 77 | +fingertipPositionTask.setKp(kp_ee * np.ones(6)) |
| 78 | +fingertipPositionTask.setKd(2.0 * np.sqrt(kp_ee) * np.ones(6)) |
| 79 | +trajFingertipPosition = tsid.TrajectorySE3Constant( |
| 80 | + "traj-fingertip-position", H_fingertip_ref |
| 81 | +) |
| 82 | +sampleFingertipPosition = trajFingertipPosition.computeNext() |
| 83 | +fingertipPositionTask.setReference(sampleFingertipPosition) |
| 84 | +invdyn.addMotionTask(fingertipPositionTask, w_ee, 1, 0.0) |
| 85 | + |
| 86 | +postureTask = tsid.TaskJointPosture("task-posture", robot) |
| 87 | +postureTask.setKp(kp_posture * np.ones(robot.nv)) |
| 88 | +postureTask.setKd(2.0 * np.sqrt(kp_posture) * np.ones(robot.nv)) |
| 89 | +invdyn.addMotionTask(postureTask, w_posture, 1, 0.0) |
| 90 | + |
| 91 | + |
| 92 | +# Creating a closed kinematic chain in TSID formulation by creating a contact between two frames, for which there are special links in URDF |
| 93 | +ContactTwoFramePositionsFingertipBottomAxis = tsid.ContactTwoFramePositions( |
| 94 | + "contact-two-frame-positions-fingertip-bottom-axis", |
| 95 | + robot, |
| 96 | + "gripper_left_motor_single_link_ckc_axis", |
| 97 | + "gripper_left_fingertip_3_link_ckc_axis", |
| 98 | + -1000, |
| 99 | + 1000, |
| 100 | +) |
| 101 | +twoFramesContact_Kp = 300 |
| 102 | +ContactTwoFramePositionsFingertipBottomAxis.setKp(twoFramesContact_Kp * np.ones(3)) |
| 103 | +ContactTwoFramePositionsFingertipBottomAxis.setKd( |
| 104 | + 2.0 * np.sqrt(twoFramesContact_Kp) * np.ones(3) |
| 105 | +) |
| 106 | + |
| 107 | +twoFramesContact_w_forceRef = 1e-5 |
| 108 | +invdyn.addRigidContact( |
| 109 | + ContactTwoFramePositionsFingertipBottomAxis, twoFramesContact_w_forceRef, 1.0, 1 |
| 110 | +) |
| 111 | + |
| 112 | +# Setting actuation to zero for passive joints in kinematic chain via TaskActuationBounds |
| 113 | +tau_max = model.effortLimit[-robot.na :] |
| 114 | +tau_max[ |
| 115 | + 0 |
| 116 | +] = 0.0 # setting gripper_left_inner_single_joint to passive contstrainig it's actuation bounds to zero |
| 117 | +tau_max[ |
| 118 | + 1 |
| 119 | +] = 0.0 # setting gripper_left_fingertip_3_joint to passive contstrainig it's actuation bounds to zero |
| 120 | +tau_min = -tau_max |
| 121 | +actuationBoundsTask = tsid.TaskActuationBounds("task-actuation-bounds", robot) |
| 122 | +actuationBoundsTask.setBounds(tau_min, tau_max) |
| 123 | +invdyn.addActuationTask(actuationBoundsTask, 1.0, 0, 0.0) |
| 124 | + |
| 125 | +q_ref = q |
| 126 | +trajPosture = tsid.TrajectoryEuclidianConstant("traj_joint", q_ref) |
| 127 | + |
| 128 | +print( |
| 129 | + "Create QP solver with ", |
| 130 | + invdyn.nVar, |
| 131 | + " variables, ", |
| 132 | + invdyn.nEq, |
| 133 | + " equality and ", |
| 134 | + invdyn.nIn, |
| 135 | + " inequality constraints", |
| 136 | +) |
| 137 | +solver = tsid.SolverHQuadProgFast("qp solver") |
| 138 | +solver.resize(invdyn.nVar, invdyn.nEq, invdyn.nIn) |
| 139 | + |
| 140 | +offset = sampleFingertipPosition.pos() |
| 141 | +offset[:3] += np.array([0, -0.04, 0]) |
| 142 | +amp = np.array([0.0, 0.04, 0.0]) |
| 143 | +two_pi_f = 2 * np.pi * np.array([0.0, 0.5, 0.0]) |
| 144 | +two_pi_f_amp = np.multiply(two_pi_f, amp) |
| 145 | +two_pi_f_squared_amp = np.multiply(two_pi_f, two_pi_f_amp) |
| 146 | + |
| 147 | +pEE = offset.copy() |
| 148 | +vEE = np.zeros(6) |
| 149 | +aEE = np.zeros(6) |
| 150 | + |
| 151 | +i = 0 |
| 152 | +while True: |
| 153 | + time_start = time.time() |
| 154 | + |
| 155 | + # Setting gripper finger task target to sine motion |
| 156 | + pEE[:3] = offset[:3] + amp * np.sin(two_pi_f * t) |
| 157 | + vEE[:3] = two_pi_f_amp * np.cos(two_pi_f * t) |
| 158 | + aEE[:3] = -two_pi_f_squared_amp * np.sin(two_pi_f * t) |
| 159 | + sampleFingertipPosition.value(pEE) |
| 160 | + sampleFingertipPosition.derivative(vEE) |
| 161 | + sampleFingertipPosition.second_derivative(aEE) |
| 162 | + |
| 163 | + fingertipPositionTask.setReference(sampleFingertipPosition) |
| 164 | + |
| 165 | + samplePosture = trajPosture.computeNext() |
| 166 | + postureTask.setReference(samplePosture) |
| 167 | + |
| 168 | + # Computing HQP |
| 169 | + HQPData = invdyn.computeProblemData(t, q, v) |
| 170 | + if i == 0: |
| 171 | + HQPData.print_all() |
| 172 | + |
| 173 | + sol = solver.solve(HQPData) |
| 174 | + if sol.status != 0: |
| 175 | + print("[%d] QP problem could not be solved! Error code:" % (i), sol.status) |
| 176 | + break |
| 177 | + |
| 178 | + tau = invdyn.getActuatorForces(sol) |
| 179 | + dv = invdyn.getAccelerations(sol) |
| 180 | + |
| 181 | + if i % PRINT_N == 0: |
| 182 | + print("Time %.3f" % (t)) |
| 183 | + |
| 184 | + v_mean = v + 0.5 * dt * dv |
| 185 | + v += dt * dv |
| 186 | + q = pin.integrate(robot.model(), q, dt * v_mean) |
| 187 | + t += dt |
| 188 | + |
| 189 | + if i % DISPLAY_N == 0: |
| 190 | + viz.display(q) |
| 191 | + |
| 192 | + time_spent = time.time() - time_start |
| 193 | + if time_spent < dt: |
| 194 | + time.sleep(dt - time_spent) |
| 195 | + |
| 196 | + i = i + 1 |
0 commit comments