运动#
介绍#
GO 运动命令让电机旋转、保持位置并精确控制速度和扭矩。
For the examples below, the configured motor will be named arm
. They will be used in all subsequent examples throughout this API documentation when referring to Motor
class methods.
以下是所有方法的列表:
动作——控制电机的运动。
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.
改变器——改变电机的不同属性。
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 – 从电机返回数据。
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)
参数 |
描述 |
---|---|
|
电机旋转的方向:
|
|
电机旋转的度数,以浮点数或整数表示。 |
|
可选
wait=False |
# 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)
参数 |
描述 |
---|---|
|
以浮点数或整数形式指定电机旋转的位置。 |
|
可选
wait=False |
# 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)
参数 |
描述 |
---|---|
|
电机旋转的速度(以百分比表示)。 |
# 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)
参数 |
描述 |
---|---|
|
电机的新最大扭矩,以浮点数或整数表示。 |
# 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)
参数 |
描述 |
---|---|
|
电机方式:
|
# Build Used: Competition Advanced 2.0
def main():
# Spin motor forward 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
设置电机功能等待达到目标的时间限制。如果电机无法在设定的时间内完成运动,它将自动停止并继续执行下一个功能。
**注意:**电机的时间限制用于防止未达到目标位置的电机功能停止项目其余部分的执行。
Usage:
arm.set_timeout(value, units)
参数 |
描述 |
---|---|
|
运动功能在停止并移动到下一个功能之前运行的最大秒数(整数或浮点数)。 |
|
可选。表示时间的单位:
|
# 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 a motor as an integer or as a 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 a motor as an integer or as a 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
返回一个布尔值,表示电机是否停止。
True
– 电机停止。False
– 电机正在旋转。
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
返回一个布尔值,指示电机是否正在旋转。
True
– 电机正在旋转。False
– 电机停止。
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)