clean up DriveForTime
[3501/2015-FRC-Spark] / src / org / usfirst / frc3501 / RiceCatRobot / subsystems / DriveTrain.java
CommitLineData
f11ce98e
KZ
1package org.usfirst.frc3501.RiceCatRobot.subsystems;
2
3import org.usfirst.frc3501.RiceCatRobot.RobotMap;
4
5import edu.wpi.first.wpilibj.CANJaguar;
6import edu.wpi.first.wpilibj.Encoder;
7import edu.wpi.first.wpilibj.CounterBase.EncodingType;
8import edu.wpi.first.wpilibj.command.Subsystem;
9
10public class DriveTrain extends Subsystem {
7682c821
D
11 public static final double MOTOR_MAX_VAL = 1;
12 public static final double MOTOR_MIN_VAL = -1;
f11ce98e 13
7682c821
D
14 private CANJaguar frontLeft, frontRight, rearLeft, rearRight;
15 private Encoder leftEncoder, rightEncoder;
f11ce98e 16
7682c821
D
17 public DriveTrain() {
18 frontLeft = new CANJaguar(RobotMap.DRIVE_FRONT_LEFT);
19 frontRight = new CANJaguar(RobotMap.DRIVE_FRONT_RIGHT);
20 rearLeft = new CANJaguar(RobotMap.DRIVE_REAR_LEFT);
21 rearRight = new CANJaguar(RobotMap.DRIVE_REAR_RIGHT);
22 leftEncoder = new Encoder(RobotMap.DRIVE_LEFT_A, RobotMap.DRIVE_LEFT_B,
23 false, EncodingType.k4X);
24 rightEncoder = new Encoder(RobotMap.DRIVE_RIGHT_A, RobotMap.DRIVE_RIGHT_B,
25 false, EncodingType.k4X);
26 leftEncoder.setDistancePerPulse(RobotMap.DISTANCE_PER_PULSE);
27 rightEncoder.setDistancePerPulse(RobotMap.DISTANCE_PER_PULSE);
28 }
f11ce98e 29
7682c821
D
30 public void resetEncoders() {
31 leftEncoder.reset();
32 rightEncoder.reset();
33 }
f11ce98e 34
7682c821
D
35 public double getRightSpeed() {
36 // Returns in per second
37 return rightEncoder.getRate();
38 }
f11ce98e 39
7682c821
D
40 public double getLeftSpeed() {
41 return leftEncoder.getRate();
42 }
f11ce98e 43
7682c821
D
44 public double getRightDistance() {
45 // Returns distance in in
46 return rightEncoder.getDistance();
47 }
f11ce98e 48
7682c821
D
49 public double getLeftDistance() {
50 // Returns distance in in
51 return leftEncoder.getDistance();
52 }
f11ce98e 53
0b38c075
D
54 public void stop() {
55 setMotorSpeeds(0, 0);
56 }
57
7682c821
D
58 public void setMotorSpeeds(double leftSpeed, double rightSpeed) {
59 if (Math.abs(leftSpeed) < RobotMap.DRIVE_DEAD_ZONE) {
60 leftSpeed = 0;
61 }
62 if (Math.abs(rightSpeed) < RobotMap.DRIVE_DEAD_ZONE) {
63 rightSpeed = 0;
64 }
65 this.frontLeft.set(leftSpeed);
66 this.frontRight.set(-rightSpeed);
67 this.rearLeft.set(leftSpeed);
68 this.rearRight.set(-rightSpeed);
69 }
f11ce98e 70
7682c821
D
71 public void arcadeDrive(double yVal, double twist) {
72 if (yVal < 0 && Math.abs(yVal) < 0.1125) {
73 yVal = 0;
74 } else if (yVal > 0 && Math.abs(yVal) < 0.25) {
75 yVal = 0;
76 } else if (yVal > 0) {
77 yVal -= 0.25;
78 } else if (yVal < 0) {
79 yVal += 0.15;
80 }
81 if (Math.abs(twist) < RobotMap.DRIVE_DEAD_ZONE) {
82 twist = 0;
f11ce98e
KZ
83 }
84
7682c821
D
85 double leftMotorSpeed;
86 double rightMotorSpeed;
87 // adjust the sensitivity (yVal+rootof (yval)) / 2
88 yVal = (yVal + Math.signum(yVal) * Math.sqrt(Math.abs(yVal))) / 2;
89 // adjust the sensitivity (twist+rootof (twist)) / 2
90 twist = (twist + Math.signum(twist) * Math.sqrt(Math.abs(twist))) / 2;
91 if (yVal > 0) {
92 if (twist > 0) {
93 leftMotorSpeed = yVal - twist;
94 rightMotorSpeed = Math.max(yVal, twist);
95 } else {
96 leftMotorSpeed = Math.max(yVal, -twist);
97 rightMotorSpeed = yVal + twist;
98 }
99 } else {
100 if (twist > 0.0) {
101 leftMotorSpeed = -Math.max(-yVal, twist);
102 rightMotorSpeed = yVal + twist;
103 } else {
104 leftMotorSpeed = yVal - twist;
105 rightMotorSpeed = -Math.max(-yVal, -twist);
106 }
f11ce98e 107 }
7682c821
D
108 setMotorSpeeds(leftMotorSpeed, rightMotorSpeed);
109 }
110
111 @Override
112 protected void initDefaultCommand() {
113 }
f11ce98e 114}