就如同各位從開始的夥伴們,你們一定很想快點看到,一台可以裝上雷射LiDAR導航,自主漫遊 + 路徑規劃 + 自主避障 + 影像AI   &^$%^#%%^%^&%^  ~ 等等集合了目前self-driving 前端科技的家用小車八 !! 我們記錄一位工程師,他是如何從開始的學習、蛻變到一位 self-driving developer的過程。

話說,這一開始的動作應該也是記錄摸索的過程,不過,這卻是最真實的、殘酷與慘痛代價的經驗。希望這些記錄可以減少後面跟進的學習者們寶貴的時間。

在此定義初階段的學習標的 :

  • 遠端遙控機器人 
  • LiDAR雷達建地圖
  • 自主導航與避障
  • 路徑規劃   (到這 ~ 基本該會的操作都已經具備了)

1-1、連線準備

使用裝備 : 

  • ACER i5 筆電 win10系統
  • 樹梅派 Pi4 B (2020 年最新版)

測試架構 : 

arduino_servo.png

1-2、上傳程式碼 

小編不再多寫,可參考官網 :

This should open the following code in your IDE:

Toggle line numbers

   1 /*
   2  * rosserial Servo Control Example
   3  *
   4  * This sketch demonstrates the control of hobby R/C servos
   5  * using ROS and the arduiono
   6  * 
   7  * For the full tutorial write up, visit
   8  * www.ros.org/wiki/rosserial_arduino_demos
   9  *
  10  * For more information on the Arduino Servo Library
  11  * Checkout :
  12  * http://www.arduino.cc/en/Reference/Servo
  13  */
  14 
  15 #if defined(ARDUINO) && ARDUINO >= 100
  16   #include "Arduino.h"
  17 #else
  18   #include <WProgram.h>
  19 #endif
  20 
  21 #include <Servo.h> 
  22 #include <ros.h>
  23 #include <std_msgs/UInt16.h>
  24 
  25 ros::NodeHandle  nh;
  26 
  27 Servo servo;
  28 
  29 void servo_cb( const std_msgs::UInt16& cmd_msg){
  30   servo.write(cmd_msg.data); //set servo angle, should be from 0-180  
  31   digitalWrite(13, HIGH-digitalRead(13));  //toggle led  
  32 }
  33 
  34 
  35 ros::Subscriber<std_msgs::UInt16> sub("servo", servo_cb);
  36 
  37 void setup(){
  38   pinMode(13, OUTPUT);
  39 
  40   nh.initNode();
  41   nh.subscribe(sub);
  42   
  43   servo.attach(9); //attach it to pin 9
  44 }
  45 
  46 void loop(){
  47   nh.spinOnce();
  48   delay(1);
  49 }

程式碼扼要說明 : 

11行 : 建立一個callback 函式 servoCb (被呼叫後立即返回),該函式是給subscriber使用的,函式的寫法是固定的, type of message is std_msgs::UInt16 ,message name 是 toggle_msg。這個callback函式很單純沒有參考回傳的內容,只是很單純地每次收到一個message,Arduino就會閃爍或熄滅一次LED。

15行 : 建立一個接下來可執行的 publisher或 subscriber。在這裡,我們建立一個subscriber,topic name 是 “servo” , type 是 std_msgs::UInt16。對於訂閱(subscriber)者,必須記住根據訊息(message)來訂閱。最後的兩個參數, Its two arguments are the topic it will be subscribing to and the callback function it will be using.

接著,執行程式

Now, launch the roscore in a new terminal window:

roscore

Next, run the rosserial client application that forwards your Arduino messages to the rest of ROS. Make sure to use the correct serial port: (要注意喔)

rosrun rosserial_python serial_node.py /dev/ttyACM0

   

再開一個 terminal,

rostopic pub servo std_msgs/UInt16  <angle>

 

Posted in ROS

8,156 thoughts on “ROS從”零”開始 – 4. ROSSerial_Arduino測試 (RC servo運轉, 示範subscriber)

  1. Excellent blog you have here but I was curious if
    you knew of any message boards that cover the same topics discussed in this
    article? I’d really love to be a part of online community where
    I can get feed-back from other knowledgeable individuals that share the same interest.
    If you have any suggestions, please let me know. Thank
    you!

  2. Hi, i read your blog occasionally and i own a similar one and i
    was just curious if you get a lot of spam feedback?
    If so how do you stop it, any plugin or anything you can advise?
    I get so much lately it’s driving me crazy so any assistance is very much appreciated.

  3. Its like you learn my mind! You seem to understand a lot approximately this, like you wrote the book in it or something.
    I feel that you just could do with some p.c. to power the
    message home a bit, but instead of that, this is excellent blog.

    A fantastic read. I will definitely be back.

  4. Nice blog here! Additionally your website loads up fast!
    What web host are you using? Can I get your associate hyperlink in your
    host? I want my site loaded up as quickly as yours lol

  5. Pingback: buy levitra viagra
  6. Pingback: cialis price
  7. Hey there! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest writing a blog article
    or vice-versa? My website discusses a lot of the same topics as yours
    and I think we could greatly benefit from each other.
    If you happen to be interested feel free to shoot me an e-mail.
    I look forward to hearing from you! Excellent blog by the way!

  8. Pingback: cialis pills
  9. Pingback: viagra
  10. Pingback: hims ed pills
  11. you will need support or suggestions, write me privately.
    I interested in your implementation/use case.
    Thank you for the feedback and, please, share problems and solutions with us.
    Thank you in advance for that.