add mathlib and smooth accelerate distance command
[3501/2015-FRC-Spark] / src / org / usfirst / frc3501 / RiceCatRobot / MathLib.java
1 package org.usfirst.frc3501.RiceCatRobot;
2
3 public class MathLib {
4
5 /***
6 * This method gives speed as a function of % distance covered so the speed
7 * forms a parabola starting and ending at minSpeed when you start and end and
8 * achieving maxSpeed exactly halfway.
9 *
10 * @param minSpeed
11 * the starting and ending speed, in range [0, 1]
12 * @param maxSpeed
13 * the max speed, achieved at percentComplete = 1/2.
14 * @param percentComplete
15 * should be currentDistance / targetDistance
16 * @return the speed (motor value) to set motors to for smooth acceleration.
17 * Note that since velocity is a parabola, acceleration is linear. It
18 * may exceed the maximum value robot can accelerate without wheel
19 * slipping.
20 */
21 public static double getSpeedForLinearAccel(double minSpeed, double maxSpeed,
22 double percentComplete) {
23 return 4 * (minSpeed - maxSpeed) * (percentComplete - 0.5)
24 * (percentComplete - 0.5) + maxSpeed;
25 }
26
27 /***
28 * This method gives speed as a function of % distance covered so the speed
29 * increases linearly from minSpeed to maxSpeed and then back down again.
30 *
31 * @param minSpeed
32 * the starting and ending speed, in range [0, 1]
33 * @param maxSpeed
34 * the max speed, achieved at percentComplete = 1/2.
35 * @param percentComplete
36 * should be currentDistance / targetDistance
37 * @return the speed (motor value) to set motors to.
38 */
39 public static double getSpeedForConstantAccel(double minSpeed,
40 double maxSpeed, double percentComplete) {
41 return maxSpeed + 2 * (minSpeed - maxSpeed)
42 * Math.abs(percentComplete - 0.5);
43 }
44
45 }