11package frc .robot .commands .visionDriveCommand ;
22
3-
43import static edu .wpi .first .units .Units .MetersPerSecond ;
54import static edu .wpi .first .units .Units .RadiansPerSecond ;
65
6+
77import edu .wpi .first .math .MathUtil ;
88import edu .wpi .first .units .Units ;
9- import edu .wpi .first .units .measure .*;
9+ import edu .wpi .first .units .measure .*;
1010import edu .wpi .first .wpilibj .smartdashboard .SmartDashboard ;
1111import edu .wpi .first .wpilibj2 .command .Command ;
1212import frc .robot .subsystems .drive .*;
1313import frc .robot .subsystems .vision .*;
1414
1515public class VisionAutoCommand extends Command {
16-
16+
1717 // config
18- // for now instead of a toml FIXME:
19- public static final double STOP_MOVING_THERSHOLD = 0.58 ;
20- public static final double STAFE_THERSHOLD = 1.5 ;
21- public static final double TURNING_THERSHOLD = 5.0 ;
22-
18+
2319 private final Vision kVisionSubsystem ;
2420 private final Drivetrain kDrivetrain ;
2521 private final Time kRobotPeriod ;
26-
27- // config
28-
29- private final LinearVelocity kStrafeSpeed = Units .MetersPerSecond .of (0.22 );
30- private final AngularVelocity kTurnSpeed = Units .RadiansPerSecond .of (0.58 );
31- private final LinearVelocity kForwardSpeed = Units .MetersPerSecond .of (1.0 );
32-
33-
34- private final LinearVelocity kMaxLinearSpeed = Units .MetersPerSecond .of (1.9 );
35-
36- private final AngularVelocity kMaxAngularSpeed = Units .RadiansPerSecond .of (1.2 );
37-
38- private final double kDistanceMultiplierForwards = 0.65 ;
39- private final double kDistanceMultiplierStafing = 0.07 ;
40- private final double kDistanceMultiplierTurning = 0.4 ;
41-
42- private final LinearVelocity kMinMoveSpeedLinear = Units .MetersPerSecond .of (0.03 );
43- private final AngularVelocity kMinMoveSpeedAngular = Units .RadiansPerSecond .of (0.1 );
4422
4523 private boolean mIsDoneStafing = false ;
4624 private boolean mIsDoneMoving = false ;
47-
48-
49- private int mTargetDriveAprilTagId ;
25+
26+ private final int mTargetDriveAprilTagId ;
5027
5128 private AngularVelocity mAngularVelocity = Units .RadiansPerSecond .zero ();
5229 private LinearVelocity mStafeVelocity = Units .MetersPerSecond .zero ();
5330 private LinearVelocity mForwardVelocity = Units .MetersPerSecond .zero ();
5431
55-
32+ private Vision .Config mConfig ;
33+
5634 public VisionAutoCommand (
57- Vision visionSubsystem ,
58- Drivetrain drivetrain ,
59- int targetDriveAprilTagId ,
60- Time period
61- ) {
35+ Vision visionSubsystem ,
36+ Drivetrain drivetrain ,
37+ int targetDriveAprilTagId ,
38+ Time period ) {
6239 mTargetDriveAprilTagId = targetDriveAprilTagId ;
6340 kVisionSubsystem = visionSubsystem ;
6441 kDrivetrain = drivetrain ;
6542 kRobotPeriod = period ;
6643
44+ mConfig = kVisionSubsystem .getConfigCopy ();
45+
6746 addRequirements (kDrivetrain , kVisionSubsystem );
6847 }
6948
@@ -72,112 +51,135 @@ public void initialize() {
7251 // reset some vars
7352 mIsDoneMoving = false ;
7453 mIsDoneStafing = false ;
54+
7555 }
7656
7757 @ Override
7858 public void execute () {
79-
59+
60+ if (mTargetDriveAprilTagId == -1 )
61+ return ;
62+
8063 Vision .VisionTagData data = kVisionSubsystem .robotDriveToAprilTag (mTargetDriveAprilTagId , 0.0 );
8164
8265 double heading = kDrivetrain .getHeading ().in (Units .Degree );
83- double targetHeading = fmod (Vision .rotations [mTargetDriveAprilTagId - 1 ], 360.0 , 180.0 );
66+ double targetHeading = fmod (Vision .rotations [mTargetDriveAprilTagId - 1 ], 360.0 , 180.0 );
8467 double distanceToTargetHeading = fmod (targetHeading - heading , 360.0 , 180.0 );
8568
8669 mAngularVelocity = Units .RadiansPerSecond .zero ();
8770 mStafeVelocity = Units .MetersPerSecond .zero ();
8871 mForwardVelocity = Units .MetersPerSecond .zero ();
8972
90- //System.out.println("[drive progress] " + Boolean.toString(mIsDoneMoving) + ", " + Boolean.toString(mIsDoneStafing));
91-
92- // System.out.println( heading + "-> " + targetHeading + " [DISTANCE] " + distanceToTargetHeading + ", " + Boolean.toString(distanceToTargetHeading > Vision.TURNING_THERSHOLD));
93-
73+ // init teleop
74+
75+ kVisionSubsystem .setFilter (-1 );
76+
77+ // System.out.println("[drive progress] " + Boolean.toString(mIsDoneMoving) + ",
78+ // " + Boolean.toString(mIsDoneStafing));
79+
80+ // System.out.println( heading + "-> " + targetHeading + " [DISTANCE] " +
81+ // distanceToTargetHeading + ", " + Boolean.toString(distanceToTargetHeading >
82+ // Vision.TURNING_THERSHOLD));
83+
9484 // turn code
95- // detect if we are not done stafing and moving then try and rotate the robot to face the april tag
96- if (!(mIsDoneMoving && mIsDoneStafing ) && Math .abs (distanceToTargetHeading ) > TURNING_THERSHOLD ) {
85+ // detect if we are not done stafing and moving then try and rotate the robot to
86+ // face the april tag
87+ if (!(mIsDoneMoving && mIsDoneStafing ) && Math .abs (distanceToTargetHeading ) > mConfig .TURNING_THERSHOLD ) {
9788 if (distanceToTargetHeading > 0.0 ) {
98- mAngularVelocity = kTurnSpeed .times (distanceToTargetHeading * kDistanceMultiplierTurning ).plus (kMinMoveSpeedAngular );
99- }
100- else {
101- mAngularVelocity = kTurnSpeed .times (distanceToTargetHeading * kDistanceMultiplierTurning ).minus (kMinMoveSpeedAngular );
89+ mAngularVelocity = mConfig .kTurnSpeed .times (distanceToTargetHeading * mConfig .kDistanceMultiplierTurning )
90+ .plus (mConfig .kMinMoveSpeedAngular );
91+ } else {
92+ mAngularVelocity = mConfig .kTurnSpeed .times (distanceToTargetHeading * mConfig .kDistanceMultiplierTurning )
93+ .minus (mConfig .kMinMoveSpeedAngular );
10294 }
10395 }
10496
10597 SmartDashboard .putNumber ("target heading" , targetHeading );
10698 SmartDashboard .putNumber ("heading error" , distanceToTargetHeading );
107-
99+
108100 // see if we can see THE april tag
109101 if (kVisionSubsystem .isAprilTagDetected ()) {
110- //System.out.printf("[drive progress] fwd dist: %f, strf dist: %f\n", data.mDistanceToAprilTag, data.mTurningDistance);
102+ // System.out.printf("[drive progress] fwd dist: %f, strf dist: %f\n",
103+ // data.mDistanceToAprilTag, data.mTurningDistance);
111104 if (!(mIsDoneStafing && mIsDoneMoving )) {
112105
113106 SmartDashboard .putNumber ("Strafe Distance" , data .mTurningDistance );
114107 SmartDashboard .putNumber ("Forward Distance" , data .mDistanceToAprilTag );
115108
116109 // if we are not in the turning thershold
117- if (Math .abs (data .mTurningDistance ) > STAFE_THERSHOLD ) {
110+ if (Math .abs (data .mTurningDistance ) > mConfig . STAFE_THERSHOLD ) {
118111 if (data .mTurningDistance > 0.0 ) {
119- //System.out.println("Turn right");
120-
121- mStafeVelocity = kStrafeSpeed .times (Math .abs (data .mTurningDistance * kDistanceMultiplierStafing )).plus (kMinMoveSpeedLinear );
122-
112+ // System.out.println("Turn right");
113+
114+ mStafeVelocity = mConfig .kStrafeSpeed
115+ .times (Math .abs (data .mTurningDistance * mConfig .kDistanceMultiplierStafing ))
116+ .plus (mConfig .kMinMoveSpeedLinear );
117+
123118 } else {
124- //System.out.println("Turn left");
125-
126- mStafeVelocity = kStrafeSpeed .times (Math .abs (data .mTurningDistance * kDistanceMultiplierStafing )).plus (kMinMoveSpeedLinear ).negate ();
119+ // System.out.println("Turn left");
120+
121+ mStafeVelocity = mConfig .kStrafeSpeed
122+ .times (Math .abs (data .mTurningDistance * mConfig .kDistanceMultiplierStafing ))
123+ .plus (mConfig .kMinMoveSpeedLinear ).negate ();
127124
128125 }
129-
130- }
126+
127+ }
131128 // this means that we are in the STAFE_THERSHOLD so we can stop stafing
132129 else {
133130 mIsDoneStafing = true ;
134131 }
135-
136-
132+
137133 // see if we are in the STOP_MOVING_THERSHOLD if so then stop
138- if (Math .abs (0.0 - data .mDistanceToAprilTag ) > STOP_MOVING_THERSHOLD ) {
134+ if (Math .abs (0.0 - data .mDistanceToAprilTag ) > mConfig . STOP_MOVING_THERSHOLD ) {
139135 // detect if we are too far from the april tag if so then have forwards
140136 if (data .mDistanceToAprilTag > 0.0 ) {
141- //System.out.println("backwards");
142-
143- mForwardVelocity = kForwardSpeed .times (Math .abs (data .mDistanceToAprilTag * kDistanceMultiplierForwards )).plus (kMinMoveSpeedLinear ).negate ();
144-
145- }
146- else {
147- //System.out.println("forwards");
148-
149- mForwardVelocity = kForwardSpeed .times (Math .abs (data .mDistanceToAprilTag * kDistanceMultiplierForwards )).plus (kMinMoveSpeedLinear );
137+ // System.out.println("backwards");
138+
139+ mForwardVelocity = mConfig .kForwardSpeed
140+ .times (Math .abs (data .mDistanceToAprilTag * mConfig .kDistanceMultiplierForwards ))
141+ .plus (mConfig .kMinMoveSpeedLinear ).negate ();
142+
143+ } else {
144+ // System.out.println("forwards");
145+
146+ mForwardVelocity = mConfig .kForwardSpeed
147+ .times (Math .abs (data .mDistanceToAprilTag * mConfig .kDistanceMultiplierForwards ))
148+ .plus (mConfig .kMinMoveSpeedLinear );
150149 }
151- }
152- else {
150+ } else {
153151 mIsDoneMoving = true ;
154152 }
155- }
153+ }
156154 }
157-
158-
155+
159156 // clamp just in case
160-
161- mForwardVelocity = clamp (mForwardVelocity , kMaxLinearSpeed .negate (), kMaxLinearSpeed );
162- mStafeVelocity = clamp (mStafeVelocity , kMaxLinearSpeed .negate (), kMaxLinearSpeed );
163- mAngularVelocity = clamp (mAngularVelocity , kMaxAngularSpeed .negate (), kMaxAngularSpeed );
157+
158+ mForwardVelocity = clamp (mForwardVelocity , mConfig . kMaxLinearSpeed .negate (), mConfig . kMaxLinearSpeed );
159+ mStafeVelocity = clamp (mStafeVelocity , mConfig . kMaxLinearSpeed .negate (), mConfig . kMaxLinearSpeed );
160+ mAngularVelocity = clamp (mAngularVelocity , mConfig . kMaxAngularSpeed .negate (), mConfig . kMaxAngularSpeed );
164161
165162 // now drive
166-
167- kDrivetrain .Drive (
168- mForwardVelocity ,
169- mStafeVelocity ,
170- mAngularVelocity ,
171- false ,
172- kRobotPeriod
173- );
174163
164+ kDrivetrain .Drive (
165+ mForwardVelocity ,
166+ mStafeVelocity ,
167+ mAngularVelocity ,
168+ false ,
169+ kRobotPeriod );
175170
176171 }
177172
178173 @ Override
179174 public boolean isFinished () {
175+ if (mIsDoneMoving && !mIsDoneStafing ) {
176+ System .out .println ("stafing is holding us up" );
177+ } else if (mIsDoneStafing && !mIsDoneMoving ) {
178+ System .out .println ("moving is holding us up" );
179+ }
180+
180181 if ((mIsDoneStafing && mIsDoneMoving )) {
182+
181183 System .out .println ("Done" );
182184
183185 return true ;
@@ -188,32 +190,29 @@ public boolean isFinished() {
188190 @ Override
189191 public void end (boolean i ) {
190192 kDrivetrain .Drive (
191- Units .MetersPerSecond .zero (),
192- Units .MetersPerSecond .zero (),
193- Units .RadiansPerSecond .zero (),
194- false ,
195- kRobotPeriod
196- );
193+ Units .MetersPerSecond .zero (),
194+ Units .MetersPerSecond .zero (),
195+ Units .RadiansPerSecond .zero (),
196+ false ,
197+ kRobotPeriod );
197198 }
198199
199200 private static LinearVelocity clamp (LinearVelocity _value , LinearVelocity _min , LinearVelocity _max ) {
200201 double value = _value .in (MetersPerSecond );
201202 double min = _min .in (MetersPerSecond );
202203 double max = _max .in (MetersPerSecond );
203-
204+
204205 return MetersPerSecond .of (MathUtil .clamp (value , min , max ));
205206 }
206207
207208 private static AngularVelocity clamp (AngularVelocity _value , AngularVelocity _min , AngularVelocity _max ) {
208209 double value = _value .in (RadiansPerSecond );
209210 double min = _min .in (RadiansPerSecond );
210211 double max = _max .in (RadiansPerSecond );
211-
212+
212213 return RadiansPerSecond .of (MathUtil .clamp (value , min , max ));
213214 }
214215
215-
216-
217216 private static double fmod (double a , double b , double bias ) {
218217 double absA = a + bias ;
219218 double absB = absA % b ;
0 commit comments