python
ros2 pkg create --build-type ament_python --license Apache-2.0 demo_python_pkg
创建功能包命令,
–build-type 类型 python
–license 证书
demo_python_pkg 创建的功能包名
船舰完成后,
cd demo_python_pkg/demo_python_pkg
touch python_node.py
python_node.py
import rclpy
from rclpy.node import Node
def main():
rclpy.init() # 初始化
node = Node("python_node")
node.get_logger().info('你好 python 节点')
rclpy.spin(node=node)
rclpy.shutdown()
修改package.xml文件,增加依赖项,即增加<depend>rclpy</depend>
package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>demo_python_pkg</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="yfzx@todo.todo">yfzx</maintainer>
<license>Apache-2.0</license>
<depend>rclpy</depend>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
修改setup.py,修改console_scripts值
from setuptools import setup
package_name = 'demo_python_pkg'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='yfzx',
maintainer_email='yfzx@todo.todo',
description='TODO: Package description',
license='Apache-2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'python_node = demo_python_pkg.python_node:main'
],
},
)
运行:
cd rosfish/chapt2
colcon build
. install/setup.bash
ros2 run demo_python_pkg python_node

cpp
ros2 pkg create --build-type ament_cmake --license Apache-2.0 demo_cpp_pkg
cd demo_cpp_pkg/src
touch cpp_node.cpp
cpp_node.cpp
#include "iostream"
#include "rclcpp/rclcpp.hpp"
int main(int argc, char** argv){
std::cout<<"参数数量:\t"<<argc<<"\n";
std::cout<<"程序名字:\t"<<argv[0]<<"\n";
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>("cpp_node"); // 使用make_shared创建智能指针
RCLCPP_INFO(node->get_logger(), "你好 cpp节点");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
修改 cmakelists文件,增加
find_package(rclcpp REQUIRED) # 直接查找到对应的头文件和库文件, REQUIRED 表示这个库为必须
add_executable(cpp_node src/cpp_node.cpp)
# target_include_directories(cpp_node PUBLIC ${rclcpp_INCLUDE_DIRS}) # 头文件包含
# target_link_libraries(cpp_node ${rclcpp_LIBRARIES}) # 库文件链接
ament_target_dependencies(cpp_node rclcpp) # 替代上面两行include和link
install(TARGETS cpp_node DESTINATION lib/${PROJECT_NAME}) # 用于复制文件到install
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(demo_cpp_pkg)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(rclcpp REQUIRED) # 直接查找到对应的头文件和库文件, REQUIRED 表示这个库为必须
add_executable(cpp_node src/cpp_node.cpp)
# target_include_directories(cpp_node PUBLIC ${rclcpp_INCLUDE_DIRS}) # 头文件包含
# target_link_libraries(cpp_node ${rclcpp_LIBRARIES}) # 库文件链接
ament_target_dependencies(cpp_node rclcpp)
install(TARGETS cpp_node DESTINATION lib/${PROJECT_NAME}) # 用于复制文件到install
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
package.xml, 增加<depend>rclcpp</depend>
package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>demo_cpp_pkg</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="yfzx@todo.todo">yfzx</maintainer>
<license>Apache-2.0</license>
<depend>rclcpp</depend>
<buildtool_depend>ament_cmake</buildtool_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
cd CHAPT2
colcon build
. install/setup.bash
ros2 run demo_cpp_pkg cpp_node

1808

被折叠的 条评论
为什么被折叠?



