本文主要是介绍webots学习记录8:R2023b如何在某个零件上添加一个恒定的力(矩),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在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);
参考链接:
https://stackoverflow.com/questions/66787530/apply-a-user-defined-force-on-a-solid-node-in-webots?
这篇关于webots学习记录8:R2023b如何在某个零件上添加一个恒定的力(矩)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!