Controlling an LED using ROS and an ESP32 Development Board

Controlling an LED using ROS and an ESP32 Development Board

 Introduction

Explain the purpose of the program and what it does.

Provide any necessary background information on the hardware and software used.

ROS setup

Explain how to install and set up ROS on your computer.

Provide any necessary configuration steps or troubleshooting tips.

Arduino setup

Explain how to set up the Arduino board and upload the sketch to it.

Provide any necessary configuration steps or troubleshooting tips.

ROS program

Explain how the ROS program works and what it does.

Provide a step-by-step explanation of the code.

Include any relevant snippets of code with comments.

Testing and usage

Explain how to test the program and use it to control the LED.

Provide examples of the rostopic pub command to control the LED.

Include any troubleshooting tips or common issues that may arise.

Conclusion

Summarize the main points of the documentation.

Provide any additional resources or references for further reading.

Remember to include clear and concise explanations, relevant code snippets, and screenshots or images where necessary to make the documentation easy to understand and follow.




#include <ros.h>

#include <std_msgs/String.h>


ros::NodeHandle nh;


const int LED_PIN = 2;


void messageCb(const std_msgs::String& toggle_msg) {

  if (toggle_msg.data == "on") {

    digitalWrite(LED_PIN, HIGH);

  } else if (toggle_msg.data == "off") {

    digitalWrite(LED_PIN, LOW);

  }

}


ros::Subscriber<std_msgs::String> sub("toggle_led", &messageCb);


void setup() {

  pinMode(LED_PIN, OUTPUT);

  nh.initNode();

  nh.subscribe(sub);

}


void loop() {

  nh.spinOnce();

  delay(1);

}



It looks like there might be an issue with the code running on the ESP32. Make sure that the LED is connected to the correct pin and that the pin is defined correctly in the code.

Here is an updated version of the code that should work to turn the LED on and off based on the "on" and "off" messages sent to the toggle_led topic:

cpp
#include <ros.h> 
#include <std_msgs/String.h> 
 ros::NodeHandle nh; 
