本文整理汇总了C++中physics::ModelPtr::GetWorld方法的典型用法代码示例。如果您正苦于以下问题:C++ ModelPtr::GetWorld方法的具体用法?C++ ModelPtr::GetWorld怎么用?C++ ModelPtr::GetWorld使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类physics::ModelPtr
的用法示例。
在下文中一共展示了ModelPtr::GetWorld方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Load
void DCMotor::Load(physics::ModelPtr model, sdf::ElementPtr sdf) {
this->model = model;
signal = 0;
// Parse SDF properties
joint = model->GetJoint(sdf->Get<std::string>("joint"));
if (sdf->HasElement("topic")) {
topic = sdf->Get<std::string>("topic");
} else {
topic = "~/"+sdf->GetAttribute("name")->GetAsString();
}
if (sdf->HasElement("multiplier")) {
multiplier = sdf->Get<double>("multiplier");
} else {
multiplier = 1;
}
gzmsg << "Initializing motor: " << topic << " joint=" << joint->GetName()
<< " multiplier=" << multiplier << std::endl;
// Connect to Gazebo transport for messaging
std::string scoped_name = model->GetWorld()->GetName()+"::"+model->GetScopedName();
boost::replace_all(scoped_name, "::", "/");
node = transport::NodePtr(new transport::Node());
node->Init(scoped_name);
sub = node->Subscribe(topic, &DCMotor::Callback, this);
// Connect to the world update event.
// This will trigger the Update function every Gazebo iteration
updateConn = event::Events::ConnectWorldUpdateBegin(boost::bind(&DCMotor::Update, this, _1));
}
示例2: Load
void Rangefinder::Load(physics::ModelPtr model, sdf::ElementPtr sdf) {
this->model = model;
// Parse SDF properties
sensor = std::dynamic_pointer_cast<sensors::SonarSensor>(
sensors::get_sensor(sdf->Get<std::string>("sensor")));
if (sdf->HasElement("topic")) {
topic = sdf->Get<std::string>("topic");
} else {
topic = "~/" + sdf->GetAttribute("name")->GetAsString();
}
gzmsg << "Initializing rangefinder: " << topic << " sensor=" << sensor->Name()
<< std::endl;
// Connect to Gazebo transport for messaging
std::string scoped_name =
model->GetWorld()->GetName() + "::" + model->GetScopedName();
boost::replace_all(scoped_name, "::", "/");
node = transport::NodePtr(new transport::Node());
node->Init(scoped_name);
pub = node->Advertise<msgs::Float64>(topic);
// Connect to the world update event.
// This will trigger the Update function every Gazebo iteration
updateConn = event::Events::ConnectWorldUpdateBegin(
boost::bind(&Rangefinder::Update, this, _1));
}
示例3: Load
void Potentiometer::Load(physics::ModelPtr model, sdf::ElementPtr sdf) {
this->model = model;
// Parse SDF properties
joint = model->GetJoint(sdf->Get<std::string>("joint"));
if (sdf->HasElement("topic")) {
topic = sdf->Get<std::string>("topic");
} else {
topic = "~/"+sdf->GetAttribute("name")->GetAsString();
}
if (sdf->HasElement("units")) {
radians = sdf->Get<std::string>("units") != "degrees";
} else {
radians = true;
}
gzmsg << "Initializing potentiometer: " << topic << " joint=" << joint->GetName()
<< " radians=" << radians << std::endl;
// Connect to Gazebo transport for messaging
std::string scoped_name = model->GetWorld()->GetName()+"::"+model->GetScopedName();
boost::replace_all(scoped_name, "::", "/");
node = transport::NodePtr(new transport::Node());
node->Init(scoped_name);
pub = node->Advertise<msgs::Float64>(topic);
// Connect to the world update event.
// This will trigger the Update function every Gazebo iteration
updateConn = event::Events::ConnectWorldUpdateBegin(boost::bind(&Potentiometer::Update, this, _1));
}
示例4: Load
////////////////////////////////////////////////////////////////////////////////
// Load the controller
void GazeboRosForce::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
// Get the world name.
this->world_ = _model->GetWorld();
// load parameters
this->robot_namespace_ = "";
if (_sdf->HasElement("robotNamespace"))
this->robot_namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>() + "/";
if (!_sdf->HasElement("bodyName"))
{
ROS_FATAL("force plugin missing <bodyName>, cannot proceed");
return;
}
else
this->link_name_ = _sdf->GetElement("bodyName")->Get<std::string>();
this->link_ = _model->GetLink(this->link_name_);
if (!this->link_)
{
ROS_FATAL("gazebo_ros_force plugin error: link named: %s does not exist\n",this->link_name_.c_str());
return;
}
if (!_sdf->HasElement("topicName"))
{
ROS_FATAL("force plugin missing <topicName>, cannot proceed");
return;
}
else
this->topic_name_ = _sdf->GetElement("topicName")->Get<std::string>();
// Make sure the ROS node for Gazebo has already been initialized
if (!ros::isInitialized())
{
ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
<< "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
return;
}
this->rosnode_ = new ros::NodeHandle(this->robot_namespace_);
// Custom Callback Queue
ros::SubscribeOptions so = ros::SubscribeOptions::create<geometry_msgs::Wrench>(
this->topic_name_,1,
boost::bind( &GazeboRosForce::UpdateObjectForce,this,_1),
ros::VoidPtr(), &this->queue_);
this->sub_ = this->rosnode_->subscribe(so);
// Custom Callback Queue
this->callback_queue_thread_ = boost::thread( boost::bind( &GazeboRosForce::QueueThread,this ) );
// New Mechanism for Updating every World Cycle
// Listen to the update event. This event is broadcast every
// simulation iteration.
this->update_connection_ = event::Events::ConnectWorldUpdateBegin(
boost::bind(&GazeboRosForce::UpdateChild, this));
}
示例5: Load
public: void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf){
world = _model->GetWorld();
model = _model;
#if(PRINT_DEBUG)
cout << "Loading the contact forces plugin" << endl;
#endif
#if(PRINT_SENSORS)
cout << "Listing all sensors: " << endl;
vector<sensors::SensorPtr> sensors = sensors::SensorManager::Instance()->GetSensors();
for(unsigned int i = 0; i < sensors.size(); ++i){
cout << sensors[i]->GetScopedName() << endl;
}
cout << endl;
#endif
for(unsigned int i = 0; i < boost::size(contacts); ++i){
sensors::SensorPtr sensor = sensors::SensorManager::Instance()->GetSensor(world->GetName() + "::" + _model->GetScopedName()
+ "::" + contacts[i]);
if(sensor == nullptr){
cout << "Could not find sensor " << contacts[i] << endl;
continue;
}
sensor->SetActive(true);
sensorConnections.push_back(sensor->ConnectUpdated(
boost::bind(&ContactForcesPlugin::updateContacts, this, sensor, contacts[i])));
}
connection = event::Events::ConnectWorldUpdateBegin(boost::bind(&ContactForcesPlugin::worldUpdate, this));
}
示例6: Load
public: void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf){
world = _model->GetWorld();
model = _model;
#if(PRINT_DEBUG)
cout << "Loading the angular momentum over time plugin" << endl;
#endif
connection = event::Events::ConnectWorldUpdateBegin(boost::bind(&AngularMomentumOverTimePlugin::worldUpdate, this));
// Get the name of the folder to store the result in
const char* resultsFolder = std::getenv("RESULTS_FOLDER");
if(resultsFolder == nullptr){
cout << "Results folder not set. Using current directory." << endl;
resultsFolder = "./";
}
const string resultsFileName = string(resultsFolder) + "/" + "angular_momentum.csv";
outputCSV.open(resultsFileName, ios::out);
assert(outputCSV.is_open());
writeHeader(outputCSV);
math::Vector3 angularMomentum;
for (unsigned int i = 0; i < boost::size(links); ++i) {
const physics::LinkPtr link = model->GetLink(links[i]);
angularMomentum += link->GetWorldInertiaMatrix() * link->GetWorldAngularVel();
}
// Write out t0
outputCSV << world->GetSimTime().Double() << ", " << angularMomentum.x / 10.0
<< ", " << angularMomentum.y / 10.0 << ", " << angularMomentum.z / 10.0 << endl;
}
示例7: Load
void Servo::Load(physics::ModelPtr model, sdf::ElementPtr sdf) {
this->model = model;
signal = 0;
// parse SDF Properries
joint = model->GetJoint(sdf->Get<std::string>("joint"));
if (sdf->HasElement("topic")) {
topic = sdf->Get<std::string>("topic");
} else {
topic = "~/" + sdf->GetAttribute("name")->GetAsString();
}
if (sdf->HasElement("torque")) {
torque = sdf->Get<double>("torque");
} else {
torque = 5;
}
gzmsg << "initializing servo: " << topic << " joint=" << joint->GetName()
<< " torque=" << torque << std::endl;
// Connect to Gazebo transport for messaging
std::string scoped_name =
model->GetWorld()->GetName() + "::" + model->GetScopedName();
boost::replace_all(scoped_name, "::", "/");
node = transport::NodePtr(new transport::Node());
node->Init(scoped_name);
sub = node->Subscribe(topic, &Servo::Callback, this);
// connect to the world update event
// this will call update every iteration
updateConn = event::Events::ConnectWorldUpdateBegin(
boost::bind(&Servo::Update, this, _1));
}
示例8: Load
void PneumaticPiston::Load(physics::ModelPtr model, sdf::ElementPtr sdf) {
this->model = model;
signal = 0;
// Parse SDF properties
joint = model->GetJoint(sdf->Get<std::string>("joint"));
if (sdf->HasElement("topic")) {
topic = sdf->Get<std::string>("topic");
} else {
topic = "~/"+sdf->GetAttribute("name")->GetAsString();
}
forward_force = sdf->Get<double>("forward-force");
reverse_force = sdf->Get<double>("reverse-force");
if (sdf->HasElement("direction") && sdf->Get<std::string>("direction") == "reversed") {
forward_force = -forward_force;
reverse_force = -reverse_force;
}
gzmsg << "Initializing piston: " << topic << " joint=" << joint->GetName()
<< " forward_force=" << forward_force
<< " reverse_force=" << reverse_force<< std::endl;
// Connect to Gazebo transport for messaging
std::string scoped_name = model->GetWorld()->GetName()+"::"+model->GetScopedName();
boost::replace_all(scoped_name, "::", "/");
node = transport::NodePtr(new transport::Node());
node->Init(scoped_name);
sub = node->Subscribe(topic, &PneumaticPiston::Callback, this);
// Connect to the world update event.
// This will trigger the Update function every Gazebo iteration
updateConn = event::Events::ConnectWorldUpdateBegin(boost::bind(&PneumaticPiston::Update, this, _1));
}
示例9: Load
void LimitSwitch::Load(physics::ModelPtr model, sdf::ElementPtr sdf) {
this->model = model;
// Parse SDF properties
if (sdf->HasElement("topic")) {
topic = sdf->Get<std::string>("topic");
} else {
topic = "~/" + sdf->GetAttribute("name")->GetAsString();
}
std::string type = sdf->Get<std::string>("type");
gzmsg << "Initializing limit switch: " << topic << " type=" << type
<< std::endl;
if (type == "internal") {
ls = new InternalLimitSwitch(model, sdf);
} else if (type == "external") {
ls = new ExternalLimitSwitch(sdf);
} else {
gzerr << "WARNING: unsupported limit switch type " << type;
}
// Connect to Gazebo transport for messaging
std::string scoped_name =
model->GetWorld()->GetName() + "::" + model->GetScopedName();
boost::replace_all(scoped_name, "::", "/");
node = transport::NodePtr(new transport::Node());
node->Init(scoped_name);
pub = node->Advertise<msgs::Bool>(topic);
// Connect to the world update event.
// This will trigger the Update function every Gazebo iteration
updateConn = event::Events::ConnectWorldUpdateBegin(
boost::bind(&LimitSwitch::Update, this, _1));
}
示例10: Load
void StatePlugin::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
GZ_ASSERT(_model != NULL, "Received NULL model pointer");
this->model = _model;
physics::WorldPtr world = _model->GetWorld();
GZ_ASSERT(world != NULL, "Model is in a NULL world");
this->physicsEngine = world->GetPhysicsEngine();
GZ_ASSERT(this->physicsEngine != NULL, "Physics engine was NULL");
GZ_ASSERT(_sdf != NULL, "Received NULL SDF pointer");
this->sdf = _sdf;
if (this->sdf->HasElement("model_offset"))
{
this->modelOffset = this->sdf->Get<math::Vector3>("model_offset");
}
else
{
this->modelOffset = math::Vector3(0.0, 0.0, 0.0);
}
// Make sure the ROS node for Gazebo has already been initialized
GZ_ASSERT(ros::isInitialized(), "ROS not initialized");
this->refSub = nh.subscribe("lqrrt/ref", 1, &StatePlugin::PoseRefUpdate, this);
}
示例11: Load
////////////////////////////////////////////////////////////////////////////////
// Load the controller
void GazeboTreasure::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
model_ = _model;
world_ = _model->GetWorld();
link_ = _model->GetLink();
link_name_ = link_->GetName();
namespace_.clear();
vel_x = vel_y = vel_yaw = 0;
static_object_ = false;
last_time_ = world_->GetSimTime();
terminated_ = false;
// load parameters from sdf
if (_sdf->HasElement("robotNamespace")) namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
if (_sdf->HasElement("bodyName") && _sdf->GetElement("bodyName")->GetValue())
{
link_name_ = _sdf->GetElement("bodyName")->Get<std::string>();
link_ = _model->GetLink(link_name_);
}
if (_sdf->HasElement("staticObject") && _sdf->GetElement("staticObject")->GetValue())
{
static_object_ = _sdf->GetElement("staticObject")->Get<std::string>() == "true"?true:false;
}
// boost::random::mt19937 rng;
boost::random::random_device rng;
boost::random::uniform_int_distribution<> x_rand(-40, 40);
boost::random::uniform_int_distribution<> y_rand(-20, 20);
double x = x_rand(rng);
double y = y_rand(rng);
model_->SetLinkWorldPose(math::Pose(x, y, 0.2, 0, 0, 0), link_);
if (!link)
{
ROS_FATAL("gazebo_ros_baro plugin error: bodyName: %s does not exist\n", link_name_.c_str());
return;
}
// Make sure the ROS node for Gazebo has already been initialized
if (!ros::isInitialized())
{
ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
<< "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
return;
}
node_handle_ = new ros::NodeHandle(namespace_);
pub_score_ = node_handle_->advertise<std_msgs::String>("score", 1, true); // set latch true
ros::NodeHandle param_handle(*node_handle_, "controller");
update_connection_ = event::Events::ConnectWorldUpdateBegin(
boost::bind(&GazeboTreasure::Update, this));
}
示例12: Load
public: void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf){
world = _model->GetWorld();
model = _model;
trunk = model->GetLink("trunk");
#if(PRINT_DEBUG)
cout << "Loading the velocity over time plugin" << endl;
#endif
connection = event::Events::ConnectWorldUpdateBegin(boost::bind(&VelocityOverTimePlugin::worldUpdate, this));
// Get the name of the folder to store the result in
const char* resultsFolder = std::getenv("RESULTS_FOLDER");
if(resultsFolder == nullptr){
cout << "Results folder not set. Using current directory." << endl;
resultsFolder = "./";
}
{
const string resultsXFileName = string(resultsFolder) + "/" + "velocities_x.csv";
bool exists = boost::filesystem::exists(resultsXFileName);
outputCSVX.open(resultsXFileName, ios::out | ios::app);
assert(outputCSVX.is_open());
if (!exists) {
writeHeader(outputCSVX);
}
}
{
const string resultsYFileName = string(resultsFolder) + "/" + "velocities_y.csv";
bool exists = boost::filesystem::exists(resultsYFileName);
outputCSVY.open(resultsYFileName, ios::out | ios::app);
assert(outputCSVY.is_open());
if (!exists) {
writeHeader(outputCSVY);
}
}
{
const string resultsZFileName = string(resultsFolder) + "/" + "velocities_z.csv";
bool exists = boost::filesystem::exists(resultsZFileName);
outputCSVZ.open(resultsZFileName, ios::out | ios::app);
assert(outputCSVZ.is_open());
if (!exists) {
writeHeader(outputCSVZ);
}
}
// Write out t0
outputCSVX << "Velocity X (m/s), " << trunk->GetWorldCoGLinearVel().x << ", ";
outputCSVY << "Velocity Y (m/s), " << trunk->GetWorldCoGLinearVel().y << ", ";
outputCSVZ << "Velocity Z (m/s), " << trunk->GetWorldCoGLinearVel().z << ", ";
}
示例13: Load
void GazeboRosJointStatePublisher::Load ( physics::ModelPtr _parent, sdf::ElementPtr _sdf ) {
// Store the pointer to the model
this->parent_ = _parent;
this->world_ = _parent->GetWorld();
this->robot_namespace_ = parent_->GetName ();
if ( !_sdf->HasElement ( "robotNamespace" ) ) {
ROS_INFO_NAMED("joint_state_publisher", "GazeboRosJointStatePublisher Plugin missing <robotNamespace>, defaults to \"%s\"",
this->robot_namespace_.c_str() );
} else {
this->robot_namespace_ = _sdf->GetElement ( "robotNamespace" )->Get<std::string>();
if ( this->robot_namespace_.empty() ) this->robot_namespace_ = parent_->GetName ();
}
if ( !robot_namespace_.empty() ) this->robot_namespace_ += "/";
rosnode_ = boost::shared_ptr<ros::NodeHandle> ( new ros::NodeHandle ( this->robot_namespace_ ) );
if ( !_sdf->HasElement ( "jointName" ) ) {
ROS_ASSERT ( "GazeboRosJointStatePublisher Plugin missing jointNames" );
} else {
sdf::ElementPtr element = _sdf->GetElement ( "jointName" ) ;
std::string joint_names = element->Get<std::string>();
boost::erase_all ( joint_names, " " );
boost::split ( joint_names_, joint_names, boost::is_any_of ( "," ) );
}
this->update_rate_ = 100.0;
if ( !_sdf->HasElement ( "updateRate" ) ) {
ROS_WARN_NAMED("joint_state_publisher", "GazeboRosJointStatePublisher Plugin (ns = %s) missing <updateRate>, defaults to %f",
this->robot_namespace_.c_str(), this->update_rate_ );
} else {
this->update_rate_ = _sdf->GetElement ( "updateRate" )->Get<double>();
}
// Initialize update rate stuff
if ( this->update_rate_ > 0.0 ) {
this->update_period_ = 1.0 / this->update_rate_;
} else {
this->update_period_ = 0.0;
}
last_update_time_ = this->world_->GetSimTime();
for ( unsigned int i = 0; i< joint_names_.size(); i++ ) {
joints_.push_back ( this->parent_->GetJoint ( joint_names_[i] ) );
ROS_INFO_NAMED("joint_state_publisher", "GazeboRosJointStatePublisher is going to publish joint: %s", joint_names_[i].c_str() );
}
ROS_INFO_NAMED("joint_state_publisher", "Starting GazeboRosJointStatePublisher Plugin (ns = %s)!, parent name: %s", this->robot_namespace_.c_str(), parent_->GetName ().c_str() );
tf_prefix_ = tf::getPrefixParam ( *rosnode_ );
joint_state_publisher_ = rosnode_->advertise<sensor_msgs::JointState> ( "joint_states",1000 );
last_update_time_ = this->world_->GetSimTime();
// Listen to the update event. This event is broadcast every
// simulation iteration.
this->updateConnection = event::Events::ConnectWorldUpdateBegin (
boost::bind ( &GazeboRosJointStatePublisher::OnUpdate, this, _1 ) );
}
示例14: Load
////////////////////////////////////////////////////////////////////////////////
// Load the controller
void GazeboPanel::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
gzwarn << "The GazeboPanel plugin is DEPRECATED in ROS hydro." << std::endl;
model_ = _model;
world_ = _model->GetWorld();
link_ = _model->GetLink();
link_name_ = link_->GetName();
namespace_.clear();
terminated_ = false;
// load parameters from sdf
if (_sdf->HasElement("robotNamespace")) namespace_ = _sdf->GetElement("robotNamespace")->Get<std::string>();
if (_sdf->HasElement("bodyName") && _sdf->GetElement("bodyName")->GetValue())
{
link_name_ = _sdf->GetElement("bodyName")->Get<std::string>();
link_ = _model->GetLink(link_name_);
}
if (_sdf->HasElement("jointName") && _sdf->GetElement("jointName")->GetValue())
{
std::string joint_name_ = _sdf->GetElement("jointName")->Get<std::string>();
joint_ = _model->GetJoint(joint_name_);
}
else
{
joint_ = _model->GetJoint("stem_joint");
}
if (!link)
{
ROS_FATAL("gazebo_ros_baro plugin error: bodyName: %s does not exist\n", link_name_.c_str());
return;
}
// Make sure the ROS node for Gazebo has already been initialized
if (!ros::isInitialized())
{
ROS_FATAL_STREAM("A ROS node for Gazebo has not been initialized, unable to load plugin. "
<< "Load the Gazebo system plugin 'libgazebo_ros_api_plugin.so' in the gazebo_ros package)");
return;
}
node_handle_ = new ros::NodeHandle(namespace_);
pub_score_ = node_handle_->advertise<std_msgs::String>("score", 1, true); // set latch true
pub_time_ = node_handle_->advertise<std_msgs::String>("remaining_time", 1);
ros::NodeHandle param_handle(*node_handle_, "controller");
update_connection_ = event::Events::ConnectWorldUpdateBegin(
boost::bind(&GazeboPanel::Update, this));
}
示例15: Load
void WorldInterface::Load(physics::ModelPtr _model, sdf::ElementPtr _sdf)
{
if (m_network!=0)
return;
m_network=new yarp::os::Network();
if (!yarp::os::Network::checkNetwork(GazeboYarpPlugins::yarpNetworkInitializationTimeout)) {
yError() << "WorldInterface::Load error: yarp network does not seem to be available, is the yarpserver running?";
return;
}
//setting up proxy
m_proxy.attachWorldPointer(_model->GetWorld());
m_proxy.attachModelPointer(_model);
//pass reference to server
m_wi_server.attachWorldProxy(&m_proxy);
//Getting .ini configuration file from sdf
bool configuration_loaded = false;
if (_sdf->HasElement("yarpConfigurationFile")) {
std::string ini_file_name = _sdf->Get<std::string>("yarpConfigurationFile");
std::string ini_file_path = gazebo::common::SystemPaths::Instance()->FindFileURI(ini_file_name);
if (ini_file_path != "" && m_parameters.fromConfigFile(ini_file_path.c_str())) {
yInfo() << "Found yarpConfigurationFile: loading from " << ini_file_path ;
configuration_loaded = true;
}
}
if (!configuration_loaded) {
yError() << "WorldInterface::Load error could not load configuration file";
return;
}
std::string portname=m_parameters.find("name").asString();
int synchronous=m_parameters.find("synchro").asInt32();
if (synchronous)
m_proxy.setSynchronousMode(true);
else
m_proxy.setSynchronousMode(false);
m_rpcport=new yarp::os::RpcServer();
m_rpcport->open(portname);
m_wi_server.yarp().attachAsServer(*m_rpcport);
// Listen to the update event. This event is broadcast every
// simulation iteration.
this->updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind(&WorldInterface::OnUpdate, this, _1));
}