ROS通过串口与单片机通信读取编码器的计数脉冲

本文介绍了如何使用ROS的serial包与单片机通过串口进行通信,读取编码器的计数脉冲。首先需要正确安装并配置serial包,避免找不到头文件的问题。接着创建ROS包,编辑CMakeList.txt文件,并定义消息结构。在C++源文件中,先发送控制指令,然后读取20位数据,通过共用体将接收到的二进制数据转换为int类型。
摘要由CSDN通过智能技术生成

1,使用C++编写需要安装serial包

serial工具包,下载完成后按照提示编译安装,默认使用

make install

会将ROS编译工程需要的serial.h头文件和serialConfig.cmake等全部必要的文件移动到/tmp/usr/local…的目录下,因此在make install后,我们需要手动将这些移动到/usr/local的根目录下,否则构建节点时,会提示以下错误信息:

  1. can not found serial/serial.h
  2. serialConfig.cmake is not found

等其他问题,解决办法就是

sudo cp -r /tmp/usr/local/* /usr/local

然后重新加载bash的环境,

source ~/.bashrc

在编译即可。
如果还是提示缺少,那就把,/usr/local的路径添加到cmake编译的路径中去,使用export指令。

2,创建一个ros pkg

$ catkin_create_pkg getserial_data std_msgs rospy roscpp

2.1 编辑CMakeList.txt

可以参考我的CMakeList.txt文件。其中msg部分,mobileRobot_velocity.msg是与此无关的,mobileRobot_msgs.msg是存放编码起信息的数据结构,如下所示:

cmake_minimum_required(VERSION 2.8.3)
project(getodomdata_serial)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  nav_msgs
  std_msgs
  geometry_msgs
  tf
  message_generation
  serial
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)


## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"
## * In this fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值