这几天有点忙,都没时间翻译,今天刚好有时间,突然忽发奇想,万一我发现原文错了,到底我的翻译要跟着错呢?还是翻译改正,原文继续错?还是索性把原文一起改了?

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”作为项目名称,你可以选择任意的文件夹去创建这个项目,在这里,我们使用了“D:\vs2017”这个文件夹。

由于这是一个全新的项目,因此你必须要选择“Create directory for solution”。

如果你的编程团队有超过一个人一起编写这个程序,你可能会想选择“Create new Git repository”,这样,你们就可以使用版本控制的功能一起编写/修改程序了。

现在,按“OK”去创建这个项目。

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”会自动被创建,我们不需要这个程序,所以,按“X”把这个程序关掉。

Figure 2.2

Now Right click the "EV3PythonLessons", select "Add", then select "New Item":

在屏幕右边项目名称“EV3PythonLessons”的位置,按鼠标右键并选择“Add”,然后选择“New Item”:

Figure 2.3

Create an Empty Python File with Name: "lesson2_01.py" and click "Add".

创建一个空白的 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:

现在我们会详细的解释这个程序:

#!/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

- 加载所有有关于 EV3 的“API”

from time import sleep, time

- loads time related functions

- 加载和时间有关的API

import traceback

- in case any error in the program, we use the system provided traceback api to log any program error

- 万一出现了程序错误,我们使用系统提供的 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”区域里出现了任何程序错误,有一个 log 档案就会被创建在同一个文件夹下,任何你可以使用 WinSCP 这个程序去查看到底程序错在哪里。

lcd = Screen()

- declare an object that can control the EV3 LCD screen

- 声明一个叫 lcd 的对象,使用这个对象的属性和方法,就可以控制 EV3 的屏幕。

lcd.clear()

- clear the LCD screen

- 清除 EV3 的屏幕内容

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.

- 更新并显示屏幕内容。

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”然后按【确认键】。

现在,你应该可以看到网络的连接状态是“Configuring”,大概 30 秒后,状态会变成“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”。

然后你会看见一个问题,问你要不要连接一个未知服务器,按“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 程序 "可执行" (译者按:由于已安装 EV3DEV 的操作系统是一个 Linux 的系统,上传 Linux 系统的程序,如果不特别设置,都是不能执行的!),我们不想每次都要改变程序的权限来让它可以被执行,所以我们需要做一个一次性的设置。

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:

要做这个设置:

- 选择 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.

选择你想上传的文档,这里我们选择“lesson2_01.py”,然后按“Upload”。

选择“OK”开始上传。

你现在应该可以看到上传的文档在右边了。

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”并且按 【确认键】。

- 选择 “lesson2_01.py”并且按 【确认键】。

After a few seonds loading the program, you should see:

几秒钟后,你应该可以看到:

Figure 2.8

Then after 10 seconds, the program quits itself.

然后 10 秒钟后,程序会自己退出来。

In our next lesson, we'll learn some basic Python Syntax.

在下一课,我们会学习更多 Python 的语法。

