-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSteikDrivetrain.java
272 lines (214 loc) · 9.23 KB
/
SteikDrivetrain.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package org.usfirst.frc.team8.robot;
import com.ctre.CANTalon;
import com.ctre.CANTalon.StatusFrameRate;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.PowerDistributionPanel;
/**
* A class to test basic mechanical functionality and tune
* loop values for the drivetrain on our 2017 robot
*
* @author Jonathan Zwiebel (frc8)
*
*/
public class SteikDrivetrain {
public static final double NATIVE_UPDATES = 100; // From documentation
public static final double NATIVE_RATE = 1000 / NATIVE_UPDATES; // Calculated
public static final double INCHES_TO_TICKS = 1440 / (2 * 3.1415 * 2); // 2 very roughly taken from 4" diameter wheels // Very roughly estimated
public static final double INCHES_PER_SECOND_TO_TICKS_PER_SECOND = INCHES_TO_TICKS / NATIVE_RATE; // Calculated
String auto_action;
CANTalon left_a; // Master
CANTalon left_b;
CANTalon left_c;
CANTalon right_a;
CANTalon right_b;
CANTalon right_c; // Master
Robot robot;
Joystick drive_stick;
Joystick turn_stick;
CheeseSteikDrive csd;
PowerDistributionPanel pdp = new PowerDistributionPanel();
public SteikDrivetrain(Robot robot) {
this.robot = robot;
left_a = new CANTalon(SteikConstants.DRIVETRAIN_LEFT_A_TALON_DEVICE_ID);
left_b = new CANTalon(SteikConstants.DRIVETRAIN_LEFT_B_TALON_DEVICE_ID);
left_c = new CANTalon(SteikConstants.DRIVETRAIN_LEFT_C_TALON_DEVICE_ID);
right_a = new CANTalon(SteikConstants.DRIVETRAIN_RIGHT_A_TALON_DEVICE_ID);
right_b = new CANTalon(SteikConstants.DRIVETRAIN_RIGHT_B_TALON_DEVICE_ID);
right_c = new CANTalon(SteikConstants.DRIVETRAIN_RIGHT_C_TALON_DEVICE_ID);
drive_stick = new Joystick(SteikConstants.DRIVE_STICK_PORT);
turn_stick = new Joystick(SteikConstants.TURN_STICK_PORT);
csd = new CheeseSteikDrive();
left_a.configMaxOutputVoltage(+12.0f);
left_b.configMaxOutputVoltage(+12.0f);
left_c.configMaxOutputVoltage(+12.0f);
right_a.configMaxOutputVoltage(+12.0f);
right_b.configMaxOutputVoltage(+12.0f);
right_c.configMaxOutputVoltage(+12.0f);
}
public void init() {
left_a.configPeakOutputVoltage(+12.0f, -12.0f);
right_c.configPeakOutputVoltage(+12.0f, -12.0f);
left_a.setCloseLoopRampRate(Integer.MAX_VALUE);
right_c.setCloseLoopRampRate(Integer.MAX_VALUE);
left_a.setVoltageRampRate(Integer.MAX_VALUE);
right_c.setCloseLoopRampRate(Integer.MAX_VALUE);
left_a.setFeedbackDevice(CANTalon.FeedbackDevice.QuadEncoder);
right_c.setFeedbackDevice(CANTalon.FeedbackDevice.QuadEncoder);
left_a.setPosition(0.0f);
right_c.setPosition(0.0f);
left_a.changeControlMode(CANTalon.TalonControlMode.PercentVbus);
right_c.changeControlMode(CANTalon.TalonControlMode.PercentVbus);
left_b.changeControlMode(CANTalon.TalonControlMode.Follower);
left_c.changeControlMode(CANTalon.TalonControlMode.Follower);
left_b.set(left_a.getDeviceID());
left_c.set(left_a.getDeviceID());
right_a.changeControlMode(CANTalon.TalonControlMode.Follower);
right_b.changeControlMode(CANTalon.TalonControlMode.Follower);
right_a.set(right_c.getDeviceID());
right_b.set(right_c.getDeviceID());
left_a.reverseOutput(false);
right_c.reverseOutput(true);
left_a.setInverted(false);
right_c.setInverted(true);
left_a.reverseSensor(false);
right_c.reverseSensor(true);
left_a.setStatusFrameRateMs(StatusFrameRate.Feedback, 100);
right_c.setStatusFrameRateMs(StatusFrameRate.Feedback, 100);
}
public void update() {
csd.update(-drive_stick.getY(), turn_stick.getX(), turn_stick.getRawButton(1), true);
float left_power = (float) csd.left_power;
float right_power = (float) csd.right_power;
left_power = Math.max(Math.min(left_power, SteikConstants.DRIVETRAIN_MAX_OUTPUT), -SteikConstants.DRIVETRAIN_MAX_OUTPUT);
right_power = Math.max(Math.min(right_power, SteikConstants.DRIVETRAIN_MAX_OUTPUT), -SteikConstants.DRIVETRAIN_MAX_OUTPUT);
left_a.set(left_power);
right_c.set(right_power);
// System.out.println("Left Drivetrain Voltage: " + left_a.getOutputVoltage());
// System.out.println("Right Drivetrain Voltage: " + right_a.getOutputVoltage());
System.out.println("Left Drivetrain Position: " + left_a.getPosition());
System.out.println("Right Drivetrain Positon: " + right_c.getPosition());
System.out.println("Left Drivetrain Speed: " + left_a.getSpeed());
System.out.println("Right Drivtrain Speed: " + right_c.getSpeed());
Robot.table.putString("status", 12 + "," + left_a.getOutputCurrent() + "," + left_b.getOutputCurrent() + "," + left_c.getOutputCurrent() + "," + -right_a.getOutputCurrent() + "," + -right_b.getOutputCurrent() + "," + -right_c.getOutputCurrent() + "\n");
updateTable();
}
public void autoInit() {
auto_action = "Drive Straight";
setupVelocityHold();
}
public void setupDriveStraight() {
left_a.setPosition(0.0f);
right_c.setPosition(0.0f);
left_a.changeControlMode(CANTalon.TalonControlMode.Position);
right_c.changeControlMode(CANTalon.TalonControlMode.Position);
left_a.configPeakOutputVoltage(+6.0f, -6.0f);
right_c.configPeakOutputVoltage(+6.0f, -6.0f);
left_a.setPID(0.4f, 0.0025f, 8.0f, 0, 100, 0, 0);
right_c.setPID(0.4f, 0.0025f, 8.0f, 0, 100, 0, 0);
//left_a.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
//right_c.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
try {
Thread.sleep(101);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setupTurnAngle() {
left_a.setPosition(0.0f);
right_c.setPosition(0.0f);
left_a.changeControlMode(CANTalon.TalonControlMode.Position);
right_c.changeControlMode(CANTalon.TalonControlMode.Position);
left_a.configPeakOutputVoltage(+8.0f, -8.0f);
right_c.configPeakOutputVoltage(+8.0f, -8.0f);
left_a.setPID(0.75f, 0.002f, 9.0f, 0.05f, 150, 0, 0);
right_c.setPID(0.75f, 0.002f, 9.0f, 0.05f, 150, 0, 0);
//left_a.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
//right_c.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
try {
Thread.sleep(101);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setupDriveEnd() {
left_a.setPosition(0.0f);
right_c.setPosition(0.0f);
left_a.changeControlMode(CANTalon.TalonControlMode.Position);
right_c.changeControlMode(CANTalon.TalonControlMode.Position);
left_a.configPeakOutputVoltage(+8.0f, -8.0f);
right_c.configPeakOutputVoltage(+8.0f, -8.0f);
left_a.setPID(0.4f, 0.0025f, 8.0f, 0, 100, 0, 0);
right_c.setPID(0.4f, 0.0025f, 8.0f, 0, 100, 0, 0);
//left_a.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
//right_c.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
try {
Thread.sleep(101);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setupVelocityHold() {
left_a.setPosition(0.0f);
right_c.setPosition(0.0f);
left_a.changeControlMode(CANTalon.TalonControlMode.Speed);
right_c.changeControlMode(CANTalon.TalonControlMode.Speed);
right_c.reverseOutput(false);
left_a.configPeakOutputVoltage(+8.0f, -8.0f);
right_c.configPeakOutputVoltage(+8.0f, -8.0f);
left_a.setPID(0, 0, 0, 1.02711f, 0, 0, 0);
right_c.setPID(0, 0, 0, 1.02711f, 0, 0, 0);
//left_a.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
//right_c.setStatusFrameRateMs(StatusFrameRate.Feedback, 1);
try {
Thread.sleep(101);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void autoUpdate() {
switch(auto_action) {
case "Drive Straight":
left_a.set(2800f);
right_c.set(2800f);
if(left_a.getSpeed() == 0 && right_c.getSpeed() == 0 && left_a.getClosedLoopError() <= 20 && right_c.getClosedLoopError() <= 20) {
auto_action = "Turn Angle";
setupTurnAngle();
}
break;
case "Turn Angle":
left_a.set(-400f);
right_c.set(400f);
if(left_a.getSpeed() <= 10 && right_c.getSpeed() <= 10 && left_a.getClosedLoopError() <= 100 && right_c.getClosedLoopError() <= 100) {
auto_action = "Drive End";
setupDriveEnd();
}
break;
case "Drive End":
left_a.set(800f);
right_c.set(800f);
break;
case "Velocity Hold":
left_a.set(INCHES_PER_SECOND_TO_TICKS_PER_SECOND * 24.0f);
right_c.set(INCHES_PER_SECOND_TO_TICKS_PER_SECOND * 24.0f);
break;
default:
System.out.println("Illegal auto action");
System.exit(1);
break;
}
System.out.println("Left Drivetrain Position: " + left_a.getPosition());
System.out.println("Right Drivetrain Positon: " + right_c.getPosition());
System.out.println("Left Drivetrain Speed: " + left_a.getSpeed());
System.out.println("Right Drivtrain Speed: " + right_c.getSpeed());
updateTable();
}
public void updateTable() {
Robot.dashboardTable.putString("driveSpeedUpdate", left_a.getSpeed() + ", " + right_c.getSpeed());
Robot.dashboardTable.putString("leftdriveencoder", left_a.getPosition() + "");
Robot.dashboardTable.putString("rightdriveencoder", right_c.getPosition() + "");
}
}