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


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