ESP8266和ROS调试一些问题汇总

举报
zhangrelay 发表于 2021/07/15 01:06:12 2021/07/15
【摘要】 在库中安装如下: 务必注意版本号,最好用0.7.8,其他版本均出现不正常状况! 这里记录两个重要的功能: ESP8266WiFi.hros.h espros在github上下载,目前还是比较小众的,星星都没有过百呢! 两段代码简单测试一下: chatter #include <ESP8266WiFi.h> #include <ros.h&g...

在库中安装如下:

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

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

  • ESP8266WiFi.h
  • ros.h

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

两段代码简单测试一下:

chatter


  
  1. #include <ESP8266WiFi.h>
  2. #include <ros.h>
  3. #include <std_msgs/String.h>
  4. #include <std_msgs/Int16.h>
  5. #include <std_msgs/Float64.h>
  6. ros::NodeHandle nh;
  7. int led;
  8. void messageCb(const std_msgs::Int16& msg) {
  9. if(msg.data > 0){
  10. led=abs(msg.data);
  11. digitalWrite(led, HIGH-digitalRead(led)); // blink the led
  12. }
  13. }
  14. std_msgs::String str_msg;
  15. ros::Publisher chatter("chatter", &str_msg);
  16. ros::Subscriber<std_msgs::Int16> sub("led", &messageCb);
  17. char hello[15] = "ESP8266 alive!";
  18. void setup()
  19. {
  20. pinMode(2, OUTPUT);
  21. pinMode(12, OUTPUT);
  22. pinMode(13, OUTPUT);
  23. pinMode(15, OUTPUT);
  24. nh.initNode();
  25. nh.advertise(chatter);
  26. nh.subscribe(sub);
  27. }
  28. void loop()
  29. {
  30. str_msg.data = hello;
  31. chatter.publish( &str_msg );
  32. nh.spinOnce();
  33. delay(1000);
  34. }

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


  
  1. Sketch uses 266816 bytes (25%) of program storage space. Maximum is 1044464 bytes.
  2. Global variables use 28452 bytes (34%) of dynamic memory, leaving 53468 bytes for local variables. Maximum is 81920 bytes.
  3. esptool.py v2.6
  4. 2.6
  5. esptool.py v2.6
  6. Serial port /dev/ttyUSB0
  7. Connecting....
  8. Chip is ESP8266EX
  9. Features: WiFi
  10. MAC: 2c:f4:32:2d:58:0a
  11. Uploading stub...
  12. Running stub...
  13. Stub running...
  14. Changing baud rate to 460800
  15. Changed.
  16. Configuring flash size...
  17. Auto-detected Flash size: 4MB
  18. Compressed 270976 bytes to 197513...
  19. Writing at 0x00000000... (7 %)
  20. Writing at 0x00004000... (15 %)
  21. Writing at 0x00008000... (23 %)
  22. Writing at 0x0000c000... (30 %)
  23. Writing at 0x00010000... (38 %)
  24. Writing at 0x00014000... (46 %)
  25. Writing at 0x00018000... (53 %)
  26. Writing at 0x0001c000... (61 %)
  27. Writing at 0x00020000... (69 %)
  28. Writing at 0x00024000... (76 %)
  29. Writing at 0x00028000... (84 %)
  30. Writing at 0x0002c000... (92 %)
  31. Writing at 0x00030000... (100 %)
  32. Wrote 270976 bytes (197513 compressed) at 0x00000000 in 4.6 seconds (effective 472.9 kbit/s)...
  33. Hash of data verified.
  34. Leaving...
  35. 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


  
  1. #include <ESP8266WiFi.h>
  2. #include <ros.h>
  3. #include <std_msgs/String.h>
  4. #include <std_msgs/Int16.h>
  5. #include <std_msgs/Float64.h>
  6. #include <Servo.h>
  7. //
  8. // WiFi Definitions //
  9. //
  10. const char* ssid = "HUAWEI_WiFi";
  11. const char* password = "xxxxxxxx";
  12. IPAddress server(192, 168, 3, 153); // ip of your ROS server
  13. IPAddress ip_address;
  14. int status = WL_IDLE_STATUS;
  15. WiFiClient client;
  16. class WiFiHardware {
  17. public:
  18. WiFiHardware() {};
  19. void init() {
  20. // do your initialization here. this probably includes TCP server/client setup
  21. client.connect(server, 11411);
  22. }
  23. // read a byte from the serial port. -1 = failure
  24. int read() {
  25. // implement this method so that it reads a byte from the TCP connection and returns it
  26. // you may return -1 is there is an error; for example if the TCP connection is not open
  27. return client.read(); //will return -1 when it will works
  28. }
  29. // write data to the connection to ROS
  30. void write(uint8_t* data, int length) {
  31. // implement this so that it takes the arguments and writes or prints them to the TCP connection
  32. for(int i=0; i<length; i++)
  33. client.write(data[i]);
  34. }
  35. // returns milliseconds since start of program
  36. unsigned long time() {
  37. return millis(); // easy; did this one for you
  38. }
  39. };
  40. Servo s;
  41. int i;
  42. void chatterCallback(const std_msgs::String& msg) {
  43. i = atoi(msg.data);
  44. s.write(i);
  45. }
  46. ros::Subscriber<std_msgs::String> sub("message", &chatterCallback);
  47. ros::NodeHandle_<WiFiHardware> nh;
  48. void setupWiFi()
  49. {
  50. WiFi.begin(ssid, password);
  51. Serial.print("\nConnecting to "); Serial.println(ssid);
  52. uint8_t i = 0;
  53. while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
  54. if(i == 21){
  55. Serial.print("Could not connect to"); Serial.println(ssid);
  56. while(1) delay(500);
  57. }
  58. Serial.print("Ready! Use ");
  59. Serial.print(WiFi.localIP());
  60. Serial.println(" to access client");
  61. }
  62. void setup() {
  63. Serial.begin(115200);
  64. setupWiFi();
  65. delay(2000);
  66. s.attach(2); // PWM pin
  67. nh.initNode();
  68. nh.subscribe(sub);
  69. }
  70. void loop() {
  71. nh.spinOnce();
  72. delay(500);
  73. }

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

