|德雷克 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()
我错过了什么?
编辑:请参阅下图以了解详情
最佳答案
情节看起来是正确的。当您每 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()
关于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/