zoukankan      html  css  js  c++  java
  • nodelet的应用

    1.创建一个包,如example_pkg

    catkin_create_pkg  example_pkg

    2.创建MyNodeletClass.h文件

    cd ~/catkin_ws/src/example_pkg/
    mkdir -p include/example_pkg
    touch include/example_pkg/MyNodeletClass.h
    vim include/example_pkg/MyNodeletClass.h
    其内容为
    #include <nodelet/nodelet.h>
    namespace example_pkg
    {
    
        class MyNodeletClass : public nodelet::Nodelet
        {
            public:
                virtual void onInit();
        };
    
    }
     

    3.创建MyNodeletClass.cpp文件

    cd ~/catkin_ws/src/example_pkg/
    mkdir src
    touch src/MyNodeletClass.cpp
    vim src/MyNodeletClass.cpp
    其内容为
    // this should really be in the implementation (.cpp file)
    #include <ros/ros.h>
    #include <pluginlib/class_list_macros.h>
    #include <example_pkg/MyNodeletClass.h>
    
    
    namespace example_pkg
    {
        void MyNodeletClass::onInit()
        {
            NODELET_DEBUG("Initializing nodelet...");
            ROS_INFO("Nodelet is Ok for test!!");
        }
    }
    
    // watch the capitalization carefully
    PLUGINLIB_DECLARE_CLASS(example_pkg, MyNodeletClass, example_pkg::MyNodeletClass, nodelet::Nodelet)
    成功则输出"Nodelet is Ok for test!!"

    4.创建nodelet_plugins.xml文件

    cd ~/catkin_ws/src/example_pkg/
    mkdir plugins
    touch plugins/nodelet_plugins.xml
    vim plugins/nodelet_plugins.xml
    其内容为
    <library path="lib/libexample_pkg">
      <class name="example_pkg/MyNodeletClass" type="example_pkg::MyNodeletClass" base_class_type="nodelet::Nodelet">
      <description>
      This is my nodelet.
      </description>
      </class>
    </library>

    5.修改package.xml文件,增加:

    cd ~/catkin_ws/src/example_pkg/
    vim package.xml
    其内容为
     <buildtool_depend>catkin</buildtool_depend>
      <build_depend>nodelet</build_depend>
      <build_depend>roscpp</build_depend>
    
      <run_depend>nodelet</run_depend>
      <run_depend>roscpp</run_depend>
    
    
      <!-- The export tag contains other, unspecified, tags -->
      <export>
        <!-- Other tools can request additional information be placed here -->
        <nodelet plugin="${prefix}/plugins/nodelet_plugins.xml" />
      </export>

    6.修改CMakeLists.txt

    cd ~/catkin_ws/src/example_pkg
    vim  CMakeLists.txt 

    其内容为
    ## Specify additional locations of header files
    ## Your package locations should be listed before other locations
    include_directories(
      include
      ${catkin_INCLUDE_DIRS}
      ${Boost_INCLUDE_DIRS}
    )
    
    ## Declare a C++ library
    add_library(${PROJECT_NAME}  src/MyNodeletClass.cpp)
    
    add_dependencies(${PROJECT_NAME} 
       ${${PROJECT_NAME}_EXPORTED_TARGETS} 
       ${catkin_EXPORTED_TARGETS}
    )
    
    target_link_libraries(${PROJECT_NAME}
       ${catkin_LIBRARIES}
    )

    6.创建mynodelet.launch文件

    d ~/catkin_ws/src/example_pkg/
    mkdir launch
    touch launch/mynodelet.launch
    vim  launch/mynodelet.launch
    其内容为
    <launch>
      <node pkg="nodelet" type="nodelet" name="standalone_nodelet"  args="manager" output="screen"/>
    
      <node pkg="nodelet" type="nodelet" name="MyNodeletClass" args="load example_pkg/MyNodeletClass standalone_nodelet" output="screen">
      </node>                 
    </launch>

    6.编译

    cd ~/catkin_ws/
    catkin_make
    rospack profile

    7.运行

    • 新终端,运行roscore
    $ roscore
    
    • 新终端,运行launch
    $ rosluanch examples_pkg mynodelet.launch
    

    参考:
    http://www.ncnynl.com/archives/201702/1326.html
    http://blog.csdn.net/zhangrelay/article/details/62048915
    http://rosclub.cn/post-164.html
    http://blog.csdn.net/yiranhaiziqi/article/details/53308657
    http://www.lai18.com/content/2386868.html


    例子
    http://blog.csdn.net/zyh821351004/article/details/52143309
    创建包nodelet_test,依赖项nodelet roscpp std_msgs

    CMakeLists.txt

    1. cmake_minimum_required(VERSION 2.8.3)  
    2. project(nodelet_test_pkg)  
    3.   
    4. find_package(catkin REQUIRED COMPONENTS nodelet roscpp std_msgs)#  
    5.   
    6. ## Setup include directories  
    7. include_directories(${catkin_INCLUDE_DIRS})  
    8.   
    9. catkin_package(  
    10. )  
    11.   
    12. add_library(nodelet_test plus.cpp)  
    13. target_link_libraries(nodelet_test ${catkin_LIBRARIES})

    package.xml

    1. <package>  
    2.   <name>nodelet_test_pkg</name>  
    3.   <version>0.0.0</version>  
    4.   <description>Nodelet test.</description>  
    5.   <maintainer email="huasheng_zyh@163.com">kint zhao</maintainer>  
    6.   <license>BSD</license>  
    7.   
    8.   <buildtool_depend>catkin</buildtool_depend>  
    9.   
    10.   <build_depend>nodelet</build_depend>  
    11.   <build_depend>roscpp</build_depend>  
    12.   <build_depend>std_msgs</build_depend>  
    13.   
    14.   <run_depend>nodelet</run_depend>  
    15.   <run_depend>roscpp</run_depend>  
    16.   <run_depend>std_msgs</run_depend>  
    17.   
    18.   <export>  
    19.     <nodelet plugin="${prefix}/nodelet_test_plugin.xml"/>  
    20.   </export>  
    21. </package> 
    plugin
    1. <library path="lib/libnodelet_test_lib">  
    2.   <class name="nodelet_ns/Plus" type="nodelet_ns::Plus" base_class_type="nodelet::Nodelet">  
    3.     <description>   
    4.       A node to add a value and republish.  
    5.     </description>  
    6.   </class>  
    7. </library> 

    launch

    1. <launch>  
    2.   <node pkg="nodelet" type="nodelet" name="manager_1"  args="manager" output="screen"/>  
    3.   
    4.   <node pkg="nodelet" type="nodelet" name="test1" args="load nodelet_ns/Plus manager_1" output="screen"/>  
    5.   <node pkg="nodelet" type="nodelet" name="test2" args="load nodelet_ns/Plus manager_1" output="screen"/>  
    6.   <node pkg="nodelet" type="nodelet" name="test3" args="load nodelet_ns/Plus manager_1" output="screen"/>  
    7.   
    8.   <node pkg="nodelet" type="nodelet" name="manager_2"  args="manager" output="screen"/>  
    9.   <node pkg="nodelet" type="nodelet" name="test4" args="load nodelet_ns/Plus manager_2" output="screen"/>  
    10.   <node pkg="nodelet" type="nodelet" name="test5" args="load nodelet_ns/Plus manager_2" output="screen"/>  
    11.   
    12.   <node pkg="nodelet" type="nodelet" name="test6" args="standalone nodelet_ns/Plus " output="screen"/>  
    13.             
    14. </launch> 

    .cpp文件

    1. #include <pluginlib/class_list_macros.h>  
    2. #include <nodelet/nodelet.h>  
    3. #include <ros/ros.h>  
    4. #include <std_msgs/Float64.h>  
    5. #include <stdio.h>  
    6.   
    7.   
    8. #include <math.h> //fabs  
    9.   
    10. namespace nodelet_ns  
    11. {  
    12.   
    13. class Plus : public nodelet::Nodelet  
    14. {  
    15. public:  
    16.   Plus()  
    17.   : value_(0)  
    18.   {}  
    19.   
    20. private:  
    21.   virtual void onInit()  
    22.   {  
    23.     ros::NodeHandle& private_nh = getPrivateNodeHandle();  
    24.     private_nh.getParam("value", value_);  
    25.     pub = private_nh.advertise<std_msgs::Float64>("out", 10);  
    26.     sub = private_nh.subscribe("in", 10, &Plus::callback, this);  
    27.   }  
    28.   
    29.   void callback(const std_msgs::Float64::ConstPtr& input)  
    30.   {  
    31.     std_msgs::Float64Ptr output(new std_msgs::Float64());  
    32.     output->data = input->data + value_;  
    33.     NODELET_DEBUG("Adding %f to get %f", value_, output->data);  
    34.     pub.publish(output);  
    35.   }  
    36.   
    37.   ros::Publisher pub;  
    38.   ros::Subscriber sub;  
    39.   double value_;  
    40. };  
    41.   
    42. PLUGINLIB_DECLARE_CLASS(nodelet_ns, Plus, nodelet_ns::Plus, nodelet::Nodelet);//*******  













  • 相关阅读:
    [SCOI2016] 幸运数字
    [CF438E] 小朋友和二叉树
    【题解】[AHOI2013]作业
    【题解】CF940F Machine Learning
    【题解】CF1207E XOR Guessing
    【题解】CF1228D Complete Tripartite
    【题解】CF1290B Irreducible Anagrams
    【题解】[JSOI2007]字符加密
    【题解】[SDOI2016]征途
    【题解】多边形染色
  • 原文地址:https://www.cnblogs.com/gary-guo/p/7049941.html
Copyright © 2011-2022 走看看