ex_3_biped_balance_with_gui.py
Go to the documentation of this file.
1 """Created on Wed Apr 17 22:31:22 2019
2 
3 @author: student
4 """
5 
6 import threading
7 import time
8 import tkinter as tk
9 from tkinter import HORIZONTAL, Button, Entry, Frame, Label, Scale, Tk, mainloop
10 
11 import numpy as np
12 import pinocchio as pin
13 import talos_conf as conf
14 
15 # import romeo_conf as conf
16 import vizutils
17 from tsid_biped import TsidBiped
18 
19 AXES = ["X", "Y", "Z"]
20 
21 
22 class Scale3d:
23  def __init__(self, master, name, from_, to, tickinterval, length, orient, command):
24  self.s = 3 * [None]
25  for i in range(3):
26  self.s[i] = Scale(
27  master,
28  label=f"{name} {AXES[i]}",
29  from_=from_[i],
30  to=to[i],
31  tickinterval=tickinterval[i],
32  orient=orient[i],
33  length=length[i],
34  command=command,
35  )
36  self.s[i].pack()
37  separator = Frame(height=2, bd=1, relief=tk.SUNKEN)
38  separator.pack(fill=tk.X, padx=5, pady=5)
39 
40  def get(self):
41  return self.s[0].get(), self.s[1].get(), self.s[2].get()
42 
43 
44 class Entry3d:
45  def __init__(self, master, name):
46  self.s = 3 * [None]
47  for i in range(3):
48  Label(master, text=f"{name} {AXES[i]}").pack() # side=tk.TOP)
49  self.s[i] = Entry(master, width=5)
50  self.s[i].pack() # side=tk.BOTTOM)
51  separator = Frame(height=1, bd=1, relief=tk.SUNKEN)
52  separator.pack(fill=tk.X, padx=2, pady=2) # , side=tk.BOTTOM)
53 
54  def get(self):
55  try:
56  return [float(self.s[i].get()) for i in range(3)]
57  except ValueError:
58  print(
59  "could not convert string to float", [self.s[i].get() for i in range(3)]
60  )
61  return 3 * [0.0]
62 
63 
64 scale_com, scale_RF, scale_LF = None, None, None
65 button_contact_RF, button_contact_LF = None, None
66 push_robot_active, push_robot_com_vel, com_vel_entry = False, 3 * [0.0], None
67 
68 
70  x, y, z = scale_com.get()
71  tsid.trajCom.setReference(com_0 + np.array([1e-2 * x, 1e-2 * y, 1e-2 * z]).T)
72 
73 
75  x, y, z = scale_RF.get()
76  H_rf_ref = H_rf_0.copy()
77  H_rf_ref.translation += +np.array([1e-2 * x, 1e-2 * y, 1e-2 * z]).T
78  tsid.trajRF.setReference(H_rf_ref)
79 
80 
82  x, y, z = scale_LF.get()
83  H_lf_ref = H_lf_0.copy()
84  H_lf_ref.translation += +np.array([1e-2 * x, 1e-2 * y, 1e-2 * z]).T
85  tsid.trajLF.setReference(H_lf_ref)
86 
87 
89  if tsid.contact_RF_active:
90  tsid.remove_contact_RF()
91  button_contact_RF.config(text="Make contact right foot")
92  else:
93  tsid.add_contact_RF()
94  button_contact_RF.config(text="Break contact right foot")
95 
96 
98  if tsid.contact_LF_active:
99  tsid.remove_contact_LF()
100  button_contact_LF.config(text="Make contact left foot")
101  else:
102  tsid.add_contact_LF()
103  button_contact_LF.config(text="Break contact left foot")
104 
105 
107  tsid.gui.setWireFrameMode("world", "WIREFRAME")
108 
109 
111  global push_robot_com_vel, push_robot_active
112  push_robot_com_vel = com_vel_entry.get()
113  push_robot_active = True
114 
115 
117  """Thread worker function"""
118  global \
119  scale_com, \
120  scale_RF, \
121  scale_LF, \
122  button_contact_RF, \
123  button_contact_LF, \
124  com_vel_entry
125  master = Tk(className="TSID GUI")
126  scale_com = Scale3d(
127  master,
128  "CoM",
129  [-10, -15, -40],
130  [10, 15, 40],
131  [5, 5, 10],
132  [200, 250, 300],
133  3 * [HORIZONTAL],
134  update_com_ref_scale,
135  )
136  scale_RF = Scale3d(
137  master,
138  "Right foot",
139  3 * [-30],
140  3 * [30],
141  3 * [10],
142  3 * [300],
143  3 * [HORIZONTAL],
144  update_RF_ref_scale,
145  )
146  scale_LF = Scale3d(
147  master,
148  "Left foot",
149  3 * [-30],
150  3 * [30],
151  3 * [10],
152  3 * [300],
153  3 * [HORIZONTAL],
154  update_LF_ref_scale,
155  )
156  button_contact_RF = Button(
157  master, text="Break contact right foot", command=switch_contact_RF
158  )
159  button_contact_RF.pack(side=tk.LEFT)
160  button_contact_LF = Button(
161  master, text="Break contact left foot", command=switch_contact_LF
162  )
163  button_contact_LF.pack(side=tk.LEFT)
164  Button(master, text="Toggle wireframe", command=toggle_wireframe_mode).pack(
165  side=tk.LEFT
166  )
167 
168  # Frame(height=2, bd=1, relief=tk.SUNKEN).pack(fill=tk.X, padx=5, pady=5)
169  Button(master, text="Push robot CoM", command=push_robot).pack()
170  com_vel_entry = Entry3d(master, "CoM vel")
171  mainloop()
172 
173 
174 def run_simu():
175  global push_robot_active
176  i, t = 0, 0.0
177  q, v = tsid.q, tsid.v
178  time_avg = 0.0
179  while True:
180  time_start = time.time()
181 
182  tsid.comTask.setReference(tsid.trajCom.computeNext())
183  tsid.postureTask.setReference(tsid.trajPosture.computeNext())
184  tsid.rightFootTask.setReference(tsid.trajRF.computeNext())
185  tsid.leftFootTask.setReference(tsid.trajLF.computeNext())
186 
187  HQPData = tsid.formulation.computeProblemData(t, q, v)
188 
189  sol = tsid.solver.solve(HQPData)
190  if sol.status != 0:
191  print("QP problem could not be solved! Error code:", sol.status)
192  break
193 
194  # tau = tsid.formulation.getActuatorForces(sol)
195  dv = tsid.formulation.getAccelerations(sol)
196  q, v = tsid.integrate_dv(q, v, dv, conf.dt)
197  i, t = i + 1, t + conf.dt
198 
199  if push_robot_active:
200  push_robot_active = False
201  data = tsid.formulation.data()
202  if tsid.contact_LF_active:
203  J_LF = tsid.contactLF.computeMotionTask(0.0, q, v, data).matrix
204  else:
205  J_LF = np.zeros((0, tsid.model.nv))
206  if tsid.contact_RF_active:
207  J_RF = tsid.contactRF.computeMotionTask(0.0, q, v, data).matrix
208  else:
209  J_RF = np.zeros((0, tsid.model.nv))
210  J = np.vstack((J_LF, J_RF))
211  J_com = tsid.comTask.compute(t, q, v, data).matrix
212  A = np.vstack((J_com, J))
213  b = np.concatenate((np.array(push_robot_com_vel), np.zeros(J.shape[0])))
214  v = np.linalg.lstsq(A, b, rcond=-1)[0]
215 
216  if i % conf.DISPLAY_N == 0:
217  tsid.display(q)
218  x_com = tsid.robot.com(tsid.formulation.data())
219  x_com_ref = tsid.trajCom.getSample(t).pos()
220  H_lf = tsid.robot.framePosition(tsid.formulation.data(), tsid.LF)
221  H_rf = tsid.robot.framePosition(tsid.formulation.data(), tsid.RF)
222  x_lf_ref = tsid.trajLF.getSample(t).pos()[:3]
223  x_rf_ref = tsid.trajRF.getSample(t).pos()[:3]
225  tsid.viz, "world/com", [*x_com.tolist(), 0, 0, 0, 1.0]
226  )
228  tsid.viz, "world/com_ref", [*x_com_ref.tolist(), 0, 0, 0, 1.0]
229  )
231  tsid.viz, "world/rf", pin.SE3ToXYZQUATtuple(H_rf)
232  )
234  tsid.viz, "world/lf", pin.SE3ToXYZQUATtuple(H_lf)
235  )
237  tsid.viz, "world/rf_ref", [*x_rf_ref.tolist(), 0, 0, 0, 1.0]
238  )
240  tsid.viz, "world/lf_ref", [*x_lf_ref.tolist(), 0, 0, 0, 1.0]
241  )
242 
243  if i % 1000 == 0:
244  print(
245  f"Average loop time: {1e3 * time_avg:.1f} (expected is {1e3 * conf.dt:.1f})"
246  )
247 
248  time_spent = time.time() - time_start
249  time_avg = (i * time_avg + time_spent) / (i + 1)
250 
251  if time_avg < 0.9 * conf.dt:
252  time.sleep(10 * (conf.dt - time_avg))
253 
254 
255 print("#" * conf.LINE_WIDTH)
256 print(" Test Task Space Inverse Dynamics ".center(conf.LINE_WIDTH, "#"))
257 print("#" * conf.LINE_WIDTH)
258 
259 tsid = TsidBiped(conf, conf.viewer)
260 tsid.q0[2] = 1.02127
261 
262 com_0 = tsid.robot.com(tsid.formulation.data())
263 H_rf_0 = tsid.robot.framePosition(
264  tsid.formulation.data(), tsid.model.getFrameId(conf.rf_frame_name)
265 )
266 H_lf_0 = tsid.robot.framePosition(
267  tsid.formulation.data(), tsid.model.getFrameId(conf.lf_frame_name)
268 )
269 
271  tsid.viz, "world/com", conf.SPHERE_RADIUS, conf.COM_SPHERE_COLOR
272 )
274  tsid.viz, "world/com_ref", conf.REF_SPHERE_RADIUS, conf.COM_REF_SPHERE_COLOR
275 )
276 vizutils.addViewerSphere(tsid.viz, "world/rf", conf.SPHERE_RADIUS, conf.RF_SPHERE_COLOR)
278  tsid.viz, "world/rf_ref", conf.REF_SPHERE_RADIUS, conf.RF_REF_SPHERE_COLOR
279 )
280 vizutils.addViewerSphere(tsid.viz, "world/lf", conf.SPHERE_RADIUS, conf.LF_SPHERE_COLOR)
282  tsid.viz, "world/lf_ref", conf.REF_SPHERE_RADIUS, conf.LF_REF_SPHERE_COLOR
283 )
284 
285 th_gui = threading.Thread(target=create_gui)
286 th_gui.start()
287 
288 th_simu = threading.Thread(target=run_simu)
289 th_simu.start()
ex_3_biped_balance_with_gui.toggle_wireframe_mode
def toggle_wireframe_mode()
Definition: ex_3_biped_balance_with_gui.py:106
ex_3_biped_balance_with_gui.run_simu
def run_simu()
Definition: ex_3_biped_balance_with_gui.py:174
ex_3_biped_balance_with_gui.create_gui
def create_gui()
Definition: ex_3_biped_balance_with_gui.py:116
vizutils.applyViewerConfiguration
def applyViewerConfiguration(viz, name, xyzquat)
Definition: vizutils.py:51
ex_3_biped_balance_with_gui.Entry3d.__init__
def __init__(self, master, name)
Definition: ex_3_biped_balance_with_gui.py:45
ex_3_biped_balance_with_gui.update_com_ref_scale
def update_com_ref_scale(value)
Definition: ex_3_biped_balance_with_gui.py:69
ex_3_biped_balance_with_gui.update_LF_ref_scale
def update_LF_ref_scale(value)
Definition: ex_3_biped_balance_with_gui.py:81
ex_3_biped_balance_with_gui.Scale3d.__init__
def __init__(self, master, name, from_, to, tickinterval, length, orient, command)
Definition: ex_3_biped_balance_with_gui.py:23
ex_3_biped_balance_with_gui.push_robot
def push_robot()
Definition: ex_3_biped_balance_with_gui.py:110
ex_3_biped_balance_with_gui.switch_contact_RF
def switch_contact_RF()
Definition: ex_3_biped_balance_with_gui.py:88
ex_3_biped_balance_with_gui.Entry3d
Definition: ex_3_biped_balance_with_gui.py:44
ex_3_biped_balance_with_gui.update_RF_ref_scale
def update_RF_ref_scale(value)
Definition: ex_3_biped_balance_with_gui.py:74
Frame
FrameTpl< context::Scalar, context::Options > Frame
ex_3_biped_balance_with_gui.switch_contact_LF
def switch_contact_LF()
Definition: ex_3_biped_balance_with_gui.py:97
ex_3_biped_balance_with_gui.Scale3d.s
s
Definition: ex_3_biped_balance_with_gui.py:24
ex_3_biped_balance_with_gui.Entry3d.get
def get(self)
Definition: ex_3_biped_balance_with_gui.py:54
center
Vec3f center
vizutils.addViewerSphere
def addViewerSphere(viz, name, size, rgba)
Definition: vizutils.py:37
ex_3_biped_balance_with_gui.Entry3d.s
s
Definition: ex_3_biped_balance_with_gui.py:46
ex_3_biped_balance_with_gui.Scale3d
Definition: ex_3_biped_balance_with_gui.py:22
tsid_biped.TsidBiped
Definition: tsid_biped.py:11
ex_3_biped_balance_with_gui.Scale3d.get
def get(self)
Definition: ex_3_biped_balance_with_gui.py:40


tsid
Author(s): Andrea Del Prete, Justin Carpentier
autogenerated on Sat May 3 2025 02:48:16