1. 程式人生 > 其它 >arduino 與 ros 通訊 (hello word!)

arduino 與 ros 通訊 (hello word!)

技術標籤:arduinoubunturosarduino

利用ros_lib庫中自帶的hello world 例子

1. 源程式如下:
/*
 * 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); }
2. 程式解析
2.1 arduino的setup函式
  • 需要初始化ROS節點,並 宣告 所有的釋出或訂閱
2.2 arduino的setup函式
  • 需要定義釋出的內容,並呼叫nh.spinOnce(),這樣所有的ROS回撥函式就會被處理
2.3 標頭檔案必須包含
#include <ros.h>  注意:ros.h前不可加ros/ 
#include <std_msgs/String.h>
3. 程式執行
3.1 roscore
3.2 其中埠號視具體而定
rosrun rosserial_python serial_node.py /dev/ttyUSB0

或者

rosrun rosserial_python serial_node.py _port:=/dev/ttyUSB0
3.3 顯示主題chatter,獲取Arduino板反饋的資訊
rostopic echo chatter