转变#

介绍#

Switch 模块帮助学生从基于模块的编程过渡到基于文本的编程。借助 Switch 模块,学生可以直接在模块项目中输入 Python 代码——编写命令、条件、值或函数定义——同时仍然可以使用模块构建项目的其他部分。单个模块也可以转换为相应的 Python 命令。

要了解如何输入 Python 命令、方法和运算符,请参阅 VEX CTE Python API

由于 Switch 代码块包含 Python 代码,因此拼写、标点符号和缩进必须正确,项目才能按预期运行。

以下是所有 Switch 模块的列表:

切换模块 — 向 Blocks 项目添加 Python 代码。

  • 堆栈 — 提供一个空间来键入与堆栈块按顺序运行的 Python 命令。

  • C Block — Provides a space to type Python control code, such as if, while, or for statements.

  • C Expandable — Provides a space to type Python control code with multiple branches, such as if, elif, or else.

  • Boolean — Provides a space to type Python code that returns True or False.

  • Reporter — 提供一个空间来键入返回值的 Python 代码。

  • Hat — 提供一个空间来定义 Python 函数。

#

堆栈 块提供了一个空间,用于键入与其他堆栈块按顺序运行的 Python 命令。

堆栈块#
[] :: custom-switch

堆栈 块可以包含一行或多行 Python 代码。

启动时,使用堆叠块将 6 轴机械臂移动到指定位置。#
当开始 :: hat events
[Move the 6-Axis Arm to a standard position at the start of the project.]
[arm.move_to(120, 120, 75, False)] :: custom-switch

C 块#

The C Block provides a space to type Python control code, such as if, while, or for statements. Blocks placed inside the C Block run as part of that Python control structure.

C 块#
if [] :: custom-switch
结束

可在C 代码块中使用的 Python 控制代码包括:

启动时,使用 C 块检查 6 轴机械臂是否可以到达某个位置。#
当开始 :: hat events
[Check if the 6-Axis Arm can move to a position.]
if [if not arm.can_arm_reach_to(0, 0, 0):] :: custom-switch
在控制台上打印 [The 6-Axis Arm can't move to this position.]◀ 并设定光标为下一行
结束

C 可扩展#

The C Expandable block provides a space to type Python control code with multiple branches, such as if, elif, or else. Additional branches can be added by selecting the plus arrow.

C 可扩展块#
if [] :: custom-switch-expand
结束

可在C Expandable代码块中使用的Python控制代码包括:

启动时,使用 C Expandable 模块检查 6 轴机械臂是否可以到达某个位置。#
当开始
[Check if the 6-Axis Arm can or can't move to a position.]
if [if not arm.can_arm_reach_to(0, 0, 0):] :: custom-switch-expand
在控制台上打印 [The 6-Axis Arm can't move to this position.]◀ 并设定光标为下一行
else [else:] :: custom-switch-expand
在控制台上打印 [The 6-Axis Arm can move to this position.]◀ 并设定光标为下一行
结束

布尔值#

The Boolean block provides a space to type Python code that returns True or False. It can be placed inside blocks with hexagonal inputs.

布尔块#
<[] :: custom-switch>

布尔值块可以包含Python条件。

带有 Python 条件的布尔块#
<[bumper_a.is_pressed()] :: custom-switch>

布尔块也可以使用逻辑运算符:

带有 Python 逻辑运算符的布尔块#
<[bumper_a.is_pressed() or bumper_b.is_pressed()] :: custom-switch>

布尔块可以使用比较运算符来比较值:

  • Greater than: >

  • Less than: <

  • Equal to: ==

  • Other comparison operators include <=, !=, and >=

带有 Python 比较运算符的布尔块#
<[drivetrain.get_heading()> 100] :: custom-switch>
启动时,使用布尔块检查 6 轴机械臂是否可以到达某个位置。#
当开始
[Check if the 6-Axis Arm can move to a position.]
如果 <非 <[arm.can_arm_reach_to(0, 0, 0)] :: custom-switch>> 那么
在控制台上打印 [The 6-Axis Arm can't move to this position.]◀ 并设定光标为下一行
结束

记者#

Reporter 代码块提供了一个用于编写返回值的 Python 代码的空间。它可以放置在带有循环输入的代码块内。

记者栏#
([] :: custom-switch)

Reporter 代码块可以包含返回值的 Python 代码。

包含返回值的 Python 代码的报告器块#
([arm.get_y()] :: custom-switch)

数学运算符可以在Reporter模块中使用:

  • Addition: +

  • Subtraction: -

  • Division: /

  • Multiplication: *

带有 Python 添加功能的 Reporter 块#
([arm.get_y() + 45] :: custom-switch)
启动时,在 6 轴机械臂移动时打印当前的 y 轴位置。#
当开始
[Display the current y-position while the 6-Axis Arm moves.]
移动 [手臂 v] 到位置x:(-100) y:(200) z:(100) [毫米 v] ◀ 并且不等待
重复直到 <[手臂 v] 已结束?>
在控制台上打印 ([arm.get_y()] :: custom-switch)◀ 并设定光标为下一行
等待 [0.25] 秒
结束

帽子#

帽子 代码块提供了一个定义 Python 函数的空间。附加在 帽子 下的代码块将成为该函数的主体。

帽块#
[] :: hat custom-switch

Hat 代码块中输入 Python 函数定义。参数放在圆括号内。如果函数不使用参数,则使用空圆括号。

带有 Python 函数定义的帽子模块#
[def function_name(parameter):] :: hat custom-switch
启动时,使用 Hat 块来定义和调用函数。#
[def increment_y_100():] :: hat custom-switch
[Create a function to increment the 6-Axis Arm.]
[手臂 v] 位置递增x:[0] y:[100] z:[0] [毫米 v] ▶

当开始 :: hat events
[increment_y_100()] :: custom-switch