首页 > 解决方案 > ROSSerial 无法与 ROS Noetic 上的 teensy 设备同步

问题描述

我在 RASPI4 上使用 teensy 和 rosserial + ROS Noetic/Ubuntu 20.04。teensy 代码是在 platformio ( https://platformio.org/lib/show/5526/... ) 上使用 ros_lib 实现的。该程序编译良好并在端口上成功上传/dev/ttyACM0。但是,当我这样做时,我rosrun rossserial_python serial_node.py _port:=/dev/ttyACM0 _baud:=500000会收到同步失败错误。

[错误] [1612795964.166906]:无法与设备同步;可能的链接问题或链接软件版本不匹配,例如 Hydro rosserial_python 与 groovy Arduino。

我已经尝试过的事情:

a) 设置正确的波特率Serial.begin(500000)

b) 禁用所有Serial.beginSerial.print语句

nh.gethardware()->setbaud(500000)c)之前设置ros节点的波特率nh.init()

d) 将 ros_lib 的 ros.h 中的默认缓冲区大小增加到 1024 typedef NodeHandle_<arduinohardware, 10,="" 10,="" 1024,="" 1024=""> NodeHandle;

e) 尝试了 Arduino 板而不是 Teensy,问题仍然存在。

然而,到目前为止,没有任何效果。

标签: arduinoraspberry-pirosplatformioteensy

解决方案


rosserial 的默认波特率为 56700。要更改它,您需要执行以下操作

修改arduino代码中的include语句

#include <ros.h>

#include "ros.h"

在 .ino/.pde 文件旁边添加以下头文件

罗斯.h

#ifndef _ROS_H_
#define _ROS_H_

#include "ros/node_handle.h"
#include "ArduinoHardware.h"

namespace ros
{
  typedef NodeHandle_<ArduinoHardware> NodeHandle;
}

#endif

ArduinoHardware.h

#ifndef ROS_ARDUINO_HARDWARE_H_
#define ROS_ARDUINO_HARDWARE_H_

#if ARDUINO>=100
  #include <Arduino.h>
#else
  #include <WProgram.h>
#endif

#include <HardwareSerial.h>
#define SERIAL_CLASS HardwareSerial

class ArduinoHardware
{
  public:
    ArduinoHardware()
    {
      iostream = &Serial;
      baud_ = 500000;
    }
  
    void setBaud(long baud)
    {
      this->baud_= baud;
    }
  
    int getBaud()
    {
      return baud_;
    }

    void init()
    {
      iostream->begin(baud_);
    }

    int read()
    {
      return iostream->read();
    };

    void write(uint8_t* data, int length)
    {
      for(int i=0; i<length; i++)
      {
        iostream->write(data[i]);
      }
    }

    unsigned long time()
    {
      return millis();
    }

  protected:
    SERIAL_CLASS* iostream;
    long baud_;
};

#endif

在我们在上面定义的 iostream 的 ArduinoHeader.h 文件中,使用如下所示的构造函数设置要使用的波特率

ArduinoHardware()
    {
      iostream = &Serial;
      baud_ = 500000;
    }

当您使用 teensy 和 USB 时,iostream 将是Serial. 如果您使用的是引脚而不是 USB,它会有所不同,Serial1如此Serial8所述。通过将此处设置为 500000可以改变 teensy 的波特率。baud_

此代码引用自NodeHandle 和 ArduinoHardware 的高级配置


推荐阅读