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

server/2024/9/25 17:22:23/


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/server/14134.html

相关文章

微信小程序4~6章总结

目录 第四章 页面组件总结 4.1 组件的定义及属性 4.2 容器视图组件 4.2.1 view 4.2.2 scroll-view 4.2.3 swiper 4.3 基础内容组件 4.3.1 icon ​编辑 4.3.2 text 4.3.3 progress ​编辑 4.4 表单组件 4.4.1 button 4.4.2 radio 4.4.3 checkbox 4.4.4 switch …

Centos7 ElasticSearch集群搭建

1. 服务器环境配置 1.1 配置hosts文件 3台服务器都要执行 vim /etc/hosts; # 将以下内容写入3台服务器hosts文件 192.168.226.148 es001 192.168.226.149 es002 192.168.226.150 es003 1.2 关闭防火墙 3台服务器都要执行 systemctl stop firewalld; systemctl disable…

枚举(enum)/共用体(union)/结构体(struct)---详解

前言 C语言包含内置类型和自定义类型。 其实C语言中有内置类型,包含:char,short,int,long,long long,float,double,long double ,这些是C语言本身支持的现成的类型。 但仅仅只有内置类型是远远不够的,在描述一个复杂对象是无法使用内置类型来…

《从零开始的Java世界》10File类与IO流

《从零开始的Java世界》系列主要讲解Javase部分,从最简单的程序设计到面向对象编程,再到异常处理、常用API的使用,最后到注解、反射,涵盖Java基础所需的所有知识点。学习者应该从学会如何使用,到知道其实现原理全方位式…

芯科科技大大简化面向无电池物联网的能量采集产品的开发

芯科科技推出其迄今最高能量效率且支持能量采集功能的无线SoC 中国,北京 – 2024年4月22日 – 致力于以安全、智能无线连接技术,建立更互联世界的全球领导厂商Silicon Labs(亦称“芯科科技”,NASDAQ:SLAB)…

20240425,模板

感觉比学C那会好了点,不怎么出现照着抄但是就是不能跑的情况,哭死,但是学的顺又不复习,第二天跟没学一样,笑死,要是能给我开个过目不忘的挂,爽的不要不要的 呵呵呵蠢女人,别忘了你C的…

uniapp配置了pages.json 的 tabbar 国际化,小程序切换语言没有实时切换

如上图,按照uniapp官方文档配置了tabbar的国际化 但是微信小程序实时切换语言没有实时刷新 解决方案: 在App.vue中加入以下代码: 在onLaunch中执行方法即可

25计算机考研院校数据分析 | 四川大学

四川大学(Sichuan University)简称“川大”,由中华人民共和国教育部直属,中央直管副部级建制,是世界一流大学建设高校、985工程”、"211工程"重点建设的高水平综合性全国重点大学,入选”2011计划"、"珠峰计划…