[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};
};