天天看點

turtlebot簡單控制_直線/圓。。。Writing Your First Script

http://www.ncnynl.com/archives/201609/796.html

編寫第一個腳本

讓我們寫我們的第一個腳本,以獲得為ROS編寫程式腳本的感覺。如果你不明白每一個線的意思,不要擔心!我們的例子是設計盡可能簡單。

  1. 關閉turtlebot所有終端視窗。打開一個新的和運作:
roslaunch turtlebot_bringup minimal.launch
           
  1. 現在移動到工作站,我們克隆GitHub庫和運作示例腳本:
mkdir ~/helloworld
cd ~/helloworld/
git clone https://github.com/markwsilliman/turtlebot/
cd turtlebot
python goforward.py
           

Turtlebot應該向前移動, 按CTRL + C 停止

  1. 現在讓我們看一看代碼。通過運作打開它:

    gedit goforward.py

你也可以檢視GitHub上如果你喜歡。

如果你一路滾動到底部,你會看到:

if __name__ ==  '__main__':
    try:
        GoForward
           

注意:如果你還沒看的Python代碼之前,Mark Lutz學習Python是一個很好的資源。

上面的代碼就是“當我們開始這個腳本嘗試跑步前進的init(初始化)功能”。從這裡的代碼中的文檔是相當簡單的,但不是設計為所有包含。要充分了解出版商,等等,我們已經聯系到一些偉大的書籍。

為了好玩讓我們修改指令TurleBot循環往複。在終端視窗中運作:

cd ~/helloworld/
cp goforward.py goincircles.py
gedit goincircles.py
           

修改線性。x = 0.2到0和角度。Z = 0到0.5。儲存和退出。

在一個新的終端視窗運作:

python goincircles.py
           

注:turtlebot隻使用線性的。X和Z值的角,因為它在一個平面作品(2D)的世界,但無人機,海洋機器人與其他機器人占據的三維環境中,線性的。Y,X和Y的角,角的值是可用的。

精度

現在讓我們試試另一個腳本是專為turtlebot畫正方形。在終端視窗中運作:

cd ~/helloworld
python draw_a_square.py
           

turtlebot開始在地闆上畫正方形但你會發現它很快開始漂離出發的路徑。這是機器人和計算機的作用非常不同的地方。如果你問一台電腦做2 + 1,你會總是收到1。如果你問一個機器人向前移動1米,它将去大約一米,而不是完全直。知道機器人是(定位)是一個經典的機器人的挑戰之一,尤其是在室内環境中,全球定位系統是不可靠的或足夠精确。

你可能會認為,因為你可以告訴機器人在0.2米/秒,這将是很容易程式設計的機器人,使它前進一米,通過釋出一個線性的。x = 0.2米/秒5秒。如果機器人是一台計算機,這是完全正确的,但對于機器人,這是非常不準确的,由于打滑,不完善的校準,和其他因素。如果你寫一個這樣的腳本驅動在一個正方形,并運作10個周期,你會結束一個完全不同的地方,從你開始!幸運的是,令人驚訝的科學家和研究人員正走在我們的前面。

在下一篇文章中,我們将開始介紹這些複雜但迷人的挑戰的解決方案,因為我們開始利用一些真正強大的工具,為我們提供了。

http://learn.turtlebot.com/2015/02/01/10/

Writing Your First Script

Let’s write our first script to get a feel for writing scripts for ROS. If you don’t understand what every line means, don’t worry- we’ll get there! Our example is designed to be as simple as possible.

Close all terminal windows on TurtleBot. Open a new one and run:

roslaunch turtlebot_bringup minimal.launch
           

Now moving to the workstation, let’s clone the github repository for these articles and run a sample script:

mkdir ~/helloworld
cd ~/helloworld/
git clone https://github.com/markwsilliman/turtlebot/
cd turtlebot
python goforward.py
           

TurtleBot should be moving forward now. Press CTRL + C to stop it.

First Python Script goforward.py

Now let’s have a look at the code. Open it by running:

gedit goforward.py
           

You can also view it on GitHub if you prefer.

If you scroll all the way to the bottom you’ll see:

if __name__ ==  '__main__':
    try:
        GoForward()
           

NOTE: If you haven’t looked at Python code before, Learning Python by Mark Lutz is a good resource.

The code above simply means “when we start this script try running GoForward’s init (initialize) function”. From here the documentation in the code is pretty straightforward but not designed to be all-inclusive. To fully understand the publisher, etc., we’ve linked to some great books.

For fun let’s modify the command so TurleBot goes in circles. In a terminal window run:

cd ~/helloworld/
cp goforward.py goincircles.py
gedit goincircles.py
           

Modify linear.x = 0.2 to 0 and angular.z = 0 to 0.5. Save and exit.

In a new terminal window run:

python goincircles.py
           

NOTE: TurtleBot only uses linear.x and angular.z values because it works in a planar (2D) world, but for drones, marine robots and other robots that occupy 3D environments, linear.y, angular.x and angular.y values are available.

Accuracy

Now let’s try another script which is designed for TurtleBot to draw squares. In a terminal window run:

cd ~/helloworld
python draw_a_square.py
           

TurtleBot starts drawing squares on the floor but you’ll see that it quickly starts to drift away from its starting path. This is where robots and computers act very differently. If you ask a computer to do 1 + 1 you’ll always receive 2. If you ask a robot to move forward 1 meter it will go roughly a meter and not perfectly straight. Knowing where the robot is (localization) is one of the classic robotic challenges, especially in indoor environments where GPS isn’t reliable or accurate enough.

You might think that because you can tell the robot to go forward at 0.2 m/s, it would be easy to program a robot so that it goes forward one meter by publishing a linear.x = 0.2 m/s for 5 seconds. If the robot were a computer this is exactly right, but for robots this is very inaccurate due to slippage, imperfect calibration, and other factors. If you write a script like this to drive in a square and run for 10 cycles, you’ll end up somewhere entirely different from where you started! Luckily, amazing scientists and researchers are way ahead of us on this.

In the next article we will start to introduce solutions to these complex but fascinating challenges as we begin to leverage some of the really powerful tools ROS provides us.

繼續閱讀