生活随笔
收集整理的這篇文章主要介紹了
ROS环境下串口通信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 環境:
操作系統: Ubuntu 14.04 ROS版本: ROS Indigo
2. 步驟:
2.1 下載安裝ROS對應版本的工具包(此處為indigo版)
sudo apt
-get -install ros
-indigo -serial
重啟終端,輸入以下命令可以檢測到serial包的路徑說明已經安裝好:(路徑為 opt/ros/indigo/share/serial)
roscd serial
2.2 使用ros自帶的serial包,編寫節點
#include < ros/ros
. h
>
#include < serial/serial
. h
>
#include < std_msgs/
String . h
>
#include < std_msgs/Empty
. h
> serial
::Serial ser;
void write_callback(const std_msgs
::String ::ConstPtr & msg)
{ ROS_INFO_STREAM(
"Writing to serial port" << msg
-> data ); ser
. write(msg
-> data );
} int main (int argc, char
** argv)
{ ros
::init (argc, argv,
"serial_example_node" ); ros
::NodeHandle nh; ros
::Subscriber write_sub
= nh
. subscribe(
"write" ,
1000 , write_callback); ros
::Publisher read_pub
= nh
. advertise
< std_msgs
::String > (
"read" ,
1000 ); try { ser
. setPort(
"/dev/ttyUSB0" ); ser
. setBaudrate(
115200 ); serial
::Timeout to = serial
::Timeout ::simpleTimeout (
1000 ); ser
. setTimeout(
to ); ser
. open(); } catch (serial
::IOException & e) { ROS_ERROR_STREAM(
"Unable to open port " );
return - 1 ; }
if (ser
. isOpen()) { ROS_INFO_STREAM(
"Serial Port initialized" ); }
else {
return - 1 ; } ros
::Rate loop_rate(
50 );
while (ros
::ok ()) {
if (ser
. available()){ ROS_INFO_STREAM(
"Reading from serial port\n" ); std_msgs
::String result; result
. data = ser
. read(ser
. available()); ROS_INFO_STREAM(
"Read: " << result
. data ); read_pub
. publish(result); } ros
::spinOnce (); loop_rate
. sleep(); }
}
3. 遇到問題
-如果出現如下錯誤,則是因為權限不夠引起的
terminate called
after throwing
an instance
of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >' what():
open : Permission denied
Aborted (core dumped)
-通過改變權限就能解決這個問題:
sudo chmod
666 /dev/ttyUSB0
總結
以上是生活随笔 為你收集整理的ROS环境下串口通信 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。