[examples] Add unit testing infrastructure (#4646)

This commit is contained in:
Starlight220
2022-12-02 18:40:14 +02:00
committed by GitHub
parent 74cc86c4c5
commit 66bb0ffb2c
15 changed files with 444 additions and 13 deletions

View File

@@ -0,0 +1,32 @@
// 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.
#include "Robot.h"
/**
* This function is called periodically during operator control.
*/
void Robot::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();
}
}
}
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -0,0 +1,26 @@
// 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.
#include "subsystems/Intake.h"
void Intake::Deploy() {
m_piston.Set(frc::DoubleSolenoid::Value::kForward);
}
void Intake::Retract() {
m_piston.Set(frc::DoubleSolenoid::Value::kReverse);
m_motor.Set(0); // turn off the motor
}
void Intake::Activate(double speed) {
if (IsDeployed()) {
m_motor.Set(speed);
} else { // if piston isn't open, do nothing
m_motor.Set(0);
}
}
bool Intake::IsDeployed() const {
return m_piston.Get() == frc::DoubleSolenoid::Value::kForward;
}

View File

@@ -0,0 +1,26 @@
// 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.
#pragma once
/**
* The Constants header provides a convenient place for teams to hold robot-wide
* numerical or bool constants. This should not be used for any other purpose.
*
* It is generally a good idea to place constants into subsystem- or
* command-specific namespaces within this header, which can then be used where
* they are needed.
*/
namespace IntakeConstants {
constexpr int kMotorPort = 1;
constexpr int kPistonFwdChannel = 0;
constexpr int kPistonRevChannel = 1;
constexpr double kIntakeSpeed = 0.5;
} // namespace IntakeConstants
namespace OperatorConstants {
constexpr int kJoystickIndex = 0;
} // namespace OperatorConstants

View File

@@ -0,0 +1,20 @@
// 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.
#pragma once
#include <frc/Joystick.h>
#include <frc/TimedRobot.h>
#include "Constants.h"
#include "subsystems/Intake.h"
class Robot : public frc::TimedRobot {
public:
void TeleopPeriodic() override;
private:
Intake m_intake;
frc::Joystick m_joystick{OperatorConstants::kJoystickIndex};
};

View File

@@ -0,0 +1,24 @@
// 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.
#pragma once
#include <frc/DoubleSolenoid.h>
#include <frc/motorcontrol/PWMSparkMax.h>
#include "Constants.h"
class Intake {
public:
void Deploy();
void Retract();
void Activate(double speed);
bool IsDeployed() const;
private:
frc::PWMSparkMax m_motor{IntakeConstants::kMotorPort};
frc::DoubleSolenoid m_piston{frc::PneumaticsModuleType::CTREPCM,
IntakeConstants::kPistonFwdChannel,
IntakeConstants::kPistonRevChannel};
};

View File

@@ -622,7 +622,6 @@
],
"foldername": "StateSpaceFlywheel",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -641,7 +640,6 @@
],
"foldername": "StateSpaceFlywheelSysId",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -657,7 +655,6 @@
],
"foldername": "StateSpaceElevator",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -673,7 +670,6 @@
],
"foldername": "StateSpaceArm",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -690,7 +686,6 @@
],
"foldername": "ElevatorSimulation",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -706,7 +701,6 @@
],
"foldername": "DifferentialDrivePoseEstimator",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -722,7 +716,6 @@
],
"foldername": "MecanumDrivePoseEstimator",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -740,7 +733,16 @@
],
"foldername": "ArmSimulation",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
"name": "UnitTesting",
"description": "Demonstrates basic unit testing for a robot project.",
"tags": [
"Testing"
],
"foldername": "UnitTest",
"gradlebase": "cpp",
"commandversion": 2
},
{
@@ -758,7 +760,6 @@
],
"foldername": "SimpleDifferentialDriveSimulation",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -776,7 +777,6 @@
],
"foldername": "StateSpaceDifferentialDriveSimulation",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
},
{
@@ -793,7 +793,6 @@
],
"foldername": "SwerveDrivePoseEstimator",
"gradlebase": "cpp",
"mainclass": "Main",
"commandversion": 2
}
]