webots学习记录8:R2023b如何在某个零件上添加一个恒定的力(矩)

ops/2024/9/25 17:13:17/


webots安装路径下,从include\controller\c\webots\supervisor.h中可以看到如下定义:

void wb_supervisor_node_add_force(WbNodeRef node, const double force[3], bool relative);
void wb_supervisor_node_add_force_with_offset(WbNodeRef node, const double force[3], const double offset[3], bool relative);
void wb_supervisor_node_add_torque(WbNodeRef node, const double torque[3], bool relative);

然而在include\controller\cpp\webots\Supervisor.hpp中却没有类似的定义,
在include\controller\cpp\webots\Node.hpp中才有类似定义:

void addForce(const double force[3], bool relative);
void addForceWithOffset(const double force[3], const double offset[3], bool relative);
void addTorque(const double torque[3], bool relative);

从include\controller\c\webots\types.h可以看到WbNodeRef的定义:

typedef struct WbNodeStructPrivate *WbNodeRef;

在https://github.com/cyberbotics/webots/blob/master/src/controller/c/supervisor.c中可以找到WbNodeStructPrivate的定义:

typedef struct WbNodeStructPrivate {int id;WbNodeType type;char *model_name;char *def_name;char *content;int parent_id;double *position;                                  // double[3]double *orientation;                               // double[9]double *center_of_mass;                            // double[3]WbNodeWbContactPointListStruct contact_points[2];  // 0 -> without descendants, 1 -> with descendantsbool contact_points_include_descendants;           // TODO: Delete with `wb_supervisor_node_get_contact_point`bool static_balance;double *solid_velocity;  // double[6] (linear[3] + angular[3])bool is_proto;bool is_proto_internal;  // FALSE if the node is visible in the scene tree, otherwise TRUEWbNodeRef parent_proto;int tag;WbNodeRef next;
} WbNodeStruct;

根据https://github.com/cyberbotics/webots/blob/master/docs/reference/supervisor.md中的说明:

The wb_supervisor_node_add_force function adds a force to the Solid node at its center of mass, the relative argument defines if the force is expressed in world coordinate system (relative set to false) or relatively to the node (relative set to true). The wb_supervisor_node_add_force_with_offset function adds a force to the Solid node at the location (expressed in the node coordinate system) defined by the offset argument. The wb_supervisor_node_add_torque function adds a torque to the Solid node.

注意要把Robot节点下面的Supervisor项由FALSE改为TRUE. 在某个零件上添加一个恒定的的示例代码如下:

webots::Supervisor* robot = webots::Supervisor::getSupervisorInstance();
webots::Node* BaseNode = robot->getFromDef("Base_link_00");
const double RopeForce[3] = { 500.0, 500.0, 500 };
const double RopeForceOffset[3] = { -30.0, 0.0, 0.0 };
//BaseNode->addForce(RopeForce, false);
BaseNode->addForceWithOffset(RopeForce, RopeForceOffset, false);

下面顺便提供用代码修改Robot的translation和rotation的方法,

webots::Supervisor* robot = webots::Supervisor::getSupervisorInstance();
webots::Node* robotNode = robot->getSelf();
webots::Field* translationField = robotNode->getField("translation");
webots::Field* rotationField = robotNode->getField("rotation");
const double translationArray[3] = {-39, 0, 0.9};
translationField->setSFVec3f(translationArray);
robot->step(32);
const double rotationArray[4] = {0.0, 1.0, 0.0, pi/2.0};
rotationField->setSFRotation(rotationArray);
robot->step(32);

当然,你也可以直接调用Webots所采用的开源物理引擎ODE中的函数实现加(矩)的效果,在webots\include\ode\ode\objects.h中,可以看到如下声明:

/*** @brief Add force at centre of mass of body in absolute coordinates.* @ingroup bodies*/
ODE_API void dBodyAddForce            (dBodyID, dReal fx, dReal fy, dReal fz);/*** @brief Add torque at centre of mass of body in absolute coordinates.* @ingroup bodies*/
ODE_API void dBodyAddTorque           (dBodyID, dReal fx, dReal fy, dReal fz);/*** @brief Add force at centre of mass of body in coordinates relative to body.* @ingroup bodies*/
ODE_API void dBodyAddRelForce         (dBodyID, dReal fx, dReal fy, dReal fz);/*** @brief Add torque at centre of mass of body in coordinates relative to body.* @ingroup bodies*/
ODE_API void dBodyAddRelTorque        (dBodyID, dReal fx, dReal fy, dReal fz);/*** @brief Add force at specified point in body in global coordinates.* @ingroup bodies*/
ODE_API void dBodyAddForceAtPos       (dBodyID, dReal fx, dReal fy, dReal fz,dReal px, dReal py, dReal pz);
/*** @brief Add force at specified point in body in local coordinates.* @ingroup bodies*/
ODE_API void dBodyAddForceAtRelPos    (dBodyID, dReal fx, dReal fy, dReal fz,dReal px, dReal py, dReal pz);
/*** @brief Add force at specified point in body in global coordinates.* @ingroup bodies*/
ODE_API void dBodyAddRelForceAtPos    (dBodyID, dReal fx, dReal fy, dReal fz,dReal px, dReal py, dReal pz);
/*** @brief Add force at specified point in body in local coordinates.* @ingroup bodies*/
ODE_API void dBodyAddRelForceAtRelPos (dBodyID, dReal fx, dReal fy, dReal fz,dReal px, dReal py, dReal pz);

