import math
class StairClimbingSim:
def __init__(self, step_h=0.15, step_d=0.25, n_steps=5):
self.step_h = step_h
self.step_d = step_d
self.n_steps = n_steps
def get_terrain_height(self, x):
if x < 0:
return 0
step_idx = int(x / self.step_d)
if step_idx >= self.n_steps:
return self.n_steps * self.step_h
return step_idx * self.step_h
def plan_foot_sequence(self):
plan = []
body_x = -0.1
body_z = 0.2
for stair in range(self.n_steps):
for leg in ['LF', 'RF', 'LB', 'RB']:
if leg in ['LF', 'RF']:
target_x = (stair + 1) * self.step_d - 0.05
target_z = self.get_terrain_height(target_x)
else:
target_x = stair * self.step_d + 0.05
target_z = self.get_terrain_height(target_x)
plan.append((stair, leg, target_x, target_z))
return plan
def simulate_climb(self):
plan = self.plan_foot_sequence()
body_x = -0.1
body_z = 0.2
body_roll = 0
body_pitch = 0
results = []
for stair, leg, tx, tz in plan:
body_x = (tx + body_x) / 2
body_z = tz + 0.2
# Pitch adjustment
front_z = self.get_terrain_height(body_x + 0.2)
back_z = self.get_terrain_height(body_x - 0.2)
body_pitch = math.atan2(front_z - back_z, 0.4)
results.append((stair, leg, tx, tz, body_x, body_z, body_pitch))
return results
scs = StairClimbingSim(step_h=0.15, step_d=0.25, n_steps=5)
print("=" * 55)
print(" Stair Climbing Simulation")
print("=" * 55)
print(f" Step: h={scs.step_h*100:.0f}cm, d={scs.step_d*100:.0f}cm, n={scs.n_steps}")
plan = scs.plan_foot_sequence()
print(f"\n [Foot Placement Plan ({len(plan)} moves)]")
for stair, leg, tx, tz in plan:
print(f" Stair {stair+1}, {leg}: target=({tx:.3f}, {tz:.3f})")
results = scs.simulate_climb()
print(f"\n [Climbing Simulation Results]")
for stair, leg, tx, tz, bx, bz, bp in results:
print(f" Stair {stair+1} {leg}: foot=({tx:.3f},{tz:.3f}) body=({bx:.3f},{bz:.3f}) pitch={bp*180/math.pi:.1f}deg")
print()
print(" OK - Stair climbing simulation complete")
仿真结果:
=======================================================
Stair Climbing Simulation
=======================================================
Step: h=15cm, d=25cm, n=5
[Foot Placement Plan (20 moves)]
Stair 1, LF: target=(0.200, 0.000)
Stair 1, RF: target=(0.200, 0.000)
Stair 1, LB: target=(0.050, 0.000)
Stair 1, RB: target=(0.050, 0.000)
Stair 2, LF: target=(0.450, 0.150)
Stair 2, RF: target=(0.450, 0.150)
Stair 2, LB: target=(0.300, 0.150)
Stair 2, RB: target=(0.300, 0.150)
Stair 3, LF: target=(0.700, 0.300)
Stair 3, RF: target=(0.700, 0.300)
Stair 3, LB: target=(0.550, 0.300)
Stair 3, RB: target=(0.550, 0.300)
Stair 4, LF: target=(0.950, 0.450)
Stair 4, RF: target=(0.950, 0.450)
Stair 4, LB: target=(0.800, 0.450)
Stair 4, RB: target=(0.800, 0.450)
Stair 5, LF: target=(1.200, 0.600)
Stair 5, RF: target=(1.200, 0.600)
Stair 5, LB: target=(1.050, 0.600)
Stair 5, RB: target=(1.050, 0.600)
[Climbing Simulation Results]
Stair 1 LF: foot=(0.200,0.000) body=(0.050,0.200) pitch=20.6deg
Stair 1 RF: foot=(0.200,0.000) body=(0.125,0.200) pitch=20.6deg
Stair 1 LB: foot=(0.050,0.000) body=(0.087,0.200) pitch=20.6deg
Stair 1 RB: foot=(0.050,0.000) body=(0.069,0.200) pitch=20.6deg
Stair 2 LF: foot=(0.450,0.150) body=(0.259,0.350) pitch=20.6deg
Stair 2 RF: foot=(0.450,0.150) body=(0.355,0.350) pitch=36.9deg
Stair 2 LB: foot=(0.300,0.150) body=(0.327,0.350) pitch=36.9deg
Stair 2 RB: foot=(0.300,0.150) body=(0.314,0.350) pitch=36.9deg
Stair 3 LF: foot=(0.700,0.300) body=(0.507,0.500) pitch=20.6deg
Stair 3 RF: foot=(0.700,0.300) body=(0.603,0.500) pitch=36.9deg
Stair 3 LB: foot=(0.550,0.300) body=(0.577,0.500) pitch=36.9deg
Stair 3 RB: foot=(0.550,0.300) body=(0.563,0.500) pitch=36.9deg
Stair 4 LF: foot=(0.950,0.450) body=(0.757,0.650) pitch=20.6deg
Stair 4 RF: foot=(0.950,0.450) body=(0.853,0.650) pitch=36.9deg
Stair 4 LB: foot=(0.800,0.450) body=(0.827,0.650) pitch=36.9deg
Stair 4 RB: foot=(0.800,0.450) body=(0.813,0.650) pitch=36.9deg
Stair 5 LF: foot=(1.200,0.600) body=(1.007,0.800) pitch=20.6deg
Stair 5 RF: foot=(1.200,0.600) body=(1.103,0.800) pitch=36.9deg
Stair 5 LB: foot=(1.050,0.600) body=(1.077,0.800) pitch=36.9deg
Stair 5 RB: foot=(1.050,0.600) body=(1.063,0.800) pitch=36.9deg
OK - Stair climbing simulation complete