• 十三、ROS中的头文件与源文件,Python模块导入


    1、设计头文件,可执行文件本身作为源文件

    1. 编写头文件

    在功能包下的include/功能包名 目录下新建hello1.h头文件

    #ifndef _HELLO_H
    #define _HELLO_H
    
    namespace hello_ns{
        class HelloPub{
            public:
            void run();
        };
    }
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2. 配置c_cpp_propertise.json文件中的includepath属性

    需要加入自己编写的头文件路径

    "/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05/src/head_test/include/**"
    
    • 1

    3.编写可执行文件

    #include"ros/ros.h"
    #include"head_test/hello1.h"
    
    namespace hello_ns{
        void HelloPub::run(){
            ROS_INFO("hello1.h头文件");
        }
    }
    
    int main(int argc, char *argv[]){
        setlocale(LC_ALL,"");
        ros::init(argc,argv,"hello1");
        hello_ns::HelloPub helloPub;
        helloPub.run();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4. 配置CMakeLists.txt文件

    • 头文件配置
      include_directories(
      include
      ${catkin_INCLUDE_DIRS}
      )
      
      • 1
      • 2
      • 3
      • 4
    • 可执行文件配置
      add_executable(hello1 src/hello1.cpp)
      
      add_dependencies(hello1 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
      
      target_link_libraries(hello1
      ${catkin_LIBRARIES}
      )
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    5.编译执行

    2、设计头文件,可执行文件与源文件分离

    1. 编写头文件

    #ifndef _HELLO_H
    #define _HELLO_H
    
    namespace hello_ns1{
        class HelloPub1{
            public:
            void run1();
        };
    }
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2. 配置c_cpp_propertise.json文件中的includepath属性

    需要加入自己编写的头文件路径

    "/media/d102/EPAN/Desktop/code_study_ubuntu/rosdemo_05/src/head_test/include/**"
    
    • 1

    3. 编写源文件

    #include"ros/ros.h"
    #include"head_test/hello2.h"
    
    namespace hello_ns1{
        void HelloPub1::run1(){
            ROS_INFO("hello2.h");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4. 编写可执行文件

    #include"ros/ros.h"
    #include"head_test/hello2.h"
    
    int main(int argc,char *argv[]){
        ros::init(argc,argv,"hello2_use");
        hello_ns1::HelloPub1 my;
        my.run1();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5. 配置CMakeLists.txt文件

    • 头文件和源文件相关配置
      include_directories(
      include
      ${catkin_INCLUDE_DIRS}
      )
      
      
      ## Declare a C++ library
      add_library(hello2      #hello2:名字随意,不作要求
      include/head_test/hello2.h  #头文件目录
      src/hello2.cpp  #源文件目录
      )
      
      #此处hello2对应上面的hello2,注意此处add_dependencise的位置
      ## Declare a C++ executable
      ## With catkin_make all packages are built within a single CMake context
      ## The recommended prefix ensures that target names across packages don't collide
      add_dependencies(hello2 ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
      
      #此处hello2对应上面的hello2
      target_link_libraries(hello2
      ${catkin_LIBRARIES}
      )
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    • 可执行文件相关配置
    add_executable(hello2_use src/hello2_use.cpp)
    
    #注意此处add_denpendencies的位置
    ## Add cmake target dependencies of the executable
    ## same as for the library above
    add_dependencies(hello2_use ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
    
    target_link_libraries(hello2_use
      hello2
      ${catkin_LIBRARIES}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    6. 编译执行

    3、Python模块导入

    1. 编写工具模块

    #! /usr/bin/env python
    
    num = 1000
    
    • 1
    • 2
    • 3

    2.编写主程序

    import os 
    import sys
    import rospy
    '''
        在Python中,一个文件引用另一个文件中的变量,
        只需保证二者在同一个文件夹下即可,Python会默
        认从当前文件夹搜索相关文件,但是,在ROS中,他
        会先搜索根目录下的文件,所以,为了顺利运行程序,
        需加入相应的搜索路径和优先级
    '''
    path = os.path.abspath(".")#获取该项目的绝对路径
    
    sys.path.insert(0,path+"/src/head_test/scripts")#加入文件搜索路径,0的优先级最高
    
    import tools
    if __name__ == "__main__":
        rospy.init_node("head_p")
        rospy.loginfo("path:%s",path)
        rospy.loginfo("num=%d",tools.num)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.CMakeLists.txt文件配置

    catkin_install_python(PROGRAMS
      scripts/main.py
      DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
    )
    
    • 1
    • 2
    • 3
    • 4

    4.编译执行

  • 相关阅读:
    SSM整合-表现层与前端数据传输数据协议定义与实现
    应该继续学习编程,还是学数控?
    nvidia-smi 报错
    SpringBoot SpringBoot 开发实用篇 6 监控 6.2 SpringBoot Admin
    .NET EF配置数据库链接
    想跳槽却简历石沉大海?一起来围观月薪 20k 的软件测试工程师真实简历
    Docker
    IDERA ER/Studio Data Architect Professional v19.3.2
    ES6生成器(Generator)和迭代器(Iterator)
    solidity
  • 原文地址:https://blog.csdn.net/qq_43280851/article/details/125436070