-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrivetrain.java
More file actions
224 lines (184 loc) · 7.19 KB
/
Drivetrain.java
File metadata and controls
224 lines (184 loc) · 7.19 KB
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
/**
* Filename: Drivetrain.java
*
* Description:
* This class makes a drivetrain out of any number of motors. This class gives
* information about the motors such as encoder counts, and it is also used to
* control the motors (for example, setPowers())
*
* Methods:
* - haltDrive
* - setMode - Not used
* - setTargetPosition
* - setPowers
* - convertInchesToEncoderCounts
* - getLeftEncoderCount
* - getRightEncoderCount()
* - resetMotorEncoder
* - toString
*
* *
* Example:
*
* robot.hardwareConfiguration.drivetrain.getLeftEncoderCount();
*
* Requirements:
* - Drive motors with encoders
*
*
* Changelog:
* -Created a long time ago in a far away place when men used stone and chisel to code.
* -Edited file description and documentation 7/24/17
*/
package org.firstinspires.ftc.teamcode.RobotCoreExtensions;
import com.qualcomm.robotcore.hardware.DcMotor;
import java.util.LinkedList;
import java.util.List;
public class Drivetrain {
private double wheelDiameter;
private double gearRatio;
private int encoderCountsPerDriverGearRotation;
private List<DcMotor> allMotors;
private List<DcMotor> allMotorsWithEncoders;
private List<DcMotor> leftMotors;
private List<DcMotor> rightMotors;
private List<DcMotor> leftMotorsWithEncoders;
private List<DcMotor> rightMotorsWithEncoders;
private double leftSpeed;
private double rightSpeed;
private Drivetrain(Builder builder) {
this.wheelDiameter = builder.wheelDiameter;
this.gearRatio = builder.gearRatio;
this.encoderCountsPerDriverGearRotation = builder.encoderCountsPerDriverGearRotation;
this.leftMotorsWithEncoders = builder.leftMotorsWithEncoders;
this.rightMotorsWithEncoders = builder.rightMotorsWithEncoders;
this.leftMotors = builder.leftMotors;
this.leftMotors.addAll(builder.leftMotorsWithEncoders);
this.rightMotors = builder.rightMotors;
this.rightMotors.addAll(builder.rightMotorsWithEncoders);
this.allMotorsWithEncoders = new LinkedList<>();
this.allMotorsWithEncoders.addAll(builder.leftMotorsWithEncoders);
this.allMotorsWithEncoders.addAll(builder.rightMotorsWithEncoders);
this.allMotors = new LinkedList<>();
this.allMotors.addAll(this.leftMotors);
this.allMotors.addAll(this.rightMotors);
for (DcMotor motor : allMotors) {
motor.setZeroPowerBehavior(builder.zeroPowerBehavior);
}
}
// Important configuration required to match the drive train - Wheel size, gear ration and type of motors.
public static class Builder {
private double wheelDiameter = 4.31; // Adjusted as a correction factor
private double gearRatio = 1.2; // Adjusted as a correction factor
// 1120 is the number for the AndyMark allMotors. Tetrix Motors are 1440 PPR
private int encoderCountsPerDriverGearRotation = 1120;
private final List<DcMotor> leftMotors = new LinkedList<>();
private final List<DcMotor> rightMotors = new LinkedList<>();
private final List<DcMotor> leftMotorsWithEncoders = new LinkedList<>();
private final List<DcMotor> rightMotorsWithEncoders = new LinkedList<>();
private DcMotor.ZeroPowerBehavior zeroPowerBehavior = DcMotor.ZeroPowerBehavior.BRAKE;
public Builder setWheelDiameter(double wheelDiameter) {
this.wheelDiameter = wheelDiameter;
return this;
}
public Builder setGearRatio(double gearRatio) {
this.gearRatio = gearRatio;
return this;
}
public Builder setEncoderCountsPerDriverGearRotation(int encoderCountsPerDriverGearRotation) {
this.encoderCountsPerDriverGearRotation = encoderCountsPerDriverGearRotation;
return this;
}
public Builder setZeroPowerBehavior(DcMotor.ZeroPowerBehavior zeroPowerBehavior) {
this.zeroPowerBehavior = zeroPowerBehavior;
return this;
}
public Builder addLeftMotor(DcMotor leftMotor) {
leftMotors.add(leftMotor);
return this;
}
public Builder addLeftMotorWithEncoder(DcMotor leftMotor) {
leftMotorsWithEncoders.add(leftMotor);
return this;
}
public Builder addRightMotor(DcMotor rightMotor) {
rightMotors.add(rightMotor);
return this;
}
public Builder addRightMotorWithEncoder(DcMotor rightMotor) {
rightMotorsWithEncoders.add(rightMotor);
return this;
}
public Drivetrain build() {
return new Drivetrain(this);
}
}
// Stops the robot by setting all drive train motors to 0 power.
public void haltDrive() {
setPowers(0.0, 0.0);
}
// Not currently used
public void setMode(DcMotor.RunMode runMode) {
for (DcMotor motor : allMotorsWithEncoders) {
motor.setMode(runMode);
}
}
// Sets Target Position for left and right sides of the drive train.
public void setTargetPosition(int leftTargetPosition, int rightTargetPosition) {
for (DcMotor leftMotor : leftMotorsWithEncoders) {
leftMotor.setTargetPosition(leftTargetPosition);
}
for (DcMotor rightMotor : rightMotorsWithEncoders) {
rightMotor.setTargetPosition(rightTargetPosition);
}
}
// Sets Power for left and right sides of the drive train.
public void setPowers(double leftSpeed, double rightSpeed) {
this.leftSpeed = leftSpeed;
this.rightSpeed = rightSpeed;
for (DcMotor motor : leftMotors) {
motor.setPower(leftSpeed);
}
for (DcMotor motor : rightMotors) {
motor.setPower(rightSpeed);
}
}
protected long convertInchesToEncoderCounts(double distance) {
return Math.round(((distance / (Math.PI * wheelDiameter)) / gearRatio) *
encoderCountsPerDriverGearRotation);
}
public double getLeftEncoderCount() {
if (leftMotorsWithEncoders.size() == 0) {
return 0;
}
double sumValue = 0;
for (DcMotor leftMotor : leftMotorsWithEncoders) {
sumValue += leftMotor.getCurrentPosition();
}
sumValue /= (leftMotorsWithEncoders.size());
return sumValue;
}
public double getRightEncoderCount() {
if (rightMotorsWithEncoders.size() == 0) {
return 0;
}
double sumValue = 0;
for (DcMotor rightMotor : rightMotorsWithEncoders) {
sumValue += rightMotor.getCurrentPosition();
}
sumValue /= rightMotorsWithEncoders.size();
return sumValue;
}
//Resets the motor encoders for the left and right sides of the drive train.
public void resetMotorEncoders() {
for (DcMotor motor : allMotorsWithEncoders) {
motor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
motor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
}
@Override
public String toString() {
return "left pwr: " + String.format("%.2f", leftSpeed) +
"\nright pwr: " + String.format("%.2f", rightSpeed);
}
}