智能驾驶#
初始化 SmartDrive 类#
使用以下构造函数创建 SmartDrive:
SmartDrive(lm,rm,g,车轮行程,轮距,轮距,单位,外部齿轮比)
此构造函数使用八个参数:
范围 |
描述 |
---|---|
|
|
|
|
|
|
|
**可选。**驱动轮的周长。默认值为 300 毫米。 |
|
**可选。**传动系统的轮距。默认值为 320 毫米。 |
|
**可选。**传动系统的轴距。默认值为 320 毫米。 |
|
可选。 为 wheelTravel、trackWidth 和 wheelBase 指定的单位提供有效的 DistanceUnit。默认值为“MM”。 |
|
**可选。**如果使用齿轮传动,则使用齿轮比来补偿驱动距离。 |
必须先创建 电机、电机组、惯性传感器 和/或 陀螺仪传感器,然后才能使用 SmartDrive 类构造函数创建对象。
# Create the Inertial Sensor.
brain_inertial = Inertial()
# Create the Motors.
left_motor = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
right_motor = Motor(Ports.PORT2, GearSetting.RATIO_18_1, True)
# Construct a 2-Motor Smart Drivetrain "drivetrain" with the
# DriveTrain class.
smartdrive = SmartDrive(left_motor, right_motor, brain_inertial, 319.19, 295, 40, MM, 1)
如果要制作带有多个电机的智能传动系统,则需要先单独创建 电机,然后再将它们分组到 电机组中。
# Create the Inertial Sensor.
brain_inertial = Inertial()
# Create the left Motors and group them under the
# MotorGroup "left_motors".
left_motor_a = Motor(Ports.PORT1, GearSetting.RATIO_18_1, False)
left_motor_b = Motor(Ports.PORT2, GearSetting.RATIO_18_1, False)
left_motors = MotorGroup(left_motor_a, left_motor_b)
# Create the right Motors and group them under the
# MotorGroup "right_motors".
right_motor_a = Motor(Ports.PORT3, GearSetting.RATIO_18_1, True)
right_motor_b = Motor(Ports.PORT4, GearSetting.RATIO_18_1, True)
right_motors = MotorGroup(right_motor_a, right_motor_b)
# Construct a 4-Motor Drivetrain "drivetrain" with the
# DriveTrain class.
smartdrive = SmartDrive(left_motors, right_motors, brain_inertial, 319.19, 295, 40, MM, 1)
当引用 SmartDrive 类方法时,此“smartdrive”对象将在整个 API 文档的所有后续示例中使用。
要创建不带 惯性传感器 或 陀螺仪传感器 的 DriveTrain 对象,请使用 DriveTrain 类构造函数。
类方法#
drive()#
drive(direction, speed, units)
方法用于永远沿指定方向驱动智能传动系统,直到使用另一种传动系统方法或项目停止。
这是一种非等待方法,允许下一个方法无延迟运行。
参数 |
描述 |
---|---|
方向 |
有效的 DirectionType。 |
速度 |
**可选。**传动系统的移动速度。将使用 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
**返回:**无。
# Drive the Smart Drivetrain forward.
smartdrive.drive(FORWARD)
# Drive the Smart Drivetrain in reverse for 100 percent velocity.
smartdrive.drive(REVERSE, 100, PERCENT)
drive_for()#
drive_for(direction, distance, units, speed, units_v, wait)
方法用于驱动智能传动系统行驶给定的距离。
根据是否使用 wait
参数,这可以是 等待 或 非等待 方法。
参数 |
描述 |
---|---|
方向 |
有效的 DirectionType。 |
距离 |
传动系统移动的距离。 |
单位 |
**可选。**有效的 DistanceUnits 类型。默认值为“INCHES”。 |
速度 |
**可选。**传动系统的移动速度。如果未提供,则使用 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
等待 |
可选。 wait 参数决定该方法是否阻止后续方法( |
**返回:**无。
# Drive forward for 10 inches.
smartdrive.drive_for(FORWARD, 10)
turn()#
turn(direction, speed, units)
方法用于将智能传动系统向左或向右转动。
这是一种非等待方法,允许下一个方法无延迟运行。
参数 |
描述 |
---|---|
方向 |
有效的 TurnType。 |
速度 |
**可选。**传动系统的转弯速度。如果未提供,则使用 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
**返回:**无。
# Turn the Smart Drivetrain to the right.
smartdrive.turn(RIGHT)
# Turn the Smart Drivetrain to the left at 25% velocity.
smartdrive.turn(LEFT, 25, PERCENT)
turn_to_heading()#
turn_to_heading(angle, units, speed, units_v, wait)
方法将智能传动系统转向特定航向。
根据是否使用 wait
参数,这可以是 等待 或 非等待 方法。
参数 |
描述 |
---|---|
角度 |
转向的航向角。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
速度 |
**可选。**使用此速度旋转智能驱动器的电机。 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
等待 |
可选。 wait 参数决定该方法是否阻止后续方法( |
**返回:**无。
# Turn to heading 180 degrees.
smartdrive.turn_to_heading(180)
turn_to_rotation()#
turn_to_rotation(angle, units, speed, units_v, wait)
方法将智能传动系统转换为特定的旋转。
根据是否使用 wait
参数,这可以是 等待 或 非等待 方法。
参数 |
描述 |
---|---|
角度 |
要旋转的旋转角度。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
速度 |
**可选。**使用此速度旋转智能传动系统的电机。 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
等待 |
可选。 wait 参数决定该方法是否阻止后续方法( |
**返回:**无。
# Turn to rotation 180 degrees.
smartdrive.turn_to_rotation(180)
turn_for()#
turn_for(angle, units, speed, units_v, wait)
方法将智能传动系统转动指定角度。
根据是否使用 wait
参数,这可以是 等待 或 非等待 方法。
参数 |
描述 |
---|---|
角度 |
转向的角度。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
速度 |
**可选。**使用此速度旋转智能传动系统的电机。 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
等待 |
可选。 wait 参数决定该方法是否阻止后续方法( |
**返回:**无。
# Turn for 180 degrees.
smartdrive.turn_for(180)
stop()#
stop()
方法用于停止智能传动系统,将其设置为 0 速度并配置当前停止模式。
**返回:**无。
# Stop the Smart Drivetrain.
smartdrive.stop()
set_drive_velocity()#
set_drive_velocity(velocity, units)
方法用于设置智能传动系统的默认速度。此速度设置会影响所有后续的传动系统方法,除非这些方法中提供了具体的速度。
参数 |
描述 |
---|---|
速度 |
为智能传动系统设置的新速度为默认速度。 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
**返回:**无。
# Set the Smart Drivetrain to drive at a velocity of
# 200 RPM.
smartdrive.set_drive_velocity(200)
# Set the Smart Drivetrain to drive at a velocity of
# 100 PERCENT.
smartdrive.set_drive_velocity(100, PERCENT)
set_turn_velocity()#
set_turn_velocity(velocity, units)
方法用于设置智能传动系统中转弯操作的默认速度。此设置指定智能传动系统执行转弯方法的速度,除非该方法中设置了特定速度。
参数 |
描述 |
---|---|
速度 |
设置为转弯操作默认的新速度。 |
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
**返回:**无。
# Set the Smart Drivetrain to turn at a velocity of
# 200 RPM.
smartdrive.set_turn_velocity(200)
# Set the Smart Drivetrain to turn at a velocity of
# 100 PERCENT.
smartdrive.set_turn_velocity(100, PERCENT)
set_stopping()#
set_stopping(mode)
方法用于设置智能传动系统上所有电机的停止模式。此设置决定了电机在收到停止方法或速度设置为零时的行为。
参数 |
描述 |
---|---|
模式 |
有效的 BrakeType。默认值为 |
**返回:**无。
# Set the stopping mode to BRAKE.
smartdrive.set_stopping(BRAKE)
set_turn_threshold()#
set_turn_threshold(value)
方法设置智能传动系统的转弯阈值。该阈值用于判断转弯是否完成。如果阈值过大,转弯将不准确。如果阈值过小,转弯可能无法完成。
参数 |
描述 |
---|---|
价值 |
新的转弯阈值(以“度”为单位)。智能传动系统的默认值为 1 度。 |
**返回:**无。
set_turn_constant()#
set_turn_constant(value)
方法设置智能传动系统的转弯常数。智能传动系统在转弯时使用一个简单的 P 控制器。这个常数,通常称为 kp,是将角度误差转换为电机速度的方程中使用的增益。
参数 |
描述 |
---|---|
价值 |
新的转弯常数,范围为 0.1 - 4.0。默认值为 1.0。 |
**返回:**无。
set_turn_direction_reverse()#
set_turn_direction_reverse(value)
方法设置正向航向变化的预期转弯方向。
参数 |
描述 |
---|---|
价值 |
用于设置方向是否反转的布尔值。 |
**返回:**无。
set_heading()#
set_heading(value, units)
方法设置智能传动系统的航向。
参数 |
描述 |
---|---|
价值 |
用于标题的新值。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**无。
# Set the value of heading to 180 degrees.
smartdrive.set_heading(180)
heading()#
heading(units)
方法返回智能传动系统的当前航向。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**以指定单位表示的智能传动系统的当前航向。
# Get the current heading for the Smart Drivetrain.
value = smartdrive.heading()
set_rotation()#
set_rotation()
方法设置智能传动系统的旋转。
参数 |
描述 |
---|---|
价值 |
用于旋转的新值。 |
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**无。
# Set the value of rotation to 180 degrees.
smartdrive.set_rotation(180)
rotation()#
rotation(units)
方法返回智能传动系统的当前旋转。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 RotationUnits 类型。默认值为 |
**返回:**智能传动系统以指定单位表示的当前旋转。
# Get the current rotation for the Smart Drivetrain.
value = smartdrive.rotation()
is_turning()#
is_turning()
方法检查智能传动系统当前是否正在转动。
返回: 如果智能传动系统当前正在转动,则返回 True
。如果当前未转动,则返回 False
。
is_moving()#
is_moving()
方法检查智能传动系统当前是否正在移动。
返回: 如果智能传动系统当前正在移动,则返回 True
。如果当前未移动,则返回 False
。
is_done()#
is_done()
方法检查智能传动系统当前是否没有移动。
返回: 如果智能传动系统当前未移动,则返回 True
。如果当前正在移动,则返回 False
。
set_timeout()#
set_timeout(timeout, units)
方法用于设置智能传动系统上所有电机的超时值。此设置决定了如果电机尚未完成运动,智能传动系统将在超时前尝试执行 drive_for
和 turn_for
方法多长时间。
参数 |
描述 |
---|---|
暂停 |
为智能传动系统操作设置的默认超时时间。 |
单位 |
**可选。**有效的 TimeUnits 类型。默认值为 |
**返回:**无。
# Set the timeout for the Smart Drivetrain to
# 1000 milliseconds.
drivetrain.set_timeout(1000)
获取超时()#
smartdrive.get_timeout()
方法获取当前超时设置。
**返回:**智能传动系统方法超时后的当前时间。
velocity()#
velocity()
方法返回左右电机的平均速度。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 VelocityUnits 类型。默认值为 |
**返回:**以提供的单位表示的智能传动系统的速度。
current()#
current()
方法返回左右电机的平均电流。
参数 |
描述 |
---|---|
单位 |
可选。 当前唯一有效的单位是“AMP”。 |
**返回:**提供的单位中的智能传动系统电流。
power()#
power()
方法返回所有智能传动系统电机使用的总功率。
参数 |
描述 |
---|---|
单位 |
可选。 功率的唯一有效单位是“瓦特”。 |
**返回:**智能传动系统的功率(以提供的单位表示)。
torque()#
torque()
方法返回所有智能传动系统电机的总扭矩。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 TorqueUnits 类型。默认值为 |
**返回:**以提供的单位表示的智能传动系统的扭矩。
efficiency()#
efficiency()
方法返回左右电机的平均效率。
参数 |
描述 |
---|---|
单位 |
**可选。**效率的唯一有效单位是“百分比”。 |
**返回:**智能传动系统在所提供单位中的效率。
temperature()#
temp()
方法返回左右电机的平均温度。
参数 |
描述 |
---|---|
单位 |
**可选。**有效的 TemperatureUnits 类型。默认值为 |
**返回:**以提供的单位表示的智能传动系统的温度。