乐高机器人投篮编程_乐高 EV3 高级编程 - 第二课:Hello World
這幾天有點忙,都沒時間翻譯,今天剛好有時間,突然忽發(fā)奇想,萬一我發(fā)現(xiàn)原文錯了,到底我的翻譯要跟著錯呢?還是翻譯改正,原文繼續(xù)錯?還是索性把原文一起改了?
EV3: Lesson 2 - First EV3 Python Program: "Hello World"
EV3:第二課 - 第一個Python程序:“Hello World”
2.1 Open the IDE and Create a New Project
2.1 打開 IDE 并新建一個項目
Open Visual Studio 2017 and create a new project:
打開 Visual Studio 2017 并且新增一個項目:
Figure 2.1
To start it as simple as possible, we choose the "Python Application" as the project type, the Name of the Project is: "EV3PythonLessons", choose an appropriate folder to create this project, we use "D:\vs2017" in this example.
You must choose "Create directory for solution" as this is a new project.
You may want to choose "Create new Git repository" if there are more than 1 person in your programming team, or you want to better protect your programs by version control.
Now, click "OK" to create this project.
使用最簡單的方法開始,我們選擇的項目類型是“Python Application”,我們用“EV3PythonLessons”作為項目名稱,你可以選擇任意的文件夾去創(chuàng)建這個項目,在這里,我們使用了“D:\vs2017”這個文件夾。
由于這是一個全新的項目,因此你必須要選擇“Create directory for solution”。
如果你的編程團隊有超過一個人一起編寫這個程序,你可能會想選擇“Create new Git repository”,這樣,你們就可以使用版本控制的功能一起編寫/修改程序了。
現(xiàn)在,按“OK”去創(chuàng)建這個項目。
2.2 First Program
2.2 第一個程序
An empty python program will be created for you, namely "EV3PythonLessons.py", we DO NOT need this! So, close this program by clicking the "X".
一個新的 python 程序 “EV3PythonLessons.py”會自動被創(chuàng)建,我們不需要這個程序,所以,按“X”把這個程序關(guān)掉。
Figure 2.2
Now Right click the "EV3PythonLessons", select "Add", then select "New Item":
在屏幕右邊項目名稱“EV3PythonLessons”的位置,按鼠標(biāo)右鍵并選擇“Add”,然后選擇“New Item”:
Figure 2.3
Create an Empty Python File with Name: "lesson2_01.py" and click "Add".
創(chuàng)建一個空白的 Python 檔案,并且命名為:"lesson2_01.py",然后按“Add”。
Copy and paste the following codes into the program:
把以下的程序拷貝到新建的程序里:
#!/usr/bin/env python3
from ev3dev.ev3 import *
from time import sleep, time
import traceback
try:
lcd = Screen()
lcd.clear()
intX = 10
intY = 5
lcd.draw.text((intX, intY), 'Hello World')
lcd.draw.rectangle((20, 20, 158, 40), fill = 'black')
lcd.draw.text((25, 25), 'Hello World', fill = 'white')
lcd.update()
sleep(10)
except:
# If there is any error, it will be stored in the log file in the same directory
logtime = str(time())
f=open("log" + logtime + ".txt",'a')
traceback.print_exc(file=f)
f.flush()
f.close()
Or, you can download the program from the following location:
你也可以在以下的鏈接下載這個程序:
Now we are going to explain this program in detail:
現(xiàn)在我們會詳細的解釋這個程序:
#!/usr/bin/env python3
- ask the EV3 to run this program in python3
- 這句的意思是讓 EV3 使用 python3 去編譯并運行這個程序
from ev3dev.ev3 import *
- loads all the "APIs" required to control EV3 using python programs
- 加載所有有關(guān)于 EV3 的“API”
from time import sleep, time
- loads time related functions
- 加載和時間有關(guān)的API
import traceback
- in case any error in the program, we use the system provided traceback api to log any program error
- 萬一出現(xiàn)了程序錯誤,我們使用系統(tǒng)提供的 traceback api 去記錄這些錯誤
try and except
- if there is any error in the "try" region, a log file will be created in the same directory, you can then open this log file in the WinSCP program to see what's wrong in your program.
- 如果在 “try”區(qū)域里出現(xiàn)了任何程序錯誤,有一個 log 檔案就會被創(chuàng)建在同一個文件夾下,任何你可以使用 WinSCP 這個程序去查看到底程序錯在哪里。
lcd = Screen()
- declare an object that can control the EV3 LCD screen
- 聲明一個叫 lcd 的對象,使用這個對象的屬性和方法,就可以控制 EV3 的屏幕。
lcd.clear()
- clear the LCD screen
- 清除 EV3 的屏幕內(nèi)容
lcd.draw.text((intX, intY), 'Hello World')
- the EV3 screen consist of 178 width x 128 height dots.
- start drawing the text "Hello World" at column intX(which is 10), row intY(which is 5).
- EV3 的屏幕寬 178 點,高 128 點。
- 在 X (橫)為 10, Y (豎)為 5 的位置,畫出“Hello World”這些文字
lcd.update()
- Update the LCD screen and display the content.
- 更新并顯示屏幕內(nèi)容。
sleep(10)
- The program wait for 10 seconds
- 程序停頓(或假死,就是什么都不做)10秒鐘。
the others are self explained.
其他的程式句子比較簡單,句子本身就解釋了自己的意思。
2.3 Upload the program
2.3 上傳程序
2.3.1 Connect your EV3 brick with the USB cable:
2.3.1 使用 USB 線連接 EV3:
- In your EV3 Brick, select "Wireless and Networks" and press the "Middle" button
- Select "All Network Connections" and press the "Middle" button
- Select "Wired" and press the "Middle" button
- Select "Connect automatically" and press the "Middle" button
- Select "Connect" and press the "Middle" button
Now you should see the state is "Configuring" and wait for around 30 seconds until the state turns into "Connected", then the IP address of the EV3 brick will be shown at the top left corner of the EV3's LCD screen.
- 在你的 EV3 主機(開機后),選擇“Wireless and Networks” 然后按中間鍵(譯者按:EV3 有 6 個按鍵,中間鍵也就是【確認鍵】的意思)。
- 選擇“All Network Connections”然后按【確認鍵】。
- 選擇 “Wired”然后按【確認鍵】。
- 選擇 “Connect automatically”然后按【確認鍵】。
- 選擇 “Connect”然后按【確認鍵】。
現(xiàn)在,你應(yīng)該可以看到網(wǎng)絡(luò)的連接狀態(tài)是“Configuring”,大概 30 秒后,狀態(tài)會變成“Connected”,然后 EV3 的 IP 地址就會顯示在屏幕的左上方。
Figure 2.4
You can test the connection by opening the command prompt and "Ping http://xxx.xxx.xxx.xxx" in which xxx is the IP address shown.
你可以打開電腦的 Command Prompt (命令行)并且使用 "Ping http://xxx.xxx.xxx.xxx" 去測試 EV3 和 電腦的連接是否成功,這些 xxx 就是 EV3 的 IP 地址。
2.3.2 Open the WinSCP program
2.3.2 打開 WinSCP 程序 (在電腦里打開,用來上傳程序到 EV3)
Figure 2.5
Choose "New Site", input the IP address in the "Host Name", and:
User name: robot
Password: maker
Then click "Login".
Click "Yes" if you are asked whether you want to connect to an unknown server.
選擇 “New Site”,在“Host Name”輸入 EV3 的 IP 地址:
User name 是 robot
Password 是 maker
然后按“Login”。
然后你會看見一個問題,問你要不要連接一個未知服務(wù)器,按“Yes”。
2.3.3 One Time Configuration of WinSCP
2.3.3 WinSCP 的一次性配置
In order to make the python programs "Executable" inside the EV3 Brick and we DO NOT want to change the programs to Executable Mode EVERYTIME when we upload the programs, we need to do a ONE TIME configuration so that all programs uploaded will be executable.
要讓上傳到 EV3 的 Python 程序 "可執(zhí)行" (譯者按:由于已安裝 EV3DEV 的操作系統(tǒng)是一個 Linux 的系統(tǒng),上傳 Linux 系統(tǒng)的程序,如果不特別設(shè)置,都是不能執(zhí)行的!),我們不想每次都要改變程序的權(quán)限來讓它可以被執(zhí)行,所以我們需要做一個一次性的設(shè)置。
To do so:
- select "Options" in the WinSCP,
- select "Preferences",
- select "Transfer"
- select "Text"
- click "Edit"
- check "Automatically select this preset when", and inside the Username mask, enter "robot"
- click the "..." under the "Set permissions" and check as follow:
要做這個設(shè)置:
- 選擇 WinSCP 菜單的“Options”
- 選擇 “Preferences”
- 選擇 “Transfer”
- 選擇 “Text”
- 選擇 “Edit”
- 打鉤 “Automatically select this preset when”,并且在 "Username mask" 里輸入 robot
- 在 “Set permissions”下面按一下 “...”,并按下圖選擇:
Figure 2.6
Then click "OK" to save the changes, click "OK" again to leave the "Preferences".
然后按 “OK”存儲,并再按一次“OK”離開 Preferences 菜單。
The left hand side represents files in your PC, the right hand side represents files in your EV3 brick.
WinSCP 程序里的左邊顯示了你電腦里的文檔,右邊顯示了你 EV3 里面的文檔。
On the left hand side, go to the folder that store the python programs:
在左邊,打開存儲 Python 程序的文件夾:
Figure 2.7
Select the program that you want to upload, in this case, "lesson2_01.py", click "Upload".
Click "OK" to perform the upload.
You should see the file is shown on the right hand side of the WinSCP.
選擇你想上傳的文檔,這里我們選擇“l(fā)esson2_01.py”,然后按“Upload”。
選擇“OK”開始上傳。
你現(xiàn)在應(yīng)該可以看到上傳的文檔在右邊了。
2.4 Running the Program
2.4 運行程序
- Press the "Upper Left Corner" button on the EV3 until you see the Main Menu of your EV3.
- Select "File Browser" and press the "Middle" button.
- Select "lesson2_01.py" and press the "Middle" button to run the program.
- 在 EV3 上,按幾下右上角的【退出鍵】直到你看見 EV3 的主菜單
- 選擇“File Browser”并且按 【確認鍵】。
- 選擇 “l(fā)esson2_01.py”并且按 【確認鍵】。
After a few seonds loading the program, you should see:
幾秒鐘后,你應(yīng)該可以看到:
Figure 2.8
Then after 10 seconds, the program quits itself.
然后 10 秒鐘后,程序會自己退出來。
In our next lesson, we'll learn some basic Python Syntax.
在下一課,我們會學(xué)習(xí)更多 Python 的語法。
總結(jié)
以上是生活随笔為你收集整理的乐高机器人投篮编程_乐高 EV3 高级编程 - 第二课:Hello World的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu 截图工具的使用
- 下一篇: matlab 介电常数,改进的基于Mat