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

Commit 532bc10

Browse files
authored
Merge pull request #26 from doawelul/testing-no-auto
changed slave controllers to VictorSPX, added Listener for NetworkTable constants --> needs testing, buttons for PID testing --> need to be removed once PIDget gets up and running
2 parents 52b441e + bb2069d commit 532bc10

9 files changed

Lines changed: 325 additions & 58 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.usfirst.frc.team199.Robot2018;
2+
3+
import org.usfirst.frc.team199.Robot2018.commands.SetDistancePerPulse;
4+
import org.usfirst.frc.team199.Robot2018.commands.UpdatePIDConstants;
5+
6+
import edu.wpi.first.networktables.EntryListenerFlags;
7+
import edu.wpi.first.networktables.NetworkTable;
8+
import edu.wpi.first.networktables.NetworkTableEntry;
9+
import edu.wpi.first.networktables.NetworkTableValue;
10+
import edu.wpi.first.networktables.TableEntryListener;
11+
import edu.wpi.first.wpilibj.command.Command;
12+
import edu.wpi.first.wpilibj.command.Scheduler;
13+
14+
public class Listener implements TableEntryListener {
15+
16+
private void addCommand(Command com) {
17+
Scheduler.getInstance().add(com);
18+
}
19+
20+
@Override
21+
public void valueChanged(NetworkTable arg0, String arg1, NetworkTableEntry arg2, NetworkTableValue arg3, int arg4) {
22+
if(arg4 == EntryListenerFlags.kNew) return;
23+
switch (arg1) {
24+
case "Const/Distance Per Pulse Left":
25+
addCommand(new SetDistancePerPulse());
26+
break;
27+
case "Const/Distance Per Pulse Right":
28+
addCommand(new SetDistancePerPulse());
29+
break;
30+
case "Const/TurnkP":
31+
addCommand(new UpdatePIDConstants());
32+
break;
33+
case "Const/TurnkI":
34+
addCommand(new UpdatePIDConstants());
35+
break;
36+
case "Const/TurnkD":
37+
addCommand(new UpdatePIDConstants());
38+
break;
39+
case "Const/MovekP":
40+
addCommand(new UpdatePIDConstants());
41+
break;
42+
case "Const/MovekI":
43+
addCommand(new UpdatePIDConstants());
44+
break;
45+
case "Const/MovekD":
46+
addCommand(new UpdatePIDConstants());
47+
break;
48+
49+
}
50+
}
51+
52+
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77

88
package org.usfirst.frc.team199.Robot2018;
99

10-
import org.usfirst.frc.team199.Robot2018.commands.*;
10+
import org.usfirst.frc.team199.Robot2018.commands.PIDMove;
11+
import org.usfirst.frc.team199.Robot2018.commands.PIDTurn;
12+
import org.usfirst.frc.team199.Robot2018.commands.SetDistancePerPulse;
13+
import org.usfirst.frc.team199.Robot2018.commands.ShiftDriveType;
14+
import org.usfirst.frc.team199.Robot2018.commands.ShiftHighGear;
15+
import org.usfirst.frc.team199.Robot2018.commands.ShiftLowGear;
16+
import org.usfirst.frc.team199.Robot2018.commands.UpdatePIDConstants;
1117

1218
import edu.wpi.first.wpilibj.Joystick;
1319
import edu.wpi.first.wpilibj.buttons.JoystickButton;
@@ -50,6 +56,8 @@ public class OI {
5056
private JoystickButton shiftLowGear;
5157
private JoystickButton shiftHighGear;
5258
private JoystickButton shiftDriveType;
59+
private JoystickButton PIDMove;
60+
private JoystickButton PIDTurn;
5361
public Joystick rightJoy;
5462
private JoystickButton updatePidConstants;
5563
private JoystickButton updateEncoderDPP;
@@ -70,13 +78,17 @@ public OI() {
7078
shiftHighGear.whenPressed(new ShiftHighGear());
7179
shiftDriveType = new JoystickButton(leftJoy, getButton("Shift Drive Type", 2));
7280
shiftDriveType.whenPressed(new ShiftDriveType());
73-
81+
PIDMove = new JoystickButton(leftJoy, getButton("PID Move", 7));
82+
PIDMove.whenPressed(new PIDMove(40, Robot.dt));
83+
PIDTurn = new JoystickButton(leftJoy, getButton("PID Turn", 8));
84+
PIDTurn.whenPressed(new PIDTurn(30, Robot.dt));
85+
7486
rightJoy = new Joystick(1);
7587
updatePidConstants = new JoystickButton(rightJoy, getButton("Get PID Constants", 8));
7688
updatePidConstants.whenPressed(new UpdatePIDConstants());
7789
updateEncoderDPP = new JoystickButton(rightJoy, getButton("Get Encoder Dist Per Pulse", 9));
7890
updateEncoderDPP.whenPressed(new SetDistancePerPulse());
79-
91+
8092
manipulator = new Joystick(2);
8193
}
8294
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class Robot extends IterativeRobot {
2828
public static final Lift lift = new Lift();
2929
public static RobotMap rmap;
3030
public static Drivetrain dt;
31+
public static Listener listen;
3132

3233
public static OI oi;
3334

@@ -57,6 +58,7 @@ public void robotInit() {
5758
rmap = new RobotMap();
5859
dt = new Drivetrain();
5960
oi = new OI();
61+
listen = new Listener();
6062
// chooser.addObject("My Auto", new MyAutoCommand());
6163
SmartDashboard.putData("Auto mode", chooser);
6264
}

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.usfirst.frc.team199.Robot2018;
99

1010
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
11+
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
1112
import com.kauailabs.navx.frc.AHRS;
1213

1314
import edu.wpi.first.wpilibj.AnalogGyro;
@@ -38,12 +39,12 @@ public class RobotMap {
3839

3940
public static Encoder leftEnc;
4041
public static WPI_TalonSRX dtLeftDrive;
41-
public static WPI_TalonSRX dtLeftSlave;
42+
public static WPI_VictorSPX dtLeftSlave;
4243
public static SpeedControllerGroup dtLeft;
4344

4445
public static Encoder rightEnc;
4546
public static WPI_TalonSRX dtRightDrive;
46-
public static WPI_TalonSRX dtRightSlave;
47+
public static WPI_VictorSPX dtRightSlave;
4748
public static SpeedControllerGroup dtRight;
4849
public static DifferentialDrive robotDrive;
4950
public static PIDController turnController;
@@ -69,21 +70,36 @@ private void configSRX(WPI_TalonSRX mc) {
6970
mc.configPeakOutputForward(1, kTimeout);
7071
mc.configPeakOutputReverse(-1, kTimeout);
7172
}
73+
74+
/**
75+
* This function takes in a VictorSPX motorController and sets nominal and peak
76+
* outputs to the default
77+
*
78+
* @param mc
79+
* the VictorSPX to configure
80+
*/
81+
private void configSPX(WPI_VictorSPX mc) {
82+
int kTimeout = (int) Robot.getConst("kTimeoutMs", 10);
83+
mc.configNominalOutputForward(0, kTimeout);
84+
mc.configNominalOutputReverse(0, kTimeout);
85+
mc.configPeakOutputForward(1, kTimeout);
86+
mc.configPeakOutputReverse(-1, kTimeout);
87+
}
7288

7389
public RobotMap() {
7490

7591
leftEnc = new Encoder(getPort("1LeftEnc", 0), getPort("2LeftEnc", 1));
7692
dtLeftDrive = new WPI_TalonSRX(getPort("LeftTalonSRXDrive", 0));
7793
configSRX(dtLeftDrive);
78-
dtLeftSlave = new WPI_TalonSRX(getPort("LeftTalonSRXSlave", 1));
79-
configSRX(dtLeftSlave);
94+
dtLeftSlave = new WPI_VictorSPX(getPort("LeftTalonSPXSlave", 1));
95+
configSPX(dtLeftSlave);
8096
dtLeft = new SpeedControllerGroup(dtLeftDrive, dtLeftSlave);
8197

8298
rightEnc = new Encoder(getPort("1RightEnc", 2), getPort("2RightEnc", 3));
8399
dtRightDrive = new WPI_TalonSRX(getPort("RightTalonSRXDrive", 2));
84100
configSRX(dtRightDrive);
85-
dtRightSlave = new WPI_TalonSRX(getPort("RightTalonSRXSlave", 3));
86-
configSRX(dtRightSlave);
101+
dtRightSlave = new WPI_VictorSPX(getPort("RightTalonSPXSlave", 3));
102+
configSPX(dtRightSlave);
87103
dtRight = new SpeedControllerGroup(dtRightDrive, dtRightSlave);
88104

89105
robotDrive = new DifferentialDrive(dtLeft, dtRight);

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.usfirst.frc.team199.Robot2018.commands;
22

33
import org.usfirst.frc.team199.Robot2018.Robot;
4+
import org.usfirst.frc.team199.Robot2018.subsystems.DrivetrainInterface;
45

56
import edu.wpi.first.wpilibj.command.Command;
67

@@ -10,35 +11,37 @@
1011
public class PIDMove extends Command {
1112

1213
double target;
14+
DrivetrainInterface dt;
1315

14-
public PIDMove(double targ) {
16+
public PIDMove(double targ, DrivetrainInterface dt) {
1517
// Use requires() here to declare subsystem dependencies
1618
// eg. requires(chassis);
1719
target = targ;
20+
this.dt = dt;
1821
requires(Robot.dt);
1922
}
2023

2124
// Called just before this Command runs the first time
2225
protected void initialize() {
23-
Robot.dt.resetEnc();
24-
Robot.dt.enableMovePid();
25-
Robot.dt.setMoveSetpoint(target);
26+
dt.resetEnc();
27+
dt.enableMovePid();
28+
dt.setMoveSetpoint(target);
2629
}
2730

2831
// Called repeatedly when this Command is scheduled to run
2932
protected void execute() {
30-
Robot.dt.arcadeDrive(Robot.dt.getPidOut(), 0);
33+
dt.arcadeDrive(dt.getPidOut(), 0);
3134
}
3235

3336
// Make this return true when this Command no longer needs to run execute()
3437
protected boolean isFinished() {
35-
return Robot.dt.onDriveTarg();
38+
return dt.onDriveTarg();
3639
}
3740

3841
// Called once after isFinished returns true
3942
protected void end() {
40-
Robot.dt.disableMovePid();
41-
Robot.dt.stopDrive();
43+
dt.disableMovePid();
44+
dt.stopDrive();
4245
}
4346

4447
// Called when another command which requires one or more of the same

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.usfirst.frc.team199.Robot2018.commands;
22

33
import org.usfirst.frc.team199.Robot2018.Robot;
4+
import org.usfirst.frc.team199.Robot2018.subsystems.DrivetrainInterface;
45

56
import edu.wpi.first.wpilibj.command.Command;
67

@@ -10,35 +11,37 @@
1011
public class PIDTurn extends Command {
1112

1213
double target;
14+
DrivetrainInterface dt;
1315

14-
public PIDTurn(double targ) {
16+
public PIDTurn(double targ, DrivetrainInterface dt) {
1517
// Use requires() here to declare subsystem dependencies
1618
// eg. requires(chassis);
1719
target = targ;
20+
this.dt = dt;
1821
requires(Robot.dt);
1922
}
2023

2124
// Called just before this Command runs the first time
2225
protected void initialize() {
23-
Robot.dt.resetAHRS();
24-
Robot.dt.setTurnSetpoint(target);
25-
Robot.dt.enableTurnPid();
26+
dt.resetAHRS();
27+
dt.setTurnSetpoint(target);
28+
dt.enableTurnPid();
2629
}
2730

2831
// Called repeatedly when this Command is scheduled to run
2932
protected void execute() {
30-
Robot.dt.arcadeDrive(0, Robot.dt.getPidOut());
33+
dt.arcadeDrive(0, dt.getPidOut());
3134
}
3235

3336
// Make this return true when this Command no longer needs to run execute()
3437
protected boolean isFinished() {
35-
return Robot.dt.onTurnTarg();
38+
return dt.onTurnTarg();
3639
}
3740

3841
// Called once after isFinished returns true
3942
protected void end() {
40-
Robot.dt.disableTurnPid();
41-
Robot.dt.stopDrive();
43+
dt.disableTurnPid();
44+
dt.stopDrive();
4245
}
4346

4447
// Called when another command which requires one or more of the same

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

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,37 @@
1111
public class SetDistancePerPulse extends Command {
1212

1313
Timer tim;
14-
public SetDistancePerPulse() {
15-
// Use requires() here to declare subsystem dependencies
16-
// eg. requires(chassis);
17-
}
18-
19-
// Called just before this Command runs the first time
20-
protected void initialize() {
21-
tim = new Timer();
22-
tim.reset();
23-
tim.start();
24-
}
25-
26-
// Called repeatedly when this Command is scheduled to run
27-
protected void execute() {
28-
Robot.dt.setDistancePerPulseLeft(Robot.getConst("Distance Per Pulse Left", 0.184));
29-
Robot.dt.setDistancePerPulseRight(Robot.getConst("Distance Per Pulse Right", 0.184));
30-
}
31-
32-
// Make this return true when this Command no longer needs to run execute()
33-
protected boolean isFinished() {
34-
return tim.get() > Robot.getConst("Update Constants Time", 0.1);
35-
}
36-
37-
// Called once after isFinished returns true
38-
protected void end() {
39-
}
40-
41-
// Called when another command which requires one or more of the same
42-
// subsystems is scheduled to run
43-
protected void interrupted() {
44-
end();
45-
}
14+
15+
public SetDistancePerPulse() {
16+
// Use requires() here to declare subsystem dependencies
17+
// eg. requires(chassis);
18+
}
19+
20+
// Called just before this Command runs the first time
21+
protected void initialize() {
22+
tim = new Timer();
23+
tim.reset();
24+
tim.start();
25+
}
26+
27+
// Called repeatedly when this Command is scheduled to run
28+
protected void execute() {
29+
Robot.dt.setDistancePerPulseLeft(Robot.getConst("Distance Per Pulse Left", 0.184));
30+
Robot.dt.setDistancePerPulseRight(Robot.getConst("Distance Per Pulse Right", 0.184));
31+
}
32+
33+
// Make this return true when this Command no longer needs to run execute()
34+
protected boolean isFinished() {
35+
return tim.get() > Robot.getConst("Update Constants Time", 0.1);
36+
}
37+
38+
// Called once after isFinished returns true
39+
protected void end() {
40+
}
41+
42+
// Called when another command which requires one or more of the same
43+
// subsystems is scheduled to run
44+
protected void interrupted() {
45+
end();
46+
}
4647
}

Robot2018/src/org/usfirst/frc/team199/Robot2018/subsystems/Drivetrain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import edu.wpi.first.wpilibj.command.Subsystem;
2525
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
2626

27-
public class Drivetrain extends Subsystem implements PIDOutput, PIDSource {
27+
public class Drivetrain extends Subsystem implements PIDOutput, PIDSource, DrivetrainInterface {
2828
// Put methods for controlling this subsystem
2929
// here. Call these from Commands.
3030

0 commit comments

Comments
 (0)