Package frc.robot.util.hyperdrive
Class Hyperdrive
- java.lang.Object
-
- frc.robot.util.hyperdrive.Hyperdrive
-
public class Hyperdrive extends java.lang.Object
A tool for autonomously driving the robot around. This tool will handle communication with the PathVisualizer app and be able to record paths and emulate them with a few simple lines of code. To start, instantiate an instance of this class into your drivetrain subsystem, and then call theupdate(double distanceTravelled, double direction)
method or theupdate(double leftDistance, double rightDistance, double heading)
in the drivetrain'speriodic()
method.
-
-
Constructor Summary
Constructors Constructor Description Hyperdrive(DriveStyle driveStyle, Units.LENGTH lengthUnit, double motorUnitsPerUnit)
Creates a new Hyperdrive, using the default robot weight, 125 pounds, and PathVisualizer port, 3695.Hyperdrive(DriveStyle driveStyle, Units.LENGTH distanceUnits, double motorUnitsPerUnit, Units.FORCE weightUnit, double robotWeight)
Creates a new Hyperdrive, using the default PathVisualizer port, 3695.Hyperdrive(DriveStyle driveStyle, Units.LENGTH lengthUnit, double motorUnitsPerUnit, Units.FORCE weightUnit, double robotWeight, int pvPort)
Creates a new Hyperdrive.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Trajectory
calculateNextMovements()
Calculates the target velocity of the robot, and the magnitude of any turn that it needs to take, all based on the current position of the robot.void
finishPath()
Frees up resources used while emulating the Path.int
getCurrentPoint()
Returns the index of the point in thePath
that the robot is trying to emulate, that the robot is currently working to achieve.Units.LENGTH
getLengthUnits()
Returns the units of length that the Hyperdrive has been configured to use.Path
getRecordedPath()
Returns thePath
that the Path Recorder has just recorded.Point2D
getRobotPositionAndHeading()
Returns the current position and heading of the robot in field space.int
getTotalPoints()
Returns the number of points that are present in thePath
that the robot is current emulating.void
initializeRecorder()
Declares, initializes, and starts the PathRecorder to record a driven Path.void
initializeRecorder(java.lang.String file)
Decalares, initalizes, and starts the PathRecorder to record a driven Path.void
loadPath(Path path, IEmulateParams parameters)
Loads aPath
for Hyperdrive to emulate.void
loadPath(java.lang.String filePath, IEmulateParams parameters)
Loads aPath
for Hyperdrive to emulate from the given file path.boolean
pathFinished()
Returns true if Hyperdrive is done driving the robot through a Path, and false otherwise.void
performInitialCalculations()
Makes the emulator perform some calculations that must take place in order for the path to be driven to the best of Hyperdrive's ability.void
sendPath(Path path, java.lang.String name)
Sends aPath
to the PathVisualizer client, if there is one.void
setPositionAndHeading(Point2D newPositionAndHeading)
Sets the position and heading of the robot.void
specifyResultsFile(java.lang.String filePath)
Forces Hyperdrive to record the results of the driven path to the file location provided.void
stopRecorder()
Stops the Path Recorder if it is currently active.double
toCommonUnits(double motorUnitMeasurement)
Converts a measurement made in motor units (ticks, revolutions, etc) to common units (inches, meters, etc) using the motorUnitsPerUnit measurement provided by the constructor.double
toMotorUnits(double commonUnitMeasurement)
Converts a measurement made in common units (inches, meters, etc) to motor units (ticks, revolutions, etc), using the motorUnitsPerUnit measurement provided by the Hyperdrive constructor.void
update(double distanceTravelled, double direction)
Updates the Hyperdrive.void
update(double leftDistance, double rightDistance, double heading)
Updates the Hyperdrive.void
update(Point2D positionAndHeading)
Updates the Hyperdrive using a manual position.void
zeroPositionAndHeading()
Sets the position and heading of the robot to 0.void
zeroPositionAndHeading(boolean waitForEncoders)
Sets the position and heading of the robot to 0.
-
-
-
Constructor Detail
-
Hyperdrive
public Hyperdrive(DriveStyle driveStyle, Units.LENGTH lengthUnit, double motorUnitsPerUnit, Units.FORCE weightUnit, double robotWeight, int pvPort)
Creates a new Hyperdrive.- Parameters:
driveStyle
- The style of drivetrain of the robot.lengthUnit
- The units of distance that the robot will use.motorUnitsPerUnit
- The number of motor position units (ticks, rotations, etc) that are in one length measurement unit (inches, feet, meters, etc). To get this value, drive the robot a set distance (more than 10 feet for most accuracy) and then record the raw displacement of the motor encoders. This value should then be equivilent to that displacement divided by the set distance. This value does not absolutely need to be accurate (the robot will emulate any path as long as it was recorded with the same value), but is required if the position tracker is to report in the correct units. This value defaults to 0.472, which is the approximate rotations per inch value of NEO motors driving wheels with pneumatic tires through a toughbox mini gearbox.weightUnit
- The units of weight that the robot is measured in.robotWeight
- The weight of the robot.pvPort
- The port to use for communicating with PathVisualizer. Defaults to 3695.
-
Hyperdrive
public Hyperdrive(DriveStyle driveStyle, Units.LENGTH distanceUnits, double motorUnitsPerUnit, Units.FORCE weightUnit, double robotWeight)
Creates a new Hyperdrive, using the default PathVisualizer port, 3695.- Parameters:
driveStyle
- The style of drivetrain of the robot.distanceUnits
- The units to measure length in.motorUnitsPerUnit
- The number of motor position units (ticks, rotations, etc) that are in one length measurement unit (inches, feet, meters, etc). See javadoc forHyperdrive(DriveStyle, Units.LENGTH, double, Units.FORCE, double, int)
for full explanation.weightUnit
- The unit of weight that the robot is measured in.robotWeight
- The weight of the robot.
-
Hyperdrive
public Hyperdrive(DriveStyle driveStyle, Units.LENGTH lengthUnit, double motorUnitsPerUnit)
Creates a new Hyperdrive, using the default robot weight, 125 pounds, and PathVisualizer port, 3695.- Parameters:
driveStyle
- The style of drivetrain of the robot.lengthUnit
- The units to measure length in.motorUnitsPerUnit
- The number of motor position units (ticks, rotations, etc) that are in one length measurement unit (inches, feet, meters, etc). See javadoc forHyperdrive(DriveStyle, Units.LENGTH, double, Units.FORCE, double, int)
for full explanation.
-
-
Method Detail
-
update
public void update(double distanceTravelled, double direction)
Updates the Hyperdrive. The Hyperdrive will use the distance that the robot has travelled and the direction that it has travelled in and calculate the robot's position on the field. That information can be viewed on SmartDashboard or Shuffleboard as "Current Robot Position", and can be accessed in code withgetRobotPositionAndHeading()
.
It is recommended that this method be called in one of the robot'speriodic()
methods. It is possible to call this method in awhile
loop in a separate Thread, but the programmer(s) should be careful of how often they reference encoder values from their motor controllers, as this can overload the CAN network and cause undesired motor behavior.- Parameters:
distanceTravelled
- The total displacment of the robot since the last zero, in encoder counts.direction
- The direction that the robot is currently travelling in, in degrees.
-
update
public void update(double leftDistance, double rightDistance, double heading)
Updates the Hyperdrive. The Hyperdrive will use the distance that the robot has travelled and the direction that it has travelled in and calculate the robot's position on the field. That information can be viewed on SmartDashboard or Shuffleboard as "Current Robot Position", or can be accessed in code withgetRobotPositionAndHeading()
Tank teams will likely find that this overload of theupdate()
method is easier to use than the other one, as they can simply pass through the positions of their drive motors.
It is recommended that this method be called in one of the robot'speriodic()
methods. It is possible to call this method in awhile
loop in a separate Thread, but the programmer(s) should be careful of how often they reference encoder values from their motor controllers, as this can overload the CAN network and cause undesired motor behavior.- Parameters:
leftDistance
- The current position of the left motor encoder.rightDistance
- The current position of the right motor encoder.heading
- The direction the robot is facing, in degrees.
-
update
public void update(Point2D positionAndHeading)
Updates the Hyperdrive using a manual position. This position could be obtained by another system that your team prefers to use, or it could be a simulated position generated by aSimulatedRobot
. Simulated positions are acceptable if Hyperdrive is being simulated, but are discouraged on physical robots. For physical robots, use a position obtained by sensors, or use theupdate(double, double)
orupdate(double, double, double)
methods. Although the simulator is highly accurate, values obtained from actual sensors will generally be better.
It is recommended taht this method be called in one of the robot'speriodic()
methods. It is possible to call this method in awhile
loop in a separate Thread, but the programmer(s) should be careful of how often they reference encoder values from their motor controllers, as this can overload the CAN network and cause undesired motor behavior.- Parameters:
positionAndHeading
- The manual position and heading of the robot.
-
getRobotPositionAndHeading
public Point2D getRobotPositionAndHeading()
Returns the current position and heading of the robot in field space.- Returns:
- Current robot position and heading.
-
zeroPositionAndHeading
public void zeroPositionAndHeading(boolean waitForEncoders)
Sets the position and heading of the robot to 0. This method should be called when the robot is standing still to ensure accurate and reliable results. This overload also provides an option to wait for the drive encoders to be 0 before actually resetting the position, which helps to prevent large position jumps that may ruin any position-related zero that was recently set. See the javadoc for the#zeroPositionAndHeading(boolean)
for full explanation.- Parameters:
waitForEncoders
- True if the position tracker should wait for the motor encoders to zero before resetting the position. This means that the position tracker will not zero until either theupdate(double, double)
method is called with a distanceTravelled value of 0, or theupdate(double, double, double)
method is called with leftDistance and rightDistance values that average out to a value that is less than 1. NOTE that the robot MUST not be moving if this option is set totrue
. If the robot is moving and this option is set totrue
, the position tracker could potentially not zero now, and instead zero at an unwanted time.
-
zeroPositionAndHeading
public void zeroPositionAndHeading()
Sets the position and heading of the robot to 0. This is equivilent to callingzeroPositionAndHeading(false)
.
-
setPositionAndHeading
public void setPositionAndHeading(Point2D newPositionAndHeading)
Sets the position and heading of the robot. This method is useful as it can be called at any time during the robot's operation, even while it is moving. You can use this method to set the robot's position relative to an object, or use it to set and maintain the robot's true position on the Field.- Parameters:
newPositionAndHeading
- The new Position and Heading of the robot.
-
getLengthUnits
public Units.LENGTH getLengthUnits()
Returns the units of length that the Hyperdrive has been configured to use.- Returns:
- The Hyperdrive's units of length.
-
sendPath
public void sendPath(Path path, java.lang.String name)
Sends aPath
to the PathVisualizer client, if there is one. If a PathVisualizer client is connected to the robot, and the "Live" option is enabled, the path being sent with this method will appear on the screen, under the name that was also given to this method.- Parameters:
path
- The path to send.name
- The name of the path. Will appear on the manifest as that name.
-
initializeRecorder
public void initializeRecorder(java.lang.String file)
Decalares, initalizes, and starts the PathRecorder to record a driven Path. After this method is called, the Recorder will automatically record the robot's movement until thestopRecorder()
method is called.- Parameters:
file
- The file to record to.
-
initializeRecorder
public void initializeRecorder()
Declares, initializes, and starts the PathRecorder to record a driven Path. The Path will be stored in the default record location:#PATH_RECORDER_DEFAULT_RECORD_PATH
. After this method is called, the Recorder will automatically record the robot's movement until thestopRecorder()
method is called.
-
stopRecorder
public void stopRecorder()
Stops the Path Recorder if it is currently active. The path being recorded will be saved to file and the Recorder will no longer record a Path automatically. The recorded Path can be accessed usinggetRecordedPath()
until the recorder is started again. This method will also send the Path that was just recorded to PathVisualizer for viewing. If PathVisualizer is connected, and the "Live" option is enabled, then the Path will appear on the screen.
-
getRecordedPath
public Path getRecordedPath()
Returns thePath
that the Path Recorder has just recorded. If no such path exists, the Path'sisValid()
method will return false.- Returns:
- The previously recorded Path.
-
loadPath
public void loadPath(Path path, IEmulateParams parameters)
Loads aPath
for Hyperdrive to emulate.- Parameters:
path
- The path that the robot should drive.parameters
- The parameters to use while driving this path.
-
loadPath
public void loadPath(java.lang.String filePath, IEmulateParams parameters)
Loads aPath
for Hyperdrive to emulate from the given file path.- Parameters:
filePath
- The location of the path file which the robot will drive.parameters
- The parameters to use when driving this path.
-
specifyResultsFile
public void specifyResultsFile(java.lang.String filePath)
Forces Hyperdrive to record the results of the driven path to the file location provided.- Parameters:
filePath
- The location of the new results file.
-
performInitialCalculations
public void performInitialCalculations()
Makes the emulator perform some calculations that must take place in order for the path to be driven to the best of Hyperdrive's ability. This method should be called in theinitalize()
method of your autonomous drive command.
-
calculateNextMovements
public Trajectory calculateNextMovements()
Calculates the target velocity of the robot, and the magnitude of any turn that it needs to take, all based on the current position of the robot. From here, the teams with tank-style robots can callgetTankTrajectory()
to calculate the target velocities for the left and right wheels of their robot. Then, those velocities must be set as setpoints for their motor velocity PIDs.
NOTE, however, that depending on the motor controller that is being used, the velocities may need to be converted to the proper time units accepted by the motor controller being used. If left as-is, the velocities will be returned as motor units per second. For teams running TalonFX or TalonSRX drivetrains, those values will need to be converted to motor units per 100 ms. This can be achieved by callingconvertTime(Units.TIME.DECASECONDS)
on the new TankTrajectory. Teams running SparkMAX drivetrains will need to convert their velocitiesto motor units per minute, which can be acheived withconvertTime(Units.TIME.MINUTES)
.- Returns:
- A Trajectory describing the robot's future course, and how to achieve it.
-
getTotalPoints
public int getTotalPoints()
Returns the number of points that are present in thePath
that the robot is current emulating.- Returns:
- The number of points in the currently loaded Path.
-
getCurrentPoint
public int getCurrentPoint()
Returns the index of the point in thePath
that the robot is trying to emulate, that the robot is currently working to achieve.- Returns:
- Current point index.
-
pathFinished
public boolean pathFinished()
Returns true if Hyperdrive is done driving the robot through a Path, and false otherwise.- Returns:
- Whether or not the loaded path has been driven.
-
finishPath
public void finishPath()
Frees up resources used while emulating the Path. Also sends the robot's actual Path to PathVisualier for viewing. If PathVisualizer is connected, and the "Live" option is enabled, then the Path will appear on the screen.
-
toMotorUnits
public double toMotorUnits(double commonUnitMeasurement)
Converts a measurement made in common units (inches, meters, etc) to motor units (ticks, revolutions, etc), using the motorUnitsPerUnit measurement provided by the Hyperdrive constructor.- Parameters:
commonUnitMeasurement
- Measurement in common units.- Returns:
- Equivilent length in motor units.
-
toCommonUnits
public double toCommonUnits(double motorUnitMeasurement)
Converts a measurement made in motor units (ticks, revolutions, etc) to common units (inches, meters, etc) using the motorUnitsPerUnit measurement provided by the constructor.- Parameters:
motorUnitMeasurement
- Measurement in motor units- Returns:
- Equivilent length in common units.
-
-