Skip to content

Commit 7f627cb

Browse files
author
Marc Bestmann
committed
improvements, made pybullet work again
1 parent cca4ec7 commit 7f627cb

File tree

6 files changed

+2916
-5153
lines changed

6 files changed

+2916
-5153
lines changed

scripts/viz.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from optuna import Study, load_study, visualization, importance
44

5-
study = load_study('webots_test5', storage='postgresql://bestmann:deepquintic@localhost/deep_quintic')
5+
study = load_study('pybullet3', storage='postgresql://bestmann:deepquintic@localhost/deep_quintic')
66
df = study.trials_dataframe()
77
#print(df)
88
print(df.keys())
@@ -42,6 +42,12 @@
4242

4343
visualization.plot_slice(study).show()#, params=['freq', 'double_support_ratio', 'trunk_height'])
4444

45+
visualization.plot_contour(study, params=['double_support_ratio', 'trunk_swing']).show()
46+
47+
visualization.plot_optimization_history(study).show()
48+
49+
visualization.plot_parallel_coordinate(study).show()
50+
4551
#%%
4652

4753
df_sorted

scripts/viz_study.ipynb

Lines changed: 2806 additions & 5107 deletions
Large diffs are not rendered by default.

src/parallel_parameter_search/optimize.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# this has to be first import, otherwise there will be an error
2+
from bitbots_quintic_walk import PyWalk
3+
import cProfile as profile
4+
15
import argparse
26
import importlib
37

@@ -25,7 +29,7 @@
2529
# exit(1)
2630

2731
seed = np.random.randint(2 ** 32 - 1)
28-
n_startup_trials = 1000
32+
n_startup_trials = 10
2933

3034
sampler = TPESampler(n_startup_trials=n_startup_trials, seed=seed)
3135
#pruner = MedianPruner(n_startup_trials=n_startup_trials, n_warmup_steps=10)
@@ -34,5 +38,12 @@
3438
sampler=sampler, load_if_exists=True)
3539

3640
#objective = args.objective()
37-
objective = WolfgangWalkOptimization('worker', gui=True)
38-
study.optimize(objective.objective, n_trials=1000, show_progress_bar=True)
41+
pr = profile.Profile()
42+
pr.enable()
43+
try:
44+
objective = WolfgangWalkOptimization('worker', gui=True, walk_as_node=False)
45+
study.optimize(objective.objective, n_trials=1000, show_progress_bar=True)
46+
finally:
47+
print("hi")
48+
pr.disable()
49+
pr.dump_stats('profile.pstat')

src/parallel_parameter_search/optimize_webots.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# this has to be first import, otherwise there will be an error
12
from bitbots_quintic_walk import PyWalk
23
import argparse
34
import importlib

src/parallel_parameter_search/simulators.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def __init__(self, namespace, gui):
4545
super(AbstractSim, self).__init__()
4646
self.namespace = namespace
4747
self.gui = gui
48-
self.sim: PybulletSim = Simulation(gui)
48+
self.sim: Simulation = Simulation(gui)
4949
self.sim_interface: ROSInterface = ROSInterface(self.sim, namespace=self.namespace + '/', node=False)
5050

5151
def step_sim(self):
52-
self.sim_interface.step()
52+
self.sim.step()
5353

5454
def set_gravity(self, on):
5555
self.sim.set_gravity(on)
@@ -63,6 +63,18 @@ def get_robot_pose_rpy(self):
6363
def reset(self):
6464
self.sim.reset()
6565

66+
def get_time(self):
67+
return self.sim.time
68+
69+
def get_imu_msg(self):
70+
return self.sim_interface.get_imu_msg()
71+
72+
def get_joint_state_msg(self):
73+
return self.sim_interface.get_joint_state_msg()
74+
75+
def set_joints(self, joint_command_msg):
76+
self.sim_interface.joint_goal_cb(joint_command_msg)
77+
6678

6779
class WebotsSim(AbstractSim):
6880

src/parallel_parameter_search/walk_optimization.py

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def __init__(self, namespace, robot_name, walk_as_node):
5757
def objective(self, trial):
5858
# get parameter to evaluate from optuna
5959
self.suggest_walk_params(trial)
60+
self.reset()
6061

6162
cost = 0
6263
# starting with hardest first, to get faster early termination
@@ -70,7 +71,7 @@ def objective(self, trial):
7071
d = 0
7172
for direction in self.directions:
7273
d += 1
73-
self.reset()
74+
self.reset_position()
7475
early_term, cost_try = self.evaluate_direction(*direction, trial, eval_try)
7576
cost += cost_try
7677
# check if we failed in this direction and terminate this trial early
@@ -88,13 +89,13 @@ def suggest_walk_params(self, trial):
8889
raise NotImplementedError
8990

