Arduino學習筆記(15)--Arduino & ROS

目錄

1. Why Arduino and ROS?

2. Arduino IDE for Linux

2.1 下載Arduino IDE for linux

2.2 安裝IDE

2.3 添加需要的庫文件

3. 安裝ros_lib into the Arduino Environment

3.1 下載源代碼

3.2 apt-get安裝

3.3 安裝ros_lib

4. 測試ROS in Arduino


1. Why Arduino and ROS?

 

ROS(Robot Operation System)是一個開源機器人操作系統,它是一個分佈式的計算架構,集成了底層硬件驅動、節點間通訊、機器人控制、仿真環境和跨平臺等功能。

Arduino主要應用於控制電機驅動和各類傳感器(比如超聲波、IMU、里程計、電機轉速PID控制等),然後通過收發消息等到ros_topic和 service、action通訊方式與ROS上機位通訊,實現數據的雙向傳輸。

The Arduino and Arduino IDE are great tools for quickly and easily programming hardware. Using the rosserial_arduino package, you can use ROS directly with the Arduino IDE. rosserial provides a ROS communication protocol that works over your Arduino's UART. It allows your Arduino to be a full fledged ROS node which can directly publish and subscribe to ROS messages, publish TF transforms, and get the ROS system time.

NOTE: If you do not already have an Arduino IDE installed, download it from the Arduino website. It is best to install the application into a folder on the application PATH, the desktop, or home folder. Once installed, launch the application to select your sketchbook location. (See arduino official website, sketchbook is a standard place to store your programs, or sketches). Close the IDE when done.

Our ROS bindings are implemented as an Arduino library. Like all Arduino libraries, ros_lib works by putting its library implementation into the libraries folder of your sketchbook. If there is not already a libraries folder in your sketchbook, make one. You can then install the library using the instructions below.


2. Arduino IDE for Linux

詳細教程請參考wiki官方教程:http://wiki.ros.org/rosserial_arduino/Tutorials/Arduino%20IDE%20Setup

2.1 下載Arduino IDE for linux

下載地址:https://www.arduino.cc/en/Main/Software

按照設備類型選擇合適的版本下載即可,這裏選擇linux 64 bits版本。

2.2 安裝IDE

熟悉linux系統下的壓縮和解壓操作:

tar -xvf 文件名


將文件夾拷貝到所需要的地方,然後cd到剛解壓的arduino目錄下,可以看到文件如下:

安裝IDE:

sudo bash ./install.sh

OK,安裝成功,測試一下。

cd ..
arduino


2.3 添加需要的庫文件

將庫文件解壓至~/Arduino/libraries目錄下即可


3. 安裝ros_lib into the Arduino Environment

3.1 下載源代碼

Source build instructions are different for groovy+ (catkin) than for earlier (rosbuild) releases. Select the build system based on your release to see appropriate instructions.

Rosserial has been catkin-ized since the groovy release, and the workflow is a bit different from fuerte and earlier releases. Rather than running the library generator over each package you want to use, you run it once and generate libraries for all installed messages. In the instructions below, <ws> represents your catkin workspace.

cd catkin_ws/src
git clone https://github.com/ros-drivers/rosserial.git
cd ..
catkin_make
catkin_make install

但是,源碼編譯容易出現各種未知錯誤,建議使用二進制apt-get安裝

若boost出錯請查看:https://www.boost.org/doc/libs/1_59_0/more/getting_started/unix-variants.html

3.2 apt-get安裝

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

 

3.3 安裝ros_lib

前面的安裝步驟創建了必要的庫,現在下面的代碼將創建ros_lib文件夾,Arduino構建環境需要此文件夾才能使Arduino程序與ROS交互。

在下面的步驟中,<sketchbook>是Linux Arduino環境保存文件的目錄。通常,這是您主目錄中的一個名爲Sketchbook或Arduino的目錄。例如cd〜/ Arduino / libraries

或者,您可以安裝到Windows Arduino環境中。

groovy源(catkin)的Ros_lib安裝說明與早期版本(rosbuild)或二進制發行版不同。確保您在上面選擇了正確的構建系統以查看適當的說明-catkin用於常規的源構建,否則爲rosbuild。


注意:您必須刪除library / ros_lib(如果存在),以便重新生成,因爲它的存在會導致錯誤。“ rosrun rosserial_arduino make_libraries.py”創建ros_lib目錄。

cd ~/Arduino/libraries
rm -rf ros_lib
rosrun rosserial_arduino make_libraries.py .

安裝完成之後可以看到在Arduino/libraries目錄下多了ros_lib文件夾


4. 測試ROS in Arduino

執行命令,打開Arduino IDE,

查看示例代碼庫,ros_lib


#include <ros.h>      //包含ros.h頭文件
#include <std_msgs/String.h>    //標準消息message
#include <std_msgs/Empty.h>

ros::NodeHandle  nh;
//聲明節點句柄 NodeHandle


void messageCb( const std_msgs::Empty& toggle_msg){
  //callback 程序,用於接收到topic上的msg後,處理該消息
  //該例子以led pin口高低電平作爲輸出
  digitalWrite(13, HIGH-digitalRead(13));   // blink the led
}

ros::Subscriber<std_msgs::Empty> sub("chatter", messageCb );  
// 聲明並定義一個Subscriber訂閱者 - >> 接收來自toggle_led話題topic的消息,並執行callback程序,messageCb


std_msgs::String str_msg; //消息爲string 類的str_msg對象
ros::Publisher pub("chatter", &str_msg); 
//聲明並定義了一個發佈者節點pub,至話題 chatter

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

void setup()
{
  // arduino參數設置
  pinMode(13, OUTPUT);
  nh.initNode();
  nh.advertise(pub);
  nh.subscribe(sub);
}

void loop()
{
  //循環體
  str_msg.data = hello;
  pub.publish( &str_msg );
  nh.spinOnce();
  delay(500);
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章