惯性#

介绍#

An Inertial Sensor measures how the robot is moving and turning.

The Inertial Sensor uses two parts to do this. The gyroscope measures turning, such as heading, rotation, gyro rate, and orientation. The accelerometer measures changes in motion, such as speeding up or slowing down along the x-, y-, and z-axes.

以下是可用方法列表:

Orientation — Read and control heading, rotation, and sensor calibration.

  • 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.

Motion — Measure acceleration, collisions, and turn direction behavior.

  • 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.

The starting heading is 0 degrees. If the sensor turns past 359.99 degrees, the heading wraps back to 0 degrees.

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.

Turning right increases the rotation, and turning left decreases the rotation. For example, making two full turns to the right will return a rotation of 720 degrees. Turning one full turn to the left from 0 degrees will return a rotation of -360 degrees.

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.

The value can be positive or negative depending on the direction the sensor is rotating on that axis.

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.

Roll, pitch, and yaw describe different ways the sensor can tilt or turn.

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.

Calibration helps the sensor measure turns correctly. Keep the sensor still during calibration. If the sensor moves during calibration, heading, rotation, gyro rate, and orientation values may not measure correctly.

The project will wait until calibration is done before the next line of code runs.

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.

For example, if the sensor has turned to face right, setting the heading to 0 degrees makes that right-facing position the new 0 degrees. Then the sensor can track other headings based on that new direction.

Usage:
inertial_1.set_heading(value, units)

参数

描述

value

The heading value, in degrees, to set for the sensor. This can be a value from 0 to 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.

For example, if the sensor has made two full turns to the right, its rotation value will be 720 degrees. Setting the rotation to 0 degrees will reset that rotation from 720 to 0 degrees. Then the sensor can track rotations based on that new value.

Usage:
inertial_1.set_rotation(value, units)

参数

描述

value

The rotation value, in degrees, to set for the sensor. This can be an integer or a decimal (float).

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.

After this method is used, the sensor’s current direction becomes the new 0 degree heading.

Usage:
inertial_1.reset_heading()

参数

描述

此方法没有参数。

reset_rotation#

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

After this method is used, the sensor tracks future turns from the new 0 degree rotation value.

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)