问题:
在代码中创建世界,然后设置机器人的关节动作,发现执行set_joint_positions后无法实时显示动作,在窗口中显示动作:
# 创建世界
world = World(stage_units_in_meters=1.0, device='cpu')
world.set_simulation_dt(physics_dt=1.0 / 60.0, rendering_dt=1.0/600.0)
# 设置机器人的关节动作
_robots = fetch_handler.create_articulation_view()
_robots.set_joint_positions(positions=jt_pos)
# 更新
world.step(render=True)
分析:
在官方搜索教程发现需要等待一段时间重新渲染才有用:
参考链接
而且采用这种方式,加入一个输入等待再step一次在仿真环境中机器人就执行动作。
world.step(render=True)
input("开始")
world.step(render=True)
最终解决方案:
加入一个时钟的类,如果想要实时显示的话,需要连续执行world.step(True),且两次执行之间必须存在时间间隔rate.sleep()。
class SteadyRate:
""" Maintains the steady cycle rate provided on initialization by adaptively sleeping an amount
of time to make up the remaining cycle time after work is done.
Usage:
rate = SteadyRate(rate_hz=30.)
while True:
do.work() # Do any work.
rate.sleep() # Sleep for the remaining cycle time.
"""
def __init__(self, rate_hz):
self.rate_hz = rate_hz
self.dt = 1.0 / rate_hz
self.last_sleep_end = time.time()
def sleep(self):
work_elapse = time.time() - self.last_sleep_end
sleep_time = self.dt - work_elapse
if sleep_time > 0.0:
time.sleep(sleep_time)
self.last_sleep_end = time.time()
# 机器人执行动作
_robots.set_joint_positions(positions=jt_pos)
# 更新
world.step(render=True)
rate.sleep()
world.step(render=True)