[examples] Clean up examples (#8674)

Move various "examples" into snippets. Several examples that were less
than a full mechanism or robot were moved to snippets. `arcadedrive` and
`tankdrive` were removed in favor of their Gamepad variants. `hidrumble`
was removed due to being too simple. `potentiometerpid` was removed
because of low utility. `gyromecanum` replaced `mecanumdrive` for
deduplication and because very few teams run holonomic drivetrains
without gyros.
This commit is contained in:
Gold856
2026-03-14 17:13:45 -04:00
committed by GitHub
parent 62ce8944aa
commit f1adce4cf7
78 changed files with 569 additions and 1665 deletions

View File

@@ -1,54 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.addressableled;
import static org.wpilib.units.Units.Meters;
import static org.wpilib.units.Units.MetersPerSecond;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.led.AddressableLED;
import org.wpilib.hardware.led.AddressableLEDBuffer;
import org.wpilib.hardware.led.LEDPattern;
import org.wpilib.units.measure.Distance;
public class Robot extends TimedRobot {
private final AddressableLED m_led;
private final AddressableLEDBuffer m_ledBuffer;
// Create an LED pattern that will display a rainbow across
// all hues at maximum saturation and half brightness
private final LEDPattern m_rainbow = LEDPattern.rainbow(255, 128);
// Our LED strip has a density of 120 LEDs per meter
private static final Distance kLedSpacing = Meters.of(1 / 120.0);
// Create a new pattern that scrolls the rainbow pattern across the LED strip, moving at a
// velocity
// of 1 meter per second.
private final LEDPattern m_scrollingRainbow =
m_rainbow.scrollAtAbsoluteVelocity(MetersPerSecond.of(1), kLedSpacing);
/** Called once at the beginning of the robot program. */
public Robot() {
// SmartIO port 1
m_led = new AddressableLED(1);
// Reuse buffer
// Default to a length of 60
m_ledBuffer = new AddressableLEDBuffer(60);
m_led.setLength(m_ledBuffer.getLength());
// Set the data
m_led.setData(m_ledBuffer);
}
@Override
public void robotPeriodic() {
// Update the buffer with the rainbow animation
m_scrollingRainbow.applyTo(m_ledBuffer);
// Set the LEDs
m_led.setData(m_ledBuffer);
}
}

View File

@@ -1,149 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.apriltagsvision;
import java.util.ArrayList;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.wpilib.framework.TimedRobot;
import org.wpilib.math.geometry.Rotation3d;
import org.wpilib.math.geometry.Transform3d;
import org.wpilib.networktables.IntegerArrayPublisher;
import org.wpilib.networktables.NetworkTable;
import org.wpilib.networktables.NetworkTableInstance;
import org.wpilib.vision.apriltag.AprilTagDetection;
import org.wpilib.vision.apriltag.AprilTagDetector;
import org.wpilib.vision.apriltag.AprilTagPoseEstimator;
import org.wpilib.vision.camera.CvSink;
import org.wpilib.vision.camera.CvSource;
import org.wpilib.vision.camera.UsbCamera;
import org.wpilib.vision.stream.CameraServer;
/**
* This is a demo program showing the detection of AprilTags. The image is acquired from the USB
* camera, then any detected AprilTags are marked up on the image and sent to the dashboard.
*
* <p>Be aware that the performance on this is much worse than a coprocessor solution!
*/
public class Robot extends TimedRobot {
/** Called once at the beginning of the robot program. */
public Robot() {
var visionThread = new Thread(this::apriltagVisionThreadProc);
visionThread.setDaemon(true);
visionThread.start();
}
void apriltagVisionThreadProc() {
var detector = new AprilTagDetector();
// look for tag36h11, correct 1 error bit (hamming distance 1)
// hamming 1 allocates 781KB, 2 allocates 27.4 MB, 3 allocates 932 MB
// max of 1 recommended for RoboRIO 1, while hamming 2 is feasible on the RoboRIO 2
detector.addFamily("tag36h11", 1);
// Set up Pose Estimator - parameters are for a Microsoft Lifecam HD-3000
// (https://www.chiefdelphi.com/t/wpilib-apriltagdetector-sample-code/421411/21)
var poseEstConfig =
new AprilTagPoseEstimator.Config(
0.1651, 699.3778103158814, 677.7161226393544, 345.6059345433618, 207.12741326228522);
var estimator = new AprilTagPoseEstimator(poseEstConfig);
// Get the UsbCamera from CameraServer
UsbCamera camera = CameraServer.startAutomaticCapture();
// Set the resolution
camera.setResolution(640, 480);
// Get a CvSink. This will capture Mats from the camera
CvSink cvSink = CameraServer.getVideo();
// Setup a CvSource. This will send images back to the Dashboard
CvSource outputStream = CameraServer.putVideo("Detected", 640, 480);
// Mats are very memory expensive. Lets reuse these.
var mat = new Mat();
var grayMat = new Mat();
// Instantiate once
ArrayList<Long> tags = new ArrayList<>();
var outlineColor = new Scalar(0, 255, 0);
var crossColor = new Scalar(0, 0, 255);
// We'll output to NT
NetworkTable tagsTable = NetworkTableInstance.getDefault().getTable("apriltags");
IntegerArrayPublisher pubTags = tagsTable.getIntegerArrayTopic("tags").publish();
// This cannot be 'true'. The program will never exit if it is. This
// lets the robot stop this thread when restarting robot code or
// deploying.
while (!Thread.interrupted()) {
// Tell the CvSink to grab a frame from the camera and put it
// in the source mat. If there is an error notify the output.
if (cvSink.grabFrame(mat) == 0) {
// Send the output the error.
outputStream.notifyError(cvSink.getError());
// skip the rest of the current iteration
continue;
}
Imgproc.cvtColor(mat, grayMat, Imgproc.COLOR_RGB2GRAY);
AprilTagDetection[] detections = detector.detect(grayMat);
// have not seen any tags yet
tags.clear();
for (AprilTagDetection detection : detections) {
// remember we saw this tag
tags.add((long) detection.getId());
// draw lines around the tag
for (var i = 0; i <= 3; i++) {
var j = (i + 1) % 4;
var pt1 = new Point(detection.getCornerX(i), detection.getCornerY(i));
var pt2 = new Point(detection.getCornerX(j), detection.getCornerY(j));
Imgproc.line(mat, pt1, pt2, outlineColor, 2);
}
// mark the center of the tag
var cx = detection.getCenterX();
var cy = detection.getCenterY();
var ll = 10;
Imgproc.line(mat, new Point(cx - ll, cy), new Point(cx + ll, cy), crossColor, 2);
Imgproc.line(mat, new Point(cx, cy - ll), new Point(cx, cy + ll), crossColor, 2);
// identify the tag
Imgproc.putText(
mat,
Integer.toString(detection.getId()),
new Point(cx + ll, cy),
Imgproc.FONT_HERSHEY_SIMPLEX,
1,
crossColor,
3);
// determine pose
Transform3d pose = estimator.estimate(detection);
// put pose into dashboard
Rotation3d rot = pose.getRotation();
tagsTable
.getEntry("pose_" + detection.getId())
.setDoubleArray(
new double[] {
pose.getX(), pose.getY(), pose.getZ(), rot.getX(), rot.getY(), rot.getZ()
});
}
// put list of tags onto dashboard
pubTags.set(tags.stream().mapToLong(Long::longValue).toArray());
// Give the output stream a new image to display
outputStream.putFrame(mat);
}
pubTags.close();
detector.close();
}
}

View File

@@ -1,42 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.arcadedrive;
import org.wpilib.drive.DifferentialDrive;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.util.sendable.SendableRegistry;
/**
* This is a demo program showing the use of the DifferentialDrive class. Runs the motors with
* arcade steering.
*/
public class Robot extends TimedRobot {
private final PWMSparkMax m_leftMotor = new PWMSparkMax(0);
private final PWMSparkMax m_rightMotor = new PWMSparkMax(1);
private final DifferentialDrive m_robotDrive =
new DifferentialDrive(m_leftMotor::setDutyCycle, m_rightMotor::setDutyCycle);
private final Joystick m_stick = new Joystick(0);
/** Called once at the beginning of the robot program. */
public Robot() {
SendableRegistry.addChild(m_robotDrive, m_leftMotor);
SendableRegistry.addChild(m_robotDrive, m_rightMotor);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.setInverted(true);
}
@Override
public void teleopPeriodic() {
// Drive with arcade drive.
// That means that the Y axis drives forward
// and backward, and the X turns left and right.
m_robotDrive.arcadeDrive(-m_stick.getY(), -m_stick.getX());
}
}

View File

@@ -1,54 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.canpdp;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.power.PowerDistribution;
import org.wpilib.smartdashboard.SmartDashboard;
/**
* This is a sample program showing how to retrieve information from the Power Distribution Panel
* via CAN. The information will be displayed under variables through the SmartDashboard.
*/
public class Robot extends TimedRobot {
private final PowerDistribution m_pdp = new PowerDistribution(0);
public Robot() {
// Put the PDP itself to the dashboard
SmartDashboard.putData("PDP", m_pdp);
}
@Override
public void robotPeriodic() {
// Get the current going through channel 7, in Amperes.
// The PDP returns the current in increments of 0.125A.
// At low currents the current readings tend to be less accurate.
double current7 = m_pdp.getCurrent(7);
SmartDashboard.putNumber("Current Channel 7", current7);
// Get the voltage going into the PDP, in Volts.
// The PDP returns the voltage in increments of 0.05 Volts.
double voltage = m_pdp.getVoltage();
SmartDashboard.putNumber("Voltage", voltage);
// Retrieves the temperature of the PDP, in degrees Celsius.
double temperatureCelsius = m_pdp.getTemperature();
SmartDashboard.putNumber("Temperature", temperatureCelsius);
// Get the total current of all channels.
double totalCurrent = m_pdp.getTotalCurrent();
SmartDashboard.putNumber("Total Current", totalCurrent);
// Get the total power of all channels.
// Power is the bus voltage multiplied by the current with the units Watts.
double totalPower = m_pdp.getTotalPower();
SmartDashboard.putNumber("Total Power", totalPower);
// Get the total energy of all channels.
// Energy is the power summed over time with units Joules.
double totalEnergy = m_pdp.getTotalEnergy();
SmartDashboard.putNumber("Total Energy", totalEnergy);
}
}

View File

@@ -1,59 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.digitalcommunication;
import java.util.Optional;
import org.wpilib.driverstation.DriverStation;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.discrete.DigitalOutput;
/**
* This is a sample program demonstrating how to communicate to a light controller from the robot
* code using the roboRIO's DIO ports.
*/
public class Robot extends TimedRobot {
// define ports for digitalcommunication with light controller
static final int kAlliancePort = 0;
static final int kEnabledPort = 1;
static final int kAutonomousPort = 2;
static final int kAlertPort = 3;
private final DigitalOutput m_allianceOutput = new DigitalOutput(kAlliancePort);
private final DigitalOutput m_enabledOutput = new DigitalOutput(kEnabledPort);
private final DigitalOutput m_autonomousOutput = new DigitalOutput(kAutonomousPort);
private final DigitalOutput m_alertOutput = new DigitalOutput(kAlertPort);
@Override
public void robotPeriodic() {
boolean setAlliance = false;
Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
if (alliance.isPresent()) {
setAlliance = alliance.get() == DriverStation.Alliance.Red;
}
// pull alliance port high if on red alliance, pull low if on blue alliance
m_allianceOutput.set(setAlliance);
// pull enabled port high if enabled, low if disabled
m_enabledOutput.set(DriverStation.isEnabled());
// pull auto port high if in autonomous, low if in teleop (or disabled)
m_autonomousOutput.set(DriverStation.isAutonomous());
// pull alert port high if match time remaining is between 30 and 25 seconds
var matchTime = DriverStation.getMatchTime();
m_alertOutput.set(matchTime <= 30 && matchTime >= 25);
}
/** Close all resources. */
@Override
public void close() {
m_allianceOutput.close();
m_enabledOutput.close();
m_autonomousOutput.close();
m_alertOutput.close();
super.close();
}
}

View File

@@ -1,30 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.dutycycleinput;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.rotation.DutyCycle;
import org.wpilib.smartdashboard.SmartDashboard;
public class Robot extends TimedRobot {
private final DutyCycle m_dutyCycle;
public Robot() {
m_dutyCycle = new DutyCycle(0);
}
@Override
public void robotPeriodic() {
// Duty Cycle Frequency in Hz
double frequency = m_dutyCycle.getFrequency();
// Output of duty cycle, ranging from 0 to 1
// 1 is fully on, 0 is fully off
double output = m_dutyCycle.getOutput();
SmartDashboard.putNumber("Frequency", frequency);
SmartDashboard.putNumber("Duty Cycle", output);
}
}

View File

@@ -1,90 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.eventloop;
import org.wpilib.driverstation.Joystick;
import org.wpilib.event.BooleanEvent;
import org.wpilib.event.EventLoop;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.hardware.rotation.Encoder;
import org.wpilib.math.controller.PIDController;
import org.wpilib.math.controller.SimpleMotorFeedforward;
public class Robot extends TimedRobot {
public static final double SHOT_VELOCITY = 200; // rpm
public static final double TOLERANCE = 8; // rpm
private final PWMSparkMax m_shooter = new PWMSparkMax(0);
private final Encoder m_shooterEncoder = new Encoder(0, 1);
private final PIDController m_controller = new PIDController(0.3, 0, 0);
private final SimpleMotorFeedforward m_ff = new SimpleMotorFeedforward(0.1, 0.065);
private final PWMSparkMax m_kicker = new PWMSparkMax(1);
private final PWMSparkMax m_intake = new PWMSparkMax(2);
private final EventLoop m_loop = new EventLoop();
private final Joystick m_joystick = new Joystick(0);
/** Called once at the beginning of the robot program. */
public Robot() {
m_controller.setTolerance(TOLERANCE);
BooleanEvent isBallAtKicker =
new BooleanEvent(m_loop, () -> false); // m_kickerSensor.getRange() < KICKER_THRESHOLD);
BooleanEvent intakeButton = new BooleanEvent(m_loop, () -> m_joystick.getRawButton(2));
// if the thumb button is held
intakeButton
// and there is not a ball at the kicker
.and(isBallAtKicker.negate())
// activate the intake
.ifHigh(() -> m_intake.setDutyCycle(0.5));
// if the thumb button is not held
intakeButton
.negate()
// or there is a ball in the kicker
.or(isBallAtKicker)
// stop the intake
.ifHigh(m_intake::stopMotor);
BooleanEvent shootTrigger = new BooleanEvent(m_loop, m_joystick::getTrigger);
// if the trigger is held
shootTrigger
// accelerate the shooter wheel
.ifHigh(
() -> {
m_shooter.setVoltage(
m_controller.calculate(m_shooterEncoder.getRate(), SHOT_VELOCITY)
+ m_ff.calculate(SHOT_VELOCITY));
});
// if not, stop
shootTrigger.negate().ifHigh(m_shooter::stopMotor);
BooleanEvent atTargetVelocity =
new BooleanEvent(m_loop, m_controller::atSetpoint)
// debounce for more stability
.debounce(0.2);
// if we're at the target velocity, kick the ball into the shooter wheel
atTargetVelocity.ifHigh(() -> m_kicker.setDutyCycle(0.7));
// when we stop being at the target velocity, it means the ball was shot
atTargetVelocity
.falling()
// so stop the kicker
.ifHigh(m_kicker::stopMotor);
}
@Override
public void robotPeriodic() {
// poll all the bindings
m_loop.poll();
}
}

View File

@@ -10,71 +10,6 @@
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Tank Drive",
"description": "Control a differential drive with twin-joystick tank drive in teleop.",
"tags": [
"Basic Robot",
"Differential Drive",
"Joystick"
],
"foldername": "tankdrive",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Arcade Drive",
"description": "Control a differential drivetrain with single-joystick arcade drive in teleop.",
"tags": [
"Basic Robot",
"Differential Drive",
"Joystick"
],
"foldername": "arcadedrive",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Mecanum Drive",
"description": "Control a mecanum drivetrain with a joystick in teleop.",
"tags": [
"Basic Robot",
"Mecanum Drive",
"Joystick"
],
"foldername": "mecanumdrive",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "PDP CAN Monitoring",
"description": "Monitor Power Distribution data such as voltage, current, temperature, etc.",
"tags": [
"Hardware",
"PDP",
"SmartDashboard"
],
"foldername": "canpdp",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Solenoids",
"description": "Control a single and double solenoid from joystick buttons.",
"tags": [
"Hardware",
"Joystick",
"Pneumatics"
],
"foldername": "solenoid",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Encoder",
"description": "View values from a quadrature encoder.",
@@ -88,35 +23,6 @@
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "EventLoop",
"description": "Manage a ball system using EventLoop and BooleanEvent.",
"tags": [
"Basic Robot",
"Flywheel",
"EventLoop"
],
"foldername": "eventloop",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Potentiometer PID",
"description": "Maintain elevator position setpoints with a potentiometer and PID control.",
"tags": [
"Basic Robot",
"Analog",
"Elevator",
"PID",
"Joystick"
],
"foldername": "potentiometerpid",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2,
"hasunittests": true
},
{
"name": "Elevator with trapezoid profiled PID",
"description": "Reach elevator position setpoints with trapezoid profiles and smart motor controller PID.",
@@ -169,7 +75,6 @@
"Differential Drive",
"PID",
"Gyro",
"Analog",
"Joystick"
],
"foldername": "gyro",
@@ -178,28 +83,15 @@
"commandversion": 2
},
{
"name": "Gyro Mecanum",
"name": "Mecanum Drive",
"description": "Drive a mecanum drivetrain using field-oriented controls with a joystick.",
"tags": [
"Basic Robot",
"Mecanum Drive",
"Gyro",
"Analog",
"Joystick"
],
"foldername": "gyromecanum",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "HID Rumble",
"description": "Make human interface devices (HID) rumble.",
"tags": [
"Hardware",
"Gamepad"
],
"foldername": "hidrumble",
"foldername": "mecanumdrive",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
@@ -221,65 +113,6 @@
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Motor Control",
"description": "Control a single motor with a joystick, displaying the movement of the motor using an encoder.",
"tags": [
"Basic Robot",
"Encoder",
"SmartDashboard",
"Joystick"
],
"foldername": "motorcontrol",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Simple Vision",
"description": "Use the CameraServer class to stream from a USB Webcam without processing the images.",
"tags": [
"Vision"
],
"foldername": "quickvision",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Intermediate Vision",
"description": "Acquire images from an attached USB camera and add some annotation to the image (as you might do for showing operators the result of some image recognition) and send it to the dashboard for display.",
"tags": [
"Vision"
],
"foldername": "intermediatevision",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "HTTP Camera",
"description": "Acquire images from an HTTP network camera and adds some annotation to the image (as you might do for showing operators the result of some image recognition), and sends it to the dashboard for display.",
"tags": [
"Vision"
],
"foldername": "httpcamera",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "AprilTags Vision",
"description": "On-roboRIO detection of AprilTags using an attached USB camera.",
"tags": [
"Vision",
"AprilTags"
],
"foldername": "apriltagsvision",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "'Traditional' Hatchbot",
"description": "A fully-functional Commandv2 hatchbot for the 2019 game, written in the 'traditional' style, i.e. commands are given their own classes.",
@@ -338,17 +171,6 @@
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Select Command Example",
"description": "Use SelectCommand to select an autonomous routine.",
"tags": [
"Commandv2"
],
"foldername": "selectcommand",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "SwerveBot",
"description": "Use kinematics and odometry with a swerve drive.",
@@ -434,32 +256,6 @@
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Duty Cycle Input",
"description": "View duty-cycle input.",
"tags": [
"Hardware",
"Duty Cycle",
"SmartDashboard"
],
"foldername": "dutycycleinput",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "Addressable LED",
"description": "Display a rainbow pattern on an addressable LED strip.",
"tags": [
"Hardware",
"Basic Robot",
"AddressableLEDs"
],
"foldername": "addressableled",
"gradlebase": "java",
"robotclass": "Robot",
"commandversion": 2
},
{
"name": "DriveDistanceOffboard",
"description": "Drive a differential drivetrain a set distance using TrapezoidProfile and smart motor controller PID.",
@@ -722,45 +518,6 @@
"xrp"
]
},
{
"name": "Digital Communication Sample",
"description": "Communicates with external devices (such as an Arduino) using the roboRIO's DIO.",
"tags": [
"Hardware",
"Digital Output"
],
"foldername": "digitalcommunication",
"gradlebase": "java",
"commandversion": 2,
"robotclass": "Robot",
"hasunittests": true
},
{
"name": "I2C Communication Sample",
"description": "Communicate with external devices (such as an Arduino) using the roboRIO's I2C port.",
"tags": [
"Hardware",
"I2C"
],
"foldername": "i2ccommunication",
"gradlebase": "java",
"commandversion": 2,
"robotclass": "Robot",
"hasunittests": true
},
{
"name": "Flywheel BangBangController",
"description": "A sample program to demonstrate the use of a BangBangController with a flywheel to control RPM",
"tags": [
"Flywheel",
"Simulation",
"Joystick"
],
"foldername": "flywheelbangbangcontroller",
"gradlebase": "java",
"commandversion": 2,
"robotclass": "Robot"
},
{
"name": "SysIdRoutine",
"description": "A sample Commandv2 robot demonstrating use of the SysIdRoutine command factory",

View File

@@ -1,102 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.flywheelbangbangcontroller;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.hardware.rotation.Encoder;
import org.wpilib.math.controller.BangBangController;
import org.wpilib.math.controller.SimpleMotorFeedforward;
import org.wpilib.math.numbers.N1;
import org.wpilib.math.system.DCMotor;
import org.wpilib.math.system.LinearSystem;
import org.wpilib.math.system.Models;
import org.wpilib.math.util.Units;
import org.wpilib.simulation.EncoderSim;
import org.wpilib.simulation.FlywheelSim;
import org.wpilib.smartdashboard.SmartDashboard;
import org.wpilib.system.RobotController;
/**
* This is a sample program to demonstrate the use of a BangBangController with a flywheel to
* control RPM.
*/
public class Robot extends TimedRobot {
private static final int kMotorPort = 0;
private static final int kEncoderAChannel = 0;
private static final int kEncoderBChannel = 1;
// Max setpoint for joystick control in RPM
private static final double kMaxSetpointValue = 6000.0;
// Joystick to control setpoint
private final Joystick m_joystick = new Joystick(0);
private final PWMSparkMax m_flywheelMotor = new PWMSparkMax(kMotorPort);
private final Encoder m_encoder = new Encoder(kEncoderAChannel, kEncoderBChannel);
private final BangBangController m_bangBangController = new BangBangController();
// Gains are for example purposes only - must be determined for your own robot!
public static final double kFlywheelKs = 0.0001; // V
public static final double kFlywheelKv = 0.000195; // V/RPM
public static final double kFlywheelKa = 0.0003; // V/(RPM/s)
private final SimpleMotorFeedforward m_feedforward =
new SimpleMotorFeedforward(kFlywheelKs, kFlywheelKv, kFlywheelKa);
// Simulation classes help us simulate our robot
// Reduction between motors and encoder, as output over input. If the flywheel
// spins slower than the motors, this number should be greater than one.
private static final double kFlywheelGearing = 1.0;
// 1/2 MR²
private static final double kFlywheelMomentOfInertia =
0.5 * Units.lbsToKilograms(1.5) * Math.pow(Units.inchesToMeters(4), 2);
private final DCMotor m_gearbox = DCMotor.getNEO(1);
private final LinearSystem<N1, N1, N1> m_plant =
Models.flywheelFromPhysicalConstants(m_gearbox, kFlywheelGearing, kFlywheelMomentOfInertia);
private final FlywheelSim m_flywheelSim = new FlywheelSim(m_plant, m_gearbox);
private final EncoderSim m_encoderSim = new EncoderSim(m_encoder);
public Robot() {
// Add bang-bang controller to SmartDashboard and networktables.
SmartDashboard.putData(m_bangBangController);
}
/** Controls flywheel to a set velocity (RPM) controlled by a joystick. */
@Override
public void teleopPeriodic() {
// Scale setpoint value between 0 and maxSetpointValue
double setpoint =
Math.max(
0.0,
m_joystick.getRawAxis(0)
* Units.rotationsPerMinuteToRadiansPerSecond(kMaxSetpointValue));
// Set setpoint and measurement of the bang-bang controller
double bangOutput = m_bangBangController.calculate(m_encoder.getRate(), setpoint) * 12.0;
// Controls a motor with the output of the BangBang controller and a
// feedforward. The feedforward is reduced slightly to avoid overspeeding
// the shooter.
m_flywheelMotor.setVoltage(bangOutput + 0.9 * m_feedforward.calculate(setpoint));
}
/** Update our simulation. This should be run every robot loop in simulation. */
@Override
public void simulationPeriodic() {
// To update our simulation, we set motor voltage inputs, update the
// simulation, and write the simulated velocities to our simulated encoder
m_flywheelSim.setInputVoltage(
m_flywheelMotor.getDutyCycle() * RobotController.getInputVoltage());
m_flywheelSim.update(0.02);
m_encoderSim.setRate(m_flywheelSim.getAngularVelocity());
}
}

View File

@@ -1,62 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.gyromecanum;
import org.wpilib.drive.MecanumDrive;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.imu.OnboardIMU;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.util.sendable.SendableRegistry;
/**
* This is a sample program that uses mecanum drive with a gyro sensor to maintain rotation vectors
* in relation to the starting orientation of the robot (field-oriented controls).
*/
public class Robot extends TimedRobot {
private static final int kFrontLeftChannel = 0;
private static final int kRearLeftChannel = 1;
private static final int kFrontRightChannel = 2;
private static final int kRearRightChannel = 3;
private static final OnboardIMU.MountOrientation kIMUMountOrientation =
OnboardIMU.MountOrientation.kFlat;
private static final int kJoystickPort = 0;
private final MecanumDrive m_robotDrive;
private final OnboardIMU m_imu = new OnboardIMU(kIMUMountOrientation);
private final Joystick m_joystick = new Joystick(kJoystickPort);
/** Called once at the beginning of the robot program. */
public Robot() {
PWMSparkMax frontLeft = new PWMSparkMax(kFrontLeftChannel);
PWMSparkMax rearLeft = new PWMSparkMax(kRearLeftChannel);
PWMSparkMax frontRight = new PWMSparkMax(kFrontRightChannel);
PWMSparkMax rearRight = new PWMSparkMax(kRearRightChannel);
// Invert the right side motors.
// You may need to change or remove this to match your robot.
frontRight.setInverted(true);
rearRight.setInverted(true);
m_robotDrive =
new MecanumDrive(
frontLeft::setDutyCycle,
rearLeft::setDutyCycle,
frontRight::setDutyCycle,
rearRight::setDutyCycle);
SendableRegistry.addChild(m_robotDrive, frontLeft);
SendableRegistry.addChild(m_robotDrive, rearLeft);
SendableRegistry.addChild(m_robotDrive, frontRight);
SendableRegistry.addChild(m_robotDrive, rearRight);
}
/** Mecanum drive is used with the gyro angle as an input. */
@Override
public void teleopPeriodic() {
m_robotDrive.driveCartesian(
-m_joystick.getY(), -m_joystick.getX(), -m_joystick.getZ(), m_imu.getRotation2d());
}
}

View File

@@ -1,28 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.hidrumble;
import org.wpilib.driverstation.Gamepad;
import org.wpilib.driverstation.GenericHID.RumbleType;
import org.wpilib.framework.TimedRobot;
/** This is a demo program showing the use of GenericHID's rumble feature. */
public class Robot extends TimedRobot {
private final Gamepad m_hid = new Gamepad(0);
@Override
public void autonomousInit() {
// Turn on rumble at the start of auto
m_hid.setRumble(RumbleType.kLeftRumble, 1.0);
m_hid.setRumble(RumbleType.kRightRumble, 1.0);
}
@Override
public void disabledInit() {
// Stop the rumble when entering disabled
m_hid.setRumble(RumbleType.kLeftRumble, 0.0);
m_hid.setRumble(RumbleType.kRightRumble, 0.0);
}
}

View File

@@ -1,69 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.httpcamera;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.wpilib.framework.TimedRobot;
import org.wpilib.vision.camera.CvSink;
import org.wpilib.vision.camera.CvSource;
import org.wpilib.vision.camera.HttpCamera;
import org.wpilib.vision.stream.CameraServer;
/**
* This is a demo program showing the use of OpenCV to do vision processing. The image is acquired
* from an HTTP camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has
* many methods for different types of processing.
*/
public class Robot extends TimedRobot {
Thread m_visionThread;
/** Called once at the beginning of the robot program. */
public Robot() {
m_visionThread =
new Thread(
() -> {
// Create an HTTP camera. The address will need to be modified to have the correct
// team number. The exact path will depend on the source.
HttpCamera camera =
new HttpCamera("HTTP Camera", "http://10.x.y.11/video/stream.mjpg");
// Start capturing images
CameraServer.startAutomaticCapture(camera);
// Set the resolution
camera.setResolution(640, 480);
// Get a CvSink. This will capture Mats from the camera
CvSink cvSink = CameraServer.getVideo();
// Setup a CvSource. This will send images back to the Dashboard
CvSource outputStream = CameraServer.putVideo("Rectangle", 640, 480);
// Mats are very memory expensive. Lets reuse this Mat.
Mat mat = new Mat();
// This cannot be 'true'. The program will never exit if it is. This
// lets the robot stop this thread when restarting robot code or
// deploying.
while (!Thread.interrupted()) {
// Tell the CvSink to grab a frame from the camera and put it
// in the source mat. If there is an error notify the output.
if (cvSink.grabFrame(mat) == 0) {
// Send the output the error.
outputStream.notifyError(cvSink.getError());
// skip the rest of the current iteration
continue;
}
// Put a rectangle on the image
Imgproc.rectangle(
mat, new Point(100, 100), new Point(400, 400), new Scalar(255, 255, 255), 5);
// Give the output stream a new image to display
outputStream.putFrame(mat);
}
});
m_visionThread.setDaemon(true);
m_visionThread.start();
}
}

View File

@@ -1,74 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.i2ccommunication;
import java.util.Optional;
import org.wpilib.driverstation.DriverStation;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.bus.I2C;
import org.wpilib.hardware.bus.I2C.Port;
/**
* This is a sample program demonstrating how to communicate to a light controller from the robot
* code using the roboRIO's I2C port.
*/
public class Robot extends TimedRobot {
static final Port kPort = Port.kPort0;
private static final int kDeviceAddress = 4;
private final I2C m_arduino = new I2C(kPort, kDeviceAddress);
private void writeString(String input) {
// Creates a char array from the input string
char[] chars = input.toCharArray();
// Creates a byte array from the char array
byte[] data = new byte[chars.length];
// Adds each character
for (int i = 0; i < chars.length; i++) {
data[i] = (byte) chars[i];
}
// Writes bytes over I2C
m_arduino.transaction(data, data.length, new byte[] {}, 0);
}
@Override
@SuppressWarnings("PMD.ConsecutiveLiteralAppends")
public void robotPeriodic() {
// Creates a string to hold current robot state information, including
// alliance, enabled state, operation mode, and match time. The message
// is sent in format "AEM###" where A is the alliance color, (R)ed or
// (B)lue, E is the enabled state, (E)nabled or (D)isabled, M is the
// operation mode, (A)utonomous or (T)eleop, and ### is the zero-padded
// time remaining in the match.
//
// For example, "RET043" would indicate that the robot is on the red
// alliance, enabled in teleop mode, with 43 seconds left in the match.
StringBuilder stateMessage = new StringBuilder(6);
String allianceString = "U";
Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
if (alliance.isPresent()) {
allianceString = alliance.get() == DriverStation.Alliance.Red ? "R" : "B";
}
stateMessage
.append(allianceString)
.append(DriverStation.isEnabled() ? "E" : "D")
.append(DriverStation.isAutonomous() ? "A" : "T")
.append(String.format("%03d", (int) DriverStation.getMatchTime()));
writeString(stateMessage.toString());
}
/** Close all resources. */
@Override
public void close() {
m_arduino.close();
super.close();
}
}

View File

@@ -1,65 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.intermediatevision;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import org.wpilib.framework.TimedRobot;
import org.wpilib.vision.camera.CvSink;
import org.wpilib.vision.camera.CvSource;
import org.wpilib.vision.camera.UsbCamera;
import org.wpilib.vision.stream.CameraServer;
/**
* This is a demo program showing the use of OpenCV to do vision processing. The image is acquired
* from the USB camera, then a rectangle is put on the image and sent to the dashboard. OpenCV has
* many methods for different types of processing.
*/
public class Robot extends TimedRobot {
Thread m_visionThread;
/** Called once at the beginning of the robot program. */
public Robot() {
m_visionThread =
new Thread(
() -> {
// Get the UsbCamera from CameraServer
UsbCamera camera = CameraServer.startAutomaticCapture();
// Set the resolution
camera.setResolution(640, 480);
// Get a CvSink. This will capture Mats from the camera
CvSink cvSink = CameraServer.getVideo();
// Setup a CvSource. This will send images back to the Dashboard
CvSource outputStream = CameraServer.putVideo("Rectangle", 640, 480);
// Mats are very memory expensive. Lets reuse this Mat.
Mat mat = new Mat();
// This cannot be 'true'. The program will never exit if it is. This
// lets the robot stop this thread when restarting robot code or
// deploying.
while (!Thread.interrupted()) {
// Tell the CvSink to grab a frame from the camera and put it
// in the source mat. If there is an error notify the output.
if (cvSink.grabFrame(mat) == 0) {
// Send the output the error.
outputStream.notifyError(cvSink.getError());
// skip the rest of the current iteration
continue;
}
// Put a rectangle on the image
Imgproc.rectangle(
mat, new Point(100, 100), new Point(400, 400), new Scalar(255, 255, 255), 5);
// Give the output stream a new image to display
outputStream.putFrame(mat);
}
});
m_visionThread.setDaemon(true);
m_visionThread.start();
}
}

View File

@@ -7,20 +7,26 @@ package org.wpilib.examples.mecanumdrive;
import org.wpilib.drive.MecanumDrive;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.imu.OnboardIMU;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.util.sendable.SendableRegistry;
/** This is a demo program showing how to use Mecanum control with the MecanumDrive class. */
/**
* This is a sample program that uses mecanum drive with a gyro sensor to maintain rotation vectors
* in relation to the starting orientation of the robot (field-oriented controls).
*/
public class Robot extends TimedRobot {
private static final int kFrontLeftChannel = 2;
private static final int kRearLeftChannel = 3;
private static final int kFrontRightChannel = 1;
private static final int kRearRightChannel = 0;
private static final int kJoystickChannel = 0;
private static final int kFrontLeftChannel = 0;
private static final int kRearLeftChannel = 1;
private static final int kFrontRightChannel = 2;
private static final int kRearRightChannel = 3;
private static final OnboardIMU.MountOrientation kIMUMountOrientation =
OnboardIMU.MountOrientation.kFlat;
private static final int kJoystickPort = 0;
private final MecanumDrive m_robotDrive;
private final Joystick m_stick;
private final OnboardIMU m_imu = new OnboardIMU(kIMUMountOrientation);
private final Joystick m_joystick = new Joystick(kJoystickPort);
/** Called once at the beginning of the robot program. */
public Robot() {
@@ -41,18 +47,18 @@ public class Robot extends TimedRobot {
frontRight::setDutyCycle,
rearRight::setDutyCycle);
m_stick = new Joystick(kJoystickChannel);
SendableRegistry.addChild(m_robotDrive, frontLeft);
SendableRegistry.addChild(m_robotDrive, rearLeft);
SendableRegistry.addChild(m_robotDrive, frontRight);
SendableRegistry.addChild(m_robotDrive, rearRight);
}
/** Mecanum drive is used with the gyro angle as an input. */
@Override
public void teleopPeriodic() {
// Use the joystick Y axis for forward movement, X axis for lateral
// movement, and Z axis for rotation.
m_robotDrive.driveCartesian(-m_stick.getY(), -m_stick.getX(), -m_stick.getZ());
m_robotDrive.driveCartesian(
-m_joystick.getY(), -m_joystick.getX(), -m_joystick.getZ(), m_imu.getRotation2d());
}
}

View File

@@ -1,57 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.motorcontrol;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.hardware.rotation.Encoder;
import org.wpilib.smartdashboard.SmartDashboard;
/**
* This sample program shows how to control a motor using a joystick. In the operator control part
* of the program, the joystick is read and the value is written to the motor.
*
* <p>Joystick analog values range from -1 to 1 and motor controller inputs also range from -1 to 1
* making it easy to work together.
*
* <p>In addition, the encoder value of an encoder connected to ports 0 and 1 is consistently sent
* to the Dashboard.
*/
public class Robot extends TimedRobot {
private static final int kMotorPort = 0;
private static final int kJoystickPort = 0;
private static final int kEncoderPortA = 0;
private static final int kEncoderPortB = 1;
private final PWMSparkMax m_motor;
private final Joystick m_joystick;
private final Encoder m_encoder;
/** Called once at the beginning of the robot program. */
public Robot() {
m_motor = new PWMSparkMax(kMotorPort);
m_joystick = new Joystick(kJoystickPort);
m_encoder = new Encoder(kEncoderPortA, kEncoderPortB);
// Use SetDistancePerPulse to set the multiplier for GetDistance
// This is set up assuming a 6 inch wheel with a 360 CPR encoder.
m_encoder.setDistancePerPulse((Math.PI * 6) / 360.0);
}
/*
* The RobotPeriodic function is called every control packet no matter the
* robot mode.
*/
@Override
public void robotPeriodic() {
SmartDashboard.putNumber("Encoder", m_encoder.getDistance());
}
/** The teleop periodic function is called every control packet in teleop. */
@Override
public void teleopPeriodic() {
m_motor.setDutyCycle(m_joystick.getY());
}
}

View File

@@ -1,79 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.potentiometerpid;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.hardware.rotation.AnalogPotentiometer;
import org.wpilib.math.controller.PIDController;
/**
* This is a sample program to demonstrate how to use a soft potentiometer and a PID controller to
* reach and maintain position setpoints on an elevator mechanism.
*/
public class Robot extends TimedRobot {
static final int kPotChannel = 1;
static final int kMotorChannel = 7;
static final int kJoystickChannel = 3;
// The elevator can move 1.5 meters from top to bottom
static final double kFullHeight = 1.5;
// Bottom, middle, and top elevator setpoints in meters
static final double[] kSetpoints = {0.2, 0.8, 1.4};
// proportional, integral, and derivative velocity constants
// DANGER: when tuning PID constants, high/inappropriate values for kP, kI,
// and kD may cause dangerous, uncontrollable, or undesired behavior!
private static final double kP = 0.7;
private static final double kI = 0.35;
private static final double kD = 0.25;
private final PIDController m_pidController = new PIDController(kP, kI, kD);
// Scaling is handled internally
private final AnalogPotentiometer m_potentiometer =
new AnalogPotentiometer(kPotChannel, kFullHeight);
private final PWMSparkMax m_elevatorMotor = new PWMSparkMax(kMotorChannel);
private final Joystick m_joystick = new Joystick(kJoystickChannel);
private int m_index;
@Override
public void teleopInit() {
// Move to the bottom setpoint when teleop starts
m_index = 0;
m_pidController.setSetpoint(kSetpoints[m_index]);
}
@Override
public void teleopPeriodic() {
// Read from the sensor
double position = m_potentiometer.get();
// Run the PID Controller
double pidOut = m_pidController.calculate(position);
// Apply PID output
m_elevatorMotor.setDutyCycle(pidOut);
// when the button is pressed once, the selected elevator setpoint is incremented
if (m_joystick.getTriggerPressed()) {
// index of the elevator setpoint wraps around.
m_index = (m_index + 1) % kSetpoints.length;
System.out.println("m_index = " + m_index);
m_pidController.setSetpoint(kSetpoints[m_index]);
}
}
@Override
public void close() {
m_elevatorMotor.close();
m_potentiometer.close();
m_pidController.close();
m_index = 0;
super.close();
}
}

View File

@@ -1,19 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.quickvision;
import org.wpilib.framework.TimedRobot;
import org.wpilib.vision.stream.CameraServer;
/**
* Uses the CameraServer class to automatically capture video from a USB webcam and send it to the
* dashboard without doing any vision processing. This is the easiest way to get camera images to
* the dashboard. Just add this to the robot class constructor.
*/
public class Robot extends TimedRobot {
public Robot() {
CameraServer.startAutomaticCapture();
}
}

View File

@@ -1,25 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.selectcommand;
/**
* The Constants class provides a convenient place for teams to hold robot-wide numerical or boolean
* constants. This class should not be used for any other purpose. All constants should be declared
* globally (i.e. public static). Do not put anything functional in this class.
*
* <p>It is advised to statically import this class (or one of its inner classes) wherever the
* constants are needed, to reduce verbosity.
*/
public final class Constants {
/**
* Example of an inner class. One can "import static [...].Constants.OIConstants.*" to gain access
* to the constants contained within without having to preface the names with the class, greatly
* reducing the amount of text required.
*/
public static final class OIConstants {
// Example: the port of the driver's controller
public static final int kDriverControllerPort = 0;
}
}

View File

@@ -1,100 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.selectcommand;
import org.wpilib.command2.Command;
import org.wpilib.command2.CommandScheduler;
import org.wpilib.framework.TimedRobot;
/**
* The methods in this class are called automatically corresponding to each mode, as described in
* the TimedRobot documentation. If you change the name of this class or the package after creating
* this project, you must also update the Main.java file in the project.
*/
public class Robot extends TimedRobot {
private Command m_autonomousCommand;
private final RobotContainer m_robotContainer;
/**
* This function is run when the robot is first started up and should be used for any
* initialization code.
*/
public Robot() {
// Instantiate our RobotContainer. This will perform all our button bindings, and put our
// autonomous chooser on the dashboard.
m_robotContainer = new RobotContainer();
}
/**
* This function is called every 20 ms, no matter the mode. Use this for items like diagnostics
* that you want ran during disabled, autonomous, teleoperated and test.
*
* <p>This runs after the mode specific periodic functions, but before LiveWindow and
* SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {
// Runs the Scheduler. This is responsible for polling buttons, adding newly-scheduled
// commands, running already-scheduled commands, removing finished or interrupted commands,
// and running subsystem periodic() methods. This must be called from the robot's periodic
// block in order for anything in the Command-based framework to work.
CommandScheduler.getInstance().run();
}
/** This function is called once each time the robot enters Disabled mode. */
@Override
public void disabledInit() {}
@Override
public void disabledPeriodic() {}
/** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */
@Override
public void autonomousInit() {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();
/*
* String autoSelected = SmartDashboard.getString("Auto Selector",
* "Default"); switch(autoSelected) { case "My Auto": autonomousCommand
* = new MyAutoCommand(); break; case "Default Auto": default:
* autonomousCommand = new ExampleCommand(); break; }
*/
// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
CommandScheduler.getInstance().schedule(m_autonomousCommand);
}
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {}
@Override
public void teleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
if (m_autonomousCommand != null) {
m_autonomousCommand.cancel();
}
}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {}
@Override
public void testInit() {
// Cancels all running commands at the start of test mode.
CommandScheduler.getInstance().cancelAll();
}
/** This function is called periodically during test mode. */
@Override
public void testPeriodic() {}
}

