發表於

透過 LED 燈的明亮,通常是入門 Arduino 的第一步,只要在指定腳位輸入 High、Low的訊號,就可以很簡單地看到成果。因此,控制LED自然也成為從ROS出發來控制Arduino板的第一步.

1.連線準備

使用裝備:

  • ASUS筆電win10 VM ubuntu
  • Arduino

2.測試架構: 

3.上傳程式

程式碼如下:

#include <ros.h>
#include <std_msgs/Empty.h>

ros::NodeHandle nh; //
  
void messageCb( const std_msgs::Empty& toggle_msg){
    digitalWrite(13, HIGH-digitalRead(13));
    delay(3000);  
    digitalWrite(13, HIGH-digitalRead(13));
}
  
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );
  
void setup()
{
    pinMode(13, OUTPUT);
    nh.initNode();
    nh.subscribe(sub);
}
  
void loop()
{
    nh.spinOnce();
    delay(1);
}

程式鍵入後,上傳.

如跳錯誤訊息:

證明USB權限沒開.到命令字元鍵入 :

sudo chmod 666 /dev/ttyUSB0

/dev/ttyUSB0 是 接收USB資料位置,可到 arduino -> Tools -> Port 查詢

4.ROS架設初始化,命令字元鍵入:

roscore

5.ROS運行Arduino,命令字元鍵入:

rosrun rosserial_python serial_node.py /dev/ttyUSB0 _baud:=57600

如有報錯,有可能是USB權限沒開.上面有寫.

6.傳送訊息給arduino,命令字元鍵入:

rostopic pub toggle_led std_msgs/Empty --once

結果arduino的pin13腳位,閃3秒關閉

7.程式範列講解:

#include <ros.h>
#include <std_msgs/Empty.h>

ros.h 標頭檔包含了標準 ROS 類別,每支 ROS 程式都要 include 此標頭檔. 接著也需載入定義 message 的標頭檔,在此為 std_msgs/Empty.h,空的message.

ros::NodeHandle nh; //創建一個ros的節點

在系統內部,NodeHandle 維護著一個引用計數,且只有當第一個物件被宣告時才會通報 ROS Master 註冊一個新的節點。同樣地,當所有的 NodeHandles 都被關閉後,節點才會被取消註冊。

void messageCb( const std_msgs::Empty& toggle_msg){
    digitalWrite(13, HIGH-digitalRead(13)); 
    delay(3000);  //停(3000為3秒)
    digitalWrite(13, HIGH-digitalRead(13)); 
}

回調函式是節點訂閱消息(message)的基礎機制,當有消息到達時,會自動以消息的指標作為參數,再呼叫回調函式,完成對消息內容的處理。以上程式用來接收 Publisher 發布的 Empty 消息,讓 Arduino 板子的第13支腳位的 LED 閃爍 

HIGH-digitalRead(13)->13腳位如果是HIGH得LOW,如果是low就得HIGH

ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );   

ros::訂閱者 <訊息rostopic pub /ardrone/takeoff std_msgs/Empty> 子(“name”,位置物件) . 訂閱節點首先須聲明訂閱的消息話題 (message topic),這些資訊會紀錄在 ROS Master中.Master會注意系統中是否存在發布該話題的節點,若存在,則會建立兩個節點的連接.

void setup()
{
    pinMode(13, OUTPUT);//設pin13為輸出
    nh.initNode();//初始化
    nh.subscribe(sub);//添加子節點
}
void loop()
{
    nh.spinOnce();//執行一次
    delay(1);//停1毫秒
}

結束~~~~

2,180 thoughts on “(深入)如何修改LED範例的亮滅週期

  1. En ucuz ve kaliteli siteden uygun fyatlara satın alabilmek için takip2018 in bütün faydalarından hemen faydalanabilirsiniz,
    Türk takipçi paketleri 1000 adet takipçiden başlayarak
    100bin takipçiye kadar alabilirsiniz.
    100bin takipçiyi sadece 12 saat içerisinde profilinize yükleyebiliyorlar.

    Takipçi Satın Al sitesinden takipçi almak için hemen takip2018.com a giderek alın.

  2. of course like your web site however you need to check the spelling on quite
    a few of your posts. Several of them are rife with spelling issues and
    I find it very troublesome to inform the reality on the other hand I’ll surely come
    again again.

  3. Sizinle büyük bir sır paylaşıyorum hemen profilimde bulunan siteye
    girerek takipçi sayısınızı kimsenin anlamayacağı şekilde yükseltebililrsiniz.

    Takipçi sayısı arttırmak için yukarıdaki linke tıklayın ve satışlarınıza büyük
    oranda katkı payı sağlayın.

  4. Hey! This is kind of off topic but I need some advice from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal
    but I can figure things out pretty quick. I’m thinking about making my own but I’m not sure where
    to start. Do you have any points or suggestions? Many thanks

  5. Wow, amazing weblog format! How lengthy have you ever been blogging for? you make blogging look easy. The overall look of your web site is wonderful, as smartly as the content!

  6. Great post. I was checking constantly this blog and I’m impressed!
    Very useful information specially the last part :
    ) I care for such information a lot. I was seeking this certain info for a very long time.

    Thank you and best of luck.

  7. I’ll right away snatch your rss feed as I can’t to find your email
    subscription hyperlink or newsletter service.

    Do you have any? Please permit me know so that I may
    just subscribe. Thanks.

  8. We stumbled over here from a different website and thought I should check things out.

    I like what I see so now i’m following you. Look forward to finding out about your
    web page repeatedly.

  9. I have been browsing online more than three hours today, yet I never found any interesting
    article like yours. It’s pretty worth enough for me.
    Personally, if all web owners and bloggers made
    good content as you did, the web will be much more useful than ever before.

  10. I’ve been surfing on-line greater than 3 hours lately, yet I never found
    any attention-grabbing article like yours. It’s beautiful worth enough for me.
    In my view, if all webmasters and bloggers made just right
    content material as you did, the net will likely be a lot more useful than ever before.

  11. I will immediately clutch your rss as I can not to find your email subscription link or e-newsletter service.
    Do you’ve any? Kindly permit me recognize so that I may just subscribe.

    Thanks.

  12. Pingback: madribet
  13. Pingback: meritroyalbet
  14. Pingback: meritking
  15. Pingback: meritking
  16. I’ve been surfing online greater than three hours nowadays, but I never
    found any fascinating article like yours. It’s pretty value sufficient for me.
    In my opinion, if all site owners and bloggers
    made excellent content as you probably did, the internet shall be
    much more useful than ever before.

  17. Pingback: eurocasino
  18. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. Personally, if all site owners and bloggers made good
    content as you did, the web will be much more useful than ever
    before.

  19. It is appropriate time to make some plans for the long
    run and it is time to be happy. I’ve read this put up and if I may I
    want to counsel you few attention-grabbing issues or advice.
    Perhaps you can write next articles relating to this article.
    I desire to read even more things about it!