惯性#
初始化惯性类#
使用以下构造函数创建惯性传感器:
惯性(端口)
范围 |
描述 |
---|---|
|
**可选。**如果使用 EXP Brain 的内置惯性传感器,则无需智能端口。如果连接外部惯性传感器,请指定惯性传感器所连接的 智能端口。 |
# Create a new object "brain_inertial" with the
# Inertial class.
brain_inertial = Inertial()
当引用 Inertial 类方法时,此“brain_inertial”对象将在整个 API 文档的所有后续示例中使用。
类方法#
calibrate()#
陀螺仪或惯性传感器将根据校准期间 EXP Brain 的方向自动调整其值,以便它在所有可能的 EXP Brain 方向上保持一致。
calibrate()
方法用于校准惯性传感器。校准应在惯性传感器静止不动时进行。校准至少需要 2 秒钟才能完成。
**返回:**无。
# 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()#
is_calibrating()
方法检查惯性传感器当前是否正在校准。
返回: 如果惯性传感器正在校准,则返回 True
。否则,返回 False
。
# 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()#
set_heading(value, units)
方法将惯性传感器的航向设置为指定值。
参数 |
描述 |
---|---|
价值 |
要设置的航向值。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**无。
# Set the Inertial Sensor's heading to 180 degrees.
brain_inertial.set_heading(180)
reset_heading()#
reset_heading()
方法将惯性传感器的航向重置为 0。
**返回:**无。
heading()#
heading(units)
方法返回惯性传感器的当前航向。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**惯性传感器的当前航向(以指定的单位表示)。
# Get the current heading for the Inertial Sensor.
value = brain_inertial.heading()
set_rotation()#
set_rotation(value, units)
方法设置惯性传感器的旋转。
参数 |
描述 |
---|---|
价值 |
要设置的旋转值。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**无。
# Set the Inertial Sensor's rotation to 180 degrees.
brain_inertial.set_rotation(180)
reset_rotation()#
reset_rotation()
方法将惯性传感器的旋转度重置为 0。
**返回:**无。
rotation()#
rotation(units)
方法返回惯性传感器的当前旋转。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**惯性传感器以指定单位表示的当前旋转。
# Print the value of the current rotation of the
# Inertial Sensor to the Brain's screen.
brain.screen.print(brain_inertial.rotation())
orientation()#
orientation(axis, units)
方法返回惯性传感器一个轴的方向。
参数 |
描述 |
---|---|
轴 |
有效的 AxisType。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**指定单位的轴方向值。
# Get the pitch value for the Inertial Sensor.
pitch = brain_inertial.orientation(OrientationType.PITCH)
gyro_rate()#
gyro_rate(axis, units)
方法返回惯性传感器一个轴的陀螺仪速率。
参数 |
描述 |
---|---|
轴 |
有效的 AxisType。 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
**返回:**以指定单位表示的轴陀螺仪速率值。
# Get the gyro rate for the Z axis of the Inertial Sensor.
zrate = brain_inertial.gyro_rate(ZAXIS)
acceleration()#
acceleration(axis)
方法返回惯性传感器的加速度。
参数 |
描述 |
---|---|
轴 |
有效的 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()#
changed(callback, arg)
方法注册一个回调函数,用于在惯性传感器的航向发生变化时使用。
参数 |
描述 |
---|---|
打回来 |
当惯性传感器航向改变时调用的回调函数。 |
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()#
collision(callback, arg)
方法注册一个当惯性传感器检测到碰撞时的回调函数。
参数 |
描述 |
---|---|
打回来 |
当检测到碰撞时调用的回调函数。 |
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()#
set_turn_type(turntype)
方法设置返回正值的方向。
参数 |
描述 |
---|---|
转盘式 |
有效的 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()#
get_turn_type()
方法返回的是返回正值的方向。
**返回:**以指定单位表示的惯性传感器的转动类型。
installed()#
installed()
方法检查惯性传感器是否已安装。
返回: 如果安装了惯性传感器,则返回 True
。如果没有安装,则返回 False
。
timestamp()#
timestamp()
方法返回从惯性传感器接收到的最后一个状态包的时间戳。
**返回:**最后接收的状态包的时间戳(以毫秒为单位)。