ESP32 NodeMCU에 연결된 LED를 ROS로 제어하는 글입니다.

1. 회로도

left, right, front LED 세 개를 연결해 실제 자동차와 비슷하게 만들었습니다.

Untitled

Untitled

2. 동작 확인

Nodemcu를 Jetson에 끼웠을때 /dev/esp32Nodemcu가 있는지 확인해주세요. ttyUSB0 또는 ttyUSB1로 symbolic link됩니다.


jetson@jp4612GCv346Py37:~/catkin_ws$ ll /dev/esp*
lrwxrwxrwx 1 root root 7  3월 23 21:07 /dev/esp32Nodemcu -> ttyUSB1

젯슨에서 아래 명령어를 입력하여 LED를 제어할 수 있습니다.

# 터미널 #1
jetson@jp4612GCv346Py37:~/catkin_ws$ roslaunch jessicar2_bringup nodemcu32.launch

# 터미널 #2, jetson or PC
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic list
/cmd_vel
/diagnostics
/left_ticks
/quaternion
/rgbled
/right_ticks
/rosout
/rosout_agg

# 터미널 #2, jetson or PC
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic pub /rgbled std_msgs/Int16 0 --once
publishing and latching message for 3.0 seconds
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic pub /rgbled std_msgs/Int16 1 --once
publishing and latching message for 3.0 seconds
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic pub /rgbled std_msgs/Int16 2 --once
publishing and latching message for 3.0 seconds
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic pub /rgbled std_msgs/Int16 3 --once
publishing and latching message for 3.0 seconds
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic pub /rgbled std_msgs/Int16 4 --once
publishing and latching message for 3.0 seconds
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic pub /rgbled std_msgs/Int16 5 --once
publishing and latching message for 3.0 seconds
jetson@jp4612GCv346Py37:~/catkin_ws$ rostopic pub /rgbled std_msgs/Int16 6 --once
publishing and latching message for 3.0 seconds
jetson@jp4612GCv346Py37:~/catkin_ws$ roslaunch jessicar2_bringup nodemcu32.launch
... logging to /home/jetson/.ros/log/c2f74bce-d05c-11ed-828c-48b02d2ecb9c/roslaunch-jp4612GCv346Py37-3713.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server <http://jp4612GCv346Py37:37223/>

SUMMARY
========

PARAMETERS
 * /pwmConstants: [75, 42, 50, 80]
 * /rosdistro: melodic
 * /rosversion: 1.14.13
 * /serial_node/baud: 115200
 * /serial_node/port: /dev/esp32Nodemcu

NODES
  /
    serial_node (rosserial_python/serial_node.py)

auto-starting new master
process[master]: started with pid [3876]
ROS_MASTER_URI=http://localhost:11311

setting /run_id to c2f74bce-d05c-11ed-828c-48b02d2ecb9c
process[rosout-1]: started with pid [3939]
started core service [/rosout]
process[serial_node-2]: started with pid [3942]

3. 아두이노 스케치

LED에 관한 코드 요약입니다.

#include <ros.h>
#include <std_msgs/Int16.h>
:
// LED control pins
#define LED_L 19
#define LED_R 18
#define LED_B 5

/***********************Enumeration variable***********************/
enum LEDBEHAV {
  ALL_OFF = 0,  //all off
  LEDBACK,      //back on
  LEDLEFT,      //left on
  LEDRIGHT,     //right on
  LEDFRONT,     //left, right on
  ALL_ON        //all on
};

/*********************************************************
Function name: RGB()
Function function: control RGB output various colors
Function parameters: color, various color reference enumeration@color
The function returns: none
*********************************************************/
void RGB(enum LEDBEHAV ledbehav) {
  switch (ledbehav) {
    case ALL_OFF:
      digitalWrite(LED_R, HIGH);
      digitalWrite(LED_L, HIGH);
      digitalWrite(LED_B, HIGH);
      break;
    case LEDBACK:
      digitalWrite(LED_R, HIGH);
      digitalWrite(LED_L, HIGH);
      digitalWrite(LED_B, LOW);
      break;
    case LEDLEFT:
      digitalWrite(LED_R, HIGH);
      digitalWrite(LED_L, LOW);
      digitalWrite(LED_B, HIGH);
      break;
    case LEDRIGHT:
      digitalWrite(LED_R, LOW);
      digitalWrite(LED_L, HIGH);
      digitalWrite(LED_B, HIGH);
      break;
    case LEDFRONT:
      digitalWrite(LED_R, LOW);
      digitalWrite(LED_L, LOW);
      digitalWrite(LED_B, HIGH);
      break;
    case ALL_ON:
      digitalWrite(LED_R, LOW);
      digitalWrite(LED_L, LOW);
      digitalWrite(LED_B, LOW);
      break;
    default:
      digitalWrite(LED_R, HIGH);
      digitalWrite(LED_L, HIGH);
      digitalWrite(LED_B, HIGH);
      break;
  }
}

void ledcb( const std_msgs::Int16& msg){
  RGB(msg.data);   // set the pin state to the message data
}

ros::Subscriber<std_msgs::Int16> subLed("rgbled", &ledcb );

void setup() {
:
  // ROS Setup
  nh.getHardware()->setBaud(115200);
  nh.initNode();
  nh.subscribe(subLed);
}
 
void loop() {   
  nh.spinOnce();   
}

5장, QnA