View File

@@ -1,68 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.selectcommand;
import java.util.Map;
import org.wpilib.command2.Command;
import org.wpilib.command2.PrintCommand;
import org.wpilib.command2.SelectCommand;
import org.wpilib.driverstation.Gamepad;
import org.wpilib.driverstation.GenericHID;
/**
* This class is where the bulk of the robot should be declared. Since Command-based is a
* "declarative" paradigm, very little robot logic should actually be handled in the {@link Robot}
* periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
* subsystems, commands, and button mappings) should be declared here.
*/
public class RobotContainer {
// The enum used as keys for selecting the command to run.
private enum CommandSelector {
ONE,
TWO,
THREE
}
// An example selector method for the selectcommand. Returns the selector that will select
// which command to run. Can base this choice on logical conditions evaluated at runtime.
private CommandSelector select() {
return CommandSelector.ONE;
}
// An example selectcommand. Will select from the three commands based on the value returned
// by the selector method at runtime. Note that selectcommand works on Object(), so the
// selector does not have to be an enum; it could be any desired type (string, integer,
// boolean, double...)
private final Command m_exampleSelectCommand =
new SelectCommand<>(
// Maps selector values to commands
Map.ofEntries(
Map.entry(CommandSelector.ONE, new PrintCommand("Command one was selected!")),
Map.entry(CommandSelector.TWO, new PrintCommand("Command two was selected!")),
Map.entry(CommandSelector.THREE, new PrintCommand("Command three was selected!"))),
this::select);
public RobotContainer() {
// Configure the button bindings
configureButtonBindings();
}
/**
* Use this method to define your button->command mappings. Buttons can be created by
* instantiating a {@link GenericHID} or one of its subclasses ({@link
* org.wpilib.driverstation.Joystick} or {@link Gamepad}), and then calling passing it to a {@link
* org.wpilib.command2.button.JoystickButton}.
*/
private void configureButtonBindings() {}
/**
* Use this to pass the autonomous command to the main {@link Robot} class.
*
* @return the command to run in autonomous
*/
public Command getAutonomousCommand() {
return m_exampleSelectCommand;
}
}

