python - Pydrake : how to get a single sine wave out of sine. get_output_port(0).Eval(sine_context)? (目前产生3波)

标签 python drake

|德雷克 0.38.0 | python 3.7 |操作系统:ubuntu 20.04 | pycharm |

我想使用正弦波(振幅:0.01,f:2)作为 kuka iiwa 模拟机器人底座的关节扭矩输入。 (即我只想看到底座来回旋转以测试其他一些代码)

我使用正弦系统构建器将正弦系统添加到我的图表中。

sine = builder.AddSystem(Sine(np.array([0.01]),np.array([2]),np.array([0])))
context = diagram.CreateDefaultContext()
sine_context = sine.GetMyMutableContextFromRoot(context)
## sine_context = sine.CreateDefaultContext() # exact same behavior occurs

...
qt = np.zeros(n_dof)
qt[0] = sine.get_output_port(0).Eval(sine_context)
plant.get_actuation_input_port().FixValue(plant_context, qt)

当我调用下面的代码(在某个时间 t)并尝试绘制 qt[0] 的图形时,我看到三个波而不是一个:正弦波、余弦波和其他异相波。 我的理解是 Sine() 的输出端口 0 = sine, 1 = cosine, 2 = -sine 并且我必须使用 qt[0] = sine.get_output_port(0).Eval(sine_context) 来获取浮点值给定时间 t 的正弦值。

simulator = Simulator(diagram, context)
simulator.set_target_realtime_rate(1.0)
# Advance the simulation slightly to initialize the GUI
simulator.AdvanceTo(0.01)
...
ml = momentum_log(5000, plant)
simulator.set_monitor(partial(momentum_log.monitor, ml))
while meshcat.GetButtonClicks(stop_sim_button) < 1:
      t = simulator.get_context().get_time()
      sine_context.SetTime(t)
      qt[0] = sine.get_output_port(0).Eval(sine_context)
      plant.get_actuation_input_port().FixValue(plant_context, qt)
      plotp(t,qt[0],0.05)
      simulator.AdvanceTo(t + 1.0)

ml.save()
plotp(t,qt[0],0.05)
plt.show()

我错过了什么?

编辑:请参阅下图以了解详情

Graph with mutlicolored dots shows the progression of a sine wave, cosine wave, and other, out of-phase wave from 0 - 50 seconds. Amplitude of the waves is 0.01 per the settings used.

最佳答案

情节看起来是正确的。当您每 2 弧度采样一次 sin(x) 时,它应该是这样的:

import matplotlib.pyplot as plt
from math import sin

amplitude = 0.01
frequency = 2.0
t = list(range(0, 50))
y = [amplitude * sin(frequency * x) for x in t]
plt.plot(t, y, 'ro')
plt.show()

output plot

关于python - Pydrake : how to get a single sine wave out of sine. get_output_port(0).Eval(sine_context)? (目前产生3波),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72370379/

相关文章:

python-3.x - 为什么这个用于查找 2 个四元数之间角速度的简单 MP 会失败?

drake - 接触雅可比的时间导数

python - 以另一个用户身份启动 python 后获取 winlows 登录名

python - 预分配 NumPy 数组的首选方法是什么?

python - 使用现有的公共(public)假期和周末专栏创建长周末新专栏

drake - 如何获得我们可以在下一步中应用渐变的动态(重新打开)

python - 使用python版本2.7创建虚拟环境,现有版本为3.7

python - 从领先的某些实例中清除列表

python - 使用 MultibodyPlant,为什么我会得到 `SolveQuadraticForTheSmallestPositiveRoot(): condition ' Delta > 0' failed`?

drake - 用 meshcat 保存视频?