专栏首页机器人课程与技术ESP8266和ROS调试一些问题汇总

ESP8266和ROS调试一些问题汇总

在库中安装如下:

务必注意版本号,最好用0.7.8,其他版本均出现不正常状况!

这里记录两个重要的功能:

  • ESP8266WiFi.h
  • ros.h

espros在github上下载,目前还是比较小众的,星星都没有过百呢!

两段代码简单测试一下:

chatter

#include <ESP8266WiFi.h>  
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Float64.h>

ros::NodeHandle nh;
int led;

void messageCb(const std_msgs::Int16& msg) {
  if(msg.data > 0){
    led=abs(msg.data);
    digitalWrite(led, HIGH-digitalRead(led));   // blink the led
  }
}

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);
ros::Subscriber<std_msgs::Int16> sub("led", &messageCb);

char hello[15] = "ESP8266 alive!";

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(15, OUTPUT);
  nh.initNode();
  nh.advertise(chatter);
  nh.subscribe(sub);
}

void loop()
{
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}

注意,2号可以开关wifi边上的小灯哦。

Sketch uses 266816 bytes (25%) of program storage space. Maximum is 1044464 bytes.
Global variables use 28452 bytes (34%) of dynamic memory, leaving 53468 bytes for local variables. Maximum is 81920 bytes.
esptool.py v2.6
2.6
esptool.py v2.6
Serial port /dev/ttyUSB0
Connecting....
Chip is ESP8266EX
Features: WiFi
MAC: 2c:f4:32:2d:58:0a
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 270976 bytes to 197513...

Writing at 0x00000000... (7 %)
Writing at 0x00004000... (15 %)
Writing at 0x00008000... (23 %)
Writing at 0x0000c000... (30 %)
Writing at 0x00010000... (38 %)
Writing at 0x00014000... (46 %)
Writing at 0x00018000... (53 %)
Writing at 0x0001c000... (61 %)
Writing at 0x00020000... (69 %)
Writing at 0x00024000... (76 %)
Writing at 0x00028000... (84 %)
Writing at 0x0002c000... (92 %)
Writing at 0x00030000... (100 %)
Wrote 270976 bytes (197513 compressed) at 0x00000000 in 4.6 seconds (effective 472.9 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

试下如下命令吧:

rosrun rosserial_arduino serial_node.py _port:=/dev/ttyUSB0

rostopic list

rostopic pub /led std_msgs/Int16 "data: 2"

rostopic echo /chatter

esproswifi

#include <ESP8266WiFi.h>
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Float64.h>
#include <Servo.h>

//
// WiFi Definitions //
//
const char* ssid = "HUAWEI_WiFi";
const char* password = "xxxxxxxx";

IPAddress server(192, 168, 3, 153); // 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
  }
};

Servo s;
int i;

void chatterCallback(const std_msgs::String& msg) {
  i = atoi(msg.data);
  s.write(i);
}

ros::Subscriber<std_msgs::String> sub("message", &chatterCallback);
ros::NodeHandle_<WiFiHardware> nh;

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");
}

void setup() {
  Serial.begin(115200);
  setupWiFi();
  delay(2000);
  s.attach(2);  // PWM pin
  nh.initNode();
  nh.subscribe(sub);
}

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

注意wifi名称和密码一定要正确!!!

然后就一切正常了:

使用如下命令:

rosrun rosserial_python serial_node.py tcp


#include <ESP8266WiFi.h>
#include <ros.h>
#include <std_msgs/String.h>
#include <std_msgs/Int16.h>
#include <std_msgs/Float64.h>
#include <Servo.h>

//
// WiFi Definitions //
//
const char* ssid = "HUAWEI_WiFi";
const char* password = "cslgcslg";

IPAddress server(192, 168, 3, 153); // 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
  }
};

Servo s;
int i;

void chatterCallback(const std_msgs::String& msg) {
  i = atoi(msg.data);
  s.write(i);
}


std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);
ros::Subscriber<std_msgs::String> sub("message", &chatterCallback);
ros::NodeHandle_<WiFiHardware> nh;
char hello[20] = "ESP8266 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");
}

void setup() {
  Serial.begin(115200);
  setupWiFi();
  delay(2000);
  s.attach(2);  // PWM pin
  nh.initNode();
  nh.advertise(chatter);
  nh.subscribe(sub);
}

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Ubuntu 18.04安装OpenCV4.0和环境配置

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明...

    zhangrelay
  • 使用机器人操作系统ROS 2和仿真软件Gazebo 9主题进阶实战(七)- mobot速度发布与里程计订阅

    在ROS2课程中已经学过并掌握了一个基本的发布器和订阅器(C++),官网的教程全部掌握大致需要20分钟吧。

    zhangrelay
  • ESP8266和ROS收发消息读取模拟量控制LED亮度

    rosrun rosserial_python serial_node.py tcp

    zhangrelay
  • 链表合并与节点交换——LeetCode 第 23&24 题

    今天的两道题目全都围绕链表,第一个是困难级别的、要合并多个排序的链表;第二题是中等难度,需要两两交换链表中的节点,昨天没能用递归法写出代码,今天就尝试用递归实现...

    TTTEED
  • Android日历控件的实现方法

    砸漏
  • k8s群集的三种Web-UI界面部署

    //这里使用的dashboard版本较高,相较于之前的版本访问必须使用火狐浏览器,这里不需要。

    小手冰凉
  • shell脚本心得(r2笔记58天)

    零零星星的接触到写一些shell也有一些日子了,发现自己已经犯了不少的错误,自我总结下。 选择合适的shell shell本身有很多种,大体有如下的几种。 /...

    jeanron100
  • 大数据面试题整理(部分)

      volatile的原理和实现机制 || volatile到底如何保证可见性和禁止指令重排序的?

    牛客网
  • 从通信到AI FreeSWITCH与WebRTC

    大家好,我是杜金房,此次分享和双向通讯服务器FreeSWITCH以及WebRTC有关。首先我会为大家简单介绍FreeSWITCH,鉴于FreeSWITCH主要用...

    LiveVideoStack
  • 翻译 | 可重入与线程安全

      在整个文档中,术语:「可重入和线程安全」用于标记类和函数,以表示它们如何在多线程应用程序中使用:

    Qt君

扫码关注云+社区

领取腾讯云代金券