esp32➡遥控篇➡turtlesim➡mobot➡turtlebot3

举报
zhangrelay 发表于 2021/09/29 23:06:24 2021/09/29
【摘要】 turtle1/cmd_velmobot/cmd_velcmd_vel 代码如下: #include <ros2arduino.h> #include <WiFi.h>#include <WiFiUdp.h>#include <WiFiClient.h> #defin...
  • turtle1/cmd_vel
  • mobot/cmd_vel
  • cmd_vel

代码如下:


  
  1. #include <ros2arduino.h>
  2. #include <WiFi.h>
  3. #include <WiFiUdp.h>
  4. #include <WiFiClient.h>
  5. #define SSID "***"
  6. #define SSID_PW "***"
  7. #define AGENT_IP "***"
  8. #define AGENT_PORT *** //AGENT port number
  9. #define LED 4
  10. #define PUBLISH_FREQUENCY 2 //hz
  11. char velflag=0;
  12. void publishVel(geometry_msgs::Twist* vel, void* arg)
  13. {
  14. (void)(arg);
  15. static int cnt = 0;
  16. if(velflag==0){
  17. vel->linear.x = 0; //线速度
  18. vel->angular.z = 0; //角速度
  19. }
  20. else if(velflag==1)
  21. {
  22. vel->linear.x = 1; //线速度
  23. vel->angular.z = 0; //角速度
  24. }
  25. else if(velflag==3)
  26. {
  27. vel->linear.x = -1; //线速度
  28. vel->angular.z = 0; //角速度
  29. }
  30. else if(velflag==2)
  31. {
  32. vel->linear.x = 0; //线速度
  33. vel->angular.z = 1; //角速度
  34. }
  35. else if(velflag==4)
  36. {
  37. vel->linear.x = 0; //线速度
  38. vel->angular.z = -1; //角速度
  39. }
  40. cnt++;
  41. }
  42. class VelPub : public ros2::Node
  43. {
  44. public:
  45. VelPub()
  46. : Node("esp32_cmdvel")
  47. {
  48. ros2::Publisher<geometry_msgs::Twist>* publisher_ = this->createPublisher<geometry_msgs::Twist>("cmd_vel");
  49. this->createWallFreq(PUBLISH_FREQUENCY, (ros2::CallbackFunc)publishVel, nullptr, publisher_);
  50. }
  51. };
  52. WiFiUDP udp;
  53. WiFiServer server(80);
  54. void setup()
  55. {
  56. pinMode(LED, OUTPUT);
  57. WiFi.begin(SSID, SSID_PW);
  58. while(WiFi.status() != WL_CONNECTED);
  59. server.begin();
  60. ros2::init(&udp, AGENT_IP, AGENT_PORT);
  61. }
  62. void loop()
  63. {
  64. static VelPub VelNode;
  65. ros2::spin(&VelNode);
  66. WiFiClient client = server.available(); // listen for incoming clients
  67. if (client) { // if you get a client,
  68. String currentLine = ""; // make a String to hold incoming data from the client
  69. while (client.connected()) { // loop while the client's connected
  70. if (client.available()) { // if there's bytes to read from the client,
  71. char c = client.read(); // read a byte, then
  72. if (c == '\n') { // if the byte is a newline character
  73. // if the current line is blank, you got two newline characters in a row.
  74. // that's the end of the client HTTP request, so send a response:
  75. if (currentLine.length() == 0) {
  76. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  77. // and a content-type so the client knows what's coming, then a blank line:
  78. client.println("HTTP/1.1 200 OK");
  79. client.println("Content-type:text/html");
  80. client.println();
  81. // the content of the HTTP response follows the header:
  82. client.print("Click <a href=\"/F\">here</a> to let the robot move forward. <br>");
  83. client.print("Click <a href=\"/B\">here</a> to let the robot move backward. <br>");
  84. client.print("Click <a href=\"/L\">here</a> to let the robot turn left. <br>");
  85. client.print("Click <a href=\"/R\">here</a> to let the robot turn right. <br>");
  86. client.print("Click <a href=\"/S\">here</a> to let the robot stop. <br>");
  87. // The HTTP response ends with another blank line:
  88. client.println();
  89. // break out of the while loop:
  90. break;
  91. } else { // if you got a newline, then clear currentLine:
  92. currentLine = "";
  93. }
  94. } else if (c != '\r') { // if you got anything else but a carriage return character,
  95. currentLine += c; // add it to the end of the currentLine
  96. }
  97. // Check to see if the client request was "GET /H" or "GET /L":
  98. if (currentLine.endsWith("GET /F")) {
  99. digitalWrite(LED, HIGH); // GET /H turns the LED on
  100. velflag=1;
  101. }
  102. if (currentLine.endsWith("GET /B")) {
  103. digitalWrite(LED, HIGH); // GET /L turns the LED off
  104. velflag=3;
  105. }
  106. if (currentLine.endsWith("GET /L")) {
  107. digitalWrite(LED, HIGH); // GET /H turns the LED on
  108. velflag=2;
  109. }
  110. if (currentLine.endsWith("GET /R")) {
  111. digitalWrite(LED, HIGH); // GET /L turns the LED off
  112. velflag=4;
  113. }
  114. if (currentLine.endsWith("GET /S")) {
  115. digitalWrite(LED, LOW); // GET /H turns the LED on
  116. velflag=0;
  117. }
  118. }
  119. }
  120. // close the connection:
  121. client.stop();
  122. }
  123. }

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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