add mathlib and smooth accelerate distance command dobervich/clean
authorDavid <david.dobervich@gmail.com>
Fri, 20 Nov 2015 19:19:18 +0000 (11:19 -0800)
committerDavid <david.dobervich@gmail.com>
Fri, 20 Nov 2015 19:19:18 +0000 (11:19 -0800)
src/org/usfirst/frc3501/RiceCatRobot/MathLib.java [new file with mode: 0644]
src/org/usfirst/frc3501/RiceCatRobot/commands/SmoothAccelerateForDistance.java [new file with mode: 0644]

diff --git a/src/org/usfirst/frc3501/RiceCatRobot/MathLib.java b/src/org/usfirst/frc3501/RiceCatRobot/MathLib.java
new file mode 100644 (file)
index 0000000..e5c1f93
--- /dev/null
@@ -0,0 +1,45 @@
+package org.usfirst.frc3501.RiceCatRobot;\r
+\r
+public class MathLib {\r
+\r
+  /***\r
+   * This method gives speed as a function of % distance covered so the speed\r
+   * forms a parabola starting and ending at minSpeed when you start and end and\r
+   * achieving maxSpeed exactly halfway.\r
+   *\r
+   * @param minSpeed\r
+   *          the starting and ending speed, in range [0, 1]\r
+   * @param maxSpeed\r
+   *          the max speed, achieved at percentComplete = 1/2.\r
+   * @param percentComplete\r
+   *          should be currentDistance / targetDistance\r
+   * @return the speed (motor value) to set motors to for smooth acceleration.\r
+   *         Note that since velocity is a parabola, acceleration is linear. It\r
+   *         may exceed the maximum value robot can accelerate without wheel\r
+   *         slipping.\r
+   */\r
+  public static double getSpeedForLinearAccel(double minSpeed, double maxSpeed,\r
+      double percentComplete) {\r
+    return 4 * (minSpeed - maxSpeed) * (percentComplete - 0.5)\r
+        * (percentComplete - 0.5) + maxSpeed;\r
+  }\r
+\r
+  /***\r
+   * This method gives speed as a function of % distance covered so the speed\r
+   * increases linearly from minSpeed to maxSpeed and then back down again.\r
+   *\r
+   * @param minSpeed\r
+   *          the starting and ending speed, in range [0, 1]\r
+   * @param maxSpeed\r
+   *          the max speed, achieved at percentComplete = 1/2.\r
+   * @param percentComplete\r
+   *          should be currentDistance / targetDistance\r
+   * @return the speed (motor value) to set motors to.\r
+   */\r
+  public static double getSpeedForConstantAccel(double minSpeed,\r
+      double maxSpeed, double percentComplete) {\r
+    return maxSpeed + 2 * (minSpeed - maxSpeed)\r
+        * Math.abs(percentComplete - 0.5);\r
+  }\r
+\r
+}\r
diff --git a/src/org/usfirst/frc3501/RiceCatRobot/commands/SmoothAccelerateForDistance.java b/src/org/usfirst/frc3501/RiceCatRobot/commands/SmoothAccelerateForDistance.java
new file mode 100644 (file)
index 0000000..b774346
--- /dev/null
@@ -0,0 +1,53 @@
+package org.usfirst.frc3501.RiceCatRobot.commands;
+
+import org.usfirst.frc3501.RiceCatRobot.Robot;
+import org.usfirst.frc3501.RiceCatRobot.RobotMap.Direction;
+import org.usfirst.frc3501.RiceCatRobot.subsystems.DriveTrain;
+
+import edu.wpi.first.wpilibj.Timer;
+import edu.wpi.first.wpilibj.command.Command;
+
+/**
+ * This command will drive the specified distance at specified speed.
+ *
+ */
+public class SmoothAccelerateForDistance extends Command {
+  private double distance; // in units of cm
+  private double speed;
+
+  public SmoothAccelerateForDistance(double distance, double speed) {
+    // Since negative values in setMotorSpeeds(...) are forwards, we reverse
+    // speed here so positive input values will move the robot forwards.
+    this.speed = -speed;
+
+    // ensure distance is positive
+    this.distance = Math.max(distance, -distance);
+  }
+
+  @Override
+  protected void initialize() {
+    Robot.driveTrain.resetEncoders();
+    Robot.driveTrain.setMotorSpeeds(speed, speed);
+  }
+
+  @Override
+  protected void execute() {
+    // nothing to do since motor speeds already set
+  }
+
+  @Override
+  protected boolean isFinished() {
+    return Math.abs(Robot.driveTrain.getLeftDistance()) >= distance
+        && Math.abs(Robot.driveTrain.getRightDistance()) >= distance;
+  }
+
+  @Override
+  protected void end() {
+    Robot.driveTrain.stop();
+  }
+
+  @Override
+  protected void interrupted() {
+    end();
+  }
+}
\ No newline at end of file