Skip to content

Commit 175efed

Browse files
committed
Add lights code
1 parent f6b8439 commit 175efed

6 files changed

Lines changed: 156 additions & 6 deletions

File tree

Robot2019/src/main/java/frc/robot/OI.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
import edu.wpi.first.wpilibj.Joystick;
1313
import edu.wpi.first.wpilibj.buttons.JoystickButton;
1414
import frc.robot.commands.ActuateClimberRails;
15-
import frc.robot.commands.NormalDrive;
1615
import frc.robot.commands.Climb;
1716
import frc.robot.commands.EjectCargo;
1817
import frc.robot.commands.IntakeOnlyCargo;
18+
import frc.robot.commands.NormalDrive;
1919
import frc.robot.commands.SetArcadeOrTank;
20+
import frc.robot.commands.SetLight;
2021
import frc.robot.commands.SlowDrive;
2122
import frc.robot.commands.ToggleCamera;
2223
import frc.robot.commands.ToggleHatch;
@@ -25,6 +26,7 @@
2526
import frc.robot.subsystems.Climber;
2627
import frc.robot.subsystems.Drivetrain;
2728
import frc.robot.subsystems.HatchPanel;
29+
import frc.robot.subsystems.Lights;
2830

2931
/**
3032
* This class is the glue that binds the controls on the physical operator
@@ -42,9 +44,10 @@ public class OI {
4244
JoystickButton climbBtn;
4345
JoystickButton toggleCameraBtn;
4446
JoystickButton wobbleDriveBtn;
47+
JoystickButton cycleLightBtn;
4548

46-
OI(Drivetrain dt, HatchPanel hp, Cargo cargo, Climber climber, UsbCamera driveCamera, UsbCamera hatchCamera,
47-
VideoSink cameraServer) {
49+
OI(Drivetrain dt, HatchPanel hp, Cargo cargo, Climber climber, Lights lights, UsbCamera driveCamera,
50+
UsbCamera hatchCamera, VideoSink cameraServer) {
4851
leftJoy = new Joystick(0);
4952
rightJoy = new Joystick(1);
5053
manipulator = new Joystick(2);
@@ -77,6 +80,9 @@ public class OI {
7780

7881
toggleCameraBtn = new JoystickButton(leftJoy, 2);
7982
toggleCameraBtn.whenPressed(new ToggleCamera(driveCamera, hatchCamera, cameraServer));
83+
84+
cycleLightBtn = new JoystickButton(manipulator, Manip.START);
85+
cycleLightBtn.whenPressed(new SetLight(lights));
8086
}
8187

8288
private class Manip {

Robot2019/src/main/java/frc/robot/Robot.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
import frc.robot.subsystems.Climber;
1919
import frc.robot.subsystems.Drivetrain;
2020
import frc.robot.subsystems.HatchPanel;
21+
import frc.robot.subsystems.Lights;
2122

2223
public class Robot extends TimedRobot {
2324
private static Drivetrain dt;
2425
private static HatchPanel hp;
2526
private static OI oi;
2627
private static Cargo cargo;
2728
private static Climber climber;
29+
private static Lights lights;
2830
private static String fname1, fname2, fname3, fname4;
2931

3032
@Override
@@ -34,8 +36,8 @@ public void robotInit() {
3436
hp = new HatchPanel(RobotMap.hatchPistons);
3537
cargo = new Cargo(RobotMap.cargoRoller, RobotMap.pdp, RobotMap.cargoPDPPort);
3638
climber = new Climber(RobotMap.climberMotor, RobotMap.climberEncoder, RobotMap.ahrs, RobotMap.climberPistons);
37-
38-
oi = new OI(dt, hp, cargo, climber, RobotMap.driveCamera, RobotMap.hatchCamera, RobotMap.cameraServer);
39+
lights = new Lights(RobotMap.lights);
40+
oi = new OI(dt, hp, cargo, climber, lights, RobotMap.driveCamera, RobotMap.hatchCamera, RobotMap.cameraServer);
3941

4042
fname1 = "/home/lvuser/drive_char_linear_for.csv";
4143
fname2 = "/home/lvuser/drive_char_stepwise_for.csv";

Robot2019/src/main/java/frc/robot/RobotMap.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class RobotMap {
4141
static VictorSP cargoRoller;
4242
static Encoder leftEnc, rightEnc;
4343
static String driveMode;
44+
static VictorSP lights;
4445
static AHRS ahrs;
4546
static PowerDistributionPanel pdp;
4647
static UsbCamera driveCamera, hatchCamera;
@@ -74,6 +75,9 @@ public class RobotMap {
7475
leftEnc = new Encoder(new DigitalInput(0), new DigitalInput(1));
7576
rightEnc = new Encoder(new DigitalInput(2), new DigitalInput(3));
7677

78+
// Initialize lights
79+
lights = new VictorSP(3); // TODO: Set this to correct port
80+
7781
ahrs = new AHRS(SPI.Port.kMXP);
7882
pdp = new PowerDistributionPanel();
7983

@@ -156,9 +160,12 @@ private static UsbCamera configureCamera(int port) {
156160

157161
/**
158162
* Checks an error code and prints it to standard out if it is not ok
163+
*
159164
* @param ec The error code to check
160165
*/
161166
private static void catchError(ErrorCode ec) {
162-
if(ec != ErrorCode.OK) System.out.println("Error configuring in RobotMap.java at line: " + new Throwable().getStackTrace()[1].getLineNumber());
167+
if (ec != ErrorCode.OK)
168+
System.out
169+
.println("Error configuring in RobotMap.java at line: " + new Throwable().getStackTrace()[1].getLineNumber());
163170
}
164171
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 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+
8+
package frc.robot.commands;
9+
10+
import edu.wpi.first.wpilibj.command.Command;
11+
import frc.robot.subsystems.Lights;
12+
13+
public class SetLight extends Command {
14+
private Lights lights;
15+
16+
public SetLight(Lights lights) {
17+
// Use requires() here to declare subsystem dependencies
18+
// eg. requires(chassis);
19+
this.lights = lights;
20+
requires(lights);
21+
}
22+
23+
// Called just before this Command runs the first time
24+
@Override
25+
protected void initialize() {
26+
}
27+
28+
// Called repeatedly when this Command is scheduled to run
29+
@Override
30+
protected void execute() {
31+
lights.setLights();
32+
}
33+
34+
// Make this return true when this Command no longer needs to run execute()
35+
@Override
36+
protected boolean isFinished() {
37+
return false;
38+
}
39+
40+
// Called once after isFinished returns true
41+
@Override
42+
protected void end() {
43+
}
44+
45+
// Called when another command which requires one or more of the same
46+
// subsystems is scheduled to run
47+
@Override
48+
protected void interrupted() {
49+
end();
50+
}
51+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 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+
8+
package frc.robot.commands;
9+
10+
import edu.wpi.first.wpilibj.command.InstantCommand;
11+
import frc.robot.subsystems.Lights;
12+
13+
/**
14+
* Add your docs here.
15+
*/
16+
public class ToggleLight extends InstantCommand {
17+
/**
18+
* Add your docs here.
19+
*/
20+
private Lights lights;
21+
22+
public ToggleLight(Lights lights) {
23+
super();
24+
// Use requires() here to declare subsystem dependencies
25+
// eg. requires(chassis);
26+
this.lights = lights;
27+
}
28+
29+
// Called once when the command executes
30+
@Override
31+
protected void initialize() {
32+
lights.toggleLights();
33+
}
34+
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*----------------------------------------------------------------------------*/
2+
/* Copyright (c) 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+
8+
package frc.robot.subsystems;
9+
10+
import edu.wpi.first.wpilibj.VictorSP;
11+
import edu.wpi.first.wpilibj.command.Subsystem;
12+
import frc.robot.commands.SetLight;
13+
14+
/**
15+
* Add your docs here.
16+
*/
17+
public class Lights extends Subsystem {
18+
// Put methods for controlling this subsystem
19+
// here. Call these from Commands.
20+
private VictorSP lights;
21+
private double lightsValue = 0;
22+
23+
/**
24+
* 0 is off 0.5 is orange 1 is yellow
25+
*/
26+
27+
public Lights(VictorSP lights) {
28+
this.lights = lights;
29+
}
30+
31+
public void setLights() {
32+
lights.set(lightsValue);
33+
}
34+
35+
public void toggleLights() {
36+
if (lightsValue == 0) {
37+
lightsValue = 0.5;
38+
} else if (lightsValue == 0.5) {
39+
lightsValue = 1;
40+
} else {
41+
lightsValue = 0;
42+
}
43+
}
44+
45+
@Override
46+
public void initDefaultCommand() {
47+
setDefaultCommand(new SetLight(this));
48+
}
49+
}

0 commit comments

Comments
 (0)