Implement team 254's 2014 driving code
[3501/stronghold-2016] / src / org / usfirst / frc / team3501 / robot / CheesyDriveHelper.java
CommitLineData
d9475515
HD
1package org.usfirst.frc.team3501.robot;
2
3import org.usfirst.frc.team3501.robot.subsystems.DriveTrain;
4
5import edu.wpi.first.wpilibj.DriverStation;
6
7/**
8 * Copied/modified from Team 254's 2014 codebase. Thanks so much guys!
9 *
10 * CheesyDriveHelper implements the calculations used in CheesyDrive, sending
11 * power to the motors.
12 */
13public class CheesyDriveHelper {
14
15 private DriveTrain drive;
16 double oldWheel, quickStopAccumulator;
17 private double throttleDeadband = 0.02;
18 private double wheelDeadband = 0.02;
19
20 public CheesyDriveHelper(DriveTrain drive) {
21 this.drive = drive;
22 }
23
24 public void cheesyDrive(double throttle, double wheel, boolean isHighGear) {
25 if (DriverStation.getInstance().isAutonomous()) {
26 return;
27 }
28
29 double wheelNonLinearity;
30
31 wheel = handleDeadband(wheel, wheelDeadband);
32 throttle = handleDeadband(throttle, throttleDeadband);
33
34 double negInertia = wheel - oldWheel;
35 oldWheel = wheel;
36
37 if (isHighGear) {
38 wheelNonLinearity = 0.6;
39 // Apply a sin function that's scaled to make it feel better.
40 wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
41 / Math.sin(Math.PI / 2.0 * wheelNonLinearity);
42 wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
43 / Math.sin(Math.PI / 2.0 * wheelNonLinearity);
44 } else {
45 wheelNonLinearity = 0.5;
46 // Apply a sin function that's scaled to make it feel better.
47 wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
48 / Math.sin(Math.PI / 2.0 * wheelNonLinearity);
49 wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
50 / Math.sin(Math.PI / 2.0 * wheelNonLinearity);
51 wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
52 / Math.sin(Math.PI / 2.0 * wheelNonLinearity);
53 }
54
55 double leftPwm, rightPwm, overPower;
56 double sensitivity;
57
58 double angularPower;
59 double linearPower;
60
61 // Negative inertia!
62 double negInertiaAccumulator = 0.0;
63 double negInertiaScalar;
64 if (isHighGear) {
65 negInertiaScalar = 5.0;
66 sensitivity = .75;
67 } else {
68 if (wheel * negInertia > 0) {
69 negInertiaScalar = 2.5;
70 } else {
71 if (Math.abs(wheel) > 0.65) {
72 negInertiaScalar = 5.0;
73 } else {
74 negInertiaScalar = 3.0;
75 }
76 }
77 sensitivity = .75;
78 }
79 double negInertiaPower = negInertia * negInertiaScalar;
80 negInertiaAccumulator += negInertiaPower;
81
82 wheel = wheel + negInertiaAccumulator;
83 if (negInertiaAccumulator > 1) {
84 negInertiaAccumulator -= 1;
85 } else if (negInertiaAccumulator < -1) {
86 negInertiaAccumulator += 1;
87 } else {
88 negInertiaAccumulator = 0;
89 }
90 linearPower = throttle;
91
92 overPower = 0.0;
93 angularPower = Math.abs(throttle) * wheel * sensitivity
94 - quickStopAccumulator;
95 if (quickStopAccumulator > 1) {
96 quickStopAccumulator -= 1;
97 } else if (quickStopAccumulator < -1) {
98 quickStopAccumulator += 1;
99 } else {
100 quickStopAccumulator = 0.0;
101 }
102
103 rightPwm = leftPwm = linearPower;
104 leftPwm += angularPower;
105 rightPwm -= angularPower;
106
107 if (leftPwm > 1.0) {
108 rightPwm -= overPower * (leftPwm - 1.0);
109 leftPwm = 1.0;
110 } else if (rightPwm > 1.0) {
111 leftPwm -= overPower * (rightPwm - 1.0);
112 rightPwm = 1.0;
113 } else if (leftPwm < -1.0) {
114 rightPwm += overPower * (-1.0 - leftPwm);
115 leftPwm = -1.0;
116 } else if (rightPwm < -1.0) {
117 leftPwm += overPower * (-1.0 - rightPwm);
118 rightPwm = -1.0;
119 }
120 drive.setMotorSpeeds(leftPwm, rightPwm);
121
122 }
123
124 public double handleDeadband(double val, double deadband) {
125 return (Math.abs(val) > Math.abs(deadband)) ? val : 0.0;
126 }
127}