运动#
介绍#
GO Motion 命令可使电机旋转、保持位置,并精确控制速度和扭矩。
For the examples below, the configured motor will be named arm. This name is used in all subsequent examples throughout this API documentation when referring to Motor class methods.
以下是所有方法的列表:
Actions — Control the movement of motors.
spin— Spins a motor in a specified direction indefinitely.spin_for— Spins a motor for a specified distance.spin_to_position— Spins a motor to an absolute position.stop— Stops a motor.
Mutators — Change the motor’s different attributes.
set_velocity— Sets the default velocity for the motor.set_max_torque— Sets the maximum torque for a motor.set_position— Sets the motor’s position to a specific value.set_stopping— Sets the stop behavior (brake, coast, or hold).set_timeout— Limits how long a motor function waits before giving up if movement is blocked.
Getters — Return data from motors.
get_position— Returns a motor’s current position.get_velocity— Returns a motor’s current velocity.get_current— Returns the current being used by a motor.is_stopped— Returns whether a motor is currently not spinning.is_moving— Returns whether a motor is currently spinning.
行动#
spin#
spin 使电机无限地沿指定方向旋转。
Usage:
arm.spin(direction)
参数 |
描述 |
|---|---|
|
电机旋转的方向:
|
# Build Used: Competition Advanced 2.0
def main():
# Spin motor in reverse, then stop
arm.spin(REVERSE)
wait(1, SECONDS)
arm.stop()
# Start threads — Do not delete
start_thread(main)
spin_for#
spin_for 使电机沿指定方向旋转特定角度。
Usage:
arm.spin_for(direction, angle, wait)
参数 |
描述 |
|---|---|
|
电机旋转的方向:
|
|
The number of degrees the motor will spin as a float or integer. |
|
Optional.
|
# Build Used: Competition Advanced 2.0
def main():
# Spin motor in both directions
arm.spin_for(REVERSE, 180)
arm.spin_for(FORWARD, 180)
# Start threads — Do not delete
start_thread(main)
spin_to_position#
spin_to_position 将电机旋转到绝对位置。
Usage:
arm.spin_to_position(angle, wait)
参数 |
描述 |
|---|---|
|
以浮点数或整数形式指定电机旋转的位置。 |
|
Optional.
|
# Build Used: Competition Advanced 2.0
def main():
# Put motor at 0 position after spinning
arm.set_position(0)
arm.spin(REVERSE)
wait(1, SECONDS)
arm.spin_to_position(0)
# Start threads — Do not delete
start_thread(main)
stop#
stop 停止电机旋转。
Usage:
arm.stop()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Competition Advanced 2.0
def main():
# Spin motor in reverse, then stop
arm.spin(REVERSE)
wait(1, SECONDS)
arm.stop()
# Start threads — Do not delete
start_thread(main)
修改器#
set_velocity#
set_velocity 设置电机的默认速度。此速度设置将用于后续调用任何电机函数。
Usage:
arm.set_velocity(velocity)
参数 |
描述 |
|---|---|
|
The velocity at which the motor will spin as a percent. |
# Build Used: Competition Advanced 2.0
def main():
# Spin motor at different velocities
arm.spin_for(REVERSE, 180)
wait(1, SECONDS)
# Spin slower
arm.set_velocity(20)
arm.spin_for(FORWARD, 180)
wait(1, SECONDS)
# Spin faster
arm.set_velocity(100)
arm.spin_for(REVERSE, 180)
# Start threads — Do not delete
start_thread(main)
set_max_torque#
set_max_torque 设置电机的最大扭矩。
Usage:
arm.set_max_torque(value)
参数 |
描述 |
|---|---|
|
The new maximum torque for the motor as a float or integer in percent. |
# Build Used: Competition Advanced 2.0
def main():
# Spin the motor at different torques
arm.spin_for(REVERSE, 180)
wait(1, SECONDS)
# Less torque
arm.set_max_torque(20)
arm.spin_for(FORWARD, 180)
wait(1, SECONDS)
# More torque
arm.set_max_torque(100)
arm.spin_for(REVERSE, 180)
# Start threads — Do not delete
start_thread(main)
set_position#
set_position 设置电机的位置。
Usage:
arm.set_position(position)
参数 |
描述 |
|---|---|
|
新位置以度为单位的整数。 |
# Build Used: Competition Advanced 2.0
def main():
# Position the motor at the new 0 degrees
arm.set_position(180)
arm.spin_to_position(0)
# Start threads — Do not delete
start_thread(main)
set_stopping#
set_stopping 设置电机的停止模式。
Usage:
arm.set_stopping(mode)
参数 |
描述 |
|---|---|
|
How the motor will stop:
|
# Build Used: Competition Advanced 2.0
def main():
# Spin motor in reverse then coast to a stop
arm.set_velocity(100)
arm.set_stopping(COAST)
arm.spin(REVERSE)
wait(2, SECONDS)
arm.stop()
# Start threads — Do not delete
start_thread(main)
set_timeout#
set_timeout 设置电机功能等待达到目标的时间限制。如果电机无法在设定的时间内完成运动,它将自动停止并继续执行下一个功能。
Note: The motor’s time limit prevents motor functions that do not reach their target position from blocking the execution of the rest of the project.
Usage:
arm.set_timeout(value, units)
参数 |
描述 |
|---|---|
|
运动功能在停止并移动到下一个功能之前运行的最大秒数(整数或浮点数)。 |
|
Optional. The unit that represents the time:
|
# Build Used: Competition Advanced 2.0
def main():
# Spin the motor for one and a half seconds then reset
arm.set_position(0)
arm.set_timeout(1.5, SECONDS)
arm.spin_for(REVERSE, 1000)
arm.spin_to_position(0)
# Start threads — Do not delete
start_thread(main)
吸气剂#
get_position#
get_position returns the current position of the motor as an integer or float in degrees.
Usage:
arm.get_position()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Competition Advanced 2.0
def main():
# Spin motor while monitoring the position
console.print("Start: ")
console.print(arm.get_position())
console.new_line()
arm.spin(REVERSE)
wait(1, SECONDS)
arm.stop()
console.print("After: ")
console.print(arm.get_position())
# Start threads — Do not delete
start_thread(main)
get_velocity#
get_velocity returns the current velocity of the motor as an integer or float in percent.
Usage:
arm.get_velocity()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Competition Advanced 2.0
def main():
# Spin the motor and display the velocity
console.print("Start: ")
console.print(arm.get_velocity())
console.new_line()
arm.spin(REVERSE)
wait(1, SECONDS)
console.print("Moving: ")
console.print(arm.get_velocity())
arm.stop()
# Start threads — Do not delete
start_thread(main)
get_current#
get_current returns the current of the motor in amps.
Usage:
arm.get_current()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Competition Advanced 2.0
def main():
# Spin the motor and display the current
console.print("Start: ")
console.print(arm.get_current())
console.new_line()
arm.spin(REVERSE)
wait(1, SECONDS)
console.print("Moving: ")
console.print(arm.get_current())
arm.stop()
# Start threads — Do not delete
start_thread(main)
is_stopped#
is_stopped returns a Boolean indicating whether or not the motor is stopped.
True— The motor is stopped.False— The motor is spinning.
Usage:
arm.is_stopped()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Competition Advanced 2.0
def main():
# Turn the eye light on when moving
arm.spin_for(REVERSE, 200, wait=False)
wait(0.1, SECONDS)
while True:
if arm.is_stopped():
eye.set_light(OFF)
else:
eye.set_light(ON)
# Start threads — Do not delete
start_thread(main)
is_moving#
is_moving returns a Boolean indicating whether or not the motor is stopped.
True— The motor is spinning.False— The motor is stopped.
Usage:
arm.is_moving()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Build Used: Competition Advanced 2.0
def main():
# Turn the eye light on while moving
arm.spin_for(FORWARD, 200, wait=False)
wait(0.1, SECONDS)
while True:
if arm.is_moving():
eye.set_light(ON)
else:
eye.set_light(OFF)
# Start threads — Do not delete
start_thread(main)