Inertial#

Initializing the Inertial Class#

The Inertial constructor creates an Inertial object in the specified Smart Port:

This constructor uses one parameter:

Parameter

Description

port

Optional. If using the IQ (2nd gen) Brain’s built-in Inertial Sensor, a Smart Port is unneeded. If connecting an external Inertial Sensor, specify the Smart Port that the Inertial Sensor is connected to.

# Create a new object "brain_inertial" with the
# Inertial class.
brain_inertial = Inertial()

This brain_inertial object will be used in all subsequent examples throughout this API documentation when referring to Inertial class methods.

Class Methods#

calibrate()#

The calibrate() method calibrates the Inertial Sensor. Calibration should be done when the Inertial Sensor is not moving. Allow at least 2 seconds for calibration to complete.

The Inertial Sensor will automatically adjust its values depending on the orientation of the IQ (2nd gen) Brain during calibration so it remains consistent across all possible IQ (2nd gen) Brain orientations.

../../_images/IQ_right_orientation.png../../_images/IQ_left_orientation.png../../_images/IQ_top_orientation.png

Returns: None.

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

is_calibrating()#

The is_calibrating() method checks if the Inertial Sensor is currently calibrating.

Returns: True if the Inertial Sensor is calibrating. False if it is not.

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

set_heading()#

The set_heading(value, units) method sets the heading of the Inertial Sensor to a specified value.

Parameters

Description

value

The heading value to set.

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: None.

# Set the Inertial Sensor's heading to 180 degrees.
brain_inertial.set_heading(180)

reset_heading()#

The reset_heading() method resets the heading of the Inertial Sensor to 0.

Returns: None.

heading()#

The heading(units) method returns the current heading of the Inertial Sensor.

Parameters

Description

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: The current heading of the Inertial Sensor in the specified units.

# Get the current heading for the Inertial Sensor.
value = brain_inertial.heading()

set_rotation()#

The set_rotation(value, units) method sets the rotation of the Inertial Sensor.

Parameters

Description

value

The rotation value to set.

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: None.

# Set the Inertial Sensor's rotation to 180 degrees.
brain_inertial.set_rotation(180)

reset_rotation()#

The reset_rotation() method resets the rotation of the Inertial Sensor to 0.

Returns: None.

rotation()#

The rotation(units) method returns the current rotation of the Inertial Sensor.

Parameters

Description

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: The current rotation of the Inertial Sensor in the specified units.

# Print the value of the current rotation of the
# Inertial Sensor to the Brain's screen.
brain.screen.print(brain_inertial.rotation())

orientation()#

The orientation(axis, units) method returns the orientation for one axis of the Inertial Sensor.

Parameters

Description

axis

A valid AxisType.

units

Optional. A valid RotationUnits type. The default is DEGREES.

Returns: A value for the axis orientation in the units specified.

# Get the pitch value for the Inertial Sensor.
pitch = brain_inertial.orientation(OrientationType.PITCH)

gyro_rate()#

The gyro_rate(axis, units) method returns the gyro rate for one axis of the Inertial Sensor.

Parameters

Description

axis

A valid AxisType.

units

Optional. A valid VelocityUnits type. The default is DPS.

Returns: A value for the gyro rate of the axis in the units specified.

# Get the gyro rate for the Z axis of the Inertial Sensor.
zrate = brain_inertial.gyro_rate(ZAXIS)

acceleration()#

The acceleration(axis) method returns the acceleration of the Inertial Sensor.

Parameters

Description

axis

A valid AxisType.

Returns: A value for the acceleration of the axis in units of gravity.

# Get the acceleration for the Z axis of the Inertial Sensor.
zaccel = brain_inertial.acceleration(ZAXIS)

# Print the value of the current acceleration of the z-axis
# of the Inertial Sensor to the Brain's screen.
brain.screen.print(zaccel)

changed()#

The changed(callback, arg) method registers a callback function for when the Inertial Sensor’s heading changes.

Parameters

Description

callback

The callback function to be called when the Inertial Sensor heading changes.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: An instance of the Event class.

# Define a function heading_changed()
def heading_changed():
    # The Brain will print that the heading changed on the
    # Brain's screen.
    brain.screen.print("heading changed ")
# Run heading_changed when the heading for this
# Inertial Sensor changes.
brain_inertial.changed(heading_changed)

collision()#

The collision(callback, arg) method registers a callback function for when the Inertial Sensor detects a collision.

Parameters

Description

callback

The callback function to be called when a collision is detected.

arg

Optional. A tuple of arguments to pass to the callback function.

Returns: An instance of the Event class.

# Define a function collision_occur().
def collision_occur():
    # The Brain will print that a collision was detected by
    # the Inertial Sensor on the Brain's screen.
    brain.screen.print("collision detected ")
# Run collision_occur when the heading for this
# Inertial Sensor detects a collision.
brain_inertial.collision(collision_occur)

set_turn_type()#

The set_turn_type(turntype) method sets the direction that returns positive values for the heading.

Parameters

Description

turntype

A valid TurnType.

Returns: None.

# Set the turn type to left.
brain_inertial.set_turn_type(LEFT)

# Set the turn type to right.
brain_inertial.set_turn_type(RIGHT)

get_turn_type()#

The get_turn_type() method returns the direction that returns positive values for heading.

Returns: The turn type of the Inertial Sensor in the specified units.

installed()#

The installed() method checks if the Inertial Sensor is installed.

Returns: True if the Inertial Sensor is installed. False if it is not.