'ROS2 Arduino WiFi

here is my basic question about ROS2-communication between Arduino (WiFi Rev2-Board) and a PC. It is my first project with ROS2, I would like to receive data from sensors and send data to control motors which are connected to the Arduino.

The communication should run via WiFi, so I looked for a tutorial about this but only found this for basic configuration:

http://wiki.ros.org/ROS/NetworkSetup

which is about two machines which use console statements (exports and variables).

I don't know, if this would also apply to the Arduino.

What I tried is to adapt an example which comes with the ROS-library (a blink example, to arduino is subscriber and the PC is publisher of a toggle_led message).

But Arduino does not receive any message.

Will I need a specific network setup for the Arduino (define variables like ROS_IP or ROS_HOSTNAME somewhere in the arduino code)?

Is there any example/tutorial available for successful communication between Arduino and a PC via WiFi?

Thank you for any hint to get one step further!

One additional question/annotation: might it be a problem, that I use roslib (ros.h) with arduino but ros2 on the PC?

To find out, where the problem could be, I tried to write an arduino script, which publishes and subscribes only to itsself, not to the PC. Therefore I set the ip to the ip-address of the Arduino (which I figured out with Wifi.localIP())

But even in this simplification of the scenario the subscriber does not receive a signal. I even tried to use 127.0.0.1 as server ip as another simplification, but again on signal. Here is my code with Arduino-IDE and an Arduino WiFi Rev 2 board:

#define ROSSERIAL_ARDUINO_TCP
#include "arduino_secrets.h"
#include <WiFiNINA.h>
#include "ros.h"
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>
ros::NodeHandle nh;

// connect to arduino itsself:
IPAddress server(192, 168, 178, 60);
IPAddress ROS_MASTER_ADDRESS(192, 168, 178, 60); // ros master ip

char* ArduinoIP = "192.168.178.60";
//char* ArduinoIP = "127.0.0.1";

char* WIFI_SSID = SECRET_SSID; // network name
char* WIFI_PASSWD = SECRET_PASS; // network password

const uint16_t serverPort = 11311;
const uint16_t ROS_MASTER_PORT = 11311;


void messageCb( const std_msgs::Empty& toggle_msg){
  digitalWrite(13, HIGH-digitalRead(13));   // blink the led
  Serial.print("msg receveid: ");
  //Serial.println(char(toggle_msg));
}

ros::Subscriber<std_msgs::Empty> sub("toggle_led", messageCb );


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

// send to arduino itsself:
ros::Publisher chatter("toggle_led", &str_msg);

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

int pingResult;

void doPing() {
  
 pingResult = WiFi.ping(ArduinoIP);

  if (pingResult >= 0) {

    Serial.print("SUCCESS! RTT = ");

    Serial.print(pingResult);

    Serial.println(" ms");

  } else {

    Serial.print("FAILED! Error code: ");

    Serial.println(pingResult);

  }
}


void setup() {
  Serial.begin(9600);   

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }  
  nh.getHardware()->setConnection(server, serverPort);
  WiFi.begin(WIFI_SSID, WIFI_PASSWD);
  while (WiFi.status() != WL_CONNECTED) delay(500);

  //Serial.begin(115200);
  Serial.println("[ROS] Setting up...");  
  
  /* Start ROS communication module */

  nh.getHardware()->setConnection(ROS_MASTER_ADDRESS, ROS_MASTER_PORT);  

  Serial.println("local IP:");  
  Serial.println(WiFi.localIP());  

  nh.initNode();
  nh.advertise(chatter);
  nh.subscribe(sub);  
  Serial.println("try a ping to master:");
  doPing();  
  Serial.println("ping done");
  
}

void loop() {
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(500);
  Serial.println("in loop");
  
  
}

The ros.h import comes from this source:

https://www.hackster.io/metehan-emlik/a-ros-package-to-communicate- espcopters-using-wifi-c3b46e

"arduino_secrets.h" is just a separate file defining SECRET_SSID and SECRET_PASS



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source