然后就一切正常了:

使用如下命令:

rosrun rosserial_python serial_node.py tcp



  
  1. #include <ESP8266WiFi.h>
  2. #include <ros.h>
  3. #include <std_msgs/String.h>
  4. #include <std_msgs/Int16.h>
  5. #include <std_msgs/Float64.h>
  6. #include <Servo.h>
  7. //
  8. // WiFi Definitions //
  9. //
  10. const char* ssid = "HUAWEI_WiFi";
  11. const char* password = "cslgcslg";
  12. IPAddress server(192, 168, 3, 153); // ip of your ROS server
  13. IPAddress ip_address;
  14. int status = WL_IDLE_STATUS;
  15. WiFiClient client;
  16. class WiFiHardware {
  17. public:
  18. WiFiHardware() {};
  19. void init() {
  20. // do your initialization here. this probably includes TCP server/client setup
  21. client.connect(server, 11411);
  22. }
  23. // read a byte from the serial port. -1 = failure
  24. int read() {
  25. // implement this method so that it reads a byte from the TCP connection and returns it
  26. // you may return -1 is there is an error; for example if the TCP connection is not open
  27. return client.read(); //will return -1 when it will works
  28. }
  29. // write data to the connection to ROS
  30. void write(uint8_t* data, int length) {
  31. // implement this so that it takes the arguments and writes or prints them to the TCP connection
  32. for(int i=0; i<length; i++)
  33. client.write(data[i]);
  34. }
  35. // returns milliseconds since start of program
  36. unsigned long time() {
  37. return millis(); // easy; did this one for you
  38. }
  39. };
  40. Servo s;
  41. int i;
  42. void chatterCallback(const std_msgs::String& msg) {
  43. i = atoi(msg.data);
  44. s.write(i);
  45. }
  46. std_msgs::String str_msg;
  47. ros::Publisher chatter("chatter", &str_msg);
  48. ros::Subscriber<std_msgs::String> sub("message", &chatterCallback);
  49. ros::NodeHandle_<WiFiHardware> nh;
  50. char hello[20] = "ESP8266 wifi alive!";
  51. void setupWiFi()
  52. {
  53. WiFi.begin(ssid, password);
  54. Serial.print("\nConnecting to "); Serial.println(ssid);
  55. uint8_t i = 0;
  56. while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
  57. if(i == 21){
  58. Serial.print("Could not connect to"); Serial.println(ssid);
  59. while(1) delay(500);
  60. }
  61. Serial.print("Ready! Use ");
  62. Serial.print(WiFi.localIP());
  63. Serial.println(" to access client");
  64. }
  65. void setup() {
  66. Serial.begin(115200);
  67. setupWiFi();
  68. delay(2000);
  69. s.attach(2); // PWM pin
  70. nh.initNode();
  71. nh.advertise(chatter);
  72. nh.subscribe(sub);
  73. }
  74. void loop() {
  75. str_msg.data = hello;
  76. chatter.publish( &str_msg );
  77. nh.spinOnce();
  78. delay(500);
  79. }


 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/108762844

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。