eps32和ros2之稳稳点亮一个LED灯(IO4)


源码:

#include <ros2arduino.h>

#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
//#include <WebServer.h>
//#include <ESPmDNS.h>

#define SSID       "***"
#define SSID_PW    "***"
#define AGENT_IP   "***"
#define AGENT_PORT *** //AGENT port number

#define LED 4
#define PUBLISH_FREQUENCY 1 //hz

char ledflag=2;

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

  static int cnt = 0;
  if(ledflag==2){
    sprintf(msg->data, "欢乐的esp32和ros2 %d", cnt++);
  }
  else if(ledflag==0)
  {
    sprintf(msg->data, "欢乐的esp32灯灭了 %d", cnt++);
  }
  else if(ledflag==1)
  {
    sprintf(msg->data, "欢乐的esp32灯亮了 %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;

WiFiServer server(80);

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

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

void loop() 
{
  static StringPub StringNode;
  ros2::spin(&StringNode);
  
  WiFiClient client = server.available();   // listen for incoming clients
  if (client) {                             // if you get a client,
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 4 on. cslg<br>");
            client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 4 off. cslg<br>");
            //client.print("Click <a href=\"/Blink\">here</a> to turn the LED on pin 2 off. cslg<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(LED, LOW);               // GET /H turns the LED on
        //  sprintf(msg->data, "灯亮了");
          ledflag=1;
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(LED, HIGH);                // GET /L turns the LED off
        //  sprintf(msg->data, "灯灭了");
          ledflag=0;
        }
      }
    }
    // close the connection:
    client.stop();
  }
}

***需要自己依据网络情况配置。 


默认灯亮,显示欢乐esp32和ros2。

点击off,效果如下:

点击on 

显示1s一次,有滞后,再关闭。

多次测试后:

稳定工作10分钟,问题不大。最多测试1小时。


 -后续,esp32做成ros2机器人遥控器-


 

 

 

 

 

要使用ROS控制Arduino点亮LED,需要进行以下步骤: 1. 在Arduino上连接一个LED一个电阻器。将LED一个引脚连接到Arduino的数字引脚(例如D13),将另一个引脚连接到电阻器,然后将电阻器的另一端连接到Arduino的GND引脚。 2. 在Arduino IDE中编写一个简单的程序,使LED在接收到来自ROS的指令时点亮或熄灭。例如,以下代码将LED连接到D13引脚,并通过ROS订阅名为“led”的主题来控制: ``` int ledPin = 13; bool ledState = false; void setup() { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); rosInit(); } void loop() { rosSpinOnce(); } void ledCallback(const std_msgs::Bool& msg) { ledState = msg.data; digitalWrite(ledPin, ledState ? HIGH : LOW); } void rosInit() { // 初始化ROS节点 int argc = 0; char** argv = NULL; ros::init(argc, argv, "arduino_led"); // 创建ROS节点句柄 nh = new ros::NodeHandle(); // 创建ROS订阅者 sub_led = nh->subscribe("led", 1, ledCallback); } void rosSpinOnce() { // 处理ROS消息 ros::spinOnce(); } ``` 3. 在ROS中创建一个名为“led”的主题,并使用rostopic命令向其发送消息以控制LED。例如,以下命令将LED点亮: ``` rostopic pub /led std_msgs/Bool "data: true" ``` 4. 检查Arduino串口输出,确保它接收到了来自ROS的消息并正确地控制了LED。例如,如果使用Serial.println()语句在Arduino程序中输出调试信息,则可以使用Serial Monitor查看这些信息。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangrelay

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

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

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

打赏作者

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

抵扣说明:

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

余额充值