Skip to content

Commit aaf762f

Browse files
committed
embedded solver example
1 parent 5a96c1e commit aaf762f

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

examples/python/embedded_solver.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Example of embedding the CartesIO solver in a Python script which is
5+
completely independent from ROS and standalone.
6+
The required input is just a URDF file of the robot.
7+
"""
8+
9+
import math
10+
import os
11+
from time import sleep
12+
13+
from cartesian_interface.pyci_all import *
14+
from xbot2_interface.pyaffine3 import Affine3
15+
from xbot2_interface import pyxbot2_interface as xb
16+
17+
# load the URDF and SRDF file
18+
urdf_path = os.path.dirname(os.path.abspath(__file__)) + "/../urdf/coman.urdf"
19+
urdf = open(urdf_path, "r").read()
20+
srdf_path = os.path.dirname(os.path.abspath(__file__)) + "/../srdf/coman.srdf"
21+
srdf = open(srdf_path, "r").read()
22+
23+
# params
24+
DT = 0.01
25+
LOG_PATH = '/tmp'
26+
TRJ_RADIUS = 0.1 # m
27+
TRJ_FREQ_HZ = 0.1 # Hz
28+
29+
# create the model interface
30+
model = xb.ModelInterface2(urdf, srdf, 'pin')
31+
32+
# load the ik problem
33+
ik_problem_path = os.path.dirname(os.path.abspath(__file__)) + "/../configs/coman_stack.yaml"
34+
ik_problem = open(ik_problem_path, "r").read()
35+
36+
# reset model position to homing
37+
model.q = model.getRobotState('home')
38+
model.update()
39+
40+
# create the solver
41+
solver = pyci.CartesianInterface.MakeInstance(
42+
solver='OpenSot',
43+
problem=ik_problem,
44+
model=model,
45+
dt=DT,
46+
log_path=LOG_PATH
47+
)
48+
49+
# get the left hand task
50+
lh_task = solver.getTask('left_hand')
51+
lh_ee_name = lh_task.getDistalLink()
52+
lh_base_name = lh_task.getBaseLink()
53+
print(f'+++ Left hand ee link: {lh_ee_name}, base link: {lh_base_name}')
54+
55+
# initial ee pose
56+
T_0 = model.getPose(lh_ee_name, lh_base_name)
57+
print(f'+++ Initial left hand ee pose:\n{T_0}')
58+
59+
# ik loop
60+
time = 0
61+
while True:
62+
63+
# define desired pose
64+
Tdes = T_0.copy()
65+
Tdes.translation[1] += TRJ_RADIUS * math.sin(2 * math.pi * TRJ_FREQ_HZ * time)
66+
Tdes.translation[2] += TRJ_RADIUS * (1 - math.cos(2 * math.pi * TRJ_FREQ_HZ * time))/2.
67+
68+
# set desired pose
69+
lh_task.setPoseReference(Tdes)
70+
71+
# solve
72+
if not solver.update(time, DT):
73+
print("!!! Solver failed!")
74+
break
75+
76+
# integrate solution
77+
model.q = model.sum(model.q, model.v * DT)
78+
model.update()
79+
80+
# print tracking error
81+
T = model.getPose(lh_ee_name, lh_base_name)
82+
error = T * Tdes.inverse()
83+
print(f'Time: {time:.2f} s, Error matrix:\n{error}')
84+
85+
# increment time
86+
time += DT
87+
88+
# sync
89+
sleep(DT)
90+
91+
92+

tests/configs/centauro_test_stack.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ Postural:
4545
type: "Postural"
4646
lambda: 0.01
4747
weight:
48-
VIRTUALJOINT_1: 0.0
49-
VIRTUALJOINT_2: 0.0
50-
VIRTUALJOINT_3: 0.0
51-
VIRTUALJOINT_4: 0.0
52-
VIRTUALJOINT_5: 0.0
53-
VIRTUALJOINT_6: 0.0
48+
reference@v0: 0.0
49+
reference@v1: 0.0
50+
reference@v2: 0.0
51+
reference@v3: 0.0
52+
reference@v4: 0.0
53+
reference@v5: 0.0
5454

5555
ComTask:
5656
type: "Com"

0 commit comments

Comments
 (0)