ros+arduino学习(9):esp32与ros局域网话题通讯

69 篇文章 22 订阅
20 篇文章 2 订阅
本文详细介绍了如何在ArduinoIDE中添加ESP32硬件支持,并重构ROS库进行通信。通过示例代码展示了如何使用ESP32与ROS节点进行连接,发布和订阅消息,以及处理ADC数据。在遇到通信问题时,建议检查无线网络设置。
摘要由CSDN通过智能技术生成

1.arduino ide添加esp32硬件支持

Arduino ide配置esp32硬件支持_qqliuzhitong的博客-CSDN博客

2.重构ros_lib库

这里直接看这个链接就可以。

ros+arduino学习(六):重构ros_lib库文件_qqliuzhitong的博客-CSDN博客

3.esp32代码

#include "WiFi.h"
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Float64.h>
#include <rosserial_arduino/Adc.h>

//
// WiFi Definitions //
//
const char* ssid = "Zhitong";
const char* password = "95359897";
 
IPAddress server(192, 168, 191, 2); // ip of your ROS server
IPAddress ip_address;
int status = WL_IDLE_STATUS;

WiFiClient client;
 
class WiFiHardware {
 
  public:
  WiFiHardware() {};
 
  void init() {
    // do your initialization here. this probably includes TCP server/client setup
    client.connect(server, 11411);
  }
 
  // read a byte from the serial port. -1 = failure
  int read() {
    // implement this method so that it reads a byte from the TCP connection and returns it
    //  you may return -1 is there is an error; for example if the TCP connection is not open
    return client.read();         //will return -1 when it will works
  }
 
  // write data to the connection to ROS
  void write(uint8_t* data, int length) {
    // implement this so that it takes the arguments and writes or prints them to the TCP connection
    for(int i=0; i<length; i++)
      client.write(data[i]);
  }
 
  // returns milliseconds since start of program
  unsigned long time() {
     return millis(); // easy; did this one for you
  }
};
 
int i;
 
void chatterCallback(const std_msgs::String& msg) {
  i = atoi(msg.data);
}
 
 
std_msgs::String str_msg;
rosserial_arduino::Adc adc_msg;
ros::Publisher chatter("chatter", &str_msg);
ros::Publisher p("adc", &adc_msg);
ros::Subscriber<std_msgs::String> sub("message", &chatterCallback);
ros::NodeHandle_<WiFiHardware> nh;
char hello[20] = "ESP32 wifi alive!";
 
 
void setupWiFi()
{
  WiFi.begin(ssid, password);
  Serial.print("\nConnecting to "); Serial.println(ssid);
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
  if(i == 21){
    Serial.print("Could not connect to"); Serial.println(ssid);
    while(1) delay(500);
  }
  Serial.print("Ready! Use ");
  Serial.print(WiFi.localIP());
  Serial.println(" to access client");
}
 
int averageAnalog(int pin){
  int v=0;
  for(int i=0; i<4; i++) v+= analogRead(pin);
  return v/4;
}
 
void setup() {
  Serial.begin(115200);
  setupWiFi();
  delay(2000);
  nh.initNode();
  nh.advertise(chatter);
  nh.subscribe(sub);
  nh.advertise(p);
}
 
void loop() {
  str_msg.data = hello;
  chatter.publish( &str_msg );
  adc_msg.adc0 = averageAnalog(0);
  p.publish(&adc_msg);
  nh.spinOnce();
  delay(500);
}

4.上传程序并测试

运行

rosrun rosserial_python serial_node.py tcp

如果一直建立不了通信,或者建立通信之后一会儿就掉线了,那可能是无线热点的问题或者路由器的问题,换一个无线路由器或者无线网卡再试试! 

连接成功后就可以查看话题消息: 

参考链接:

ESP32与ROS调试笔记(Linux和Windows)_zhangrelay的专栏-CSDN博客

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen953

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值