View File

@@ -1,120 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.solenoid;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.pneumatic.Compressor;
import org.wpilib.hardware.pneumatic.DoubleSolenoid;
import org.wpilib.hardware.pneumatic.PneumaticsModuleType;
import org.wpilib.hardware.pneumatic.Solenoid;
import org.wpilib.smartdashboard.SmartDashboard;
/**
* This is a sample program showing the use of the solenoid classes during operator control. Three
* buttons from a joystick will be used to control two solenoids: One button to control the position
* of a single solenoid and the other two buttons to control a double solenoid. Single solenoids can
* either be on or off, such that the air diverted through them goes through either one channel or
* the other. Double solenoids have three states: Off, Forward, and Reverse. Forward and Reverse
* divert the air through the two channels and correspond to the on and off of a single solenoid,
* but a double solenoid can also be "off", where the solenoid will remain in its default power off
* state. Additionally, double solenoids take up two channels on your PCM whereas single solenoids
* only take a single channel.
*/
public class Robot extends TimedRobot {
private final Joystick m_stick = new Joystick(0);
// Solenoid corresponds to a single solenoid.
// In this case, it's connected to channel 0 of a PH with the default CAN ID.
private final Solenoid m_solenoid = new Solenoid(0, PneumaticsModuleType.REVPH, 0);
// DoubleSolenoid corresponds to a double solenoid.
// In this case, it's connected to channels 1 and 2 of a PH with the default CAN ID.
private final DoubleSolenoid m_doubleSolenoid =
new DoubleSolenoid(0, PneumaticsModuleType.REVPH, 1, 2);
// Compressor connected to a PH with a default CAN ID (1)
private final Compressor m_compressor = new Compressor(0, PneumaticsModuleType.REVPH);
static final int kSolenoidButton = 1;
static final int kDoubleSolenoidForwardButton = 2;
static final int kDoubleSolenoidReverseButton = 3;
static final int kCompressorButton = 4;
/** Called once at the beginning of the robot program. */
public Robot() {
// Publish elements to dashboard.
SmartDashboard.putData("Single Solenoid", m_solenoid);
SmartDashboard.putData("Double Solenoid", m_doubleSolenoid);
SmartDashboard.putData("Compressor", m_compressor);
}
@SuppressWarnings("PMD.UnconditionalIfStatement")
@Override
public void teleopPeriodic() {
// Publish some raw data
// Get the pressure (in PSI) from the analog sensor connected to the PH.
// This function is supported only on the PH!
// On a PCM, this function will return 0.
SmartDashboard.putNumber("PH Pressure [PSI]", m_compressor.getPressure());
// Get compressor current draw.
SmartDashboard.putNumber("Compressor Current", m_compressor.getCurrent());
// Get whether the compressor is active.
SmartDashboard.putBoolean("Compressor Active", m_compressor.isEnabled());
// Get the digital pressure switch connected to the PCM/PH.
// The switch is open when the pressure is over ~120 PSI.
SmartDashboard.putBoolean("Pressure Switch", m_compressor.getPressureSwitchValue());
/*
* The output of GetRawButton is true/false depending on whether
* the button is pressed; Set takes a boolean for whether
* to retract the solenoid (false) or extend it (true).
*/
m_solenoid.set(m_stick.getRawButton(kSolenoidButton));
/*
* GetRawButtonPressed will only return true once per press.
* If a button is pressed, set the solenoid to the respective channel.
*/
if (m_stick.getRawButtonPressed(kDoubleSolenoidForwardButton)) {
m_doubleSolenoid.set(DoubleSolenoid.Value.kForward);
} else if (m_stick.getRawButtonPressed(kDoubleSolenoidReverseButton)) {
m_doubleSolenoid.set(DoubleSolenoid.Value.kReverse);
}
// On button press, toggle the compressor.
if (m_stick.getRawButtonPressed(kCompressorButton)) {
// Check whether the compressor is currently enabled.
boolean isCompressorEnabled = m_compressor.isEnabled();
if (isCompressorEnabled) {
// Disable closed-loop mode on the compressor.
m_compressor.disable();
} else {
// Change the if statements to select the closed-loop you want to use:
if (false) {
// Enable closed-loop mode based on the digital pressure switch connected to the PCM/PH.
// The switch is open when the pressure is over ~120 PSI.
m_compressor.enableDigital();
}
if (true) {
// Enable closed-loop mode based on the analog pressure sensor connected to the PH.
// The compressor will run while the pressure reported by the sensor is in the
// specified range ([70 PSI, 120 PSI] in this example).
// Analog mode exists only on the PH! On the PCM, this enables digital control.
m_compressor.enableAnalog(70, 120);
}
if (false) {
// Enable closed-loop mode based on both the digital pressure switch AND the analog
// pressure sensor connected to the PH.
// The compressor will run while the pressure reported by the analog sensor is in the
// specified range ([70 PSI, 120 PSI] in this example) AND the digital switch reports
// that the system is not full.
// Hybrid mode exists only on the PH! On the PCM, this enables digital control.
m_compressor.enableHybrid(70, 120);
}
}
}
}
}

