Gazebo | Ignition | Community
Ask Your Question

Light message not working

asked 2017-10-05 22:39:18 -0500

Ash_100 gravatar image

i am trying to load a light using light message but it is not working. Here is the code

namespace gazebo { class WorldPluginTutorial : public WorldPlugin { public: WorldPluginTutorial() : WorldPlugin() { printf("Hello World!\n"); }

public: void Load(physics::WorldPtr _world, sdf::ElementPtr _sdf)
          printf("Hello World Load!\n");
      this->model = _world;
          printf("%s\n", (this->model->GetName()).c_str());

          // Initialize ros, if it has not already bee initialized.
        if (!ros::isInitialized())
            int argc = 0;
            char **argv = NULL;
            ros::init(argc, argv, "gazebo_client",

            // Create our ROS node. This acts in a similar manner to
        // the Gazebo node
        this->rosNode.reset(new ros::NodeHandle("gazebo_client"));

        // Create a named topic, and subscribe to it.
        ros::SubscribeOptions so =
            "/" + this->model->GetName() + "/vel_cmd",
            boost::bind(&WorldPluginTutorial::OnRosMsg, this, _1),
            ros::VoidPtr(), &this->rosQueue);
        this->rosSub = this->rosNode->subscribe(so);

            // Spin up the queue helper thread.
        this->rosQueueThread =
            std::thread(std::bind(&WorldPluginTutorial::QueueThread, this));

    public: void OnRosMsg(const std_msgs::Float32ConstPtr &_msg)
    printf("Entered WorldPluginTutorial::OnRosMsg\n");
    //physics::WorldPtr w = this->model->GetWorld();
    //unsigned int i = this->model->LightCount();
    sdf::SDF point;
    point.SetFromString("<?xml version='1.0' ?>\
        <sdf version='1.5'>\
        <!-- Light Source -->\
        <light name='point_light' type='point'>\
                <pose frame=''>2 -2 1 0 -0 0</pose>\
                <diffuse>0.5 0.5 0.5 1</diffuse>\
                <specular>0.1 0.1 0.1 1</specular>\
                <direction>0 0 -1</direction>\
    sdf::ElementPtr light = point.Root()->GetElement("light");
    msgs::Light msg = gazebo::msgs::LightFromSDF(light);
    transport::NodePtr node(new transport::Node());


    transport::PublisherPtr lightPub 
        = node->Advertise<msgs::Light>("~/light");


    unsigned int i = this->model->Lights().size();
        printf("Light count = %d\n", i);    

/// \brief ROS helper function that processes messages
    private: void QueueThread()
        static const double timeout = 0.01;
        while (this->rosNode->ok())
    private: physics::WorldPtr model;
private: std::unique_ptr<ros::NodeHandle> rosNode;
    /// \brief A ROS subscriber
    private: ros::Subscriber rosSub;

    /// \brief A ROS callbackqueue that helps process messages
    private: ros::CallbackQueue rosQueue;

    /// \brief A thread the keeps running the rosQueue
    private: std::thread rosQueueThread;

}; GZ_REGISTER_WORLD_PLUGIN(WorldPluginTutorial) }

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-10-06 09:30:02 -0500

chapulina gravatar image

Could you try advertising to ~/factory/light instead? Also, when you run Gazebo in verbose mode gazebo --verbose, do you see any errors?

edit flag offensive delete link more


Thanks ~/factory/light works, but still there is an issue. Light is added but not visible. When I send the same message next time I get an error the "Light [point_light] already exists.", however, the first light gets visible in Gazebo then. How can refresh the world instantaneously to view the light?

Ash_100 gravatar imageAsh_100 ( 2017-10-06 13:59:02 -0500 )edit

Question Tools

1 follower


Asked: 2017-10-05 22:39:18 -0500

Seen: 1,049 times

Last updated: Oct 06 '17