惯性#

介绍#

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

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

以下是可用方法列表:

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

  • heading — Returns the direction the sensor is facing, from 0 to 359.99 degrees.

  • rotation — Returns how far the sensor has turned.

  • gyro_rate — Returns how fast the sensor is rotating on the selected axis.

  • orientation — Returns the Inertial Sensor’s roll, pitch, or yaw angle.

  • calibrate — Calibrates the Inertial Sensor.

  • set_heading — Sets the Inertial Sensor’s current heading to a new heading value.

  • set_rotation — Sets the Inertial Sensor’s current rotation to a new rotation value.

  • reset_heading — Resets the Inertial Sensor’s current heading to 0 degrees.

  • reset_rotation — Resets the Inertial Sensor’s current rotation to 0 degrees.

  • is_calibrating — Returns whether the Inertial Sensor is currently calibrating.

  • changed — Registers a function to run when the Inertial Sensor’s value changes.

  • installed — Checks if the Inertial Sensor is installed.

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

  • acceleration — Returns how quickly the sensor is speeding up or slowing down on the selected axis.

  • collision — Registers a function to run when the Inertial Sensor detects a collision.

  • set_turn_type — Sets which turn direction returns positive heading values.

  • get_turn_type — Returns which turn direction returns positive heading values.

构造器

  • Inertial – Creates an Inertial Sensor.

方向#

heading#

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. heading returns the sensor’s current heading as a float.

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

Usage:
inertial_1.heading(units)

参数

描述

units

Optional. The heading unit: DEGREES (default) or TURNS

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

rotation#

Rotation is how much the sensor has turned, measured in degrees. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees. At the beginning of a project, the rotation value is set to 0 degrees. rotation returns the Inertial Sensor’s current rotation as a float.

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

Usage:
inertial_1.rotation(units)

参数

描述

units

Optional. The rotation unit: DEGREES (default) or TURNS

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

gyro_rate#

Gyro rate is how fast the Inertial Sensor is rotating. gyro_rate returns the current rotation speed of the Inertial Sensor on the selected axis as a float.

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

Usage:
inertial_1.gyro_rate(axis, units)

参数

描述

axis

The axis to return the gyro rate from:

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

units

Optional. The gyro rate unit:

  • VelocityUnits.DPS (default)
  • 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 is the Inertial Sensor’s current angle on a selected turning axis. orientation returns the roll, pitch, or yaw of the Inertial Sensor, from -180.00 to 180.00 degrees, as a float.

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

Usage:
inertial_1.orientation(type, units)

参数

描述

type

The orientation angle to return:

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

units

Optional. The orientation unit:

  • DEGREES (default)
  • 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)

calibrate#

calibrate calibrates the Inertial Sensor.

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

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

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)

set_heading#

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. set_heading changes the Inertial Sensor’s current heading to a new heading value.

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

Usage:
inertial_1.set_heading(value, units)

参数

描述

value

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

units

Optional. The heading unit: DEGREES (default) or TURNS

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

set_rotation#

Rotation is how much the sensor has turned, measured in degrees. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees. At the beginning of a project, the rotation value is set to 0 degrees. set_rotation changes the Inertial Sensor’s current rotation to a new value.

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

Usage:
inertial_1.set_rotation(value, units)

参数

描述

value

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

units

Optional. The rotation unit: DEGREES (default) or TURNS

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

reset_heading#

reset_heading resets the Inertial Sensor’s current heading to 0 degrees.

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

Usage:
inertial_1.reset_heading()

参数

描述

此方法没有参数。

reset_rotation#

reset_rotation resets the Inertial Sensor’s current rotation to 0 degrees.

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

Usage:
inertial_1.reset_rotation()

参数

描述

此方法没有参数。

is_calibrating#

is_calibrating returns whether the Inertial Sensor is currently calibrating.

  • True — The Inertial Sensor is calibrating.

  • False — The Inertial Sensor is not calibrating.

Usage:
inertial_1.is_calibrating()

参数

描述

此方法没有参数。

changed#

changed registers a function to be called whenever the Inertial Sensor’s value changes.

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 checks if the Inertial Sensor is installed.

  • True — The Inertial Sensor is installed.

  • False — The Inertial Sensor is not installed.

Usage:
inertial_1.installed()

参数

描述

此方法没有参数。

运动#

acceleration#

Acceleration is how quickly the sensor is speeding up or slowing down. acceleration returns the acceleration of the Inertial Sensor on the selected axis, from -4.0 G to 4.0 G, as a float.

A G is a unit used to measure acceleration. 1 G is about the acceleration you feel from gravity while sitting still.

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

The axis to measure acceleration on:

  • 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 registers a function to be called whenever the Inertial Sensor detects a sudden impact or 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 sets which turn direction returns positive heading values.

Usage:
inertial_1.set_turn_type(turntype)

参数

描述

turntype

The turn type to set:

  • LEFT
  • RIGHT

get_turn_type#

get_turn_type returns which turn direction returns positive heading values.

  • LEFT — Left turns return positive heading values.

  • RIGHT — Right turns return positive heading values.

Usage:
inertial_1.get_turn_type()

参数

描述

此方法没有参数。

构造函数#

Inertial#

Inertial creates an Inertial Sensor.

Usage:
Inertial(smartport)

参数

描述

smartport

The Smart Port that the Inertial Sensor is connected to, written as Ports.PORTx, where x is the port number.

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