不过要注意,根据ODE官网(http://ode.org/wiki/index.php/Manual)说明,加在物体上的在每个时间步之后都会被置0,这似乎意味着,想要在物体上加一个类似于重的恒,每个时间步都得调用加函数。我还没仔细研究,以后研究清楚了再来更新文章。

Add forces to bodies (absolute or relative coordinates). The forces are accumulated on to each body, and the accumulators are zeroed after each time step.

The ...RelForce and ...RelTorque functions take force vectors that are relative to the body's own frame of reference.

The ...ForceAtPos and ...ForceAtRelPos functions take an extra position vector (in global or body-relative coordinates respectively) that specifies the point at which the force is applied. All other functions apply the force at the center of mass.

还有一招,Help > How do I apply a force or a torque to an object? 这样加上去的似乎也只在一个时间步生效,立刻就会消失。

友情提示:有道词典的“取词”功能会导致webots卡死,在我的电脑(Win 11, Webots R2023b,有道10.2.4)上可以100%复现。


http://www.ppmy.cn/ops/13786.html

相关文章

润石科技(RUNIC)汽车电子应用方案和物料选型

一、润石科技(RUNIC)简介 江苏润石科技有限公司是一家专注于高性能、高品质模拟/混合信号集成电路研发和销售的高科技半导体设计公司。公司主要产品线分为两类:信号链和电源管理,其中信号链包含运算放大器、比较器、模拟开关、数…

C++的初步知识——命名空间,缺省参数,重载函数

C 首先写一段代码&#xff1a; #include <stdio.h>int main() {printf("Hello world\n");return 0; }这段C语言代码在cpp文件中仍可运行。我们了解C是兼容C语言的&#xff0c;C的关键字中就包含了C语言的关键字和自身的关键字。关于关键字&#xff0c;我们简…

(救命)Kali Linux或者其他linux系统的触控板右键按下没反应,失效的解决办法

我每次安装kali的时候都会选择gnome桌面&#xff0c;每次安装好右键都是禁用的&#xff0c;按下和左键效果一样&#xff0c;每次都得去调鼠标右键&#xff0c;原来就不好找到那个选项&#xff0c;这次踏马居然连那个选项都没了&#xff0c;如果你去网上找教程你会发现网上根本没…

[Android]使用CompositionLocal隐式传值

1.相关概念 CompositionLocal 是定义数据的方式&#xff0c;而 CompositionLocalProvider 是在 Compose UI 树中传递这些数据的工具。二者合作&#xff0c;为 Compose 应用提供了一个强大的状态和数据流管理机制&#xff0c;使得数据可以在组件间按需传递&#xff0c;而无需通…

视频评价工具AVQT介绍

AVQT介绍 AVQT(Advanced Video Quality Tool)是一个用于评估压缩视频感知质量的工具。它通过模拟人类如何评价压缩视频的质量来进行工作。AVQT 是是苹果在 WWDC 21 上推出的一款评估视频感知质量的工具。AVQT可以用于计算视频的帧级和片段级得分,其中片段通常持续几秒钟。这…

Go源码--Strings库

1. 简介 strings库 存储了 一些针对 字符串的具体操作 其 代码短小精悍 可以学习到很多编程的思路 尤其是 涉及到字符串使用性能的方面&#xff0c;其源码库有好多的优秀案例可以学习。向强者对齐不一定成为强者&#xff0c;但向弱者对齐一定变为弱者。 介绍思路是先介绍 stri…

ChatGPT实战100例 - (18) 用事件风暴玩转DDD

文章目录 ChatGPT实战100例 - (18) 用事件风暴玩转DDD一、标准流程二、定义目标和范围三、准备工具和环境四、列举业务事件五、 组织和排序事件六、确定聚合并引入命令七、明确界限上下文八、识别领域事件和领域服务九、验证和修正模型十、生成并验证软件设计十一、总结 ChatGP…

Java中的异常和错误

Exception ArrayIndexOutOfBoundsException&#xff08;数组下标越界&#xff09; NullPionterException&#xff08;空指针异常&#xff09; ArithmeticException&#xff08;算数异常&#xff09; MissingResourceException&#xff08;丢失资源&#xff09; ClassNotFo…