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

Commit 2fd8b6e

Browse files
committed
Merge remote-tracking branch 'daremote/master'
2 parents febb807 + 73e6ebc commit 2fd8b6e

5 files changed

Lines changed: 156 additions & 64 deletions

File tree

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
3+
/* Open Source Software - may be modified and shared by FRC teams. The code */
4+
/* must be accompanied by the FIRST BSD license file in the root directory of */
5+
/* the project. */
6+
/*----------------------------------------------------------------------------*/
17

28
package org.usfirst.frc.team199.Robot2018;
39

@@ -41,22 +47,13 @@ public class Robot extends TimedRobot {
4147
public static Listener listen;
4248

4349
public static OI oi;
44-
50+
4551
public static Map<String, ArrayList<String[]>> autoScripts;
4652

4753
Command autonomousCommand;
4854
SendableChooser<Position> posChooser = new SendableChooser<Position>();
4955
Map<String, SendableChooser<Strategy>> stratChoosers = new HashMap<String, SendableChooser<Strategy>>();
50-
String[] fmsPossibilities = {"LL", "LR", "RL", "RR"};
51-
52-
public static SmartDashboardInterface sd = new SmartDashboardInterface() {
53-
public double getConst(String key, double def) {
54-
if (!SmartDashboard.containsKey("Const/" + key)) {
55-
SmartDashboard.putNumber("Const/" + key, def);
56-
}
57-
return SmartDashboard.getNumber("Const/" + key, def);
58-
}
59-
};
56+
String[] fmsPossibilities = { "LL", "LR", "RL", "RR" };
6057

6158
public static double getConst(String key, double def) {
6259
return sd.getConst(key, def);
@@ -82,13 +79,13 @@ public void robotInit() {
8279
lift = new Lift();
8380
dt = new Drivetrain();
8481
oi = new OI();
85-
82+
8683
// auto position chooser
8784
for (Position p : Position.values()) {
8885
posChooser.addObject(p.getSDName(), p);
8986
}
9087
SmartDashboard.putData("Starting Position", posChooser);
91-
88+
9289
// auto strategy choosers
9390
for (String input : fmsPossibilities) {
9491
SendableChooser<Strategy> chooser = new SendableChooser<Strategy>();
@@ -98,11 +95,12 @@ public void robotInit() {
9895
SmartDashboard.putData(input, chooser);
9996
stratChoosers.put(input, chooser);
10097
}
101-
98+
10299
// auto delay chooser
103100
SmartDashboard.putNumber("Auto Delay", 0);
104-
105-
// parse scripts from Preferences, which maintains values throughout reboots
101+
102+
// parse scripts from Preferences, which maintains values throughout
103+
// reboots
106104
autoScripts = AutoUtils.parseScriptFile(Preferences.getInstance().getString("autoscripts", ""));
107105

108106
listen = new Listener();
@@ -124,23 +122,23 @@ public void disabledPeriodic() {
124122
}
125123

126124
/**
127-
* This function is called once during the start of autonomous in order to
128-
* grab values from SmartDashboard and the FMS and call the Autonomous
129-
* command with those values.
125+
* This function is called once during the start of autonomous in order to grab
126+
* values from SmartDashboard and the FMS and call the Autonomous command with
127+
* those values.
130128
*/
131129
@Override
132130
public void autonomousInit() {
133131
String fmsInput = DriverStation.getInstance().getGameSpecificMessage();
134132
Position startPos = posChooser.getSelected();
135133
double autoDelay = SmartDashboard.getNumber("Auto Delay", 0);
136-
134+
137135
Map<String, Strategy> strategies = new HashMap<String, Strategy>();
138136
for (Map.Entry<String, SendableChooser<Strategy>> entry : stratChoosers.entrySet()) {
139-
String key = entry.getKey();
140-
SendableChooser<Strategy> chooser = entry.getValue();
141-
strategies.put(key, chooser.getSelected());
137+
String key = entry.getKey();
138+
SendableChooser<Strategy> chooser = entry.getValue();
139+
strategies.put(key, chooser.getSelected());
142140
}
143-
141+
144142
Autonomous auto = new Autonomous(startPos, strategies, autoDelay, fmsInput, false);
145143
auto.start();
146144
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,20 @@ public class RobotMap {
6868
* the TalonSRX to configure
6969
*/
7070
private void configSRX(WPI_TalonSRX mc) {
71+
// stuff cole said to put
7172
int kTimeout = (int) Robot.getConst("kTimeoutMs", 10);
7273
mc.configNominalOutputForward(0, kTimeout);
7374
mc.configNominalOutputReverse(0, kTimeout);
7475
mc.configPeakOutputForward(1, kTimeout);
7576
mc.configPeakOutputReverse(-1, kTimeout);
77+
78+
// current limiting stuff cole said to put
79+
mc.configPeakCurrentLimit(0, 0);
80+
mc.configPeakCurrentDuration(0, 0);
81+
// 40 amps is the amp limit of a CIM. also, the PDP has 40 amp circuit breakers,
82+
// so if we went above 40, the motors would stop completely.
83+
mc.configContinuousCurrentLimit(40, 0);
84+
mc.enableCurrentLimit(true);
7685
}
7786

7887
/**
@@ -83,6 +92,7 @@ private void configSRX(WPI_TalonSRX mc) {
8392
* the VictorSPX to configure
8493
*/
8594
private void configSPX(WPI_VictorSPX mc) {
95+
// stuff cole said to put
8696
int kTimeout = (int) Robot.getConst("kTimeoutMs", 10);
8797
mc.configNominalOutputForward(0, kTimeout);
8898
mc.configNominalOutputReverse(0, kTimeout);

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

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
11
package org.usfirst.frc.team199.Robot2018.commands;
22

33
import org.usfirst.frc.team199.Robot2018.Robot;
4-
import org.usfirst.frc.team199.Robot2018.SmartDashboardInterface;
54
import org.usfirst.frc.team199.Robot2018.autonomous.PIDSourceAverage;
65
import org.usfirst.frc.team199.Robot2018.subsystems.DrivetrainInterface;
76

87
import edu.wpi.first.wpilibj.PIDController;
9-
import edu.wpi.first.wpilibj.PIDSource;
108
import edu.wpi.first.wpilibj.PIDOutput;
119
import edu.wpi.first.wpilibj.command.Command;
1210

1311
/**
14-
*
12+
* Drives the robot a certain target distance using PID. Implements PIDOutput in
13+
* order to keep move PID centralized in this command.
1514
*/
1615
public class PIDMove extends Command implements PIDOutput {
1716

1817
private double target;
1918
private DrivetrainInterface dt;
2019
private PIDController moveController;
2120

22-
public PIDMove(double targ, DrivetrainInterface dt, SmartDashboardInterface sd, PIDSource avg) {
21+
/**
22+
* Constructs this command with a new PIDController. Sets all of the
23+
* controller's PID constants based on SD prefs. Sets the controller's PIDSource
24+
* to the encoder average object and sets its PIDOutput to this command which
25+
* implements PIDOutput's pidWrite() method.
26+
*
27+
* @param targ
28+
* the target distance (in inches) to move to
29+
* @param dt
30+
* the Drivetrain (for actual code) or a DrivetrainInterface (for
31+
* testing)
32+
* @param avg
33+
* the PIDSourceAverage of the DT's two Encoders
34+
*/
35+
public PIDMove(double targ, DrivetrainInterface dt, PIDSourceAverage avg) {
2336
// Use requires() here to declare subsystem dependencies
24-
// eg. requires(chassis);
37+
requires(Robot.dt);
2538
target = targ;
2639
this.dt = dt;
27-
if (Robot.dt != null) {
28-
requires(Robot.dt);
29-
}
30-
moveController = new PIDController(sd.getConst("MovekP", 1), sd.getConst("MovekI", 0),
31-
sd.getConst("MovekD", 0), avg, this);
40+
double kf = 1 / (dt.getCurrentMaxSpeed() * Robot.getConst("Default PID Update Time", 0.05));
41+
moveController = new PIDController(Robot.getConst("MovekP", 1), Robot.getConst("MovekI", 0),
42+
Robot.getConst("MovekD", 0), kf, avg, this);
3243
}
3344

34-
// Called just before this Command runs the first time
45+
/**
46+
* Called just before this Command runs the first time. Resets the distance
47+
* encoders, sets the moveController's settings, and then enables it.
48+
*/
3549
@Override
3650
public void initialize() {
3751
dt.resetDistEncs();
@@ -45,33 +59,57 @@ public void initialize() {
4559
moveController.enable();
4660
}
4761

48-
// Called repeatedly when this Command is scheduled to run
62+
/**
63+
* Called repeatedly when this Command is scheduled to run. This method is empty
64+
* bc the moveController runs on a different thread as soon as it is enabled in
65+
* initialize().
66+
*/
4967
@Override
5068
protected void execute() {
51-
// This method is empty bc the moveController runs on a different thread as soon
52-
// as it is enabled.
5369
}
5470

55-
// Make this return true when this Command no longer needs to run execute()
71+
/**
72+
* Tells this command to terminate when the moveController has reached its
73+
* target.
74+
*
75+
* @return true if the robot has moved the correct distance, false if not
76+
*/
5677
@Override
5778
protected boolean isFinished() {
5879
return moveController.onTarget();
5980
}
6081

61-
// Called once after isFinished returns true
82+
/**
83+
* Called once after isFinished returns true. Disables and frees the
84+
* moveController, essentially turning it "off" and "deleting" the thread it was
85+
* running on.
86+
*/
6287
@Override
6388
protected void end() {
6489
moveController.disable();
6590
moveController.free();
6691
}
6792

68-
// Called when another command which requires one or more of the same
69-
// subsystems is scheduled to run
93+
/**
94+
* Called when another command which requires one or more of the same subsystems
95+
* is scheduled to run. Disables and frees the moveController, essentially
96+
* turning it "off" and "deleting" the thread it was running on.
97+
*/
7098
@Override
7199
protected void interrupted() {
72100
end();
73101
}
74102

103+
/**
104+
* Implementation of PIDOutput's pidWrite method. Sends the moveController's
105+
* output speed to the motors as the drive/forward speed in DifferentialDrive's
106+
* arcadeDrive method.
107+
*
108+
* @param output
109+
* the output drive/forward speed [-1, 1], calculated by the move
110+
* PIDController, to pass in as the drive/forward speed in
111+
* arcadeDrive()
112+
*/
75113
@Override
76114
public void pidWrite(double output) {
77115
dt.arcadeDrive(output, 0);

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

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,57 @@
11
package org.usfirst.frc.team199.Robot2018.commands;
22

33
import org.usfirst.frc.team199.Robot2018.Robot;
4-
import org.usfirst.frc.team199.Robot2018.SmartDashboardInterface;
54
import org.usfirst.frc.team199.Robot2018.subsystems.DrivetrainInterface;
65

6+
import com.kauailabs.navx.frc.AHRS;
7+
78
import edu.wpi.first.wpilibj.PIDController;
8-
import edu.wpi.first.wpilibj.PIDSource;
99
import edu.wpi.first.wpilibj.PIDOutput;
1010
import edu.wpi.first.wpilibj.command.Command;
1111

1212
/**
13-
*
13+
* Turns the robot to a certain target bearing using PID. Implements PIDOutput
14+
* in order to keep turn PID centralized in this command.
1415
*/
1516
public class PIDTurn extends Command implements PIDOutput {
1617

17-
double target;
18-
DrivetrainInterface dt;
18+
private double target;
19+
private DrivetrainInterface dt;
1920
private PIDController turnController;
2021

21-
public PIDTurn(double targ, DrivetrainInterface dt, PIDSource ahrs, SmartDashboardInterface sd) {
22+
/**
23+
* Constructs this command with a new PIDController. Sets all of the
24+
* controller's PID constants based on SD prefs. Sets the controller's PIDSource
25+
* to the AHRS (gyro) object and sets its PIDOutput to this command which
26+
* implements PIDOutput's pidWrite() method.
27+
*
28+
* @param targ
29+
* the target bearing (in degrees) to turn to (so negative if turning
30+
* left, positive if turning right)
31+
* @param dt
32+
* the Drivetrain (for actual code) or a DrivetrainInterface (for
33+
* testing)
34+
* @param ahrs
35+
* the AHRS (gyro)
36+
*/
37+
public PIDTurn(double targ, DrivetrainInterface dt, AHRS ahrs) {
2238
// Use requires() here to declare subsystem dependencies
23-
// eg. requires(chassis);
39+
requires(Robot.dt);
2440
target = targ;
2541
this.dt = dt;
26-
if (Robot.dt != null) {
27-
requires(Robot.dt);
28-
}
29-
turnController = new PIDController(sd.getConst("TurnkP", 1), sd.getConst("TurnkI", 0),
30-
sd.getConst("TurnkD", 0), ahrs, this);
42+
// calculates the maximum turning speed in degrees/sec based on the max linear
43+
// speed in inches/s and the distance (inches) between sides of the DT
44+
double maxTurnSpeed = dt.getCurrentMaxSpeed() * 360
45+
/ (Math.PI * Robot.getConst("Distance Between Wheels", 26.25));
46+
double kf = 1 / (maxTurnSpeed * Robot.getConst("Default PID Update Time", 0.05));
47+
turnController = new PIDController(Robot.getConst("TurnkP", 1), Robot.getConst("TurnkI", 0),
48+
Robot.getConst("TurnkD", 0), kf, ahrs, this);
3149
}
3250

33-
// Called just before this Command runs the first time
51+
/**
52+
* Called just before this Command runs the first time. Resets the gyro, sets
53+
* the turnController's settings, and then enables it.
54+
*/
3455
@Override
3556
protected void initialize() {
3657
dt.resetAHRS();
@@ -45,33 +66,57 @@ protected void initialize() {
4566
turnController.enable();
4667
}
4768

48-
// Called repeatedly when this Command is scheduled to run
69+
/**
70+
* Called repeatedly when this Command is scheduled to run. This method is empty
71+
* bc the turnController runs on a different thread as soon as it is enabled in
72+
* initialize().
73+
*/
4974
@Override
5075
protected void execute() {
51-
// This method is empty bc the moveController runs on a different thread as soon
52-
// as it is enabled.
5376
}
5477

55-
// Make this return true when this Command no longer needs to run execute()
78+
/**
79+
* Tells this command to terminate when the turnController has reached its
80+
* target.
81+
*
82+
* @return true if the robot has turned to the correct target bearing, false if
83+
* not
84+
*/
5685
@Override
5786
protected boolean isFinished() {
5887
return turnController.onTarget();
5988
}
6089

61-
// Called once after isFinished returns true
90+
/**
91+
* Called once after isFinished returns true. Disables and frees the
92+
* turnController, essentially turning it "off" and "deleting" the thread it was
93+
* running on.
94+
*/
6295
@Override
6396
protected void end() {
6497
turnController.disable();
6598
turnController.free();
6699
}
67100

68-
// Called when another command which requires one or more of the same
69-
// subsystems is scheduled to run
101+
/**
102+
* Called when another command which requires one or more of the same subsystems
103+
* is scheduled to run. Disables and frees the turnController, essentially
104+
* turning it "off" and "deleting" the thread it was running on.
105+
*/
70106
@Override
71107
protected void interrupted() {
72108
end();
73109
}
74110

111+
/**
112+
* Implementation of PIDOutput's pidWrite method. Sends the turnController's
113+
* output speed to the motors as the turn speed in DifferentialDrive's
114+
* arcadeDrive method.
115+
*
116+
* @param output
117+
* the output turn speed [-1, 1], calculated by the turn
118+
* PIDController, to pass in as the turn speed in arcadeDrive()
119+
*/
75120
@Override
76121
public void pidWrite(double output) {
77122
dt.arcadeDrive(0, output);

0 commit comments

Comments
 (0)