惯性#
初始化惯性类#
使用以下构造函数创建惯性传感器:
Inertial(port)
范围 |
描述 |
---|---|
|
**可选。**如果使用 EXP Brain 的内置惯性传感器,则无需智能端口。如果连接外部惯性传感器,请指定惯性传感器所连接的 智能端口。 |
# 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.
类方法#
calibrate()#
陀螺仪或惯性传感器将根据校准期间 EXP Brain 的方向自动调整其值,以便它在所有可能的 EXP Brain 方向上保持一致。
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.
**返回:**无。
# 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.
参数 |
描述 |
---|---|
价值 |
要设置的航向值。 |
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**无。
# 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.
**返回:**无。
heading()#
The heading(units)
method returns the current heading of the Inertial Sensor.
参数 |
描述 |
---|---|
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**惯性传感器的当前航向(以指定的单位表示)。
# 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.
参数 |
描述 |
---|---|
价值 |
要设置的旋转值。 |
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**无。
# 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.
**返回:**无。
rotation()#
The rotation(units)
method returns the current rotation of the Inertial Sensor.
参数 |
描述 |
---|---|
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**惯性传感器以指定单位表示的当前旋转。
# 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.
参数 |
描述 |
---|---|
轴 |
有效的 AxisType。 |
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**指定单位的轴方向值。
# 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.
参数 |
描述 |
---|---|
轴 |
有效的 AxisType。 |
单位 |
Optional. A valid VelocityUnits type. The default is |
**返回:**以指定单位表示的轴陀螺仪速率值。
# 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.
参数 |
描述 |
---|---|
轴 |
有效的 AxisType。 |
**返回:**以重力单位表示的轴加速度值。
# 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.
参数 |
描述 |
---|---|
打回来 |
当惯性传感器航向改变时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# 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.
参数 |
描述 |
---|---|
打回来 |
当检测到碰撞时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# 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.
参数 |
描述 |
---|---|
转盘式 |
有效的 TurnType。 |
**返回:**无。
# 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.
**返回:**以指定单位表示的惯性传感器的转动类型。
installed()#
The installed()
method checks if the Inertial Sensor is installed.
Returns: True
if the Inertial Sensor is installed. False
if it is not.
timestamp()#
The timestamp()
method returns the timestamp of the last received status packet from the Inertial Sensor.
**返回:**最后接收的状态包的时间戳(以毫秒为单位)。