Skip to content
This repository was archived by the owner on Sep 14, 2019. It is now read-only.

Commit 7102df4

Browse files
committed
modify changing and getting AutoUtils values
PIDTurn and PIDMove can now move to an absolute value and they also update the position and angle based on encoder and gyro values instead of the assumed values
1 parent 218e528 commit 7102df4

4 files changed

Lines changed: 71 additions & 72 deletions

File tree

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/AutoMove.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/AutoTurn.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDMove.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.usfirst.frc.team199.Robot2018.Robot;
44
import org.usfirst.frc.team199.Robot2018.SmartDashboardInterface;
5+
import org.usfirst.frc.team199.Robot2018.autonomous.AutoUtils;
56
import org.usfirst.frc.team199.Robot2018.subsystems.DrivetrainInterface;
67

78
import edu.wpi.first.wpilibj.PIDController;
@@ -32,6 +33,8 @@ public class PIDMove extends Command implements PIDOutput {
3233
* @param dt
3334
* the Drivetrain (for actual code) or a DrivetrainInterface (for
3435
* testing)
36+
* @param sd
37+
* the SmartDashboard
3538
* @param avg
3639
* the PIDSourceAverage of the DT's two Encoders
3740
*/
@@ -50,6 +53,42 @@ public PIDMove(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
5053
kf, avg, this);
5154
}
5255

56+
/**
57+
* Constructs this command with a new PIDController. Sets all of the
58+
* controller's PID constants based on SD prefs. Sets the controller's PIDSource
59+
* to the encoder average object and sets its PIDOutput to this command which
60+
* implements PIDOutput's pidWrite() method.
61+
*
62+
* @param point
63+
* the target point in inches, absolute distance from the starting
64+
* point
65+
* @param dt
66+
* the Drivetrain (for actual code) or a DrivetrainInterface (for
67+
* testing)
68+
* @param sd
69+
* the SmartDashboard
70+
* @param avg
71+
* the PIDSorceAverage of the DT's two Encoders
72+
*/
73+
public PIDMove(double[] point, DrivetrainInterface dt, SmartDashboardInterface sd, PIDSource avg) {
74+
double dx = point[0] - AutoUtils.position.getX();
75+
double dy = point[1] - AutoUtils.position.getY();
76+
77+
double dist = Math.sqrt(dx * dx + dy * dy); // pythagorean theorem to find distance
78+
79+
this.target = dist;
80+
this.dt = dt;
81+
this.avg = avg;
82+
if (Robot.dt != null) {
83+
requires(Robot.dt);
84+
}
85+
moveController = new PIDController(sd.getConst("MovekP", 1), sd.getConst("MovekI", 0), sd.getConst("MovekD", 0),
86+
avg, this);
87+
double kf = 1 / (dt.getCurrentMaxSpeed() * sd.getConst("Default PID Update Time", 0.05));
88+
moveController = new PIDController(sd.getConst("MovekP", 1), sd.getConst("MovekI", 0), sd.getConst("MovekD", 0),
89+
kf, avg, this);
90+
}
91+
5392
/**
5493
* Called just before this Command runs the first time. Resets the distance
5594
* encoders, sets the moveController's settings, and then enables it.
@@ -107,6 +146,13 @@ protected void end() {
107146
moveController.disable();
108147
System.out.println("End");
109148
// moveController.free();
149+
150+
double angle = Math.toRadians(AutoUtils.position.getRot());
151+
double dist = avg.pidGet();
152+
double x = Math.cos(angle) * dist;
153+
double y = Math.sin(angle) * dist;
154+
AutoUtils.position.changeX(x);
155+
AutoUtils.position.changeY(y);
110156
}
111157

112158
/**

Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDTurn.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.usfirst.frc.team199.Robot2018.Robot;
44
import org.usfirst.frc.team199.Robot2018.SmartDashboardInterface;
5+
import org.usfirst.frc.team199.Robot2018.autonomous.AutoUtils;
56
import org.usfirst.frc.team199.Robot2018.subsystems.DrivetrainInterface;
67

78
import edu.wpi.first.wpilibj.PIDController;
@@ -60,6 +61,28 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
6061
// tim = new Timer();
6162
}
6263

64+
public PIDTurn(double[] point, DrivetrainInterface dt, SmartDashboardInterface sd, PIDSource ahrs) {
65+
this.dt = dt;
66+
this.ahrs = ahrs;
67+
68+
double dx = point[0] - AutoUtils.position.getX();
69+
double dy = point[1] - AutoUtils.position.getY();
70+
71+
double absTurn = Math.toDegrees(Math.atan(dx / dy));
72+
target = absTurn - AutoUtils.position.getRot();
73+
74+
if (Robot.dt != null) {
75+
requires(Robot.dt);
76+
}
77+
// calculates the maximum turning speed in degrees/sec based on the max linear
78+
// speed in inches/s and the distance (inches) between sides of the DT
79+
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360 / (Math.PI * sd.getConst("Distance Between Wheels", 26.25));
80+
double kf = 1 / (maxTurnSpeed * sd.getConst("Default PID Update Time", 0.05));
81+
turnController = new PIDController(sd.getConst("TurnkP", 1), sd.getConst("TurnkI", 0), sd.getConst("TurnkD", 0),
82+
kf, ahrs, this);
83+
// tim = new Timer();
84+
}
85+
6386
/**
6487
* Called just before this Command runs the first time. Resets the gyro, sets
6588
* the turnController's settings, and then enables it.
@@ -137,6 +160,8 @@ protected void end() {
137160
SmartDashboard.putNumber("Turn PID Result", turnController.get());
138161
SmartDashboard.putNumber("Turn PID Error", turnController.getError());
139162
// turnController.free();
163+
164+
AutoUtils.position.changeRot(dt.getAHRSAngle());
140165
}
141166

142167
/**

0 commit comments

Comments
 (0)