Spawn a Matrix of Objects over a Heightmap
I'm trying to spawn a matrix of objects to create a field. Ideally this could be done while the model is running so I thought using a plugin was the right strategy. I successfully modified this plugin http://gazebosim.org/tutorials?tut=plugins_world to place a model at the correct height, but I cannot figure out how to change the name so I can spawn several of the same model. I used the factory portion of the tutorial (option 3) as it will be most convenient to store models in a directory and then call them and place them at the correct location. The main code is copied below. I tried several iterations of using msg.setclonemutable_name and some other options with no luck.
// Insert model from file via message passing.
{
// Create a new transport node
transport::NodePtr node(new transport::Node());
// Initialize the node with the world name
node->Init(_parent->Name());
// Create a publisher on the ~/factory topic
transport::PublisherPtr factoryPub = node->Advertise<msgs::Factory>("~/factory");
// Create the messagecatkin
msgs::Factory msg;
msg.set_sdf_filename("model://Cylinder2/");
physics::WorldPtr world = _parent;
physics::ModelPtr model = world->ModelByName("heightmap");
physics::CollisionPtr collision = model->GetLink("link")->GetCollision("collision");
physics::HeightmapShapePtr heightmap =
boost::dynamic_pointer_cast<physics::HeightmapShape>(collision->GetShape());
/* Spawn a matrix of cylinders, doesn't work without changing name.*/
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
// getting the height :
float z_value = heightmap->GetHeight(i, j);
// Pose to initialize the model to
msgs::Set(msg.mutable_pose(),
ignition::math::Pose3d(ignition::math::Vector3d(i, j, z_value+0.5), ignition::math::Quaterniond(0, 0, 0)));
// Send the message
factoryPub->Publish(msg);
}
}
Asked by jonamu on 2020-02-25 11:55:01 UTC
Answers
I'm not an expert in this. But I feel that you are modifying only the collision part of the heightmap. You may need to do the same for visual part of the heightmap as well to see the changes in simulator
Asked by lakshmi on 2020-07-02 16:19:35 UTC
Comments
Hi, I am doing the same thing. Have you found any solution to the problem? Thx
Asked by XLW on 2021-10-19 22:32:31 UTC