如何在 Drake 模拟期间记录计算的多体数量

How to record calculated multibody quantities during a simulation in Drake

drake multibody plant class 有很多函数用于计算我在模拟过程中记录的感兴趣的量(例如动量、质心等。 ).像这样记录数据的最佳方式是什么?我没有广泛使用 drake,但我有一些想法:

  1. 运行 循环中的模拟具有定义的时间步长(即 simulator.AdvanceTo(current_time + dt))并使用 multibody 工厂直接计算数量。
    • 似乎有点受限(即不能使用对 AdvanceTo() 的单个调用来 运行 模拟)并且可能需要非常小的时间步长才能获得我正在寻找的分辨率.
  2. 使用VectorLogSink块记录多body植物输出端口的可用数量(例如body空间速度,body姿势等),并求解通过从值重建 Context 并调用 multibody 工厂计算函数,在模拟结束后获得感兴趣的数量
    • 不确定这是否可行;似乎有点迂回;感兴趣的数量在模拟过程中不可用
  3. 创建一个可以连接到多body 工厂的系统块,以在每个内部模拟时间步执行这些计算。然后该块可以连接到 VectorLogSink 块以记录数据。
    • 不确定这是否可行或从哪里开始

任何人都可以提供一些关于如何记录多个body数量的指导吗?

从这个 issue 移植这个问题以供继续讨论

来自@jwnimmer-tri

的初始回复

Your assessment of option (1) is correct. If you already know a reasonable dt step size for your logging that suits your needs, it can work well. If the "interesting" times are not always on a fixed schedule, this way can be difficult.

Relatedly, there is an option (4) you hadn't found yet. The Simulator::set_monitor() can be used to set a simulation callback. You could use that for the ability to log at every simulation step, no matter how large or small the step was.

Option (2) should work as well. Assuming that the only state in your simulation is the multibody plant positions and velocities, you could log those during the simulation, and then be able to set the context state to those values offline later, and make any queries on the plant that you need. If you have extra state (e.g., controller state), this approach becomes more difficult.

Option (3) is also possible, but more complicated to explain.