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

Commit e0c0cfa

Browse files
dfang314kevinzwang
authored andcommitted
First commit of DavidRobot2018 (#5)
* First commit of DavidRobot2018 adding basic drivetrain functionality * changed up workspace organization, functionailty should not be changed * integrated DavidRobot2018 into Robot2018 * fixed project setup and migrated all packages to one standard * deleted DavidRobot2018 (previously merged manually into Robot2018) * split drivetrain gear shift into 2 commands (from 1) and added more flexibility
1 parent 4e54b1c commit e0c0cfa

11 files changed

Lines changed: 669 additions & 1 deletion

File tree

Robot2018/.classpath

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
<classpathentry kind="var" path="opencv" sourcepath="opencv.sources"/>
88
<classpathentry kind="var" path="cscore" sourcepath="cscore.sources"/>
99
<classpathentry kind="var" path="wpiutil" sourcepath="wpiutil.sources"/>
10+
<classpathentry kind="lib" path="/Robot2017/libs/jars/navx_frc.jar"/>
11+
<classpathentry kind="var" path="USERLIBS_DIR/CTRE_Phoenix.jar"/>
1012
<classpathentry kind="output" path="bin"/>
1113
</classpath>

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
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+
/*----------------------------------------------------------------------------*/
7+
18
package org.usfirst.frc.team199.Robot2018;
29

3-
import edu.wpi.first.wpilibj.buttons.Button;
10+
import org.usfirst.frc.team199.Robot2018.commands.ShiftLowGear;
11+
import org.usfirst.frc.team199.Robot2018.commands.ShiftDriveType;
12+
13+
import edu.wpi.first.wpilibj.Joystick;
14+
import edu.wpi.first.wpilibj.buttons.JoystickButton;
15+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
416

517
/**
618
* This class is the glue that binds the controls on the physical operator
@@ -34,4 +46,27 @@ public class OI {
3446
// Start the command when the button is released and let it run the command
3547
// until it is finished as determined by it's isFinished method.
3648
// button.whenReleased(new ExampleCommand());
49+
50+
public Joystick leftJoy;
51+
public JoystickButton shiftDrive;
52+
public JoystickButton shiftDriveType;
53+
public Joystick rightJoy;
54+
public Joystick manipulator;
55+
56+
57+
public int getButton(String key, int def) {
58+
if(!SmartDashboard.containsKey(key)) {
59+
SmartDashboard.putNumber(key, def);
60+
}
61+
return (int) SmartDashboard.getNumber(key, def);
62+
}
63+
public OI() {
64+
leftJoy = new Joystick(0);
65+
shiftDrive = new JoystickButton(leftJoy, getButton("Button Shift Drive", 1));
66+
shiftDrive.whenPressed(new ShiftLowGear());
67+
shiftDriveType = new JoystickButton(leftJoy, getButton("Button Shift Drive Type", 2));
68+
shiftDriveType.whenPressed(new ShiftDriveType());
69+
rightJoy = new Joystick(1);
70+
manipulator = new Joystick(2);
71+
}
3772
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

22
package org.usfirst.frc.team199.Robot2018;
33

4+
import java.util.ArrayList;
5+
46
import org.usfirst.frc.team199.Robot2018.subsystems.Climber;
57
import org.usfirst.frc.team199.Robot2018.subsystems.ClimberAssist;
8+
import org.usfirst.frc.team199.Robot2018.subsystems.Drivetrain;
69
import org.usfirst.frc.team199.Robot2018.subsystems.IntakeEject;
710
import org.usfirst.frc.team199.Robot2018.subsystems.Lift;
811

@@ -26,18 +29,48 @@ public class Robot extends IterativeRobot {
2629
public static final ClimberAssist climberAssist = new ClimberAssist();
2730
public static final IntakeEject intakeEject = new IntakeEject();
2831
public static final Lift lift = new Lift();
32+
public static Drivetrain dt;
33+
2934
public static OI oi;
3035

3136
Command autonomousCommand;
3237
SendableChooser<Command> chooser = new SendableChooser<>();
3338

39+
40+
public static double getConst(String key, double def) {
41+
if(!SmartDashboard.containsKey(key)) {
42+
SmartDashboard.putNumber(key, def);
43+
}
44+
return SmartDashboard.getNumber(key, def);
45+
}
46+
public void sendValuesToDashboard() {
47+
ArrayList<String> boolKeys = new ArrayList<String>();
48+
ArrayList<Boolean> boolDef = new ArrayList<Boolean>();
49+
boolKeys.add("Arcade Drive");
50+
boolKeys.add("Arcade Drive Default Setup");
51+
boolKeys.add("Square Drive Values");
52+
boolKeys.add("High Gear");
53+
54+
boolDef.add(true);
55+
boolDef.add(true);
56+
boolDef.add(false);
57+
boolDef.add(false);
58+
for(int i = 0; i < boolKeys.size(); i++) {
59+
if(!SmartDashboard.containsKey(boolKeys.get(i))) {
60+
SmartDashboard.putBoolean(boolKeys.get(i), boolDef.get(i));
61+
}
62+
}
63+
}
64+
3465
/**
3566
* This function is run when the robot is first started up and should be
3667
* used for any initialization code.
3768
*/
3869
@Override
3970
public void robotInit() {
71+
dt = new Drivetrain();
4072
oi = new OI();
73+
sendValuesToDashboard();
4174
// chooser.addObject("My Auto", new MyAutoCommand());
4275
SmartDashboard.putData("Auto mode", chooser);
4376
}

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
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+
/*----------------------------------------------------------------------------*/
7+
18
package org.usfirst.frc.team199.Robot2018;
29

10+
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
11+
import com.kauailabs.navx.frc.AHRS;
12+
13+
import edu.wpi.first.wpilibj.AnalogGyro;
14+
import edu.wpi.first.wpilibj.DoubleSolenoid;
15+
import edu.wpi.first.wpilibj.Encoder;
16+
import edu.wpi.first.wpilibj.PIDController;
17+
import edu.wpi.first.wpilibj.SerialPort;
18+
import edu.wpi.first.wpilibj.SpeedControllerGroup;
19+
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
20+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
21+
322
/**
423
* The RobotMap is a mapping from the ports sensors and actuators are wired into
524
* to a variable name. This provides flexibility changing wiring, makes checking
@@ -16,4 +35,91 @@ public class RobotMap {
1635
// number and the module. For example you with a rangefinder:
1736
// public static int rangefinderPort = 1;
1837
// public static int rangefinderModule = 1;
38+
39+
public static Encoder leftEnc;
40+
public static WPI_TalonSRX dtLeftDrive;
41+
public static WPI_TalonSRX dtLeftSlave;
42+
public static SpeedControllerGroup dtLeft;
43+
44+
public static Encoder rightEnc;
45+
public static WPI_TalonSRX dtRightDrive;
46+
public static WPI_TalonSRX dtRightSlave;
47+
public static SpeedControllerGroup dtRight;
48+
public static DifferentialDrive robotDrive;
49+
public static PIDController turnController;
50+
public static PIDController moveController;
51+
// public static PIDController moveLeftController;
52+
// public static PIDController moveRightController;
53+
54+
public static AHRS ahrs;
55+
public static AnalogGyro dtGyro;
56+
public static DoubleSolenoid dtGear;
57+
58+
private void configSRX(WPI_TalonSRX mc) {
59+
int kTimeout = (int) Robot.getConst("ConstkTimeoutMs", 10);
60+
mc.configNominalOutputForward(0, kTimeout);
61+
mc.configNominalOutputReverse(0, kTimeout);
62+
mc.configPeakOutputForward(1, kTimeout);
63+
mc.configPeakOutputReverse(-1, kTimeout);
64+
}
65+
public RobotMap() {
66+
67+
leftEnc = new Encoder(getPort("Port1LeftEnc", 0), getPort("Port2LeftEnc", 1));
68+
dtLeftDrive = new WPI_TalonSRX(getPort("PortLeftTalonSRXDrive", 1));
69+
configSRX(dtLeftDrive);
70+
dtLeftSlave = new WPI_TalonSRX(getPort("PortLeftTalonSRXSlave", 1));
71+
configSRX(dtLeftSlave);
72+
dtLeft = new SpeedControllerGroup(dtLeftDrive, dtLeftSlave);
73+
74+
rightEnc = new Encoder(getPort("Port1RightEnc", 2), getPort("Port2RightEnc", 3));
75+
dtRightDrive = new WPI_TalonSRX(getPort("PortRightTalonSRXDrive", 2));
76+
configSRX(dtRightDrive);
77+
dtRightSlave = new WPI_TalonSRX(getPort("PortRightTalonSRXSlave", 1));
78+
configSRX(dtRightSlave);
79+
dtRight = new SpeedControllerGroup(dtRightDrive, dtRightSlave);
80+
81+
robotDrive = new DifferentialDrive(dtLeft, dtRight);
82+
turnController = new PIDController(Robot.getConst("ConstTurnkP", 1), Robot.getConst("ConstTurnkI", 0), Robot.getConst("ConstTurnkD", 0), ahrs, Robot.dt);
83+
turnController.disable();
84+
turnController.setInputRange(-180, 180);
85+
turnController.setOutputRange(-1.0, 1.0);
86+
turnController.setContinuous();
87+
turnController.setAbsoluteTolerance(Robot.getConst("ConstTurnTolerance", 1));
88+
moveController = new PIDController(Robot.getConst("ConstMovekP", 1), Robot.getConst("ConstMovekI", 0), Robot.getConst("ConstMovekD", 0), Robot.dt, Robot.dt);
89+
turnController.disable();
90+
turnController.setInputRange(-180, 180);
91+
turnController.setOutputRange(-1.0, 1.0);
92+
turnController.setContinuous();
93+
turnController.setAbsoluteTolerance(Robot.getConst("ConstMoveTolerance", 2));
94+
// moveLeftController = new PIDController(Robot.getConst("ConstMoveLeftkP", 1), Robot.getConst("ConstMoveLeftkI", 0), Robot.getConst("ConstMoveLeftkD", 0), Robot.dt.getLeftDrive(), (PIDOutput) Robot.dt.getLeftDrive());
95+
// moveLeftController.disable();
96+
// moveLeftController.setInputRange(0, Double.MAX_VALUE);
97+
// moveLeftController.setOutputRange(-1.0, 1.0);
98+
// moveLeftController.setContinuous(false);
99+
// moveLeftController.setAbsoluteTolerance(Robot.getConst("ConstMoveToleranceLeft", 2));
100+
// moveRightController = new PIDController(Robot.getConst("ConstMoveRightkP", 1), Robot.getConst("ConstMoveRightkI", 0), Robot.getConst("ConstMoveRightkD", 0), Robot.dt.getRightDrive(), (PIDOutput) Robot.dt.getRightDrive());
101+
// moveRightController.disable();
102+
// moveRightController.setInputRange(0, Double.MAX_VALUE);
103+
// moveRightController.setOutputRange(-1.0, 1.0);
104+
// moveRightController.setContinuous(false);
105+
// moveRightController.setAbsoluteTolerance(Robot.getConst("ConstMoveToleranceRight", 2));
106+
107+
ahrs = new AHRS(SerialPort.Port.kMXP);
108+
dtGyro = new AnalogGyro(getPort("PortGyro", 0));
109+
dtGear = new DoubleSolenoid(getPort("Port1dtGearSolenoid", 0), getPort("Port2dtGearSolenoid", 1));
110+
111+
}
112+
113+
/**
114+
* Used in RobotMap to find ports for robot components, getPort also puts numbers if they don't exist yet.
115+
* @param key The port key
116+
* @param def The default value
117+
* @return returns the Port
118+
*/
119+
public int getPort(String key, int def) {
120+
if(!SmartDashboard.containsKey(key)) {
121+
SmartDashboard.putNumber(key, def);
122+
}
123+
return (int)SmartDashboard.getNumber(key, def);
124+
}
19125
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.usfirst.frc.team199.Robot2018.commands;
2+
3+
import org.usfirst.frc.team199.Robot2018.Robot;
4+
5+
import edu.wpi.first.wpilibj.command.Command;
6+
7+
/**
8+
*
9+
*/
10+
public class PIDDrive extends Command {
11+
12+
double target;
13+
public PIDDrive(double targ) {
14+
// Use requires() here to declare subsystem dependencies
15+
// eg. requires(chassis);
16+
target = targ;
17+
requires(Robot.dt);
18+
}
19+
20+
// Called just before this Command runs the first time
21+
protected void initialize() {
22+
Robot.dt.enableMovePid();
23+
Robot.dt.setSetMove(target);
24+
}
25+
26+
// Called repeatedly when this Command is scheduled to run
27+
protected void execute() {
28+
Robot.dt.arcadeDrive(Robot.dt.getPidOut(), 0);
29+
}
30+
31+
// Make this return true when this Command no longer needs to run execute()
32+
protected boolean isFinished() {
33+
return Robot.dt.onDriveTarg();
34+
}
35+
36+
// Called once after isFinished returns true
37+
protected void end() {
38+
Robot.dt.stopDrive();
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+
}
46+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.usfirst.frc.team199.Robot2018.commands;
2+
3+
import org.usfirst.frc.team199.Robot2018.Robot;
4+
import org.usfirst.frc.team199.Robot2018.RobotMap;
5+
6+
import edu.wpi.first.wpilibj.command.Command;
7+
8+
/**
9+
*
10+
*/
11+
public class PIDTurn extends Command {
12+
13+
double target;
14+
public PIDTurn(double targ) {
15+
// Use requires() here to declare subsystem dependencies
16+
// eg. requires(chassis);
17+
target = targ;
18+
requires(Robot.dt);
19+
}
20+
21+
// Called just before this Command runs the first time
22+
protected void initialize() {
23+
Robot.dt.resetAHRS();
24+
Robot.dt.setSetTurn(target);
25+
Robot.dt.enableTurnPid();
26+
}
27+
28+
// Called repeatedly when this Command is scheduled to run
29+
protected void execute() {
30+
Robot.dt.arcadeDrive(0, Robot.dt.getPidOut());
31+
}
32+
33+
// Make this return true when this Command no longer needs to run execute()
34+
protected boolean isFinished() {
35+
return Robot.dt.onTurnTarg();
36+
}
37+
38+
// Called once after isFinished returns true
39+
protected void end() {
40+
Robot.dt.stopDrive();
41+
}
42+
43+
// Called when another command which requires one or more of the same
44+
// subsystems is scheduled to run
45+
protected void interrupted() {
46+
end();
47+
}
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.usfirst.frc.team199.Robot2018.commands;
2+
3+
import edu.wpi.first.wpilibj.Timer;
4+
import edu.wpi.first.wpilibj.command.Command;
5+
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
6+
7+
/**
8+
*
9+
*/
10+
public class ShiftDriveType extends Command {
11+
12+
Timer tim;
13+
public ShiftDriveType() {
14+
// Use requires() here to declare subsystem dependencies
15+
// eg. requires(chassis);
16+
}
17+
18+
// Called just before this Command runs the first time
19+
protected void initialize() {
20+
tim = new Timer();
21+
tim.reset();
22+
tim.start();
23+
}
24+
25+
// Called repeatedly when this Command is scheduled to run
26+
protected void execute() {
27+
SmartDashboard.putBoolean("Arcade Drive", !SmartDashboard.getBoolean("Arcade Drive", false));
28+
}
29+
30+
// Make this return true when this Command no longer needs to run execute()
31+
protected boolean isFinished() {
32+
return tim.get() > 0.125;
33+
}
34+
35+
// Called once after isFinished returns true
36+
protected void end() {
37+
}
38+
39+
// Called when another command which requires one or more of the same
40+
// subsystems is scheduled to run
41+
protected void interrupted() {
42+
}
43+
}

0 commit comments

Comments
 (0)