66import com .revrobotics .RelativeEncoder ;
77import com .revrobotics .SparkMaxRelativeEncoder ;
88import edu .wpi .first .math .controller .*;
9+ import frc .team5115 .Classes .Software .Arm ;
910import edu .wpi .first .math .MathUtil ;
1011
1112public class HardwareDrivetrain {
13+ // Competition feedforward values - 6 inch diameter on KITT comp robot with arm and ballasts
14+ private final double leftKs = 0.0378 ;
15+ private final double leftKv = 2.7479 ;
16+ private final double leftKa = 0.32825 ;
17+
18+ private final double rightKs = 0.0528 ;
19+ private final double rightKv = 2.8399 ;
20+ private final double rightKa = 0.26071 ;
21+
22+ // private final double combinedkP = 3.4349;
23+ // END of comp robot values
1224
13-
14-
15- // Competition feedforward and feedback (pid) values
16- // 6 inch diameter on COMP ROBOT WITH ARM and dumbells in back
17- private final double leftKs = 0.17463 ;
18- private final double leftKv = 2.8104 ;
19- private final double leftKa = 0.82143 ;
25+ // Testbed feedforward values - 6 inch diameter on KATT testbed
26+ // private final double leftKs = 0.090949;
27+ // private final double leftKv = 2.783;
28+ // private final double leftKa = 0.16477;
2029
21- private final double rightKs = 0.17463 ;
22- private final double rightKv = 2.8104 ;
23- private final double rightKa = 0.82143 ;
24-
25- // private final double leftKp = 1.6455;
26- // private final double rightKp = 1.6220;
27- // private final double Ki = 0.0;
28- // private final double Kd = 0.0;
29- // // END of comp robot values
30-
31- // Testbed feedforward and feedback (pid) values - 6 inch diameter on testbed
32- /* private final double leftKs = 0.090949;
33- private final double leftKv = 2.783;
34- private final double leftKa = 0.16477;
30+ // private final double rightKs = 0.099706;
31+ // private final double rightKv = 2.8314;
32+ // private final double rightKa = 0.14565;
33+ // END of testbed values
3534
36- private final double rightKs = 0.099706;
37- private final double rightKv = 2.8314;
38- private final double rightKa = 0.14565;
39- */
40-
41- private final double leftKp = 0.0 ; // 3.7203 according to sysid
42- private final double rightKp = 0.0 ; // 3.7203 according to sysid
43- private final double Ki = 0.0 ;
44- private final double Kd = 0.0 ;
35+
36+ private final double leftKp = 0.0 ;
37+ private final double rightKp = 0.0 ;
38+ private final double Ki = 0.1 ;
39+ private final double Kd = 0.1 ;
4540 // END of testbed values
4641
4742 private final SimpleMotorFeedforward leftFeedForward = new SimpleMotorFeedforward (leftKs , leftKv , leftKa );
@@ -57,7 +52,10 @@ public class HardwareDrivetrain{
5752 private final RelativeEncoder leftEncoder = frontLeft .getEncoder (SparkMaxRelativeEncoder .Type .kHallSensor , 42 );
5853 private final RelativeEncoder rightEncoder = frontRight .getEncoder (SparkMaxRelativeEncoder .Type .kHallSensor , 42 );
5954
60- public HardwareDrivetrain (){
55+ private final Arm arm ;
56+
57+ public HardwareDrivetrain (Arm arm ){
58+ this .arm = arm ;
6159 resetEncoders ();
6260 frontRight .setInverted (true );
6361 }
@@ -141,21 +139,23 @@ public void PlugandVoltDrive(double frontLeftVoltage, double frontRightVoltage,
141139 * @param rightSpeed the speed for the right motors in meters per second
142140 */
143141 public void plugandFFDrive (double leftSpeed , double rightSpeed ) {
144-
145- if (Math .abs (leftSpeed - (leftEncoder .getVelocity ()*NEO_VELOCITY_CALIBRATION ))>1.5 ){
146- //System.out.println("Left too fast");
147- leftSpeed = (leftEncoder .getVelocity ()*NEO_VELOCITY_CALIBRATION ) + 1.5 *Math .signum ((leftSpeed - (leftEncoder .getVelocity ()*NEO_VELOCITY_CALIBRATION )));
148- }
142+ final double accelerationLimit = getAccelerationLimit (); // can't bother figuring the units, but it's not m/s^2
143+ final double currentLeftVelocity = getLeftVelocity ();
144+ final double currentRightVelocity = getRightVelocity ();
149145
150- if (Math .abs (rightSpeed - (rightEncoder .getVelocity ()*NEO_VELOCITY_CALIBRATION ))>1 ){
151- //System.out.println("Right too fast");
152- rightSpeed = (rightEncoder .getVelocity ()*NEO_VELOCITY_CALIBRATION ) + 1.5 *Math .signum ((rightSpeed - (rightEncoder .getVelocity ()*NEO_VELOCITY_CALIBRATION )));
153- }
146+ // limit left acceleration
147+ if (Math .abs (leftSpeed - currentLeftVelocity ) > accelerationLimit ) {
148+ leftSpeed = currentLeftVelocity + accelerationLimit * Math .signum (leftSpeed - currentLeftVelocity );
149+ }
150+ // limit right acceleration
151+ if (Math .abs (rightSpeed - currentRightVelocity ) > accelerationLimit ) {
152+ rightSpeed = currentRightVelocity + accelerationLimit * Math .signum (rightSpeed - currentRightVelocity );
153+ }
154154
155155 double leftVoltage = leftFeedForward .calculate (leftSpeed );
156156 double rightVoltage = rightFeedForward .calculate (rightSpeed );
157- leftVoltage += leftPID .calculate (leftEncoder . getVelocity () * NEO_VELOCITY_CALIBRATION , leftSpeed );
158- rightVoltage += rightPID .calculate (rightEncoder . getVelocity () * NEO_VELOCITY_CALIBRATION , rightSpeed );
157+ leftVoltage += leftPID .calculate (currentLeftVelocity , leftSpeed );
158+ rightVoltage += rightPID .calculate (currentRightVelocity , rightSpeed );
159159 // Work on better PID Analyzer
160160
161161 leftVoltage = MathUtil .clamp (leftVoltage , -DRIVE_MOTOR_MAX_VOLTAGE , DRIVE_MOTOR_MAX_VOLTAGE );
@@ -167,8 +167,25 @@ public void plugandFFDrive(double leftSpeed, double rightSpeed) {
167167 frontRight .setVoltage (rightVoltage );
168168 }
169169
170+ private double getAccelerationLimit () {
171+ return 1.5 ;
172+ // final double angle = arm.getAngle();
173+ // final double length = (arm.getBottomWinchLength() + arm.getTopWinchLength()) / 2;
174+ // final double angleConstant = 0.01;
175+ // final double lengthConstant = 0.017;
176+ // return 1.0 / (((angle + 90.0) * angleConstant) + (length * lengthConstant) + 1.0) + 0.5;
177+ }
178+
170179 public void resetEncoders (){
171180 leftEncoder .setPosition (0 );
172181 rightEncoder .setPosition (0 );
173182 }
183+
184+ public double getLeftVelocity () {
185+ return leftEncoder .getVelocity () * NEO_VELOCITY_CALIBRATION ;
186+ }
187+
188+ public double getRightVelocity () {
189+ return rightEncoder .getVelocity () * NEO_VELOCITY_CALIBRATION ;
190+ }
174191}
0 commit comments