2
4

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 3 years have passed since last update.

ArduinoをWiFi経由でROS制御する

Last updated at Posted at 2021-06-27

はじめに

ArduinoデバイスをWiFi経由でROS制御(Joystick)するための手順をまとめました。

環境

  • SparkFun ESP32 Thing Plus
  • Arduino IDE 1.8.13
  • Ubuntu 18.04
  • ROS melodic + rosserial

ROSサーバー側

インストール

$ sudo apt install ros-melodic-rosserial ros-melodic-rosserial-arduino

ROSノードの立ち上げ

joyとrosserialのノードを立ち上げます。

main.launch
<launch>
  <node pkg="joy" type="joy_node" name="joy_node">
    <param name="autorepeat_rate" value="100" />
  </node> 

  <node pkg="rosserial_python" type="serial_node.py" name="serial_node" args="tcp">
  </node>
</launch>
$ roslaunch main.launch

Arduinoデバイス側

インストール

ROSサーバ側で以下のコマンドでros_libを生成します。

$ rosrun rosserial_arduino make_library.py ${PATH_TO_SKETCHBOOK}/libraries

プログラム

以下のようなコードを書きます。

#include <SPI.h>
#include <WiFi.h>

#define ROSSERIAL_ARDUINO_TCP

#include <ros.h>
#include <sensor_msgs/Joy.h>

void messageCb(const sensor_msgs::Joy&);

const char* ssid = "YOUR SSID";
const char* password = "YOUR PASSWORD";

ros::NodeHandle  nh;
ros::Subscriber<sensor_msgs::Joy> sub("/joy", &messageCb );

// SET YOUR ROSSERIAL SERVER IP & PORT
IPAddress server(192, 168, 1, 100);
uint16_t serverPort = 11411;

void messageCb(const sensor_msgs::Joy& joy) {
  // WRITE YOUR CODES
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected.");
  nh.getHardware()->setConnection(server, serverPort);
  nh.initNode();
  nh.subscribe(sub);
}

void loop()
{
  nh.spinOnce();
  delay(1);
}

あとはデバイスにアップロードして実行すれば、ROSのJoyメッセージを受信できます。

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?