惯性#
介绍#
惯性传感器测量机器人的运动和转向情况。
惯性传感器由两部分组成。陀螺仪测量转向,例如航向、旋转、陀螺仪速率和方向。加速度计测量运动变化,例如沿 x 轴、y 轴和 z 轴的加速或减速。
以下是可用方法列表:
方向控制 — 读取和控制航向、旋转和传感器校准。
heading— 返回传感器朝向的方向,从 0 到 359.99 度。rotation— 返回传感器旋转了多远。gyro_rate— 返回传感器在选定轴上的旋转速度。orientation— 返回惯性传感器的横滚角、俯仰角或偏航角。calibrate— 校准惯性传感器。set_heading— 将惯性传感器的当前航向设置为新的航向值。set_rotation— 将惯性传感器的当前旋转设置为新的旋转值。reset_heading— 将惯性传感器的当前航向重置为 0 度。reset_rotation— 将惯性传感器的当前旋转重置为 0 度。is_calibrating— 返回惯性传感器当前是否正在校准。changed— 注册一个函数,当惯性传感器的值发生变化时运行。installed— 检查惯性传感器是否已安装。
运动——测量加速度、碰撞和转向行为。
acceleration— 返回传感器在选定轴上加速或减速的速度。collision— 注册一个当惯性传感器检测到碰撞时要运行的函数。set_turn_type— 设置哪个转弯方向返回正航向值。get_turn_type— 返回哪个转弯方向返回正航向值。
构造器
Inertial– 创建一个惯性传感器。
方向#
heading#
航向是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。0 航向 heading。
初始航向为0度。如果传感器旋转超过359.99度,航向将回落至0度。
Usage:
inertial_1.heading(units)
参数 |
描述 |
|---|---|
|
可选。航向单位: |
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(inertial_1.heading())
rotation#
旋转角度是指传感器转动的程度,以度为单位。与航向不同,旋转角度可以超过 359.99 度 rotation 返回惯性传感器的当前旋转角度(浮点数)。
向右转动会增加旋转角度,向左转动会减少旋转角度。例如,向右转动两圈将旋转 720 度。从 0 度向左转动一圈将旋转 -360 度。
Usage:
inertial_1.rotation(units)
参数 |
描述 |
|---|---|
|
可选。旋转单位: |
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(inertial_1.rotation())
gyro_rate#
陀螺仪速率是指惯性传感器的旋转速度。gyro_rate gyro_rate ,返回所选轴上惯性传感器的当前旋转速度,数值为浮点数。
该值可以是正数也可以是负数,具体取决于传感器绕该轴旋转的方向。
Usage:
inertial_1.gyro_rate(axis, units)
参数 |
描述 |
|---|---|
|
返回陀螺仪速率的轴:
|
|
可选。陀螺仪速率单位:
|
# 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 返回惯性传感器的横滚角、俯仰角或偏航角,范围从 -180.00 度到 180.00 度,以浮点数形式表示。
横滚、俯仰和偏航描述了传感器倾斜或旋转的不同方式。
Usage:
inertial_1.orientation(type, units)
参数 |
描述 |
|---|---|
|
要返回的姿态角:
|
|
可选。方向单位:
|
# 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 校准惯性传感器。
校准有助于传感器正确测量转弯角度。校准过程中请保持传感器静止。如果在校准过程中传感器移动,则航向、旋转、陀螺仪速率和方向值可能无法正确测量。
程序将等待校准完成后再运行下一行代码。
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#
航向是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。 set_heading 将惯性传感器的当前航向更改为新的航向值。
例如,如果传感器已转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度。然后,传感器可以根据该新方向跟踪其他航向。
Usage:
inertial_1.set_heading(value, units)
参数 |
描述 |
|---|---|
|
要设置的传感器航向值,单位为度。该值可以介于 0 到 359.99 之间。 |
|
可选。航向单位: |
# Turn the robot around
inertial_1.set_heading(180, DEGREES)
drivetrain.turn_to_heading(0)
set_rotation#
旋转角度是指传感器转动的程度,以度为单位。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。项目开始时,旋转角度值设置为 0 度。`set_rotation 会将惯性传感器的当前旋转角度更改为新值。
例如,如果传感器向右旋转了两圈,其旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,传感器可以根据这个新值跟踪旋转。
Usage:
inertial_1.set_rotation(value, units)
参数 |
描述 |
|---|---|
|
要设置的传感器旋转值,单位为度。可以是整数或浮点数(十进制数)。 |
|
可选。旋转单位: |
# Turn the robot around
inertial_1.set_rotation(-180, DEGREES)
drivetrain.turn_to_rotation(0)
reset_heading#
reset_heading 将惯性传感器的当前航向重置为 0 度。
使用此方法后,传感器的当前方向将变为新的 0 度航向。
Usage:
inertial_1.reset_heading()
参数 |
描述 |
|---|---|
此方法没有参数。 |
reset_rotation#
reset_rotation 将惯性传感器的当前旋转角度重置为 0 度。
使用这种方法后,传感器会从新的 0 度旋转值开始跟踪未来的旋转。
Usage:
inertial_1.reset_rotation()
参数 |
描述 |
|---|---|
此方法没有参数。 |
is_calibrating#
is_calibrating 返回惯性传感器当前是否正在校准。
True— 惯性传感器正在校准。False— 惯性传感器未进行校准。
Usage:
inertial_1.is_calibrating()
参数 |
描述 |
|---|---|
此方法没有参数。 |
changed#
changed 注册一个函数,当惯性传感器的值发生变化时,该函数将被调用。
Usage:
inertial_1.changed(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 检查惯性传感器是否已安装。
True— 惯性传感器已安装。False— 惯性传感器未安装。
Usage:
inertial_1.installed()
参数 |
描述 |
|---|---|
此方法没有参数。 |
运动#
acceleration#
加速度是指传感器加速或减速的快慢。加速度为 acceleration 返回惯性传感器在所选轴上的加速度,范围从 -4.0 G 到 4.0 G,以浮点数形式表示。
G 相当于你静止坐着时感受到的重力加速度。
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)
参数 |
描述 |
|---|---|
|
测量加速度的轴:
|
# Return the acceleration for the Z axis of the Inertial Sensor
zaccel = inertial_1.acceleration(AxisType.ZAXIS)
collision#
collision 注册一个函数,当惯性传感器检测到突然的冲击或碰撞时,该函数将被调用。
Usage:
inertial_1.collision(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 设置哪个转弯方向返回正航向值。
Usage:
inertial_1.set_turn_type(turntype)
参数 |
描述 |
|---|---|
|
要设置的转向类型:
|
get_turn_type#
get_turn_type 返回哪个转弯方向返回正航向值。
LEFT— 左转返回正航向值。RIGHT— 右转返回正航向值。
Usage:
inertial_1.get_turn_type()
参数 |
描述 |
|---|---|
此方法没有参数。 |
构造函数#
Inertial#
Inertial 创建一个惯性传感器。
Usage:
Inertial(smartport)
参数 |
描述 |
|---|---|
|
惯性传感器连接的智能端口,写成 |
# Create a new object "inertial_1" with the Inertial class.
inertial_1 = Inertial(Ports.PORT1)