esp32与ros2的欢乐启程

2021-12-02 13:16:02 浏览数 (2)

超级简单,先看效果:

esp32的代码也超级简单(udp4):
代码语言:javascript复制
#include <ros2arduino.h>

#include <WiFi.h>
#include <WiFiUdp.h>
#define SSID       "Relay_iPhone"
#define SSID_PW    "66666666"
#define AGENT_IP   "172.20.10.2"
#define AGENT_PORT 2021 //AGENT port number

#define PUBLISH_FREQUENCY 1 //hz

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

  static int cnt = 0;
  sprintf(msg->data, "欢乐的esp32和ros2 %d", cnt  );
}

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

WiFiUDP udp;

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

  ros2::init(&udp, AGENT_IP, AGENT_PORT);
}

void loop() 
{
  static StringPub StringNode;

  ros2::spin(&StringNode);
}

稳定程度感觉上一般。

更有趣的各种功能会逐步介绍的。

比如esp32 wemos版

具体引脚:

代码语言:javascript复制
static const uint8_t TX = 1;
static const uint8_t RX = 3;

static const uint8_t SDA = 21;
static const uint8_t SCL = 22;

static const uint8_t SS    = 5;
static const uint8_t MOSI  = 23;
static const uint8_t MISO  = 19;
static const uint8_t SCK   = 18;

static const uint8_t A0 = 36;
static const uint8_t A3 = 39;
static const uint8_t A4 = 32;
static const uint8_t A5 = 33;
static const uint8_t A6 = 34;
static const uint8_t A7 = 35;
static const uint8_t A10 = 4;
static const uint8_t A11 = 0;
static const uint8_t A12 = 2;
static const uint8_t A13 = 15;
static const uint8_t A14 = 13;
static const uint8_t A15 = 12;
static const uint8_t A16 = 14;
static const uint8_t A17 = 27;
static const uint8_t A18 = 25;
static const uint8_t A19 = 26;

static const uint8_t T0 = 4;
static const uint8_t T1 = 0;
static const uint8_t T2 = 2;
static const uint8_t T3 = 15;
static const uint8_t T4 = 13;
static const uint8_t T5 = 12;
static const uint8_t T6 = 14;
static const uint8_t T7 = 27;
static const uint8_t T8 = 33;
static const uint8_t T9 = 32;

static const uint8_t DAC1 = 25;
static const uint8_t DAC2 = 26;

LED_BUILTIN等

代码语言:javascript复制
static const uint8_t LED_BUILTIN = 2;
#define BUILTIN_LED  LED_BUILTIN // backward compatibility
static const uint8_t _VBAT = 35; // battery voltage

#define PIN_WIRE_SDA SDA // backward compatibility
#define PIN_WIRE_SCL SCL // backward compatibility

static const uint8_t D0   = 26;
static const uint8_t D1   = 22;
static const uint8_t D2   = 21;
static const uint8_t D3   = 17;
static const uint8_t D4   = 16;
static const uint8_t D5   = 18;
static const uint8_t D6   = 19;
static const uint8_t D7   = 23;
static const uint8_t D8   = 5;
static const uint8_t RXD  = 3;
static const uint8_t TXD  = 1;

与图示对应。

0 人点赞