ros2arduinoを使う(+Micro XRCE DDSとMicro XRCE DDS Genのインストール)

Micro XRCE DDSのインストール

$ mkdir ~/micro_dds
$ cd ~/micro_dds
$ git clone https://github.com/eProsima/Micro-XRCE-DDS-Agent
$ cd Micro-XRCE-DDS-Agent
$ git checkout v1.1.0

$ mkdir build
$ cd build
$ cmake ..
$ make
$ sudo make install
$ sudo ldconfig

動作確認

$ MicroXRCEAgent -h
eProsima Micro XRCE-DDS Agent
Usage: [OPTIONS] SUBCOMMAND

Options:
  -h,--help                               Print this help message and exit

Subcommands:
  udp                                     Launch a UDP server
  tcp                                     Launch a TCP server
  serial                                  Launch a Serial server
  pseudo-serial                           Launch a Pseudo-Serial server
  q                                       Exit

次にMicro XRCE DDS Genのインストール

$ cd ~/micro_dds
$ git clone --recursive https://github.com/eProsima/Micro-XRCE-DDS-Gen
$ sudo add-apt-repository ppa:cwchien/gradle
$ sudo apt update
$ sudo apt install default-jre gradle
$ cd ~/micro_dds/Micro-XRCE-DDS-Gen
$ gradle build -Dbranch=v1.2.5 #参考 https://asciinema.org/a/nEJKfmwpxF6rEzCPUYd

動作確認

$ cd scripts
$ mkdir test
$ ./microxrceddsgen ../test/TestType.idl -d test
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
Loading templates...
Processing the file ../test/TestType.idl...
Generating Type definition files...
$ ls test/
TestType.c  TestType.h

では実際に通信してみる。
ESP32のArduinoコード

#include <ros2arduino.h>

#include <WiFi.h>
#include <WiFiUdp.h>
#define SSID       "your wifi ssid"
#define SSID_PW    "your wifi password"
#define AGENT_IP   "192.168.1.246"
#define AGENT_PORT 2018 //AGENT port number

#define PUBLISH_FREQUENCY 2 //hz

void publishString(std_msgs::String* msg, void* arg)
{
  (void)(arg);

  static int cnt = 0;
  sprintf(msg->data, "Hello ros2arduino %d", cnt++);
}

class StringPub : public ros2::Node
{
public:
  StringPub()
  : Node("roshoge")
  {
    ros2::Publisher<std_msgs::String>* publisher_ = this->createPublisher<std_msgs::String>("chatter");
    this->createWallFreq(PUBLISH_FREQUENCY, (ros2::CallbackFunc)publishString, nullptr, publisher_);
  }
};

WiFiUDP udp;

void setup() 
{
  Serial.begin(115200);
  delay(1000);
  Serial.println("init start");

  WiFi.begin(SSID, SSID_PW);
  while(WiFi.status() != WL_CONNECTED);

  ros2::init(&udp, AGENT_IP, AGENT_PORT);
  Serial.println("init done");
}

void loop() 
{
  static StringPub StringNode;

  ros2::spin(&StringNode);
}

サーバを起動
注意:LD_PRELOADをつけないとUDPサーバが落ちる。>||
$ LD_PRELOAD="/usr/local/lib/libfastrtps.so.1 /usr/local/lib/libfastcdr.so.1" MicroXRCEAgent udp -p 2018 -v 6
|