3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

M5stackとROS(rosserial)との通信方法(USB接続)

3
Last updated at Posted at 2020-09-08

試した環境

  • ホストPC
    • ROS Melodic (rosversion: 1.14.5)
    • ros-melodic-rosserial-arduino (0.8.0)
    • Ubuntu 18.04
  • M5Stack GRAY

経緯

  • M5Stackを使って、ROSとの通信をUSB接続で行いたかったが、ドキュメントどおりに行ってもうまく行かなかった。
    • Melodic + Wifiの記事は多く見つかったが、USB通信の記事が見つからなかった。
    • ros_libのソースを見たところ、ros.hでは、M5Stack(ESP32)を使うとデフォルトで、TCP IP通信となってしまうことが判明。
  • Arduino側のソースを変更することで回避(下記のコードを参照)

環境構築

M5StackとROSの環境構築方法は、下記の記事などを参考

コード

M5StackROSHelloWorld.ino

# include <M5Stack.h>
# undef ESP32
# include <ros.h>
# define ESP32
# include <std_msgs/String.h>

ros::NodeHandle  nh;
std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char hello[13] = "hello world!";

void setup()
{
  M5.begin();
  M5.Speaker.begin();
  M5.Speaker.mute();  
  
  nh.initNode();
  nh.getHardware()->setBaud(57600);
  nh.advertise(chatter);
  
  M5.Lcd.printf("ROS Hello World Publisher Started\n");

}

void loop()
{
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}

ポイント

追記 (2020.9.9)

ros-serial-aruduino(melodic-devel)の最新版のソースだとros.hの前に、ESP_SERIAL を#defineすればいけるらしい(動作確認はしていない)。

# define ESP_SERIAL
# include <ros.h>

動作確認

ホストPCとの接続

$ roscore
$ rosrun rosserial_arduino serial_node.py _baud:=57600

別ターミナルで

$ rostopic echo /chatter

下記のメッセージが出力されていればOK

---
data: "hello world!"
---

まとめ

  • ROS MelodicとM5StackをUSB接続で通信する方法を紹介
  • ros.hをincludeする前に、#undef ESP32 を入れる必要がある
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?