mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[examples] Add unit testing infrastructure (#4646)
This commit is contained in:
@@ -733,6 +733,17 @@
|
||||
"mainclass": "Main",
|
||||
"commandversion": 2
|
||||
},
|
||||
{
|
||||
"name": "UnitTesting",
|
||||
"description": "Demonstrates basic unit testing for a robot project.",
|
||||
"tags": [
|
||||
"Testing"
|
||||
],
|
||||
"foldername": "unittest",
|
||||
"gradlebase": "java",
|
||||
"mainclass": "Main",
|
||||
"commandversion": 2
|
||||
},
|
||||
{
|
||||
"name": "DifferentialDrivePoseEstimator",
|
||||
"description": "Demonstrates the use of the DifferentialDrivePoseEstimator as a replacement for differential drive odometry.",
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.unittest;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
public static final int kJoystickIndex = 0;
|
||||
|
||||
public static final class IntakeConstants {
|
||||
public static final int kMotorPort = 1;
|
||||
|
||||
public static final int kPistonFwdChannel = 0;
|
||||
public static final int kPistonRevChannel = 1;
|
||||
public static final double kIntakeSpeed = 0.5;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.unittest;
|
||||
|
||||
import edu.wpi.first.wpilibj.RobotBase;
|
||||
|
||||
/**
|
||||
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
|
||||
* you are doing, do not modify this file except to change the parameter class to the startRobot
|
||||
* call.
|
||||
*/
|
||||
public final class Main {
|
||||
private Main() {}
|
||||
|
||||
/**
|
||||
* Main initialization function. Do not perform any initialization here.
|
||||
*
|
||||
* <p>If you change your main robot class, change the parameter type.
|
||||
*/
|
||||
public static void main(String... args) {
|
||||
RobotBase.startRobot(Robot::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.unittest;
|
||||
|
||||
import edu.wpi.first.wpilibj.Joystick;
|
||||
import edu.wpi.first.wpilibj.TimedRobot;
|
||||
import edu.wpi.first.wpilibj.examples.unittest.Constants.IntakeConstants;
|
||||
import edu.wpi.first.wpilibj.examples.unittest.subsystems.Intake;
|
||||
|
||||
/**
|
||||
* The VM is configured to automatically run this class, and to call the functions 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 build.gradle file in the
|
||||
* project.
|
||||
*/
|
||||
public class Robot extends TimedRobot {
|
||||
private Intake m_intake = new Intake();
|
||||
private Joystick m_joystick = new Joystick(Constants.kJoystickIndex);
|
||||
|
||||
/** This function is called periodically during operator control. */
|
||||
@Override
|
||||
public void teleopPeriodic() {
|
||||
// Activate the intake while the trigger is held
|
||||
if (m_joystick.getTrigger()) {
|
||||
m_intake.activate(IntakeConstants.kIntakeSpeed);
|
||||
} else {
|
||||
m_intake.activate(0);
|
||||
}
|
||||
|
||||
// Toggle deploying the intake when the top button is pressed
|
||||
if (m_joystick.getTop()) {
|
||||
if (m_intake.isDeployed()) {
|
||||
m_intake.retract();
|
||||
} else {
|
||||
m_intake.deploy();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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 edu.wpi.first.wpilibj.examples.unittest.subsystems;
|
||||
|
||||
import edu.wpi.first.wpilibj.DoubleSolenoid;
|
||||
import edu.wpi.first.wpilibj.PneumaticsModuleType;
|
||||
import edu.wpi.first.wpilibj.examples.unittest.Constants.IntakeConstants;
|
||||
import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;
|
||||
|
||||
public class Intake implements AutoCloseable {
|
||||
private final PWMSparkMax m_motor;
|
||||
private final DoubleSolenoid m_piston;
|
||||
|
||||
public Intake() {
|
||||
m_motor = new PWMSparkMax(IntakeConstants.kMotorPort);
|
||||
m_piston =
|
||||
new DoubleSolenoid(
|
||||
PneumaticsModuleType.CTREPCM,
|
||||
IntakeConstants.kPistonFwdChannel,
|
||||
IntakeConstants.kPistonRevChannel);
|
||||
}
|
||||
|
||||
public void deploy() {
|
||||
m_piston.set(DoubleSolenoid.Value.kForward);
|
||||
}
|
||||
|
||||
public void retract() {
|
||||
m_piston.set(DoubleSolenoid.Value.kReverse);
|
||||
m_motor.set(0); // turn off the motor
|
||||
}
|
||||
|
||||
public void activate(double speed) {
|
||||
if (isDeployed()) {
|
||||
m_motor.set(speed);
|
||||
} else { // if piston isn't open, do nothing
|
||||
m_motor.set(0);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDeployed() {
|
||||
return m_piston.get() == DoubleSolenoid.Value.kForward;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
m_piston.close();
|
||||
m_motor.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user