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

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

1. 编写头文件

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

#ifndef _HELLO_H
#define _HELLO_H

namespace hello_ns{
    class HelloPub{
        public:
        void run();
    };
}

#endif

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

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

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

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;
}

4. 配置CMakeLists.txt文件

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

5.编译执行

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

1. 编写头文件

#ifndef _HELLO_H
#define _HELLO_H

namespace hello_ns1{
    class HelloPub1{
        public:
        void run1();
    };
}

#endif

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

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

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

3. 编写源文件

#include"ros/ros.h"
#include"head_test/hello2.h"

namespace hello_ns1{
    void HelloPub1::run1(){
        ROS_INFO("hello2.h");
    }
}

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;
}

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}
    )
    
  • 可执行文件相关配置
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}
)

6. 编译执行

3、Python模块导入

1. 编写工具模块

#! /usr/bin/env python

num = 1000

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)

3.CMakeLists.txt文件配置

catkin_install_python(PROGRAMS
  scripts/main.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

4.编译执行

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值