樂高 EV3 高級編程 - 第一課
來自專欄樂高EV3及Arduino高級編程
我的學生寫的教程,英文版的,好吧,我翻譯一下!
先介紹一下我的學生,今年 12 歲 (2018年),普通 2 等中學初中 2 年級(學校名字就不說了,說了你也沒聽過!),除了數學和科學比較好,其他科目成績一般,學習電腦編程 3 年,大概每星期學 2 小時。所以,以下的教程,喜歡編程的小學生和初中生都可以看!
學生寫的教程有用嗎?好吧,他雖然是個普通學生,但是編程得獎不少,包括本地賽冠軍(3 年 4 次)和世界賽冠軍(2 年 2 次)。不是說他特別厲害,只不過他用文字編程和別人的圖形編程比賽,就像是跑車和自行車比賽,贏了也是正常的!
以下就是他寫的教程,關於 EV3 的暫時有 8 課,我一課一課的翻譯!
EV3: Lesson 1 - Introduction to EV3 Python and Installation
EV3: 第一課 - EV3 Python 編程的介紹及安裝
Preface 前言
Who should learn EV3 Python?
Anyone that are interested in robot programming using Lego EV3.
誰應該學習 EV3 Python 編程?
任何人有興趣使用樂高 EV3 機器人編程
What do you need to learn EV3 Python?
- an EV3 set, either the Home Edition 31313, or the Education Edition 45544 and 45560 (Education Extension Set).
- a Windows PC, with at least 4GB memory and 100GB hard disk space, running Windows 7 or above.
你需要什麼才能學習 EV3 Python 編程
- 一台 EV3,可以是 31313 家庭版,或者是 教育版 45544 和 45560
- 一台 Windows 電腦,最少 4GB 內存和 100GB 硬碟,操作系統 Win 7 或以上
1.1 Compare EV3 Lego and EV3 Python
1.1 比較 EV3 樂高圖形編程 及 EV3 Python 文字編程
Lets take a look at a simple python program, this program stores the largest of a,b,c into the variable strBiggest.
我們一起看看一個簡單的 Python 程序,這個程序比較 3 個變數 a, b, c 並把最大的存到字元串變數 strBiggest 里。
a = 1b = 2c = 3strBiggest = ""if (a > b): if (a > c): strBiggest = "a" else: strBiggest = "c"else: if (b > c): strBiggest = "b" else: strBiggest = "c"
To create the same program using Lego Graphical Programming, the program will be like this:
同樣的程序如果用樂高圖形編程,程序會像下面這樣:
Figure 1.1
It is obvious that EV3 python is more "Readable" than EV3 Lego programs, and, this is only a 2 levels "if" statements, for 4 levels or 5 levels "if", the EV3 Lego graphical program will become very difficult to read and maintain.
很明顯的,EV3 Python 文字編程比 樂高圖形編程 有更好的【可讀性】,以上只是兩層 if ,如果出現 4 到 5 層 if, 那麼樂高圖形編程的圖形就會太大(太高),變得難以查看及修改。
In addtion, features provided by Lego Graphical Programming are limited, youll never be able to write some robot programs in Lego grahphical programming, e.g. To control an EV3 using a PC or a mobile phone over the internet!!!
再者,樂高圖形編程功能有限,有些程序是肯定寫不了的,比方說,你不可能寫一個透過互聯網,手機遠程控制 EV3 的程序!!!
1.2 EV3 Python Installation
1.2 EV3 Python 安裝
1.2.1 Download the EV3DEV Operating System
1.2.1 下載 EV3DEV 操作系統
In order to use Python program to control EV3, we need to use a micro sd-card to boot the EV3, this micro sd-card is loaded with a Debian Linux system called EV3DEV.You can switch back to Lego Operating System at any time by removing the sd-card and reboot the EV3, so it is safe to do so.
為了使用 Python 編程,我們需要一張安裝了一個叫 EV3DEV 的 Debian Linux 系統的Micro-SD 卡並插入 EV3 開機。
使用 EV3DEV 操作系統是安全的,你隨時可以擺掉 SD 卡,重新開機,EV3 就會用回原來的樂高操作系統。
To download the EV3DEV image, open a browser and go to this web site:
要下載 EV3DEV,用電腦瀏覽器打開以下網址:
http://www.ev3dev.org
then click "Downloads":
然後按【Downloads】:
Figure 1.2
Now, click "Download for EV3", unzip the download file and you will get an .img file.
現在,按【Download for EV3】,解壓下載的文檔,你會得到一個 .img 檔案。
1.2.2 Download the Win32 Disk Imager
1.2.2 下載 Win32 Disk Imager
To make the sd-card bootable, we need to use a program called "Win32 Disk Imager", download and install it using the following url:
要讓那張 SD 卡開機,我們必須用一個叫【Win32 Disk Imager】的程序,使用下面的鏈接下載它:
https://sourceforge.net/projects/win32diskimager/files/latest/download
Now, insert the sd-card into your PC, run the Win32 Disk Imager, select the .img file, select your sd-card drive number, and click "Write":
現在,把空白的 SD 卡插入電腦,運行 Win32 Disk Imager, 選擇剛才解壓的 .img 檔案,選擇你的 SD 卡驅動號,然後點擊【Write】:
Figure 1.3
The sd-card will be prepared within a few minutes.
幾分鐘後,sd-card 就會準備好了
1.2.3 Boot the EV3 brick into the EV3DEV system
1.2.3 使用 EV3DEV 開機
Power off your EV3 brick if it is on, insert the micro sd-card and power on again, after a couple of minutes, it should look like this:
如果你的 EV3 是在開機狀態,把它關機,插入 SD 卡,然後開機,幾分鐘後,畫面會變成這樣:
Figure 1.4
The "8.00" is the approximate voltage of the battery remaining, you need to recharge and replace your battery if this value is under 7.00, otherwise it may not have enough voltage to run the motors.
右上角的【8.00】表示電池電壓,如果低於 7.00,你就要換電池了,不然它可能推不動馬達。
1.3 Choose an appropriate IDE
1.3 選擇合適的編程工具 IDE
To write python programs, youll need an IDE, you can use "Notepad", or "Sublime Text 3", or any other IDE you like. In later lessons, well learn how to use Client Server Socket connection to control an EV3 over the Internet, that would require some Microsoft Technologies, so well use Microsoft Visual Studio 2017 Community as the IDE.
要寫 Python 程序,你需要使用編程工具 IDE,你可以使用【寫字板】,或者【Sublime Text 3】,或者其他你喜歡的 IDE。在後面的課程,我們有機會學習如何使用【客戶端/伺服器 Socket 套接字連接】,並透過互聯網控制 EV3,這個程序需要一些【微軟】的技術,因此,我們會使用 Microsoft Visual Studio 2017 Community 作為 IDE。
Another reason is that we can use the Team Development features of the Visual Studio, so that your programming team can write/modify the same programs with version controls.
另外一個原因是,很多時候我們需要幾個人一起寫同一個項目/程序,Visual Studio 裡面有團隊編寫程序功能,可以使用版本控制功能多人一起修改程序。
To download Microsoft Visual Studio 2017 Community, go to the following web-site:
要下載 Microsoft Visual Studio 2017 Community,請到以下網址下載:
http://www.visualstudio.com
Then download and run Visual Studio Community installation program. For future developments, we must choose at least the following components during the installation:
然後安裝,為了將來的編程,我們最少要安裝以下的功能:
- Windows Platform Development
- .NET Desktop Development
- http://ASP.NET and Web Development
- Python Development
- Node.js Development
- Javascripe Cross Platform Mobile Development
It may take more than an hour to finish the installation.
安裝過程大概需要超過 1 小時。
1.4 Tools to upload Python programs into the EV3 brick
1.4 把 Python 程序上傳到 EV3 的工具
To upload the python program into the EV3 brick, well need another program called WinSCP, download and install the WinSCP in the following url:
把 Python 程序上傳到 EV3,我們需要一個叫 【WinSCP】的程序,可以在下面鏈接下載並 安裝:
https://winscp.net/eng/download.php
Now, we are going to write our first "Hello World" program in our next lesson.
下一課,我們將會寫第一個 EV3 Python 程序 【Hello World】。
推薦閱讀:
※這才是高科技調戲植物大法 | 未來式
※《一席》中有哪些讓你印象深刻的演講?
※iPhone SE 值不值得購買?
※關於socket的困惑???