惯性#

介绍#

惯性传感器测量机器人的运动和转向情况。

惯性传感器由两部分组成。陀螺仪测量转向,例如航向、旋转、陀螺仪速率和方向。加速度计测量运动变化,例如沿 x 轴、y 轴和 z 轴的加速或减速。

要使惯性命令出现在 VEXcode V5 中,必须在设备窗口中配置惯性传感器。

以下是可用方法列表:

方向控制 — 读取和控制航向、旋转和传感器校准。

  • heading — 返回传感器朝向的方向,从 0 到 359.99 度。

  • rotation — 返回传感器旋转了多远。

  • gyro_rate — 返回传感器在选定轴上的旋转速度。

  • orientation — 返回惯性传感器的横滚角、俯仰角或偏航角。

  • set_heading — 将惯性传感器的当前航向设置为新的航向值。

  • set_rotation — 将惯性传感器的当前旋转设置为新的旋转值。

  • calibrate — 校准惯性传感器。

  • reset_heading — 将惯性传感器的当前航向重置为 0 度。

  • reset_rotation — 将惯性传感器的当前旋转重置为 0 度。

  • is_calibrating — 返回惯性传感器当前是否正在校准。

  • changed — 注册一个函数,当惯性传感器的值发生变化时运行。

  • installed — 检查惯性传感器是否已安装。

运动——测量加速度、碰撞和转向行为。

  • acceleration — 返回传感器在选定轴上加速或减速的速度。

  • collision — 注册一个当惯性传感器检测到碰撞时要运行的函数。

  • set_turn_type — 设置哪个转弯方向返回正航向值。

  • get_turn_type — 返回哪个转弯方向返回正航向值。

构造器

  • Inertial – 创建一个惯性传感器。

方向#

heading#

航向是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。0 航向 heading。

初始航向为0度。如果传感器旋转超过359.99度,航向将回落至0度。

Usage:
inertial_1.heading(units)

参数

描述

units

可选。航向单位: DEGREES (默认)或 TURNS

# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(inertial_1.heading())

rotation#

旋转角度是指传感器转动的程度,以度为单位。与航向不同,旋转角度可以超过 359.99 度 rotation 返回惯性传感器的当前旋转角度(浮点数)。

向右转动会增加旋转角度,向左转动会减少旋转角度。例如,向右转动两圈将旋转 720 度。从 0 度向左转动一圈将旋转 -360 度。

Usage:
inertial_1.rotation(units)

参数

描述

units

可选。旋转单位: DEGREES (默认)或 TURNS

# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(inertial_1.rotation())

gyro_rate#

陀螺仪速率是指惯性传感器的旋转速度。gyro_rate gyro_rate ,返回所选轴上惯性传感器的当前旋转速度,数值为浮点数。

该值可以是正数也可以是负数,具体取决于传感器绕该轴旋转的方向。

Usage:
inertial_1.gyro_rate(axis, units)

参数

描述

axis

返回陀螺仪速率的轴:

  • AxisType.XAXIS
  • AxisType.YAXIS
  • AxisType.ZAXIS

units

可选。陀螺仪速率单位:

  • VelocityUnits.DPS (默认)
  • VelocityUnits.RPM
  • VelocityUnits.PERCENT

# Display the rate of change of
# rotation while turning
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(inertial_1.gyro_rate(AxisType.ZAXIS, VelocityUnits.DPS))
drivetrain.stop()

orientation#

方向是指惯性传感器在选定的转动轴上的当前角度。方向 orientation 返回惯性传感器的横滚角、俯仰角或偏航角,范围从 -180.00 度到 180.00 度,以浮点数形式表示。

横滚、俯仰和偏航描述了传感器倾斜或旋转的不同方式。

Usage:
inertial_1.orientation(type, units)

参数

描述

type

要返回的姿态角:

  • AxisType.PITCH
  • AxisType.ROLL
  • AxisType.YAW

units

可选。方向单位:

  • DEGREES (默认)
  • RotationUnits.TURNS

# Display the roll, pitch, and yaw of the Brain as it
# is rotated by hand
while True:
    brain.screen.clear_screen()
    brain.screen.print(inertial_1.orientation(AxisType.ROLL, DEGREES))
    brain.screen.next_row()
    brain.screen.print(inertial_1.orientation(AxisType.PITCH, DEGREES))
    brain.screen.next_row()
    brain.screen.print(inertial_1.orientation(AxisType.YAW, DEGREES))
    brain.screen.next_row()
    brain.screen.set_cursor(1, 1)
    wait(0.1, SECONDS)

set_heading#

航向是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。 set_heading 将惯性传感器的当前航向更改为新的航向值。

例如,如果传感器已转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度。然后,传感器可以根据该新方向跟踪其他航向。

Usage:
inertial_1.set_heading(value, units)

参数

描述

value

要设置的传感器航向值,单位为度。该值可以介于 0 到 359.99 之间。

