1. 工作空间设置
cd catkin_ws/src/
2. 创建Package
catkin_creat_pkg ssr_pkg rospy roscpp std_msgs
3. 回访依赖包
roscd roscpp ls code package.xml
下载包可直接运行,源文件须编译运行
4. 创建Node节点
#include <ros/ros.h> int main(int argc, char const *argv[]) { /* code */ return 0; }
代码误报删除c_cpp_properties文件
5. 源码编译
#include <ros/ros.h> int main(int argc, char const *argv[]) { /* code */ printf("HelloWorld!\n"); return 0; }
add_executable(chao_node src/chao_node.cpp)
ctrl+shift+b编译
6. 运行Node节点
roscore
rosrun ssr_pkg chao_node
如果报错如下操作
code ~/.bashrc
# Set ROS melodic source /opt/ros/melodic/setup.bash source ~/catkin_ws/devel/setup.bash
7. Node节点完善
#include <ros/ros.h> int main(int argc, char *argv[]) { /* code */ ros :: init ( argc, argv, "chao_node"); printf("我是个弟弟!\n"); return 0; }
add_executable(chao_node src/chao_node.cpp) target_link_libraries(chao_node ${catkin_LIBRARIES} )
编译运行
保持运行,加while循环
#include <ros/ros.h> int main(int argc, char *argv[]) { /* code */ ros :: init ( argc, argv, "chao_node"); printf("我是个弟弟!\n"); while (true) { /* code */ printf("我是大哥!\n"); } return 0; }
编译运行
#include <ros/ros.h> int main(int argc, char *argv[]) { /* code */ ros :: init ( argc, argv, "chao_node"); printf("我是个弟弟!\n"); while (ros::ok()) { /* code */ printf("我是大哥!\n"); } return 0; }
8. 总结