-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02-casadi-example-1.py
115 lines (93 loc) · 2.91 KB
/
02-casadi-example-1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import time
import unittest
import threading
import example_robot_data
import numpy as np
import casadi
import pinocchio as pin
import pinocchio.casadi as cpin
from meshcat_viewer_wrapper import MeshcatVisualizer, colors
# Robot Arm End Effector Example
robot = example_robot_data.load("ur10")
model = robot.model
data = robot.data
viz = MeshcatVisualizer(robot, url=None)
viz.display(robot.q0)
robot.q0 = np.array([0, -np.pi / 2, 0, 0, 0, 0])
q = robot.q0
tool_id = model.getFrameId("tool0")
in_world_M_target = pin.SE3(
pin.utils.rotate("x", np.pi / 4),
np.array([-0.5, 0.1, 0.2]),
)
# --- Add box to represent target
# Add a vizualization for the target
boxID = "world/box"
viz.addBox(boxID, [0.05, 0.1, 0.2], [1.0, 0.2, 0.2, 0.5])
# Add a vizualisation for the tip of the arm.
tipID = "world/blue"
viz.addBox(tipID, [0.08] * 3, [0.2, 0.2, 1.0, 0.5])
lock = threading.Lock()
def displayScene(q):
"""
Given the robot configuration, display:
- the robot
- a box representing tool_id
- a box representing in_world_M_target
"""
pin.framesForwardKinematics(model, data, q)
in_world_M_tool = data.oMf[tool_id]
viz.applyConfiguration(boxID, in_world_M_target)
viz.applyConfiguration(tipID, in_world_M_tool)
viz.display(q)
time.sleep(1e-1)
# Function to update the visualizer in a separate thread
def visualizer_thread():
while True:
with lock:
viz.display(q) # Update the visualizer with the latest configuration
time.sleep(0.05) # Adjust for desired update rate
# Start the visualizer thread
thread = threading.Thread(target=visualizer_thread, daemon=True)
thread.start()
curr_time = 0.0
while True:
# set target pos
delta_x = 0.1 * np.sin(curr_time)
delta_y = 0.1 * np.sin(curr_time)
in_world_M_target = pin.SE3(
pin.utils.rotate("x", np.pi / 4),
np.array([-0.5, 0.1 + delta_y, 0.2]),
)
curr_time += 0.1
# --- Casadi helpers
cmodel = cpin.Model(model)
cdata = cmodel.createData()
cq = casadi.SX.sym("q", model.nq, 1)
cpin.framesForwardKinematics(cmodel, cdata, cq)
# setup optimization
error_tool = casadi.Function(
"etool",
[cq],
[cpin.log6(cdata.oMf[tool_id].inverse() * cpin.SE3(in_world_M_target)).vector],
)
opti = casadi.Opti()
var_q = opti.variable(model.nq)
opti.set_initial(var_q, q)
totalcost = casadi.sumsqr(error_tool(var_q))
opti.minimize(totalcost)
opti.solver("ipopt") # select the backend solver
opti.callback(lambda i: displayScene(opti.debug.value(var_q)))
# calculate a solution
try:
sol = opti.solve_limited()
sol_q = opti.value(var_q)
except:
print("ERROR in convergence, plotting debug info.")
sol_q = opti.debug.value(var_q)
pin.framesForwardKinematics(model, data, sol_q)
with lock:
q = sol_q
# robot.q0 = q
displayScene(sol_q)
time.sleep(0.05)