Python#

本指南详细介绍了如何在 VEXcode IQ(第二代)中使用 Python 方法。您将学习如何阅读和理解每种方法,并了解其详尽的描述、参数和使用示例。

注意: IQ(第二代)大脑通过内存有限的虚拟机运行 Python 程序。虽然源代码大小有限制,但程序能否运行还取决于其复杂度(例如函数、变量和逻辑的数量)。因此,即使程序大小不超过限制,大型或复杂的程序也可能无法编译或运行。C/C++ 程序通常受 IQ(第二代)的这些限制影响较小。

了解方法条目#

API 参考中的每个方法条目都包含以下组件:

  • 命令名称 – 命令的正式名称。

  • 描述 – 对命令的作用和可能返回的内容的简要说明。

  • 定义 – 命令的语法,显示如何在代码中编写它。

  • 参数表——列出命令接受的所有输入。

  • 示例代码 – 以复制粘贴代码块形式提供的使用示例。

示例方法条目#

驾驶#

drive moves the drivetrain in a specified direction indefinitely.

Usage:
drivetrain.drive(direction, velocity, units)

参数

描述

direction

The direction in which to drive: FORWARD or REVERSE

velocity

Optional. The velocity at which the drivetrain will move. This can be an integer or decimal (float). If the velocity is not specified, the default velocity is 50%.

units

Optional. The unit that represents the velocity:

  • PERCENT
  • RPM (Rotations per minute)
  • VelocityUnits.DPS (Degrees per second)

# Drive forward, then stop
drivetrain.drive(FORWARD)
wait(2, SECONDS)
drivetrain.stop()

# Drive slowly in reverse then stop
drivetrain.drive(REVERSE, 20, PERCENT)
wait(2, SECONDS)
drivetrain.stop()