-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheeseSteikDrive.java
147 lines (129 loc) · 4.13 KB
/
CheeseSteikDrive.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package org.usfirst.frc.team8.robot;
/**
* A clone of Team 254's Cheesy Drive set up to run on Steik
* @author Jonathan Zwiebel
*
*/
public class CheeseSteikDrive {
public static final float hg_sensitivity = 0.85f; // Originally 0.85
public static final float qs_alpha = 0.3f; // Originally 0.3
public static final float qs_change_value = 0.5f; // Originally 1.0
private double mOldWheel, mQuickStopAccumulator;
private final double kWheelStickDeadband = 0.01;
private final double kThrottleStickDeadband = 0.01;
public double left_power = 0, right_power = 0;
public void update(double throttle, double wheel, boolean isQuickTurn, boolean isHighGear) {
double wheelNonLinearity;
wheel = handleDeadband(wheel, kWheelStickDeadband);
throttle = handleDeadband(throttle, kThrottleStickDeadband);
double negInertia = wheel - mOldWheel;
mOldWheel = wheel;
if (isHighGear) {
wheelNonLinearity = 0.6;
// Apply a sin function that's scaled to make it feel better.
wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
/ Math.sin(Math.PI / 2.0 * wheelNonLinearity);
wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
/ Math.sin(Math.PI / 2.0 * wheelNonLinearity);
} else {
wheelNonLinearity = 0.5;
// Apply a sin function that's scaled to make it feel better.
wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
/ Math.sin(Math.PI / 2.0 * wheelNonLinearity);
wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
/ Math.sin(Math.PI / 2.0 * wheelNonLinearity);
wheel = Math.sin(Math.PI / 2.0 * wheelNonLinearity * wheel)
/ Math.sin(Math.PI / 2.0 * wheelNonLinearity);
}
double leftPwm, rightPwm, overPower;
double sensitivity;
double angularPower;
double linearPower;
// Negative inertia!
double negInertiaAccumulator = 0.0;
double negInertiaScalar;
if (isHighGear) {
negInertiaScalar = 4.0;
sensitivity = hg_sensitivity;
} else {
if (wheel * negInertia > 0) {
negInertiaScalar = 2.5;
} else {
if (Math.abs(wheel) > 0.65) {
negInertiaScalar = 5.0;
} else {
negInertiaScalar = 3.0;
}
}
sensitivity = hg_sensitivity;
}
double negInertiaPower = negInertia * negInertiaScalar;
negInertiaAccumulator += negInertiaPower;
wheel = wheel + negInertiaAccumulator;
if (negInertiaAccumulator > 1) {
negInertiaAccumulator -= 1;
} else if (negInertiaAccumulator < -1) {
negInertiaAccumulator += 1;
} else {
negInertiaAccumulator = 0;
}
linearPower = throttle;
// Quickturn!
if (isQuickTurn) {
if (Math.abs(linearPower) < 0.2) {
// Can be tuned
double alpha = qs_alpha;
mQuickStopAccumulator = (1 - alpha) * mQuickStopAccumulator
+ alpha * limit(wheel, 1.0) * 5;
}
overPower = 1.0;
if (isHighGear) {
sensitivity = 1.0;
} else {
sensitivity = 1.0;
}
angularPower = wheel;
} else {
overPower = 0.0;
angularPower = Math.abs(throttle) * wheel * sensitivity
- mQuickStopAccumulator;
if (mQuickStopAccumulator > qs_change_value) {
mQuickStopAccumulator -= qs_change_value;
} else if (mQuickStopAccumulator < -qs_change_value) {
mQuickStopAccumulator += qs_change_value;
} else {
mQuickStopAccumulator = 0.0;
}
}
rightPwm = leftPwm = linearPower;
leftPwm += angularPower;
rightPwm -= angularPower;
if (leftPwm > 1.0) {
rightPwm -= overPower * (leftPwm - 1.0);
leftPwm = 1.0;
} else if (rightPwm > 1.0) {
leftPwm -= overPower * (rightPwm - 1.0);
rightPwm = 1.0;
} else if (leftPwm < -1.0) {
rightPwm += overPower * (-1.0 - leftPwm);
leftPwm = -1.0;
} else if (rightPwm < -1.0) {
leftPwm += overPower * (-1.0 - rightPwm);
rightPwm = -1.0;
}
left_power = leftPwm;
right_power = rightPwm;
}
private double handleDeadband(double val, double deadband) {
return (Math.abs(val) > Math.abs(deadband)) ? val : 0.0;
}
private double limit(double wheel, double d) {
if(wheel > d) {
return d;
}
if(wheel < -d) {
return - d;
}
return wheel;
}
}