units

可选。航向单位: DEGREES (默认)或 TURNS

# Turn the robot around
inertial_1.set_heading(180, DEGREES)
drivetrain.turn_to_heading(0)

set_rotation#

旋转角度是指传感器转动的程度,以度为单位。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。项目开始时,旋转角度值设置为 0 度。`set_rotation 会将惯性传感器的当前旋转角度更改为新值。

例如,如果传感器向右旋转了两圈,其旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,传感器可以根据这个新值跟踪旋转。

Usage:
inertial_1.set_rotation(value, units)

参数

描述

value

要设置的传感器旋转值,单位为度。可以是整数或浮点数(十进制数)。

units

可选。旋转单位: DEGREES (默认)或 TURNS

# Turn the robot around
inertial_1.set_rotation(-180, DEGREES)
drivetrain.turn_to_rotation(0)

calibrate#

calibrate 校准惯性传感器。

校准有助于传感器正确测量转弯角度。校准过程中请保持传感器静止。如果在校准过程中传感器移动,则航向、旋转、陀螺仪速率和方向值可能无法正确测量。

程序将等待校准完成后再运行下一行代码。

Usage:
inertial_1.calibrate()

参数

描述

此方法没有参数。

# Start calibration.
inertial_1.calibrate()
# Print that the Inertial Sensor is calibrating while
# waiting for it to finish calibrating.
while inertial_1.is_calibrating():
    brain.screen.clear()
    brain.screen.print("Inertial Sensor Calibrating")
    wait(50, MSEC)

reset_heading#

reset_heading 将惯性传感器的当前航向重置为 0 度。

使用此方法后,传感器的当前方向将变为新的 0 度航向。

Usage:
inertial_1.reset_heading()

参数

描述

此方法没有参数。

reset_rotation#

reset_rotation 将惯性传感器的当前旋转角度重置为 0 度。

使用这种方法后,传感器会从新的 0 度旋转值开始跟踪未来的旋转。

Usage:
inertial_1.reset_rotation()

参数

描述

此方法没有参数。

is_calibrating#

is_calibrating 返回惯性传感器当前是否正在校准。

  • True — 惯性传感器正在校准。

  • False ​​— 惯性传感器未进行校准。

Usage:
inertial_1.is_calibrating()

参数

描述

此方法没有参数。

changed#

changed 注册一个函数,当惯性传感器的值发生变化时,该函数将被调用。

Usage:
inertial_1.changed(callback, arg)

参数

描述

callback

先前定义的函数,当惯性传感器的值发生变化时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。

def my_function():
  brain.screen.print("Value changed!!")

# Call my_function whenever inertial_1's value changes
inertial_1.changed(my_function)

installed#

installed 检查惯性传感器是否已安装。

  • True — 惯性传感器已安装。

  • False ​​— 惯性传感器未安装。

Usage:
inertial_1.installed()

参数

描述

此方法没有参数。

运动#

acceleration#

加速度是指传感器加速或减速的快慢。加速度为 acceleration 返回惯性传感器在所选轴上的加速度,范围从 -4.0 G 到 4.0 G,以浮点数形式表示。

G 相当于你静止坐着时感受到的重力加速度。

The value can be positive or negative depending on the direction of acceleration on the selected axis. The axis options are AxisType.XAXIS, AxisType.YAXIS, and AxisType.ZAXIS.

Usage:
inertial_1.acceleration(type)

参数

描述

type

测量加速度的轴:

  • AxisType.XAXIS
  • AxisType.YAXIS
  • AxisType.ZAXIS

# Return the acceleration for the Z axis of the Inertial Sensor
zaccel = inertial_1.acceleration(AxisType.ZAXIS)

collision#

collision 注册一个函数,当惯性传感器检测到突然冲击或碰撞时,该函数将被调用。

Usage:
inertial_1.collision(callback, arg)

参数

描述

callback

先前定义的函数,当检测到碰撞时执行。

arg

可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。

def my_function():
  brain.screen.print("Collision!")

# Call my_function whenever a collision is detected
inertial_1.collision(my_function)

set_turn_type#

set_turn_type 设置哪个转弯方向返回正航向值。

Usage:
inertial_1.set_turn_type(turntype)

参数

描述

turntype

要设置的转向类型:

  • LEFT
  • RIGHT

get_turn_type#

get_turn_type 返回哪个转弯方向返回正航向值。

  • LEFT — 左转返回正航向值。

  • RIGHT — 右转返回正航向值。

Usage:
inertial_1.get_turn_type()

参数

描述

此方法没有参数。

构造函数#

Inertial#

Inertial 创建一个惯性传感器。

Usage:
Inertial(port)

参数

描述

port

惯性传感器所连接的智能端口,编号为 1 至 21。

# Create a new object "inertial_1" with the Inertial class.
inertial_1 = Inertial(Ports.PORT1)