2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2017-09-28 23:30:00 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.drive;
|
2017-09-28 23:30:00 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.hardware.motor.MotorSafety;
|
2017-09-28 23:30:00 -07:00
|
|
|
|
2022-03-20 21:54:43 -07:00
|
|
|
/**
|
|
|
|
|
* Common base class for drive platforms.
|
|
|
|
|
*
|
2025-11-07 19:55:43 -05:00
|
|
|
* <p>{@link org.wpilib.hardware.motor.MotorSafety} is enabled by default.
|
2022-03-20 21:54:43 -07:00
|
|
|
*/
|
2019-09-14 15:22:54 -05:00
|
|
|
public abstract class RobotDriveBase extends MotorSafety {
|
2024-01-05 07:35:59 -08:00
|
|
|
/** Default input deadband. */
|
2017-11-29 21:41:00 -08:00
|
|
|
public static final double kDefaultDeadband = 0.02;
|
2024-01-05 07:35:59 -08:00
|
|
|
|
|
|
|
|
/** Default maximum output. */
|
2017-11-29 21:41:00 -08:00
|
|
|
public static final double kDefaultMaxOutput = 1.0;
|
|
|
|
|
|
2024-01-05 07:35:59 -08:00
|
|
|
/** Input deadband. */
|
2017-11-29 21:41:00 -08:00
|
|
|
protected double m_deadband = kDefaultDeadband;
|
2024-01-05 07:35:59 -08:00
|
|
|
|
|
|
|
|
/** Maximum output. */
|
2017-11-29 21:41:00 -08:00
|
|
|
protected double m_maxOutput = kDefaultMaxOutput;
|
2018-11-22 21:15:26 -08:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** The location of a motor on the robot for the purpose of driving. */
|
2017-09-28 23:30:00 -07:00
|
|
|
public enum MotorType {
|
2024-01-05 00:36:26 -05:00
|
|
|
/** Front left motor. */
|
2026-03-17 16:40:35 -07:00
|
|
|
FRONT_LEFT(0),
|
2024-01-05 00:36:26 -05:00
|
|
|
/** Front right motor. */
|
2026-03-17 16:40:35 -07:00
|
|
|
FRONT_RIGHT(1),
|
2024-01-05 00:36:26 -05:00
|
|
|
/** Rear left motor. */
|
2026-03-17 16:40:35 -07:00
|
|
|
REAR_LEFT(2),
|
2024-08-17 10:44:34 -04:00
|
|
|
/** Rear right motor. */
|
2026-03-17 16:40:35 -07:00
|
|
|
REAR_RIGHT(3),
|
2024-01-05 00:36:26 -05:00
|
|
|
/** Left motor. */
|
2026-03-17 16:40:35 -07:00
|
|
|
LEFT(0),
|
2024-01-05 00:36:26 -05:00
|
|
|
/** Right motor. */
|
2026-03-17 16:40:35 -07:00
|
|
|
RIGHT(1),
|
2024-01-05 00:36:26 -05:00
|
|
|
/** Back motor. */
|
2026-03-17 16:40:35 -07:00
|
|
|
BACK(2);
|
2017-09-28 23:30:00 -07:00
|
|
|
|
2024-01-05 07:35:59 -08:00
|
|
|
/** MotorType value. */
|
2017-09-28 23:30:00 -07:00
|
|
|
public final int value;
|
|
|
|
|
|
|
|
|
|
MotorType(int value) {
|
|
|
|
|
this.value = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** RobotDriveBase constructor. */
|
2023-12-09 21:45:02 -08:00
|
|
|
@SuppressWarnings("this-escape")
|
2017-09-28 23:30:00 -07:00
|
|
|
public RobotDriveBase() {
|
2018-11-22 21:15:26 -08:00
|
|
|
setSafetyEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 21:41:00 -08:00
|
|
|
/**
|
2018-03-03 01:57:59 -08:00
|
|
|
* Sets the deadband applied to the drive inputs (e.g., joystick values).
|
|
|
|
|
*
|
|
|
|
|
* <p>The default value is {@value #kDefaultDeadband}. Inputs smaller than the deadband are set to
|
2020-12-29 22:45:16 -08:00
|
|
|
* 0.0 while inputs larger than the deadband are scaled from 0.0 to 1.0. See {@link
|
2025-11-07 19:55:43 -05:00
|
|
|
* org.wpilib.math.util.MathUtil#applyDeadband}.
|
2017-11-29 21:41:00 -08:00
|
|
|
*
|
|
|
|
|
* @param deadband The deadband to set.
|
|
|
|
|
*/
|
2017-09-28 23:30:00 -07:00
|
|
|
public void setDeadband(double deadband) {
|
|
|
|
|
m_deadband = deadband;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-11-29 21:41:00 -08:00
|
|
|
* Configure the scaling factor for using drive methods with motor controllers in a mode other
|
|
|
|
|
* than PercentVbus or to limit the maximum output.
|
|
|
|
|
*
|
|
|
|
|
* <p>The default value is {@value #kDefaultMaxOutput}.
|
2017-09-28 23:30:00 -07:00
|
|
|
*
|
|
|
|
|
* @param maxOutput Multiplied with the output percentage computed by the drive functions.
|
|
|
|
|
*/
|
|
|
|
|
public void setMaxOutput(double maxOutput) {
|
|
|
|
|
m_maxOutput = maxOutput;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 23:18:55 -04:00
|
|
|
/**
|
|
|
|
|
* Feed the motor safety object. Resets the timer that will stop the motors if it completes.
|
|
|
|
|
*
|
2018-11-22 21:15:26 -08:00
|
|
|
* @see MotorSafety#feed()
|
2018-05-09 23:18:55 -04:00
|
|
|
*/
|
|
|
|
|
public void feedWatchdog() {
|
2018-11-22 21:15:26 -08:00
|
|
|
feed();
|
2017-09-28 23:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public abstract void stopMotor();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public abstract String getDescription();
|
|
|
|
|
|
2021-06-10 20:46:47 -07:00
|
|
|
/**
|
2026-03-06 14:19:15 -08:00
|
|
|
* Normalize all wheel velocities if the magnitude of any wheel is greater than 1.0.
|
2021-06-10 20:46:47 -07:00
|
|
|
*
|
2026-03-06 14:19:15 -08:00
|
|
|
* @param wheelVelocities List of wheel velocities to normalize.
|
2021-06-10 20:46:47 -07:00
|
|
|
*/
|
2026-03-06 14:19:15 -08:00
|
|
|
protected static void normalize(double[] wheelVelocities) {
|
|
|
|
|
double maxMagnitude = Math.abs(wheelVelocities[0]);
|
|
|
|
|
for (int i = 1; i < wheelVelocities.length; i++) {
|
|
|
|
|
double temp = Math.abs(wheelVelocities[i]);
|
2017-09-28 23:30:00 -07:00
|
|
|
if (maxMagnitude < temp) {
|
|
|
|
|
maxMagnitude = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (maxMagnitude > 1.0) {
|
2026-03-06 14:19:15 -08:00
|
|
|
for (int i = 0; i < wheelVelocities.length; i++) {
|
|
|
|
|
wheelVelocities[i] = wheelVelocities[i] / maxMagnitude;
|
2017-09-28 23:30:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|