Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

translate Velodyne LiDAR plugin code to ros2

Hi I want to translate the Velodyne LiDAR plugin script from ros1 to ros2.: http://gazebosim.org/tutorials?cat=guided_i&tut=guided_i6 there is three blocks of code which are added to plugin code to create a node and a topic and subscribe to it:

block1:

private: std::unique_ptr<ros::NodeHandle> rosNode;
private: ros::Subscriber rosSub;
private: ros::CallbackQueue rosQueue;
private: std::thread rosQueueThread;

block2:

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

      ros::init(argc, argv, "gazebo_client",
          ros::init_options::NoSigintHandler);
}

// 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 =
  ros::SubscribeOptions::create<std_msgs::Float32>(
      "/" + this->model->GetName() + "/vel_cmd",
      1,
      boost::bind(&VelodynePlugin::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(&VelodynePlugin::QueueThread, this));

block3:

public: void OnRosMsg(const std_msgs::Float32ConstPtr &_msg)
{
  this->SetVelocity(_msg->data);
}

/// \brief ROS helper function that processes messages
private: void QueueThread()
{
  static const double timeout = 0.01;
  while (this->rosNode->ok())
  {
    this->rosQueue.callAvailable(ros::WallDuration(timeout));
   }
}

i have tried common methods which are used to create a ros2 node but i get many errors like other people: https://answers.gazebosim.org//question/24340/ros-eloquent-gazebo10-integration/ https://www.reddit.com/r/ROS/comments/f5xrq7/ros2_subscriber_node_in_a_gazebo_plugin_error/

I really appreciate any help to solve this problem.