乐高机器人投篮编程_乐高 EV3 高级编程 - 第二课:Hello World相关推荐

  1. 乐高机器人 搭建钟摆_乐高机器人初级课程表X

    乐高机器人初级课程 适合 2-3 个学生为一组搭建和编程设计机器人方案. 共 431 个组件 的套装中包括一个 NXT 可编程的微型电脑, 一块可充电的锂电池, 三个伺服电机, 二个触动传感器,一个光 ...

  2. 乐高机器人 搭建钟摆_乐高创意百变31088拼搭指北:深海生物大鲨鱼,拼成3变变形金刚...

    乐高创意百变31088拼搭指北:深海生物大鲨鱼,拼成3变变形金刚 2019-06-27 07:30:00 32点赞 67收藏 11评论 又又又见面了各位值友,618期间,我入手了两套Lego 3107 ...

  3. 乐高机器人骨奥_乐高机器人-Building blocks robot

    乐高机器人-Building blocks robot 一款趣味性强又能触发儿童动手动脑的玩具可以在玩耍的过程中无形开发大脑智慧.乐高机器人就是这样一款玩具.集合了可编程主机.电动马达.传感器.Leg ...

  4. Linux环境高级编程函数,Linux环境高级编程--出错处理(CLStatus)

    很多程序库对外提供若干类,每个方法出错时如何告知调用者是否出错,以及出错码(在Linux上在error.h中的全局errno就是保存我们Linux程序执行的出错码的)?方法很多,为了简化起见,函数将返 ...

  5. 学python编程_少儿学Python编程的一些思考

    自从孩子上了初中,孩子妈就开始盯着各种真假难辨的中考.高考新政传言.当她从铺天盖地的少儿编程广告里获悉,编程将纳入中考,高考范围,并且2018年高考,多个省份的数学卷甚至都出现了编程题时,就变得异常兴 ...

  6. 华兴数控g71外圆循环编程_数控车床加工编程典型实例分析(西门子802S数控系统)...

    这是一篇带有教学色彩的习作,文章对数控编程的方式和步骤进行了简明的阐述,并针对一个典型零件的数控车削加工给出了一套程序.程序是以西门子802S数控系统为例编写的. 数控机床是一种技术密集度及自动化程度 ...

  7. python数据分析第二讲_七月在线 Python数据分析 第二课 Numpy

    本人有一丢丢编程基础,特此学习七月在线课程--Python 数据分析.本文记录了学习第二课Numpy的一些感受和心得. 第二讲的内容包括Numpy, ndarray, ndarray的取值赋值与切片. ...

  8. 中鸣机器人走黑线_乐高机器人EV3颜色传感器如何沿着黑线?

    2016-08-01 乐高EV3机器人机械爪怎么搭? 知识背景 1920年,捷克作家卡雷尔·卡佩克发表了科幻剧本<罗萨姆的万能机器人>.在剧中,卡佩克把模拟人的机器人称为"Rob ...

  9. python高级编程 豆瓣_《Python高级编程》读书笔记:方法解释顺序浅析

    Python在2.2引入了New-style object(ref),而且在2.3引入了新的方法解释顺序(Method resolution order,以下简称MRO),新的MRO解决了多继承下的方 ...

  10. python贺卡编程_一种卡片编程机及其编程方法与流程

    本发明涉及编程领域,特别涉及一种卡片编程机及其编程方法. 背景技术: 人工智能日趋火热,家长们也逐渐意识到孩子的培养方向,编程教育不仅为孩子的将来提前打下基础,而且还能够培养孩子的逻辑思维能力,优化孩 ...

最新文章

  1. 【思维导图总结——数据库系统概论】关系数据库标准语言SQL
  2. 数据挖掘 —— 无监督学习(关联)
  3. 【转载】jvm内存回收
  4. 左偏树 P3377【模板】左偏树(可并堆)
  5. 菜鸟ING的博客终于开园了。
  6. 栈的pop和peek_从堆栈中移除项目,而不使用pop、peek、push
  7. 微信停止服务印度用户;阿里回应“马云遭印度法院传唤”;fastjson 1.2.73 发布 | 极客头条...
  8. [胡言乱语] 20170622
  9. Idea 新建类 快捷键
  10. win10为单个网卡配置多个IP地址
  11. NOI2014--起床困难症
  12. freyja将重新把重心放在orm、sharding、cache上
  13. 用sci-hub的文献检索方法
  14. 7-46 新浪微博热门话题 (30 分)
  15. 略晓网:经验真的能换钱
  16. 元核云赋能银行业,智能双录产品助力银保业务合规高效响应
  17. Eagle电路板绘制
  18. linux signal用法,Linux 信号 signal 用法详解及注意事项
  19. centos上安装cdh2.6的方法
  20. 电源接反了烧电路怎么办?电源防反接技术讨论

热门文章

  1. 三十正青春!苏宁818要用“好服务”抢占年轻用户心智
  2. endnotex8使用教程_EndNote X8使用教程
  3. java 生成中文字符乱码,java汉字乱码的原因与解决方法
  4. axure原型图手机界面尺寸
  5. Scala下载与安装
  6. C语言笔试经典编程题目(汇总帖)
  7. 很不错的在线Office控件:IWebOffice与SOAOffice
  8. 工具类官网Web原型制作分享-Adobe
  9. 刷重庆高校网课的方法
  10. ArcGIS版本更新对比