ROS topic 编程
创建一个ROS工作空间并在工作空间内创建src源码路径
初始化这个工作空间
mkdir -p catkin_ws/src
cd catkin_ws/src
catkin_init_workspace
cd ..
catkin_make
在工作空间的根目录下编译并 设置环境变量 并检查是否设置正确
source devel/setup.zsh
echo $ROS_PACKAGE_PATH
ROS 项目开发流程
$ catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
创建并编译功能包
cd catkin_ws/src
catkin_create_pkg learning_communication rospy roscpp std_msgs std_srvs
cd .. //(编译一定是在工作空间的根目录)
catkin_make
source devel/setup.zsh
新增代码之后需要修改 CMakeLists.txt
- 设置需要编译的代码和生成可执行文件
- 设置链接库
vi catkin_ws/src/learning_communication/CMakeLists.txt
add_executable(string_publisher src/string_publisher.cpp)
target_link_libraries(string_publisher ${catkin_LIBRARIES})
add_executable(string_subscriber src/string_subscriber.cpp)
target_link_libraries(string_subscriber ${catkin_LIBRARIES})
运行、检验
source devel/setup.zsh
roscore &
rosrun learning_communication string_publisher
rosrun learning_communication string_subscriber //新建一个终端
自定义 msg
创建 msg 文件夹及文件
cd catkin_ws/src/learning_communication
mkdir msg
cd msg
touch PersonMsg.msg
在 PersonMsg.msg 添加内容
string name
uint8 age
uint8 sex
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
在 pachage.xml 中添加功能包依赖
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
在 CMakeLists.txt 中添加编译选项
find_package(... message_generation)
catkin_package(... message_runtime)
add_message_files(FILES PersonMsg.msg)
generate_messages(DEPENDENCIES std_msgs)
add_executable(person_publisher src/person_publisher.cpp)
target_link_libraries(person_publisher ${catkin_LIBRARIES})
add_dependencies(person_publisher ${PROJECT_NAME}_gencpp)
add_executable(person_subscriber src/person_subscriber.cpp)
target_link_libraries(person_subscriber ${catkin_LIBRARIES})
add_dependencies(person_subscriber ${PROJECT_NAME}_gencpp)
运行、检验
source devel/setup.zsh
roscore &
rosrun learning_communication person_publisher
rosrun learning_communication person_subscriber //新建一个终端
ROS service 编程
自定义 srv
创建 msg 文件夹及文件
cd catkin_ws/src/learning_communication
mkdir srv
cd srv
touch PersonSrv.srv
在 PersonSrv.srv 添加内容
string name
uint8 age
uint8 sex
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
---
string result
三横线以上是 request 内容 以下是 respond 内容
在 CMakeLists.txt 添加一行
add_service_files(FILES PersonSrv.srv)
其实还应该在package.xml中添加依赖。以及链接库,由于msg已经添加过,所以可以省
ROS查看节点类型一节该类型的结构体成员
rostopic type /turtle1/pose
rosmsg show turtlesim/Pose
rostopic type /turtle1/cmd_vel
rosmsg show geometry_msgs/Twist