我正在设置一个简单的模拟,我希望一个物体(一个盒子(在重力场中落下半秒钟,然后由于我施加的力而上升,克服重力。
我尝试使用三种方法实现这种力,分别使用 AddInForce
、AddInForceInWorld
和 AddForceElement
。由于这是在模拟运行时调用的,因此AddForceElement
按预期抛出错误。然而,对于另外两个,没有抛出任何错误或警告,但盒子显然没有新的力量作用于它。
我也检查了一个新的力num_force_elements()
元素,但没有添加任何元素。
这是我对此事件计时的循环:
while( current_time < FLAGS_duration && !terminate){
if (current_time > 0.5 && !forced) {
std::cout << "nAdding Force of type " << FLAGS_box_f << " at 0.5 seconds...n";
// Add Rising Force to box
auto forces = drake::multibody::MultibodyForces<double>(plant);
drake::multibody::SpatialForce<double> forceup(Vector3d::Zero(), Vector3d(0, 0, 100));
if (FLAGS_box_f == "spring") {
plant.AddForceElement<LinearSpringDamper>(
plant.GetBodyByName("Box"), Vector3d::Zero(),
plant.world_body(), Vector3d(0, 0, 1),
0., 10., 1.);
} else if (FLAGS_box_f == "world") {
plant.GetBodyByName("Box").AddInForceInWorld(
plant_context,
forceup,
&forces);
} else {
plant.GetBodyByName("Box").AddInForce(
plant_context,
Vector3d::Zero(),
forceup,
plant.GetBodyByName("Box").body_frame(),
&forces);
}
plant.CalcForceElementsContribution(plant_context, &forces);
std::cout << "Plant: " << plant.num_force_elements() << " force_elementsn";
forced = true;
}
simulator.StepTo(current_time + time_delta);
current_time = simulator_context.get_time();
}
我不确定我是否正确地完成了这些动作,并且仍然不太了解我在调用中存储力的多体力量对象。
@Joaquin Giraldo,
向模型添加外力的方法是使用输入力。德雷克中的所有内容都是输入/输出端口。在这种情况下,您必须使用 MultibodyPlant::get_applied_spatial_force_input_port()
.有关如何执行此操作的示例,请参阅文件 multibody/plant/test/externally_applied_spatial_force_test.cc
。
快速说明:您需要的是一个始终存在的力元素,但会根据时间改变其力输出。没有一个内置的以这种方式运行,所以你必须写一个。