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

Commit 3ca73f3

Browse files
Merge branch 'master' of
https://github.com/DeepBlueRobotics/RobotCode2018.git Conflicts: Robot2018/lib/User_Libraries.properties Robot2018/src/org/usfirst/frc/team199/Robot2018/autonomous/AutoUtils.java Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/AutoMoveTo.java Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/DefaultIntake.java Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDMove.java Robot2018/src/org/usfirst/frc/team199/Robot2018/commands/PIDTurn.java
2 parents da6d0f0 + 64b5912 commit 3ca73f3

13 files changed

Lines changed: 352 additions & 60 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#Sun Feb 18 17:40:43 PST 2018
1+
#Tue Feb 20 09:44:13 PST 2018
22
C\:\\Users\\dean\\git\\RobotCode2018\\Robot2018\\lib\\libpathfinderjava.so=4087ee38c338446b8305f67716260df4

Robot2018/src/org/usfirst/frc/team199/Robot2018/OI.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
* interface to the commands and command groups that allow control of the robot.
2727
*/
2828
public class OI {
29+
/*
30+
* WHENEVER YOU ADD OR CHANGE WHAT A BUTTON OR JOYSTICK DOES, indicate in
31+
* /docs/controllers.txt to keep that reference up to date.
32+
*/
2933

3034
public Joystick leftJoy;
3135
private JoystickButton shiftLowGearButton;
@@ -86,7 +90,7 @@ public OI() {
8690
MoveLiftUpButton.whileHeld(new RunLift(Robot.lift, true));
8791
MoveLiftDownButton.whileHeld(new RunLift(Robot.lift, false));
8892

89-
// manipulator = new Joystick(2);
93+
manipulator = new Joystick(2);
9094
// closeIntake = new JoystickButton(manipulator, getButton("Close Intake
9195
// Button", 1));
9296
// closeIntake.whenPressed(new CloseIntake());

Robot2018/src/org/usfirst/frc/team199/Robot2018/Robot.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import java.util.Map;
1313

1414
import org.usfirst.frc.team199.Robot2018.autonomous.AutoUtils;
15+
import org.usfirst.frc.team199.Robot2018.autonomous.Position;
1516
import org.usfirst.frc.team199.Robot2018.commands.Autonomous;
16-
import org.usfirst.frc.team199.Robot2018.commands.Autonomous.Position;
1717
import org.usfirst.frc.team199.Robot2018.commands.Autonomous.Strategy;
1818
import org.usfirst.frc.team199.Robot2018.commands.ShiftLowGear;
1919
import org.usfirst.frc.team199.Robot2018.subsystems.Climber;
@@ -22,7 +22,6 @@
2222
import org.usfirst.frc.team199.Robot2018.subsystems.IntakeEject;
2323
import org.usfirst.frc.team199.Robot2018.subsystems.Lift;
2424

25-
import edu.wpi.first.wpilibj.CameraServer;
2625
import edu.wpi.first.wpilibj.DriverStation;
2726
import edu.wpi.first.wpilibj.IterativeRobot;
2827
import edu.wpi.first.wpilibj.PIDController;
@@ -54,7 +53,7 @@ public class Robot extends IterativeRobot {
5453
public static Map<String, ArrayList<String[]>> autoScripts;
5554

5655
Command autonomousCommand;
57-
SendableChooser<Position> posChooser = new SendableChooser<Position>();
56+
SendableChooser<Autonomous.Position> posChooser = new SendableChooser<Autonomous.Position>();
5857
Map<String, SendableChooser<Strategy>> stratChoosers = new HashMap<String, SendableChooser<Strategy>>();
5958
String[] fmsPossibilities = { "LL", "LR", "RL", "RR" };
6059

@@ -118,7 +117,7 @@ public void robotInit() {
118117
oi = new OI();
119118

120119
// auto position chooser
121-
for (Position p : Position.values()) {
120+
for (Autonomous.Position p : Autonomous.Position.values()) {
122121
posChooser.addObject(p.getSDName(), p);
123122
}
124123
SmartDashboard.putData("Starting Position", posChooser);
@@ -140,8 +139,8 @@ public void robotInit() {
140139
autoScripts = AutoUtils.parseScriptFile(Preferences.getInstance().getString("autoscripts", ""));
141140

142141
listen = new Listener();
143-
CameraServer.getInstance().startAutomaticCapture(0);
144-
CameraServer.getInstance().startAutomaticCapture(1);
142+
// CameraServer.getInstance().startAutomaticCapture(0);
143+
// CameraServer.getInstance().startAutomaticCapture(1);
145144
}
146145

147146
/**
@@ -166,9 +165,11 @@ public void disabledPeriodic() {
166165
*/
167166
@Override
168167
public void autonomousInit() {
168+
dt.resetAHRS();
169+
AutoUtils.position = new Position(0, 0, 0);
169170
Scheduler.getInstance().add(new ShiftLowGear());
170171
String fmsInput = DriverStation.getInstance().getGameSpecificMessage();
171-
Position startPos = posChooser.getSelected();
172+
Autonomous.Position startPos = posChooser.getSelected();
172173
double autoDelay = SmartDashboard.getNumber("Auto Delay", 0);
173174

174175
Map<String, Strategy> strategies = new HashMap<String, Strategy>();

Robot2018/src/org/usfirst/frc/team199/Robot2018/autonomous/AutoUtils.java

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

77
public class AutoUtils {
88

9-
public static Position position = new Position(0, 0, 0);
9+
public static Position position;
1010

1111
/**
1212
* Parses the inputted script file into a map of scripts
@@ -25,6 +25,7 @@ public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile
2525
String currScriptName = "";
2626

2727
int count = 0;
28+
int errorCount = 0;
2829
for (String line : lines) {
2930
count++;
3031
// remove comments
@@ -75,6 +76,8 @@ public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile
7576
if (isValidCommand(instruction, args, count)) {
7677
String[] command = { instruction, args };
7778
currScript.add(command);
79+
} else {
80+
errorCount++;
7881
}
7982
}
8083

@@ -86,6 +89,8 @@ public static Map<String, ArrayList<String[]>> parseScriptFile(String scriptFile
8689
// remove the stray one in the beginning
8790
autoScripts.remove("");
8891

92+
System.out.println("[INFO] Successfuly parsed " + count + " lines with " + errorCount + " errors.");
93+
8994
return autoScripts;
9095
}
9196

@@ -244,7 +249,7 @@ public static double[] parsePoint(String cmdArgs) {
244249
double[] point = new double[2];
245250
String parentheseless;
246251
String[] pointparts;
247-
if (AutoUtils.isPoint(cmdArgs)) {
252+
if (isPoint(cmdArgs)) {
248253
parentheseless = cmdArgs.substring(1, cmdArgs.length() - 1);
249254
pointparts = parentheseless.split(",");
250255
try {

Robot2018/src/org/usfirst/frc/team199/Robot2018/autonomous/scriptupload/scriptupload.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nt setup
22
from networktables import NetworkTables
33
import __future__
4+
import time
45
NetworkTables.initialize(server='10.1.99.2')
56
prefs = NetworkTables.getTable("Preferences")
67

@@ -30,6 +31,8 @@
3031
prefs.putString("autoscripts", oneline)
3132
print("Uploading %s as a String[] to key \"autoscripts\"" % filename)
3233

34+
time.sleep(0.1)
35+
3336
#checks if the key has been filled
3437
tester = "" #a variable to check if autoscripts is None
3538

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717
*/
1818
public class AutoMoveTo extends CommandGroup {
1919

20-
public AutoMoveTo(String[] args, DrivetrainInterface dt, SmartDashboardInterface sd, PIDSource pidMoveSrc) {
20+
public AutoMoveTo(String[] args, DrivetrainInterface dt, SmartDashboardInterface sd, PIDSource pidMoveSrc,
21+
PIDSource pidTurnSource) {
2122
// requires(Drivetrain);
2223
double rotation;
2324
double[] point = { 0, 0 };
2425
for (String arg : args) {
2526
if (AutoUtils.isDouble(arg)) {
2627
rotation = Double.valueOf(arg);
27-
addSequential(new PIDTurn(rotation, dt, sd, pidMoveSrc, true));
28+
addSequential(new PIDTurn(rotation, dt, sd, pidTurnSource, true));
2829
} else if (AutoUtils.isPoint(arg)) {
2930
point = AutoUtils.parsePoint(arg);
30-
addSequential(new PIDTurn(point, dt, sd, pidMoveSrc));
31+
addSequential(new PIDTurn(point, dt, sd, pidTurnSource));
3132
addSequential(new PIDMove(point, dt, sd, pidMoveSrc));
3233
} else {
3334
throw new IllegalArgumentException();
@@ -36,6 +37,6 @@ public AutoMoveTo(String[] args, DrivetrainInterface dt, SmartDashboardInterface
3637
}
3738

3839
public AutoMoveTo(String[] args) {
39-
this(args, Robot.dt, Robot.sd, Robot.dt.getDistEncAvg());
40+
this(args, Robot.dt, Robot.sd, Robot.dt.getDistEncAvg(), Robot.dt.getGyro());
4041
}
4142
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
public class DefaultIntake extends Command {
1111

12+
private boolean manipulatorPluggedIn = true;
13+
1214
public DefaultIntake() {
1315
// Use requires() here to declare subsystem dependencies
1416
// eg. requires(chassis);
@@ -17,13 +19,21 @@ public DefaultIntake() {
1719

1820
// Called just before this Command runs the first time
1921
protected void initialize() {
22+
try {
23+
Robot.oi.manipulator.getRawAxis(1);
24+
} catch (NullPointerException e) {
25+
System.err.println("[ERROR] Manipulator not plugged in.");
26+
manipulatorPluggedIn = false;
27+
}
2028
}
2129

2230
// Called repeatedly when this Command is scheduled to run
2331
protected void execute() {
2432
// 1 and 5 represent the axes' index in driver station
25-
Robot.intakeEject.runLeftIntake(Robot.oi.manipulator.getRawAxis(1));
26-
Robot.intakeEject.runRightIntake(Robot.oi.manipulator.getRawAxis(5));
33+
if (manipulatorPluggedIn) {
34+
Robot.intakeEject.runLeftIntake(Robot.oi.manipulator.getRawAxis(1));
35+
Robot.intakeEject.runRightIntake(Robot.oi.manipulator.getRawAxis(5));
36+
}
2737
}
2838

2939
// Make this return true when this Command no longer needs to run execute()

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class PIDMove extends Command implements PIDOutput {
2121
private PIDController moveController;
2222
private PIDSource avg;
2323
private SmartDashboardInterface sd;
24+
private double pointX;
25+
private double pointY;
2426

2527
/**
2628
* Constructs this command with a new PIDController. Sets all of the
@@ -71,12 +73,9 @@ public PIDMove(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
7173
* the PIDSorceAverage of the DT's two Encoders
7274
*/
7375
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+
pointX = point[0];
77+
pointY = point[1];
7678

77-
double dist = Math.sqrt(dx * dx + dy * dy); // pythagorean theorem to find distance
78-
79-
this.target = dist;
8079
this.dt = dt;
8180
this.avg = avg;
8281
this.sd = sd;
@@ -86,6 +85,7 @@ public PIDMove(double[] point, DrivetrainInterface dt, SmartDashboardInterface s
8685
double kf = 1 / (dt.getCurrentMaxSpeed() * sd.getConst("Default PID Update Time", 0.05));
8786
moveController = new PIDController(sd.getConst("MovekP", 1), sd.getConst("MovekI", 0), sd.getConst("MovekD", 0),
8887
kf, avg, this);
88+
sd.putData("Move PID", moveController);
8989
}
9090

9191
/**
@@ -94,6 +94,12 @@ public PIDMove(double[] point, DrivetrainInterface dt, SmartDashboardInterface s
9494
*/
9595
@Override
9696
public void initialize() {
97+
double dx = pointX - AutoUtils.position.getX();
98+
double dy = pointY - AutoUtils.position.getY();
99+
100+
double dist = Math.sqrt(dx * dx + dy * dy); // pythagorean theorem to find distance
101+
this.target = dist;
102+
97103
dt.resetDistEncs();
98104
moveController.disable();
99105
// input is in inches
@@ -102,6 +108,7 @@ public void initialize() {
102108
moveController.setOutputRange(-1.0, 1.0);
103109
moveController.setContinuous(false);
104110
moveController.setAbsoluteTolerance(Robot.getConst("MoveTolerance", 0.1));
111+
System.out.println("move target = " + target);
105112
moveController.setSetpoint(target);
106113

107114
moveController.enable();
@@ -146,10 +153,13 @@ protected void end() {
146153

147154
double angle = Math.toRadians(AutoUtils.position.getRot());
148155
double dist = avg.pidGet();
149-
double x = Math.cos(angle) * dist;
150-
double y = Math.sin(angle) * dist;
156+
// x and y are switched because we are using bearings
157+
double y = Math.cos(angle) * dist;
158+
double x = Math.sin(angle) * dist;
151159
AutoUtils.position.changeX(x);
152160
AutoUtils.position.changeY(y);
161+
162+
AutoUtils.position.setRot(dt.getAHRSAngle());
153163
}
154164

155165
/**

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ 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;
30+
private boolean turnToPoint;
31+
private boolean absoluteRotation;
2832

2933
/**
3034
* Constructs this command with a new PIDController. Sets all of the
@@ -52,11 +56,9 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
5256
this.ahrs = ahrs;
5357
this.sd = sd;
5458

55-
if (absolute) {
56-
target = targ - AutoUtils.position.getRot();
57-
} else {
58-
target = targ;
59-
}
59+
turnToPoint = false;
60+
target = targ;
61+
absoluteRotation = absolute;
6062

6163
if (Robot.dt != null) {
6264
requires(Robot.dt);
@@ -113,12 +115,12 @@ public PIDTurn(double targ, DrivetrainInterface dt, SmartDashboardInterface sd,
113115
public PIDTurn(double[] point, DrivetrainInterface dt, SmartDashboardInterface sd, PIDSource ahrs) {
114116
this.dt = dt;
115117
this.ahrs = ahrs;
118+
this.sd = sd;
116119

117-
double dx = point[0] - AutoUtils.position.getX();
118-
double dy = point[1] - AutoUtils.position.getY();
120+
pointX = point[0];
121+
pointY = point[1];
119122

120-
double absTurn = Math.toDegrees(Math.atan2(dy, dx));
121-
target = absTurn - AutoUtils.position.getRot();
123+
turnToPoint = true;
122124

123125
if (Robot.dt != null) {
124126
requires(Robot.dt);
@@ -139,6 +141,26 @@ public PIDTurn(double[] point, DrivetrainInterface dt, SmartDashboardInterface s
139141
*/
140142
@Override
141143
protected void initialize() {
144+
145+
if (!turnToPoint) {
146+
if (absoluteRotation) {
147+
target -= AutoUtils.position.getRot();
148+
}
149+
} else {
150+
double dx = pointX - AutoUtils.position.getX();
151+
double dy = pointY - AutoUtils.position.getY();
152+
153+
System.out.println("x = " + dx + ", y = " + dy);
154+
155+
// x and y are switched because we are using bearings
156+
double absTurn = Math.toDegrees(Math.atan2(dx, dy));
157+
target = absTurn - AutoUtils.position.getRot();
158+
System.out.println("current bearing = " + AutoUtils.position.getRot());
159+
System.out.println("target bearing = " + target);
160+
}
161+
162+
System.out.println("Turn to point: " + turnToPoint);
163+
142164
turnController.disable();
143165
// dt.enableVelocityPIDs();
144166
System.out.println("initialize2s");
@@ -155,6 +177,7 @@ protected void initialize() {
155177
while (Math.abs(newSetPoint) > 180) {
156178
newSetPoint = newSetPoint - Math.signum(newSetPoint) * 360;
157179
}
180+
System.out.println("set point = " + newSetPoint);
158181
turnController.setSetpoint(newSetPoint);
159182

160183
turnController.enable();
@@ -209,7 +232,7 @@ protected void end() {
209232
sd.putNumber("Turn PID Error", turnController.getError());
210233
// turnController.free();
211234

212-
AutoUtils.position.changeRot(dt.getAHRSAngle());
235+
AutoUtils.position.setRot(dt.getAHRSAngle());
213236
}
214237

215238
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
public class RunScript extends CommandGroup {
1515

1616
public RunScript(String scriptName) {
17-
ArrayList<String[]> script = Robot.autoScripts.getOrDefault(scriptName, new ArrayList<String[]>());
17+
ArrayList<String[]> script;
18+
if (Robot.autoScripts.containsKey(scriptName)) {
19+
script = Robot.autoScripts.get(scriptName);
20+
} else {
21+
System.err.println("[ERROR] auto scripts file does not contain script `" + scriptName + "`.");
22+
return;
23+
}
1824

1925
outerloop: for (String[] cmd : script) {
2026
String cmdName = cmd[0];

0 commit comments

Comments
 (0)