|
| 1 | +package com.pedropathing; |
| 2 | + |
| 3 | +import com.pedropathing.math.Vector; |
| 4 | +import com.pedropathing.math.MathFunctions; |
| 5 | + |
| 6 | +public abstract class Drivetrain { |
| 7 | + /** |
| 8 | + * These are the movement vectors for the drivetrain, essentially the directions of the forces created by each wheel, such that x-components are scaled by the x velocity and y-components are scaled by the y velocity. |
| 9 | + */ |
| 10 | + protected Vector[] vectors; |
| 11 | + |
| 12 | + /** |
| 13 | + * This is the maximum power scaling for the drivetrain. This is used to limit the maximum |
| 14 | + * power that can be applied to the motors, which is useful for preventing damage to the |
| 15 | + * drivetrain or for controlling the speed of the robot. |
| 16 | + */ |
| 17 | + protected double maxPowerScaling; |
| 18 | + |
| 19 | + /** |
| 20 | + * This is used to determine whether the drivetrain should use voltage compensation or not. |
| 21 | + */ |
| 22 | + protected boolean voltageCompensation; |
| 23 | + |
| 24 | + /** |
| 25 | + * This is the nominal voltage for the drivetrain. This is used for voltage compensation. |
| 26 | + * It is set to 12.0V by default, which is the nominal voltage for most FTC robots. |
| 27 | + */ |
| 28 | + protected double nominalVoltage; |
| 29 | + |
| 30 | + /** |
| 31 | + * This takes in vectors for corrective power, heading power, and pathing power and outputs |
| 32 | + * an Array. |
| 33 | + * IMPORTANT NOTE: all vector inputs are clamped between 0 and 1 inclusive in magnitude. |
| 34 | + * |
| 35 | + * @param correctivePower this Vector includes the centrifugal force scaling Vector as well as a |
| 36 | + * translational power Vector to correct onto the Bezier curve the Follower |
| 37 | + * is following. |
| 38 | + * @param headingPower this Vector points in the direction of the robot's current heading, and |
| 39 | + * the magnitude tells the robot how much it should turn and in which |
| 40 | + * direction. |
| 41 | + * @param pathingPower this Vector points in the direction the robot needs to go to continue along |
| 42 | + * the Path. |
| 43 | + * @param robotHeading this is the current heading of the robot, which is used to calculate how |
| 44 | + * much power to allocate to each wheel. |
| 45 | + * @return this returns an Array of doubles with a length of 4, which contains the wheel powers. |
| 46 | + */ |
| 47 | + public abstract double[] calculateDrive(Vector correctivePower, Vector headingPower, Vector pathingPower, double robotHeading); |
| 48 | + |
| 49 | + /** |
| 50 | + * This sets the maximum power scaling for the drivetrain. This is used to limit the maximum |
| 51 | + * power that can be applied to the motors, which is useful for preventing damage to the |
| 52 | + * drivetrain or for controlling the speed of the robot. |
| 53 | + * |
| 54 | + * @param maxPowerScaling this is a double between 0 and 1 inclusive that represents the maximum |
| 55 | + * power scaling factor. |
| 56 | + */ |
| 57 | + public void setMaxPowerScaling(double maxPowerScaling) { |
| 58 | + this.maxPowerScaling = MathFunctions.clamp(maxPowerScaling, 0, 1); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * This gets the maximum power scaling for the drivetrain. This is used to limit the maximum |
| 63 | + * power that can be applied to the motors, which is useful for preventing damage to the |
| 64 | + * drivetrain or for controlling the speed of the robot. |
| 65 | + * |
| 66 | + * @return this returns a double between 0 and 1 inclusive that represents the maximum power |
| 67 | + * scaling factor. |
| 68 | + */ |
| 69 | + public double getMaxPowerScaling() { |
| 70 | + return maxPowerScaling; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * This updates the constants used by the drivetrain. |
| 75 | + */ |
| 76 | + public abstract void updateConstants(); |
| 77 | + |
| 78 | + /** |
| 79 | + * This is used to break the drivetrain's following. This is useful for stopping the robot from following a Path or PathChain. |
| 80 | + */ |
| 81 | + public abstract void breakFollowing(); |
| 82 | + |
| 83 | + /** |
| 84 | + * This runs the drivetrain with the specified drive powers. This is used to set the power of the motors directly. |
| 85 | + * |
| 86 | + * @param drivePowers this is an Array of doubles with a length of 4, which contains the wheel powers. |
| 87 | + */ |
| 88 | + public abstract void runDrive(double[] drivePowers); |
| 89 | + |
| 90 | + /** |
| 91 | + * This gets the drive powers and runs them immediately. |
| 92 | + * |
| 93 | + * @param correctivePower this Vector includes the centrifugal force scaling Vector as well as a |
| 94 | + * translational power Vector to correct onto the Bezier curve the Follower |
| 95 | + * is following. |
| 96 | + * @param headingPower this Vector points in the direction of the robot's current heading, and |
| 97 | + * the magnitude tells the robot how much it should turn and in which |
| 98 | + * direction. |
| 99 | + * @param pathingPower this Vector points in the direction the robot needs to go to continue along |
| 100 | + * the Path. |
| 101 | + * @param robotHeading this is the current heading of the robot, which is used to calculate how |
| 102 | + * much power to allocate to each wheel. |
| 103 | + */ |
| 104 | + public void runDrive(Vector correctivePower, Vector headingPower, Vector pathingPower, double robotHeading) { |
| 105 | + runDrive(calculateDrive(correctivePower, headingPower, pathingPower, robotHeading)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * This starts the TeleOp drive mode. This is used to set the drivetrain into TeleOp mode, where |
| 110 | + * it can be controlled by the driver. |
| 111 | + */ |
| 112 | + public abstract void startTeleopDrive(); |
| 113 | + |
| 114 | + /** |
| 115 | + * This starts the TeleOp drive mode with a specified brake mode. This is used to set the drivetrain |
| 116 | + * into TeleOp mode, where it can be controlled by the driver, and allows for setting the brake mode. |
| 117 | + * |
| 118 | + * @param brakeMode this is a boolean that specifies whether the drivetrain should use brake mode or not. |
| 119 | + */ |
| 120 | + public abstract void startTeleopDrive(boolean brakeMode); |
| 121 | + |
| 122 | + /** |
| 123 | + * This gets the current x velocity of the drivetrain. |
| 124 | + * @return this returns the x velocity of the drivetrain. |
| 125 | + */ |
| 126 | + public abstract double xVelocity(); |
| 127 | + |
| 128 | + /** |
| 129 | + * This gets the current y velocity of the drivetrain. |
| 130 | + * @return this returns the y velocity of the drivetrain. |
| 131 | + */ |
| 132 | + public abstract double yVelocity(); |
| 133 | + |
| 134 | + /** |
| 135 | + * This sets the x velocity of the drivetrain. |
| 136 | + * @param xMovement this is the x velocity to set. |
| 137 | + */ |
| 138 | + public abstract void setXVelocity(double xMovement); |
| 139 | + |
| 140 | + /** |
| 141 | + * This sets the y velocity of the drivetrain. |
| 142 | + * @param yMovement this is the y velocity to set. |
| 143 | + */ |
| 144 | + public abstract void setYVelocity(double yMovement); |
| 145 | + |
| 146 | + /** |
| 147 | + * This sets whether the drivetrain should use voltage compensation or not. |
| 148 | + * @param use this is a boolean that specifies whether the drivetrain should use voltage compensation or not. |
| 149 | + */ |
| 150 | + public void useVoltageCompensation(boolean use) { |
| 151 | + this.voltageCompensation = use; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * This gets whether the drivetrain is using voltage compensation or not. |
| 156 | + * @return this returns a boolean that specifies whether the drivetrain is using voltage compensation or not. |
| 157 | + */ |
| 158 | + public boolean isVoltageCompensation() { |
| 159 | + return voltageCompensation; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * This gets the nominal voltage for the drivetrain. |
| 164 | + * @return this returns the nominal voltage for the drivetrain. |
| 165 | + */ |
| 166 | + public double getNominalVoltage() { |
| 167 | + return nominalVoltage; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * This sets the nominal voltage for the drivetrain. This is used for voltage compensation. |
| 172 | + * @param set this is the nominal voltage to set. |
| 173 | + */ |
| 174 | + public void setNominalVoltage(double set) { |
| 175 | + this.nominalVoltage = set; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * This is used to get the voltage of the drivetrain. This is useful for debugging purposes. |
| 180 | + * It should be called periodically to get the current voltage of the drivetrain. |
| 181 | + */ |
| 182 | + public abstract double getVoltage(); |
| 183 | + |
| 184 | + /** |
| 185 | + * This is used to get a debug string for the drivetrain. This is useful for debugging purposes. |
| 186 | + * |
| 187 | + * @return this returns a String that contains the debug information for the drivetrain. |
| 188 | + */ |
| 189 | + public abstract String debugString(); |
| 190 | + |
| 191 | +} |
0 commit comments