View File

@@ -1,44 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package org.wpilib.examples.tankdrive;
import org.wpilib.drive.DifferentialDrive;
import org.wpilib.driverstation.Joystick;
import org.wpilib.framework.TimedRobot;
import org.wpilib.hardware.motor.PWMSparkMax;
import org.wpilib.util.sendable.SendableRegistry;
/**
* This is a demo program showing the use of the DifferentialDrive class, specifically it contains
* the code necessary to operate a robot with tank drive.
*/
public class Robot extends TimedRobot {
private final DifferentialDrive m_robotDrive;
private final Joystick m_leftStick;
private final Joystick m_rightStick;
private final PWMSparkMax m_leftMotor = new PWMSparkMax(0);
private final PWMSparkMax m_rightMotor = new PWMSparkMax(1);
/** Called once at the beginning of the robot program. */
public Robot() {
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.setInverted(true);
m_robotDrive = new DifferentialDrive(m_leftMotor::setDutyCycle, m_rightMotor::setDutyCycle);
m_leftStick = new Joystick(0);
m_rightStick = new Joystick(1);
SendableRegistry.addChild(m_robotDrive, m_leftMotor);
SendableRegistry.addChild(m_robotDrive, m_rightMotor);
}
@Override
public void teleopPeriodic() {
m_robotDrive.tankDrive(-m_leftStick.getY(), -m_rightStick.getY());
}
}