9091
def evaluate_direction(self, x, y, yaw, trial: optuna.Trial, iteration):
91-
start_time = rospy.get_time()
92+
start_time = self.sim.get_time()
9293
self.set_cmd_vel(x * iteration, y * iteration, yaw * iteration)
9394
print(F'cmd: {x * iteration} {y * iteration} {yaw * iteration}')
9495

9596
# wait till time for test is up or stopping condition has been reached
9697
while not rospy.is_shutdown():
97-
passed_time = rospy.get_time() - start_time
98+
passed_time = self.sim.get_time() - start_time
9899
if passed_time > self.time_limit:
99100
# reached time limit, stop robot
100101
self.set_cmd_vel(0, 0, 0)
@@ -176,6 +177,13 @@ def compute_cost(self, x, y, yaw):
176177
early_term = True
177178
return early_term, cost
178179

180+
def reset_position(self):
181+
height = self.current_params['trunk_height'] + self.reset_height_offset
182+
pitch = self.current_params['trunk_pitch']
183+
(x, y, z, w) = tf.transformations.quaternion_from_euler(0, pitch, 0)
184+
185+
self.sim.reset_robot_pose((0, 0, height), (x, y, z, w))
186+
179187
def reset(self):
180188
# reset simulation
181189
# self.sim.reset()
@@ -193,11 +201,7 @@ def reset(self):
193201
else:
194202
self.run_walking(duration=2)
195203
self.sim.set_gravity(True)
196-
height = self.current_params['trunk_height'] + self.reset_height_offset
197-
pitch = self.current_params['trunk_pitch']
198-
(x, y, z, w) = tf.transformations.quaternion_from_euler(0, pitch, 0)
199-
200-
self.sim.reset_robot_pose((0, 0, height), (x, y, z, w))
204+
self.reset_position()
201205
if self.walk_as_node:
202206
self.sim.run_simulation(duration=2, sleep=0.01)
203207
else:
@@ -215,8 +219,8 @@ def set_cmd_vel(self, x, y, yaw):
215219

216220

