Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For gazebo 7, this method work for me. Assume _parent is model pointer and this code is in Load method of a model plugin

transport::NodePtr node(new transport::Node());
node->Init();
visPub = node->Advertise<msgs::Visual>("~/visual");
std::string link_name = _parent->GetName()+"::container";
visMsg.set_parent_name(link_name);
visMsg.set_name(link_name+"::visual");
visMsg.set_visible(false);
visPub.publish(visMsg);

For gazebo 7, this method work for me. Assume _parent is model pointer and this code is in Load method of a model plugin

transport::NodePtr node(new transport::Node());
node->Init();
visPub = node->Advertise<msgs::Visual>("~/visual");
std::string link_name = _parent->GetName()+"::container";
msgs::Visual visMsg
visMsg.set_parent_name(link_name);
visMsg.set_name(link_name+"::visual");
visMsg.set_visible(false);
visPub.publish(visMsg);

For gazebo 7, this method work for me. Assume _parent is model pointer and this code is in Load method of a model pluginplugin. "::container" is link of the corresponding model that I want to set invisible.

transport::NodePtr node(new transport::Node());
node->Init();
visPub = node->Advertise<msgs::Visual>("~/visual");
std::string link_name = _parent->GetName()+"::container";
msgs::Visual visMsg
visMsg.set_parent_name(link_name);
visMsg.set_name(link_name+"::visual");
visMsg.set_visible(false);
visPub.publish(visMsg);

For gazebo 7, this method work for me. Assume _parent is model pointer and this code is in Load method of a model plugin. "::container" is link of the corresponding model that I want to set invisible.exodia_randomizer.hh

/**
 * @file RandomSpawn.hh
 * @author Achmad Fathoni (fathoni.id@gmail.com)
 * @brief Spawn model at random pose inside box
 * @version 0.1
 * @date 2018-11-20
 * 
 * @copyright Copyright (c) 2018
 * 
 */

#ifndef GAZEBO_PLUGINS_RANDOMPLUGIN_HH_
#define GAZEBO_PLUGINS_RANDOMPLUGIN_HH_

#include "gazebo/gazebo.hh"
#include "gazebo/physics/physics.hh"

#include <chrono>
#include <random>
#include <thread>

#include "ros/ros.h"

#include <std_srvs/Empty.h>
#include <exodia_gazebo/toggleInvisible.h>


namespace gazebo{
    class GAZEBO_VISIBLE exodiaRandomSpawn : public ModelPlugin{
        public:
            exodiaRandomSpawn();
            void Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf);
        private:       
            //ROS
            ros::NodeHandle n;

            ros::ServiceServer toggleInvisible;
            bool toggleInvisibleCb(
                exodia_gazebo::toggleInvisible::Request &req,
                exodia_gazebo::toggleInvisible::Response &res
            );
            physics::ModelPtr parent;

            transport::PublisherPtr visPub;
            transport::NodePtr node(new transport::Node());
node;
            msgs::Visual visMsg;
    };
}

#endif

exodia_randomizer.cpp

/**
 * @file exodiaRandomSpawn.cpp
 * @author Achmad Fathoni (fathoni.id@gmail.com)
 * @brief 
 * @version 0.1
 * @date 2018-12-20
 * 
 * @copyright Copyright (c) 2018
 * 
 */
#include "gazebo_plugins/exodiaRandomSpawn.hh"

namespace gazebo{
    exodiaRandomSpawn::exodiaRandomSpawn():node(new transport::Node())
    {
        ROS_INFO("Activate invisiblitiy");
    };

    void exodiaRandomSpawn::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf){
        parent = _parent;
        ROS_INFO("Activate invisiblitiy");
        toggleInvisible = n.advertiseService(
            "/toggle_invisible", 
            &exodiaRandomSpawn::toggleInvisibleCb, 
            this
        );
        node->Init();
 visPub = node->Advertise<msgs::Visual>("~/visual");
        visMsg.set_visible(true);
    }

    bool exodiaRandomSpawn::toggleInvisibleCb(
        exodia_gazebo::toggleInvisible::Request &req,
        exodia_gazebo::toggleInvisible::Response &res
    ){

        visMsg.set_visible(req.visible);

        for(auto link : parent->GetLinks()){
            std::string link_name = _parent->GetName()+"::container";
msgs::Visual visMsg

                parent->GetName()+"::"+link->GetName();
            ROS_INFO("%s", link_name.c_str());
            visMsg.set_parent_name(link_name);
            visMsg.set_parent_id(parent->GetId());
            visMsg.set_name(link_name+"::visual");
visMsg.set_visible(false);
visPub.publish(visMsg);
            visMsg.set_id(link->GetId());
            visPub->Publish(visMsg);
        }


        return true;
    };

    GZ_REGISTER_MODEL_PLUGIN(exodiaRandomSpawn)
}

toggleinvisible.srv

bool visible
----