跳到主要內容

[ROS] Nodelet Example

In order to successfully build a node that uses a nodelet for transport, we have to fix a lot of things when using catkin_create_pkg :
1. CMakeList.txt
2. package.xml
3. nodelet_plugin.xml
4. main.cpp

Explain in order:
1. CMakeList.txt
Pay special attention to adding a nodelet

``` code
Cmake_minimum_required(VERSION 2.8.3)
Project(lidars_grabber_nodelet)

Find_package(catkin REQUIRED COMPONENTS
  Nodelet
  Roscpp
  Rospy
  Std_msgs
)

# Because the nodelet is referenced as a lib, so add this paragraph
Add_library(${PROJECT_NAME}
   Src/lidars_grabber_nodelet.cpp
 )

#把 nodelet as a dependent library
If(catkin_EXPORTED_LIBRARIES)
  Add_dependencies(lidars_grabber_nodelet ${catkin_EXPORTED_LIBRARIES})
Endif()

# Remember to attach nodelet_plugins.xml
# nodelet_plugins.xml is manually added
 Install(FILES
  # myfile1
  # myfile2
  Nodelet_plugins.xml
  DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
 )
```

2. package.xml
In addition to the build_depend of the nodelet, remember to fill in the block at the end.

```code


  lidars_grabber_nodelet
  0.0.0
  The lidars_grabber_nodelet package

  TODO

  catkin
  nodelet
  roscpp
  rospy
  std_msgs

  nodelet
  roscpp
  rospy
  std_msgs

  nodelet
  roscpp
  rospy
  std_msgs

 
   
 


```

3. nodelet_plugin.xml
Basically just fill in the class name you wrote in cpp

```code

 
 
  This is lidars grabber nodelet.
 
 

```


4. main.cpp
Since the nodelet is treated as a class for loading, the content needs to be written in class.

```code
#include
#include
#include
#include
#include


#include //fabs

Namespace nodelet_tutorial_math
{

Class Plus : public nodelet::Nodelet
{
Public:
  Plus()
  : value_(0)
  {}

Private:
  Virtual void onInit()
  {
    Ros::NodeHandle& private_nh = getPrivateNodeHandle();
    private_nh.getParam("value", value_);
    Pub = private_nh.advertise("out", 10);
    Sub = private_nh.subscribe("in", 10, &Plus::callback, this);
  }

  Void callback(const std_msgs::Float64::ConstPtr& input)
  {
    Std_msgs::Float64Ptr output(new std_msgs::Float64());
    Output->data = input->data + value_;
    NODELET_DEBUG("Adding %f to get %f", value_, output->data);
    Pub.publish(output);
  }

  Ros::Publisher pub;
  Ros::Subscriber sub;
  Double value_;
};

PLUGINLIB_EXPORT_CLASS(nodelet_tutorial_math::Plus, nodelet::Nodelet)
}

```

留言