- 乐高机器人:EV3与Scratch机器人基础与应用实例
- 林文编著
- 1098字
- 2025-02-17 22:44:23
6.2 编程模块
下面将介绍各种编程模块。
6.2.1 EV3 Scratch界面
EV3 Scratch家庭版、教育版软件界面截图如下。


6.2.2 电机
电机模块组有中型电机、大型电机、电机旋转模块的功能。


6.2.3 运动
运动模块组有移动转向、移动槽模块的功能。


6.2.4 显示
显示模块组有显示、文本、程序块状态灯模块的功能。

按程序块按钮上、下、左、右、中键,显示眼睛的上、下、左、右、中对应图像。

6.2.5 声音
声音模块组有播放文件、播放音符的功能。

超声波吉他:使用超声波传感器检测的数值作为对应的播放音符。

6.2.6 事件
事件模块组有开始、等待模块的功能。

悬崖勒马程序:
当端口3的触碰传感器被按压时,将运转电机设置为A和D。进入循环后,如果颜色传感器检测到反射光线强度小于50%,后退一圈,否则向前直行。

6.2.7 控制
控制模块组有循环、切换、循环中断、停止程序模块功能。

巡线程序:
当端口3的触碰传感器被按压时,将运转电机设置为A和D。进入循环后,如果端口4的颜色传感器检测到的反射光线强度小于30%,电机向右转60°。否则向左转60°。

6.2.8 传感器1
传感器模块组有颜色传感器、触碰传感器、超声波传感器。

EV3 Scratch家庭版在默认的情况下,不显示超声波传感器模块。
当EV3程序块连接了EV3 Scratch家庭版编程软件后,家庭版EV3 Scratch检测到EV3程序块上连接了超声波传感器。
EV3 Scratch家庭版的传感器模块组里才会出现超声波传感器模块。
EV3 Scratch教育版编程软件默认不显示红外线传感器模块。


6.2.9 传感器2
传感器模块组有红外线传感器、陀螺仪传感器、程序块按钮。

传感器是有默认端口的。仔细观察后,你会发现传感器的默认端口。每个端口默认对应一种传感器。

在EV3屏幕上实时显示陀螺仪传感器的角度值、角速度值。
在EV3屏幕上实时显示颜色传感器的反射光线强度,超声波传感器检测到的距离。
在EV3屏幕上实时显示按了哪个程序块按钮,A电机的运转度数。
6.2.10 运算符
运算符模块组有数学、比较、舍入、逻辑运算、范围、随机。

在EV3屏幕上实时显示运算符计算过的传感器数值。
包括四舍五入超声波传感器检测到的距离值;陀螺仪传感器检测的角度,角速度的绝对值;在1和10之间的随机数,颜色传感器的反射光线强度+环境光强度的值。
6.2.11 变量、列表与我的模块
EV3 Scratch变量、列表、我的模块功能与EV3头脑风暴是一样的。


使用EV3程序块按钮的上、下、左、右、中键控制EV3文字在屏幕上移动。
6.2.12 弹文字游戏
用EV3 Scratch编写一个类似弹球游戏的程序。

三个并行程序:
(1)用变量x计算EV3屏幕X的坐标(X坐标值的范围:0-177)。
(2)用变量y计算EV3屏幕Y的坐标(Y坐标值的范围:0-127)。
(3)每隔0·5秒在EV3屏幕上的X,Y坐标上写出EV3文字。
当X坐标增加到大于150时,再进入递减程序。当X坐标小于7时,进入递增。
当Y坐标增加到大于115时,再进入递减程序。当Y坐标小于7时,进入递增。

EV3弹球程序示意图
