arduino 与 ROS 串口通信

arduino 与ROS串口通信

1. 编写arduino通信程序

新建arduino代码管理空间,用于存放arduino的串口通信程序。

rosnoetic@rosnoetic-VirtualBox:~$ mkdir arduino_ws

需求:通过串口,由arduino向计算机发送数据

实现:

新建arduino文件

添加如下代码:

/*
 * 需求:由arduino向PC发送数据:hello world
 * 实现:
 *  1.设置波特率
 *  2.Serial.print()或println()发送数据
 */

//初始化
void setup() {
  // put your setup code here, to run once:
  // 设置波特率115200
  Serial.begin(115200);
}

// 循环
void loop() {
  // put your main code here, to run repeatedly:
  // 延迟0.1s
  delay(100);
  // 打印信息
  Serial.print("hello  ");
  Serial.println("world");
}
  • 问答

    问:9600的波特率每秒可以传输多少个字节?

    答:波特率的单位是bit/s,表示1s的时间可以传输多少比特的数据,一个字节包含8bit(为什么是8bit呢,因为一个字节通常可以表示一个ASCII码),那么直观上来说9600的波特率表示每秒可以传输9600/8=1200的字节数。但是在串口通信传输中传输的不是字节流,而是数据包!所要传输的字节是包含在数据包中的,数据包中还包含了其余的辅助传输位。例如对于UART数据包,其含有1个起始位,数据位的个数一般是5到9个,一个可选的奇偶校验位以及1个或2个停止位,默认是。在UART数据包中起始位和停止位是必须的。那么在只有起始位和1个停止位的情况下,传输一个字节的数据包长度为10bits,在这种情况下,每秒传输的数据包个数为:9600/10=960个,实际上解析之后,收到了960个字节数。

保存为comm文件

点击验证,没有红色的输出,则表示验证通过

点击上传,没有红色的输出,则表示上传成功

打开串口监视器,选择115200波特率,即可查看得到相应的结果

同时观察Arduino,可以看到RX灯在闪烁。

2. 编写上位机接收程序

2.1 创建 ROS 工作空间

rosnoetic@rosnoetic-VirtualBox:~$ mkdir -p communication/src

rosnoetic@rosnoetic-VirtualBox:~$ cd communication/

rosnoetic@rosnoetic-VirtualBox:~/communication$ catkin_make

  • catkin_make

2.2 启动 VSCode

rosnoetic@rosnoetic-VirtualBox:~/communication$ code .

2.3 VSCode 中编译 ROS

快捷键 Ctrl+Shift+B 调用编译,选择:catkin_make:build

可以点击配置设置为默认

修改.vscode/tasks.json文件

替换成如下内容:

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "label": "catkin_make:debug", //代表提示的描述性信息
            "type": "shell",  //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
            "command": "catkin_make",//这个是我们需要运行的命令
            "args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
            "group": {"kind":"build","isDefault":true},
            "presentation": {
                "reveal": "always"//可选always或者silence,代表是否输出信息
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

2.4 创建 ROS 功能包

右击工作空间下的src,选择“create catkin package

设置包名:plumbing_pub_sub

添加依赖:roscpp rospy std_msgs

2.5 python 实现

右击功能包plumbing_pub_sub ,选择”新建文件夹

新建文件夹:scripts

右击scripts ,选择“新建文件

新建python文件:comm.py

python文件中添加如下内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import serial
import rospy

"""
    使用python实现下位机消息读取
            1. 导包
            2. 初始化ros节点
            3. 读取串口数据并打印
            4. 循环
"""

if __name__ == '__main__':
    # 串口号
    port = '/dev/ttyUSB0'  
    # 下位机波特率
    baud = 115200  
    # 初始化ros节点
    rospy.init_node("serial_node")
    ser = serial.Serial(port, baud, timeout=0.5)
    rate = rospy.Rate(1)
    # 如果端口开通
    if ser.isOpen():
        rospy.loginfo("成功打开端口")
        while not rospy.is_shutdown():
             data = ser.readline() # 读取数据并去掉换行符
             rospy.loginfo(data)
             rate.sleep()  #休眠
        # rospy.spin()作用是当节点停止时让python程序退出
        rospy.spin() 
    else:
        rospy.loginfo("无法打开端口")

添加可执行权限

右击scripts,选择“在集成终端中打开

修改 python 文件的可执行权限,在终端执行:chmod +x *.py

rosnoetic@rosnoetic-VirtualBox:~/communication/src/plumbing_pub_sub/scripts$ chmod +x *.py

2.6 配置 CMakeLists.txt

Python配置

修改功能包plumbing_pub_sub下的CMakeLists.txt

CMakeLists.txt 中找到如下部分:

修改为如下内容

catkin_install_python(PROGRAMS
  scripts/comm.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

2.7 编译执行

2.7.1 编译

ctrl+alt+T打开终端,执行如下指令进行编译:

rosnoetic@rosnoetic-VirtualBox:~$ cd communication/

rosnoetic@rosnoetic-VirtualBox:~/communication$ catkin_make

  • catkin_make

2.7.2 执行

arduino连接上虚拟机,注意不要打开arduino串口监视器,因为串口只能给其中的一方发送数据。

ctrl+alt+T打开终端,执行如下指令启动 ros 核心

rosnoetic@rosnoetic-VirtualBox:~$ roscore

ctrl+alt+T打开新的终端,重新刷新我们的环境变量

rosnoetic@rosnoetic-VirtualBox:~$ cd communication/

rosnoetic@rosnoetic-VirtualBox:~/communication$ source ./devel/setup.bash 

rosnoetic@rosnoetic-VirtualBox:~/communication$ rosrun plumbing_pub_sub comm.py 

即可看到如下图所示的hello world输出

  • 22
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值