@@ -25,8 +25,7 @@ public class PIDTurn extends Command implements PIDOutput {
2525 private Timer tim ;
2626 private double lastTime ;
2727 private SmartDashboardInterface sd ;
28- private double pointX ;
29- private double pointY ;
28+ private double [] point ;
3029 private boolean turnToPoint ;
3130 private boolean absoluteRotation ;
3231
@@ -36,9 +35,12 @@ public class PIDTurn extends Command implements PIDOutput {
3635 * to the AHRS (gyro) object and sets its PIDOutput to this command which
3736 * implements PIDOutput's pidWrite() method.
3837 *
39- * @param targ
38+ * @param target
4039 * the target bearing (in degrees) to turn to (so negative if turning
4140 * left, positive if turning right)
41+ * @param point
42+ * the target point (in inches) to turn to, relative to the starting
43+ * position
4244 * @param dt
4345 * the Drivetrain (for actual code) or a DrivetrainInterface (for
4446 * testing)
@@ -47,25 +49,28 @@ public class PIDTurn extends Command implements PIDOutput {
4749 * @param sd
4850 * the Smart Dashboard reference, or a SmartDashboardInterface for
4951 * testing
50- * @param absolute
52+ * @param absoluteRotation
5153 * whether the target passed is absolute or relative
54+ * @param turnToPoint
55+ * whether to use the point or the target for turning
5256 */
53- public PIDTurn (double targ , DrivetrainInterface dt , SmartDashboardInterface sd , PIDSource ahrs , boolean absolute ) {
54- // Use requires() here to declare subsystem dependencies
57+ public PIDTurn (double target , double [] point , DrivetrainInterface dt , SmartDashboardInterface sd , PIDSource ahrs ,
58+ boolean absoluteRotation , boolean turnToPoint ) {
59+ this .target = target ;
60+ this .point = point ;
5561 this .dt = dt ;
5662 this .ahrs = ahrs ;
5763 this .sd = sd ;
58-
59- turnToPoint = false ;
60- target = targ ;
61- absoluteRotation = absolute ;
64+ this .turnToPoint = turnToPoint ;
65+ this .target = target ;
66+ this .absoluteRotation = absoluteRotation ;
6267
6368 if (Robot .dt != null ) {
6469 requires (Robot .dt );
6570 }
6671 // calculates the maximum turning speed in degrees/sec based on the max linear
6772 // speed in inches/s and the distance (inches) between sides of the DT
68- double maxTurnSpeed = dt .getCurrentMaxSpeed () * 360 / (Math .PI * sd . getConst ( "Distance Between Wheels" , 26.25 ));
73+ double maxTurnSpeed = dt .getCurrentMaxSpeed () * 360 / (Math .PI * getDistanceBetweenWheels ( ));
6974 double kf = 1 / (maxTurnSpeed * sd .getConst ("Default PID Update Time" , 0.05 ));
7075 turnController = new PIDController (sd .getConst ("TurnkP" , 1 ), sd .getConst ("TurnkI" , 0 ), sd .getConst ("TurnkD" , 0 ),
7176 kf , ahrs , this ) {
@@ -84,6 +89,7 @@ protected double calculateFeedForward() {
8489 }
8590 };
8691 // tim = new Timer();
92+ sd .putData ("Turn PID" , turnController );
8793 }
8894
8995 /**
@@ -92,7 +98,7 @@ protected double calculateFeedForward() {
9298 * to the AHRS (gyro) object and sets its PIDOutput to this command which
9399 * implements PIDOutput's pidWrite() method.
94100 *
95- * @param targ
101+ * @param target
96102 * the target bearing (in degrees) to turn to (so negative if turning
97103 * left, positive if turning right)
98104 * @param dt
@@ -103,9 +109,34 @@ protected double calculateFeedForward() {
103109 * @param sd
104110 * the Smart Dashboard reference, or a SmartDashboardInterface for
105111 * testing
112+ * @param absoluteRotation
113+ * whether the target passed is absolute or relative
106114 */
107- public PIDTurn (double targ , DrivetrainInterface dt , SmartDashboardInterface sd , PIDSource ahrs ) {
108- this (targ , dt , sd , ahrs , false );
115+ public PIDTurn (double target , DrivetrainInterface dt , SmartDashboardInterface sd , PIDSource ahrs ,
116+ boolean absoluteRotation ) {
117+ this (target , null , dt , sd , ahrs , absoluteRotation , false );
118+ }
119+
120+ /**
121+ * Constructs this command with a new PIDController. Sets all of the
122+ * controller's PID constants based on SD prefs. Sets the controller's PIDSource
123+ * to the AHRS (gyro) object and sets its PIDOutput to this command which
124+ * implements PIDOutput's pidWrite() method.
125+ *
126+ * @param target
127+ * the target bearing (in degrees) to turn to (so negative if turning
128+ * left, positive if turning right)
129+ * @param dt
130+ * the Drivetrain (for actual code) or a DrivetrainInterface (for
131+ * testing)
132+ * @param ahrs
133+ * the AHRS (gyro)
134+ * @param sd
135+ * the Smart Dashboard reference, or a SmartDashboardInterface for
136+ * testing
137+ */
138+ public PIDTurn (double target , DrivetrainInterface dt , SmartDashboardInterface sd , PIDSource ahrs ) {
139+ this (target , dt , sd , ahrs , false );
109140 }
110141
111142 /**
@@ -127,41 +158,7 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
127158 * testing
128159 */
129160 public PIDTurn (double [] point , DrivetrainInterface dt , SmartDashboardInterface sd , PIDSource ahrs ) {
130- this .dt = dt ;
131- this .sd = sd ;
132- this .ahrs = ahrs ;
133- this .sd = sd ;
134-
135- pointX = point [0 ];
136- pointY = point [1 ];
137-
138- turnToPoint = true ;
139-
140- if (Robot .dt != null ) {
141- requires (Robot .dt );
142- }
143- // calculates the maximum turning speed in degrees/sec based on the max linear
144- // speed in inches/s and the distance (inches) between sides of the DT
145- double maxTurnSpeed = dt .getCurrentMaxSpeed () * 360 / (Math .PI * getDistanceBetweenWheels ());
146- double kf = 1 / (maxTurnSpeed * sd .getConst ("Default PID Update Time" , 0.05 ));
147- turnController = new PIDController (sd .getConst ("TurnkP" , 1 ), sd .getConst ("TurnkI" , 0 ), sd .getConst ("TurnkD" , 0 ),
148- kf , ahrs , this ) {
149- /**
150- * Turn Velocity: V = 4r sqrt((T*G*theta) / (R*m)) where r = half of distance
151- * between wheels T = max torque of wheels G = gear ratio theta = rotational
152- * distance to end of turn R = radius of wheels m = mass
153- */
154- @ Override
155- protected double calculateFeedForward () {
156- double originalFF = super .calculateFeedForward ();
157- double feedForwardConst = dt .getPIDTurnConstant ();
158- double error = getError ();
159- return feedForwardConst * (getDistanceBetweenWheels () / 2 ) * Math .signum (error )
160- * Math .sqrt (Math .abs (error )) + originalFF ;
161- }
162- };
163- // tim = new Timer();
164- sd .putData ("Turn PID" , turnController );
161+ this (0 , point , dt , sd , ahrs , true , true );
165162 }
166163
167164 /**
@@ -176,8 +173,8 @@ protected void initialize() {
176173 target -= AutoUtils .state .getRot ();
177174 }
178175 } else {
179- double dx = pointX - AutoUtils .state .getX ();
180- double dy = pointY - AutoUtils .state .getY ();
176+ double dx = point [ 0 ] - AutoUtils .state .getX ();
177+ double dy = point [ 1 ] - AutoUtils .state .getY ();
181178
182179 System .out .println ("x = " + dx + ", y = " + dy );
183180
0 commit comments