217221
class WolfgangWalkOptimization(AbstractWalkOptimization):
218-
def __init__(self, namespace, gui):
219-
super(WolfgangWalkOptimization, self).__init__(namespace, 'wolfgang')
222+
def __init__(self, namespace, gui, walk_as_node):
223+
super(WolfgangWalkOptimization, self).__init__(namespace, 'wolfgang', walk_as_node)
220224
self.reset_height_offset = 0.005
221225
self.directions = [[0.1, 0, 0],
222226
[-0.1, 0, 0],
@@ -237,36 +241,59 @@ def suggest_walk_params(self, trial):
237241
def add(name, min_value, max_value):
238242
param_dict[name] = trial.suggest_uniform(name, min_value, max_value)
239243

240-
add('double_support_ratio', 0.0, 1.0)
241-
add('freq', 0.1, 5)
242-
add('foot_apex_phase', 0.0, 1.0)
244+
add('double_support_ratio', 0.0, 0.45)
245+
add('freq', 1.5, 3)
243246
add('foot_distance', 0.1, 0.3)
244-
add('foot_rise', 0.01, 0.1)
245-
add('foot_overshoot_phase', 0.0, 1.0)
246-
add('foot_overshoot_ratio', 0.0, 1.0)
247-
add('trunk_height', 0.25, 0.5)
247+
add('trunk_height', 0.38, 0.45)
248248
add('trunk_phase', -0.5, 0.5)
249-
add('trunk_pitch', -0.5, 0.5)
250249
add('trunk_swing', 0.0, 1.0)
251-
add('trunk_x_offset', -0.05, 0.05)
252-
add('trunk_y_offset', -0.05, 0.05)
253-
add('first_step_swing_factor', 0.5, 2)
254-
add('first_step_trunk_phase', -0.5, 0.5)
250+
add('trunk_x_offset', -0.03, 0.03)
255251

256252
add('trunk_x_offset_p_coef_forward', -1, 1)
257253
add('trunk_x_offset_p_coef_turn', -1, 1)
258-
add('trunk_pitch_p_coef_forward', -5, 5)
259-
add('trunk_pitch_p_coef_turn', -5, 5)
260-
add('foot_z_pause', 0, 1)
261-
add('foot_put_down_phase', 0, 1)
262-
add('trunk_pause', 0, 1)
254+
255+
# add('first_step_swing_factor', 0.0, 2)
256+
# add('first_step_trunk_phase', -0.5, 0.5)
257+
param_dict['first_step_swing_factor'] = 1
258+
param_dict['first_step_trunk_phase'] = -0.5
259+
260+
# add('foot_overshoot_phase', 0.0, 1.0)
261+
# add('foot_overshoot_ratio', 0.0, 1.0)
262+
param_dict['foot_overshoot_phase'] = 1
263+
param_dict['foot_overshoot_ratio'] = 0.0
264+
265+
# add('trunk_y_offset', -0.03, 0.03)
266+
# add('foot_rise', 0.04, 0.08)
267+
# add('foot_apex_phase', 0.0, 1.0)
268+
param_dict['trunk_y_offset'] = 0
269+
param_dict['foot_rise'] = 0.1
270+
param_dict['foot_apex_phase'] = 0.5
271+
# todo put this as addition arguments to trial
272+
273+
# add('trunk_pitch', -1.0, 1.0)
274+
# add('trunk_pitch_p_coef_forward', -5, 5)
275+
# add('trunk_pitch_p_coef_turn', -5, 5)
276+
param_dict['trunk_pitch'] = 0
277+
param_dict['trunk_pitch_p_coef_forward'] = 0
278+
param_dict['trunk_pitch_p_coef_turn'] = 0
279+
280+
# add('foot_z_pause', 0, 1)
281+
# add('foot_put_down_phase', 0, 1)
282+
# add('trunk_pause', 0, 1)
283+
param_dict['foot_z_pause'] = 0
284+
param_dict['foot_put_down_phase'] = 1
285+
param_dict['trunk_pause'] = 0
263286

264287
# todo 'trunk' nochmal ander nennen? body?
265288

266289
# todo also find PID values, maybe in a second step after finding walking params
267290
# todo de/activate phase reset while searching params? yes
268291

269-
self.set_params(param_dict)
292+
if self.walk_as_node:
293+
self.set_params(param_dict)
294+
else:
295+
self.current_params = param_dict
296+
self.walk.set_engine_dyn_reconf(param_dict)
270297

271298

272299
class DarwinWalkOptimization(AbstractWalkOptimization):
@@ -275,9 +302,13 @@ def __init__(self, namespace, gui, walk_as_node):
275302
self.reset_height_offset = 0.09
276303
self.directions = [[0.05, 0, 0],
277304
[-0.05, 0, 0],
305+
[0, 0.025, 0],
306+
[0, -0.025, 0],
278307
[0, 0, 0.25],
279308
[0, 0, -0.25],
280-
[0.05, 0, 0.25],
309+
[0.05, 0.25, 0],
310+
[0.05, -0.25, 0],
311+
[0.05, 0, -0.25],
281312
[-0.05, 0, 0.25],
282313
]
283314
self.sim = WebotsSim(self.namespace, gui)
@@ -288,31 +319,34 @@ def suggest_walk_params(self, trial):
288319
def add(name, min_value, max_value):
289320
param_dict[name] = trial.suggest_uniform(name, min_value, max_value)
290321

291-
# todo try without overshoot
292-
# todo try without apex phase
293-
# todo try without pitch values, only x offset
294-
add('double_support_ratio', 0.0, 0.8)
295-
add('freq', 0.5, 3)
322+
add('double_support_ratio', 0.0, 0.5)
323+
add('freq', 1.5, 3)
296324
add('foot_distance', 0.08, 0.17)
297-
add('foot_overshoot_phase', 0.0, 1.0)
298-
add('foot_overshoot_ratio', 0.0, 1.0)
299325
add('trunk_height', 0.18, 0.24)
300326
add('trunk_phase', -0.5, 0.5)
301327
add('trunk_swing', 0.0, 1.0)
302328
add('trunk_x_offset', -0.03, 0.03)
303-
add('first_step_swing_factor', 0.0, 2)
304-
add('first_step_trunk_phase', -0.5, 0.5)
305329

306330
add('trunk_x_offset_p_coef_forward', -1, 1)
307331
add('trunk_x_offset_p_coef_turn', -1, 1)
308332

333+
#add('first_step_swing_factor', 0.0, 2)
334+
#add('first_step_trunk_phase', -0.5, 0.5)
335+
param_dict['first_step_swing_factor'] = 1
336+
param_dict['first_step_trunk_phase'] = -0.5
337+
338+
#add('foot_overshoot_phase', 0.0, 1.0)
339+
#add('foot_overshoot_ratio', 0.0, 1.0)
340+
param_dict['foot_overshoot_phase'] = 1
341+
param_dict['foot_overshoot_ratio'] = 0.0
342+
309343
# add('trunk_y_offset', -0.03, 0.03)
310344
# add('foot_rise', 0.04, 0.08)
311345
# add('foot_apex_phase', 0.0, 1.0)
312346
param_dict['trunk_y_offset'] = 0
313-
param_dict['foot_rise'] = 0.04
347+
param_dict['foot_rise'] = 0.05
314348
param_dict['foot_apex_phase'] = 0.5
315-
# todo put this as addition arguments to trail
349+
# todo put this as addition arguments to trial
316350

317351
# add('trunk_pitch', -1.0, 1.0)
318352
# add('trunk_pitch_p_coef_forward', -5, 5)

0 commit comments

Comments
 (0)