Skip to content

Commit 1843961

Browse files
authored
Merge pull request #82 from DeepBlueRobotics/issue-81
Add lights code
2 parents 657379e + ad72b1f commit 1843961

6 files changed

Lines changed: 165 additions & 5 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import frc.robot.commands.SlowDrive;
2222
import frc.robot.commands.ToggleCamera;
2323
import frc.robot.commands.ToggleHatch;
24+
import frc.robot.commands.ToggleLight;
2425
import frc.robot.commands.WobbleDrive;
2526
import frc.robot.subsystems.Cargo;
2627
import frc.robot.subsystems.Climber;
2728
import frc.robot.subsystems.Drivetrain;
2829
import frc.robot.subsystems.HatchPanel;
30+
import frc.robot.subsystems.Lights;
2931

3032
/**
3133
* This class is the glue that binds the controls on the physical operator
@@ -43,9 +45,10 @@ public class OI {
4345
JoystickButton climbBtn;
4446
JoystickButton toggleCameraBtn;
4547
JoystickButton wobbleDriveBtn;
48+
JoystickButton cycleLightBtn;
4649

47-
OI(Drivetrain dt, HatchPanel hp, Cargo cargo, Climber climber, UsbCamera driveCamera, UsbCamera hatchCamera,
48-
VideoSink cameraServer) {
50+
OI(Drivetrain dt, HatchPanel hp, Cargo cargo, Climber climber, Lights lights, UsbCamera driveCamera,
51+
UsbCamera hatchCamera, VideoSink cameraServer) {
4952
leftJoy = new Joystick(0);
5053
rightJoy = new Joystick(1);
5154
manipulator = new Joystick(2);
@@ -79,6 +82,9 @@ public class OI {
7982

8083
toggleCameraBtn = new JoystickButton(leftJoy, 2);
8184
toggleCameraBtn.whenPressed(new ToggleCamera(driveCamera, hatchCamera, cameraServer));
85+
86+
cycleLightBtn = new JoystickButton(manipulator, Manip.START);
87+
cycleLightBtn.whenPressed(new ToggleLight(lights));
8288
}
8389

8490
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

@@ -146,9 +150,12 @@ private static UsbCamera configureCamera(int port) {
146150

147151
/**
148152
* Checks an error code and prints it to standard out if it is not ok
153+
*
149154
* @param ec The error code to check
150155
*/
151156
private static void catchError(ErrorCode ec) {
152-
if(ec != ErrorCode.OK) System.out.println("Error configuring in RobotMap.java at line: " + new Throwable().getStackTrace()[1].getLineNumber());
157+
if (ec != ErrorCode.OK)
158+
System.out
159+
.println("Error configuring in RobotMap.java at line: " + new Throwable().getStackTrace()[1].getLineNumber());
153160
}
154161
}
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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
13+
import frc.robot.commands.SetLight;
14+
15+
/**
16+
* Add your docs here.
17+
*/
18+
public class Lights extends Subsystem {
19+
// Put methods for controlling this subsystem
20+
// here. Call these from Commands.
21+
private VictorSP lights;
22+
private double lightsValue = 0;
23+
24+
/**
25+
* 0 is off 0.5 is orange 1 is yellow
26+
*/
27+
28+
public Lights(VictorSP lights) {
29+
this.lights = lights;
30+
}
31+
32+
public void setLights() {
33+
lights.set(lightsValue);
34+
String color;
35+
if (lightsValue == 0) {
36+
color = "None";
37+
} else if (lightsValue == 0.5) {
38+
color = "Yellow";
39+
} else {
40+
color = "Orange";
41+
}
42+
SmartDashboard.putString("Lights Current Color", color);
43+
}
44+
45+
public void toggleLights() {
46+
if (lightsValue == 0) {
47+
lightsValue = 0.5;
48+
} else if (lightsValue == 0.5) {
49+
lightsValue = 1;
50+
} else {
51+
lightsValue = 0;
52+
}
53+
}
54+
55+
@Override
56+
public void initDefaultCommand() {
57+
setDefaultCommand(new SetLight(this));
58+
}
59+
}

0 commit comments

Comments
 (0)