加速度计#
初始化加速度计类#
使用以下构造函数创建加速度计:
Accelerometer(port, sensitivity)
此构造函数使用两个参数:
范围 |
描述 |
---|---|
|
|
|
Optional. Enables high sensitivity mode (+/- 2g) on the Accelerometer. |
必须先创建 Brain 或 3-Wire Expander,然后才能使用加速度计类构造函数创建对象。
# Create the Brain.
brain = Brain()
# Construct an Accelerometer "accel" with the
# Accelerometer class.
accel = Accelerometer(brain.three_wire_port.a)
This accel
object will be used in all subsequent examples throughout this API documentation when referring to Accelerometer class methods.
为了获得最佳功能,建议为每个轴使用单独的加速度计,并单独初始化它们:
accel_x = Accelerometer(brain.three_wire_port.a)
accel_y = Accelerometer(brain.three_wire_port.b)
accel_z = Accelerometer(brain.three_wire_port.c)
类方法#
acceleration()#
The acceleration()
method reads the value of the Accelerometer scaled to units of gravity.
返回: 范围内的值 +/- 6G,如果设置了高灵敏度模式,则为 +/-2G。
# Drive the robot forward.
drivetrain.drive(FORWARD)
# Get the Accelerometer's acceleration.
value = accel.acceleration()
changed()#
The changed(callback, arg)
method registers a function to be called when the value of the accelerometer changes.
参数 |
描述 |
---|---|
打回来 |
当轴值改变时调用的函数。 |
arg |
**可选。**用于向回调函数传递参数的元组。 |
**返回:**事件类的一个实例。
# Drive the robot forward.
drivetrain.drive(FORWARD)
# Define the function accelerometer_changed.
def accelerometer_changed():
# The Brain will print that the Accelerometer's value
# changed on the Brain's screen.
brain.screen.print("accelerometer changed")
# Run accelerometer_changed when the value of the
# Accelerometer changes.
accel.changed(accelerometer_changed)