ROS2 and Arduino serial port and WiFi test records (Due and ESP32)

There are mature solutions for ROS2 and Arduino debugging. I wrote a detailed blog post more than a year ago. Now that the relevant IoT MCU course (ESP32) has been developed and tested, I will review the previous ones (this article does not include ROS1).

>ESP32_wifi code at the end of the article<

In 2020, the port is also changed to 2020 (the default is 2018) to test:

Now the official has been updated, but the installation and use process is exactly the same, please refer to the official tutorial (ros2arduino).

Serial port!

Due in English

Of course, Chinese can be fully supported, as shown below:

Just modify the corresponding position of the following code:

The development board type can be seen in the lower right corner.

Then I debugged ESP32 and found that the same is OK, the serial port is very easy to use, and the effect is very good.

The specific output is as follows:

Wireless LAN!

So let's test the wifi, adjust the code first, as shown below:

It was found that the direct success did not encounter any problems!

Note that you must start xrcedds, the IP address of the ESP32 development board is as follows:

At present, tcp is not supported, it is best to use udp, the test is very stable.

publisher_wifi_udpesp32.ino

#include <ros2arduino.h>

#include <WiFi.h>
#include <WiFiUdp.h>

#define PUBLISH_FREQUENCY 2 //hz

const char* SSID="*********";
const char* SSID_PW="*********";

#define AGENT_IP   "172.20.10.3"
#define AGENT_PORT 2020 //AGENT port number

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

  static int cnt = 0;
  sprintf(msg->data, "来一起学习ros2吧(ESP32WiFi测试) %d", cnt++);
}

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

WiFiUDP udp;

void setup() 
{
  Serial.begin(115200);
  WiFi.begin(SSID, SSID_PW);
  while(WiFi.status() != WL_CONNECTED)
  {   
    Serial.print("\n正在连接 "); 
    Serial.println(SSID);
    delay(500);
  }
  Serial.print("已经准备好,使用 ");
  Serial.print(WiFi.localIP());
  Serial.println(" 连接ROS2");
  ros2::init(&udp, AGENT_IP, AGENT_PORT);
}

void loop() 
{
  static StringPub StringNode;

  ros2::spin(&StringNode);
}

If you think this code is too LOW, it doesn't matter, there are the following configuration modes, using micro-ros2 is great, and you can update it when you have time!

 


 

Guess you like

Origin blog.csdn.net/ZhangRelay/article/details/109409535