const int LED_PIN = 2
void messageCb(const std_msgs::String& toggle_msg) 
{ if (toggle_msg.data == "on"
{ digitalWrite(LED_PIN, HIGH); 
 } 
else if (toggle_msg.data == "off"
{ digitalWrite(LED_PIN, LOW); } } 
ros::Subscriber<std_msgs::String> sub("toggle_led", &messageCb)
void setup() 
pinMode(LED_PIN, OUTPUT); 
 nh.initNode(); nh.subscribe(sub); 
void loop() 
 nh.spinOnce();
delay(1); 
}

Make sure to save this code as a new file and upload it to the ESP32 board. Once the code is running on the board, you can use the following commands to turn the LED on and off:

rust
rostopic pub /toggle_led std_msgs/String "data: 'on'" 
rostopic pub /toggle_led std_msgs/String "data: 'off'"

These commands should send the "on" and "off" messages to the toggle_led topic, which will trigger the messageCb function to turn the LED on and off.







Controlling an LED using ROS and an ESP32 Development Board

In this tutorial, we will learn how to control an LED using ROS (Robot Operating System) and an ESP32 development board. We will use ROS to send a message to the ESP32 board to toggle the state of the LED. This tutorial assumes that you have some experience with ROS and programming in C++.

Materials

  • An ESP32 development board
  • A breadboard
  • An LED
  • A 220 ohm resistor
  • Jumper wires

Setting up the hardware

First, let's set up the hardware. Connect the LED and resistor to the ESP32 development board as shown below:

ESP32 breadboard setup

  • Connect the anode (positive) leg of the LED to pin 14 of the ESP32.
  • Connect the cathode (negative) leg of the LED to the resistor.
  • Connect the other end of the resistor to the ground (GND) pin of the ESP32.

Setting up the software

Next, we need to set up the software. We will use the Arduino IDE to program the ESP32 board.

Installing the ROS library for Arduino

First, we need to install the ROS library for Arduino. This library allows us to communicate with ROS over serial. To install the library, follow these steps:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. In the search bar, search for "rosserial".
  4. Find the library called "rosserial-arduino" and click on the "Install" button.

Uploading the sketch to the ESP32 board

Next, we need to upload the sketch to the ESP32 board. Open the Arduino IDE and create a new sketch. Copy the following code into the sketch:

cpp
#include <ros.h> #include <std_msgs/Empty.h> ros::NodeHandle nh; void toggle_led(const std_msgs::Empty& msg) { static bool led_state = false; led_state = !led_state; digitalWrite(14, led_state); } ros::Subscriber<std_msgs::Empty> sub("toggle_led", &toggle_led); void setup() { pinMode(14, OUTPUT); nh.initNode(); nh.subscribe(sub); } void loop() { nh.spinOnce(); delay(1); }

This code sets up a ROS node that subscribes to the toggle_led topic. When a message is received on the topic, the toggle_led function is called, which toggles the state of the LED connected to pin 14 of the ESP32 board.

Before uploading the sketch, make sure that the ESP32 board is connected to your computer via USB. Also, make sure that you have selected the correct board and port in the Arduino IDE. To upload the sketch, click on the "Upload" button in the Arduino IDE.

Running the ROS node

Now that we have uploaded the sketch to the ESP32 board, we can run the ROS node on our computer. Follow these steps:

  1. Open a terminal and run the following command to start the ROS core:

    ruby
    $ roscore
  2. Open another terminal and run the following command to start the serial node:

    go
    $ rosrun rosserial_arduino serial_node.py _port:=/dev/ttyUSB0 _baud:=57600

    Note that the _port argument may need to be modified depending on the port to which the ESP32 board is connected.


  1. Open the Arduino IDE and create a new sketch.

  2. In the sketch, include the "ros.h" library by adding the following line at the top of the sketch:

    #include <ros.h>

  3. Declare a ROS node by adding the following lines of code:

    arduino
    ros::NodeHandle nh;
  4. Create a subscriber to listen for ROS messages on the "toggle_led" topic by adding the following lines of code:

    php
    void toggleLed(const std_msgs::Empty& toggle_msg){ // Code to toggle LED } ros::Subscriber<std_msgs::Empty> sub("toggle_led", toggleLed);
  5. In the setup function, initialize the ROS node and the subscriber by adding the following lines of code:

    perl
    nh.initNode(); nh.subscribe(sub);
  6. In the loop function, add the following line of code to allow the node to handle incoming messages:

    scss
    nh.spinOnce();
  7. Add the code to toggle the LED inside the "toggleLed" function.

  8. Upload the sketch to the ESP32 board and monitor the serial output to ensure that the node is successfully connected to ROS.

With these steps, we have successfully integrated the ESP32 board with ROS and created a ROS node to control the LED on the board using ROS messages.






Controlling an LED with ROS and Arduino

In this tutorial, we will use ROS and an Arduino board to control an LED. We will set up ROS on our computer, upload a sketch to the Arduino board, and write a ROS program to control the LED.

Hardware and software requirements

  • Arduino board
  • LED and resistor
  • Breadboard and wires
  • Computer running Ubuntu 18.04 or later with ROS installed
  • Arduino IDE

Setting up ROS

  1. Install ROS on your computer by following the instructions on the ROS wiki.

  2. Create a ROS workspace by running the following commands in the terminal:

    bash
    mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catkin_make
  3. Source your workspace by running the following command:

    bash
    source ~/catkin_ws/devel/setup.bash

Setting up the Arduino board

  1. Connect an LED to pin 2 of the Arduino board, with a resistor in series to limit the current.

  2. Open the Arduino IDE and create a new sketch.

  3. Copy the following code into the sketch:

    C++
    void setup() { pinMode(2, OUTPUT); } void loop() { digitalWrite(2, HIGH); delay(1000); digitalWrite(2, LOW); delay(1000); }
  4. Upload the sketch to the Arduino board.

Writing the ROS program

  1. Create a new ROS package by running the following commands in the terminal:

    bash
    cd ~/catkin_ws/src catkin_create_pkg led_control roscpp std_msgs
  2. Create a new file called led_control.cpp in the src directory of your package.

  3. Copy the following code into led_control.cpp:

    C++
    #include <ros.h> #include <std_msgs/Empty.h> ros::NodeHandle nh; const int LED_PIN = 2; void messageCb(const std_msgs::Empty& toggle_msg) { digitalWrite(LED_PIN, HIGH - digitalRead(LED_PIN)); // blink the led } ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb); void setup() { pinMode(LED_PIN, OUTPUT); nh.initNode(); nh.subscribe(sub); } void loop() { nh.spinOnce(); delay(1); }
  4. Build your ROS package by running the following command in the terminal:

    catkin_make
  5. Run the following command in the terminal to start the ROS program:

    rosrun led_control led_control

Testing and usage

  1. Open a new terminal and run the following command to list the available ROS topics:

    rostopic list
  2. You should see a topic called /toggle_led. Run the following command to get more information about the topic:

    bash
    rostopic info /toggle_led
  3. Open another terminal and run the following command to toggle the LED:

    bash
    rostopic pub /toggle_led std_msgs/Empty "{}"
  4. You should see the LED on the Arduino board blink on and off.

Conclusion

In this tutorial, we have shown how to use ROS and an Arduino board to control an LED. We have set up ROS on our computer, uploaded a sketch to the Arduino board, and written a ROS program to control the LED. By following these steps, you can control the LED using ROS



ubuntu@ubuntu-Aspire-A515-57G:~$ rosrun turtlesim turtle_teleop_key 
Reading from keyboard
---------------------------
Use arrow keys to move the turtle. 'q' to quit.


















Post a Comment

Previous Post Next Post