title: Why won't Gazebo start ROS?
body: <p>I am working through the Gazebo tutorial to connect a Gazebo sensor to ROS and pass messages. <a href="http://gazebosim.org/tutorials?cat=guided_i&amp;tut=guided_i6" rel="nofollow">http://gazebosim.org/tutorials?cat=guided_i&amp;tut=guided_i6</a></p>
  
  <p>This program builds a Gazebo ModelPlugin object, and initializes ROS from within this object. Then, it creates a ROS node, subscriber, queue, and a standard thread to run the ROS queue. This program works using Gazebo's transport objects, but when I try to add in ROS transport objects (as noted previously), the program does not work. My issue stems from the fact that the roscore nodes (including rosmaster) do not initialize.</p>
  
  <p>My code for the sensor plugin is below. ROS integration begins on line 70:</p>
  
  <pre><code>#ifndef _VELODYNE_PLUGIN_HH_
  #define _VELODYNE_PLUGIN_HH_
  
  #include &lt;gazebo/gazebo.hh&gt;
  #include &lt;gazebo/msgs/msgs.hh&gt;
  #include &lt;gazebo/physics/physics.hh&gt;
  #include &lt;gazebo/transport/transport.hh&gt;
  #include &lt;ros/ros.h&gt;
  #include &lt;ros/callback_queue.h&gt;
  #include &lt;ros/subscribe_options.h&gt;
  #include &lt;thread&gt;
  #include &lt;std_msgs/Float32.h&gt;
  
  namespace gazebo
  {
    /// \brief A plugin to control a Velodyne sensor.
    class VelodynePlugin : public ModelPlugin
    {
      /// \brief Constructor
      public: VelodynePlugin() {}
  
      /// \brief The load function is called by Gazebo when the plugin is
      /// inserted into simulation
      /// \param[in] _model A pointer to the model that this plugin is
      /// attached to.
      /// \param[in] _sdf A pointer to the plugin's SDF element.
      public: virtual void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf) {
              gzwarn &lt;&lt; "HERE";
        if (_model-&gt;GetJointCount() == 0) {
          std::cerr &lt;&lt;
            "Invalid joint count, Velodyne plugin not loaded\n";
        }
  
        // Store the model pointer for convenience.
        this-&gt;model = _model;
  
        // Get the first joint. We are making an assumption about the
        // model having one joint that is the rotational joint.
        this-&gt;joint = _model-&gt;GetJoints()[0];
  
        // Setup a P-controller with a gain of 0.1.
        this-&gt;pid=common::PID(0.1,0,0);
  
        // Apply the P-controller to the joint.
        this-&gt;model-&gt;GetJointController()-&gt;SetVelocityPID(
            this-&gt;joint-&gt;GetScopedName(), this-&gt;pid);
  
        // Default to zero velocity
        double velocity=0;
  
        // Check that the velocity element exists, then read the value
        if (_sdf-&gt;HasElement("velocity"))
          velocity=_sdf-&gt;Get&lt;double&gt;("velocity");
  
        this-&gt;SetVelocity(velocity);
  
        // Create the node
        this-&gt;node = transport::NodePtr(new transport::Node());
        this-&gt;node-&gt;Init(this-&gt;model-&gt;GetWorld()-&gt;GetName());
  
        // Create a topic name
        std::string topicName = "~/" + this-&gt;model-&gt;GetName() +
          "/vel_cmd";
  
        // Subscribe to the topic, and register a callback.
        this-&gt;sub = this-&gt;node-&gt;Subscribe(topicName,
            &amp;VelodynePlugin::OnMsg, this);
  
              // Initialize ros, if it has not already been initialized.
              if (!ros::isInitialized()) {
                  std::cout &lt;&lt; "initializing ros" &lt;&lt; std::endl;
                  int argc = 0;
                  char **argv=NULL;
                  ros::init(argc,argv,"gazebo_client",
                      ros::init_options::NoSigintHandler);
              } else { std::cout &lt;&lt; "NOT initializing ros" &lt;&lt; std::endl; }
  
              // Create our ROS node. This acts in a similar manner to the
              // Gazebo node.
              this-&gt;rosNode.reset(new ros::NodeHandle("gazebo_client"));
  
        // Create a named topic, and subscribe to it.
        ros::SubscribeOptions so =
          ros::SubscribeOptions::create&lt;std_msgs::Float32&gt;(
              "/"+this-&gt;model-&gt;GetName()+"/vel_cmd",
              1,
              boost::bind(&amp;VelodynePlugin::OnRosMsg, this, _1),
              ros::VoidPtr(), &amp;this-&gt;rosQueue);
  
        this-&gt;rosSub = this-&gt;rosNode-&gt;subscribe(so);
  
        // Spin up the queue helper thread
        this-&gt;rosQueueThread = 
          std::thread(std::bind(&amp;VelodynePlugin::QueueThread,this));
      }
  
      /// \brief Set the velocity of the Velodyne
      /// \param[in] _vel New target velocity
      public: void SetVelocity(const double &amp;_vel) {
        // Set the joint's target velocity.
        this-&gt;model-&gt;GetJointController()-&gt;SetVelocityTarget(
            this-&gt;joint-&gt;GetScopedName(), _vel);
      }
  
      /// \brief Handle incoming message
      /// \param[in] _msg Repurpose a vector3 message. This function will
      /// only use the x component.
      private: void OnMsg(ConstVector3dPtr &amp;_msg) {
        this-&gt;SetVelocity(_msg-&gt;x());
      }
  
      /// \brief Handle an incoming message from ROS
      /// \param[in] _msg A float value that is used to set the velocity
      /// of the Velodyne.
      public: void OnRosMsg(const std_msgs::Float32ConstPtr &amp;_msg) {
        this-&gt;SetVelocity(_msg-&gt;data);
      }
  
      /// \brief ROS helper function that processes messages
      private: void QueueThread() {
        static const double timeout = .01;
        while (this-&gt;rosNode-&gt;ok()) {
          this-&gt;rosQueue.callAvailable(ros::WallDuration(timeout));
        }
      }
  
  
      /// \brief Pointer to the model;
      private: physics::ModelPtr model;
  
      /// \brief Control surfaces joints.
      private: physics::JointPtr joint;
  
      /// \brief Velocity PID for the propeller.
      private: common::PID pid;
  
      /// \brief A node used for transport
      private: transport::NodePtr node;
  
      /// \brief A subscriber to a named topic.
      private: transport::SubscriberPtr sub;
  
      /// \brief A node used for ROS transport
      private: std::unique_ptr&lt;ros::NodeHandle&gt; rosNode;
  
      /// \brief A ROS subscriber
      private: ros::Subscriber rosSub;
  
      /// \brief A ROS callbackqueue that helps process messages
      private: ros::CallbackQueue rosQueue;
  
      /// \brief A thread that keeps running the rosQueue
      private: std::thread rosQueueThread;
  
    };
  
    // Tell Gazebo about this plugin, so that Gazebo can call Load on this plugin.
    GZ_REGISTER_MODEL_PLUGIN(VelodynePlugin)
  }
  #endif
  </code></pre>
  
  <p>Let me know how I can be more specific about this question. The first problem is definitely that ROS is not initialized. I can tell this because a call in a different terminal to <code>rostopic list</code> outputs:</p>
  
  <pre><code>ERROR: Unable to communicate with master
  </code></pre>