ros1_melodic入门程序编写

c程序编译运行

// 编译,gcc是编译的命令,-o表示编译后生成的可执行二进制文件
gcc cfile.c -o cfile
// 运行
./cfile
为什么输入“./”:当您键入命令名称(a.out与此方面的任何其他命令名称没有区别)时,shell将搜索具有该名称的可执行文件。它使用存储在$PATH环境变量中的目录名列表执行此搜索。./告诉bash在当前目录中查找文件cfile

main(int argc, char *argv[])   中argc表示程序输入参数个数,argv表示参数列表
rosrun topic_pkg  publisher// 注意publisher是节点不是文件

rostopic   echo  topicname  // print messages to screen打印话题信   如下图rostopic echo /turtle1/cmdele

在这里插入图片描述

ros节点通信

注意图片中的c++标准和includePath。(generate_messages的作用是自动创建我们自定义的消息类型*.msg与服务类型*.srv相对应的*.h)生成的.h文件放在devel目录下,如果引用头文件报错,在includePath中添加路径引用头文件报错

在这里插入图片描述source devel/setup.bash作用: 设置环境变量。设置环境变量目的是为了让系统找到工作空间中对应的功能包,路径设置。
在这里插入图片描述

ros参数设置

roscore在启动时会输出。可以通过 nh.setParam(“nh_int”,10000); ros::param::set(“param_int”,20);两种方法实现。可以在控制台输入下图中的命令查看。
在这里插入图片描述

自定义源文件调用

第一步

###########
## Build ##
###########

## Specify additional locations of header files        指定头文件的其他位置
## Your package locations should be listed before other locations  # 向工程添加多个特定的头文件搜索路径
include_directories(
  include
  ${catkin_INCLUDE_DIRS}
)
## Declare a C++ library   #自定义库文件名字
add_library(a
  src/headFileSrc.cpp
)
## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
add_dependencies(a ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

第二步,

## 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_executable(hello src/hello.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
add_dependencies(hello ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
target_link_libraries(hello
  a  ##第二步,可执行文件里面添加自定义库文件
  ${catkin_LIBRARIES}
)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值