프로그래밍/ROS1

[ROS serial] rosserial을 이용해 아두이노와 통신하기

Se-chan Oh 2021. 8. 3. 18:41

1. 개발환경

 

OS : 우분투 20.04 Desktop

ROS 버전 : ROS noetic

아두이노 : 아두이노 우노

 

2. 아두이노 설치

 

아두이노 설치 경로 : https://www.arduino.cc/en/software

 

Software

Open-source electronic prototyping platform enabling users to create interactive electronic objects.

www.arduino.cc

 

아두이노 tar 파일 압축 풀기 과정은 생략하겠다.

 

아두이노 IDE 설치한다.

cd ~/arduino-1.8.15/
sudo ./install.sh

 

rosserial 기본 예제파일을 설치한다.

cd ~/arduino-1.8.15/libraries
rm -rf ros_lib   #기존에 ros_lib가 설치되어 있었다면 제거부터해줘야한다.
rosrun rosserial_arduino make_libraries.py .

 

아두이노를 실행한다.

arduino

 

만약 아두이노 IDE의 글자가 깨져서 보인다면 ctrl + , 을 눌러서 환경설정 창에 진입한다.

'System Default'라고 적힌 곳을 클릭해서 English로 변경한다.

설정 창의 맨 아래 2개의 버튼 중에서 왼쪽 버튼을 눌러야 설정이 적용된다.

 

이제 아두이노 IDE에 rosserial 라이브러리를 설치하겠다.

Sketch -> Include Library -> Manage Libraries 매뉴로 들어가서 rosserial 을 검색하고 설치한다.

이제 아두이노에서 ros.h를 사용할 수 있다.

 

3. ROS에서 개발환경 세팅

 

rosserial 패키지를 다운로드한다.

sudo apt-get install ros-melodic-rosserial ros-melodic-rosserial-arduino

 

다음 명령어를 입력하여 포트 연결이 되어있는지 확인한다. (필자는 ttyUSB0 포트에 연결했다.)

dmesg | grep tty

 

포트 사용을 위해 모든 유저에게 읽고 쓰는 권한을 준다.

sudo chmod a+rw /dev/ttyUSB0

 

4. 예제파일 실행해보기

 

아두이노를 열고 파일 > 예제 > ros_lib > HelloWorld 선택하고 아두이노에 업로드한다.

 

다음 명령어를 각각 다른 터미널에서 실행한다.

roscore
rosrun rosserial_python serial_node.py _port:=/dev/ttyUSB0 _baud:=57600
rostopic echo chatter

 

5. 소스 코드

 

HelloWorld 파일의 내용은 다음과 같다.

/*
 * rosserial Publisher Example
 * Prints "hello world!"
 */
#include <ros.h>
#include <std_msgs/String.h>

ros::NodeHandle  nh;

std_msgs::String str_msg;
ros::Publisher chatter("chatter", &str_msg);

char hello[13] = "hello world!";

void setup()
{
  nh.initNode();
  nh.advertise(chatter);
}

void loop()
{  
  str_msg.data = hello;
  chatter.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}

 

6. 참고

 

블로그

 

https://happyobo.github.io/ros/%EC%95%84%EB%91%90%EC%9D%B4%EB%85%B8/ros7-post/

 

https://95mkr.tistory.com/entry/ROS8

 

https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=chandong83&logNo=220851270328

 

ROS.org 공식 문서 Arduino IDE Setup