mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[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:
@@ -1,166 +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.
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "Robot.hpp"
|
||||
#include "wpi/hal/DriverStationTypes.h"
|
||||
#include "wpi/hal/simulation/MockHooks.h"
|
||||
#include "wpi/math/system/DCMotor.hpp"
|
||||
#include "wpi/simulation/AnalogInputSim.hpp"
|
||||
#include "wpi/simulation/DriverStationSim.hpp"
|
||||
#include "wpi/simulation/ElevatorSim.hpp"
|
||||
#include "wpi/simulation/JoystickSim.hpp"
|
||||
#include "wpi/simulation/PWMMotorControllerSim.hpp"
|
||||
#include "wpi/simulation/SimHooks.hpp"
|
||||
#include "wpi/system/RobotController.hpp"
|
||||
#include "wpi/units/length.hpp"
|
||||
#include "wpi/units/mass.hpp"
|
||||
#include "wpi/units/time.hpp"
|
||||
|
||||
class PotentiometerPIDTest : public testing::Test {
|
||||
wpi::math::DCMotor m_elevatorGearbox = wpi::math::DCMotor::Vex775Pro(4);
|
||||
static constexpr double kElevatorGearing = 10.0;
|
||||
static constexpr wpi::units::meter_t kElevatorDrumRadius = 2.0_in;
|
||||
static constexpr wpi::units::kilogram_t kCarriageMass = 4.0_kg;
|
||||
|
||||
Robot m_robot;
|
||||
std::optional<std::thread> m_thread;
|
||||
|
||||
protected:
|
||||
wpi::sim::ElevatorSim m_elevatorSim{m_elevatorGearbox,
|
||||
kElevatorGearing,
|
||||
kCarriageMass,
|
||||
kElevatorDrumRadius,
|
||||
0.0_m,
|
||||
Robot::kFullHeight,
|
||||
true,
|
||||
0.0_m};
|
||||
wpi::sim::PWMMotorControllerSim m_motorSim{Robot::kMotorChannel};
|
||||
wpi::sim::AnalogInputSim m_analogSim{Robot::kPotChannel};
|
||||
wpi::sim::JoystickSim m_joystickSim{Robot::kJoystickChannel};
|
||||
int32_t m_callback;
|
||||
int32_t m_port;
|
||||
|
||||
public:
|
||||
void SimPeriodicBefore() {
|
||||
m_elevatorSim.SetInputVoltage(m_motorSim.GetDutyCycle() *
|
||||
wpi::RobotController::GetBatteryVoltage());
|
||||
m_elevatorSim.Update(20_ms);
|
||||
|
||||
/*
|
||||
meters = (v / 3.3v) * range
|
||||
meters / range = v / 3.3v
|
||||
3.3v * (meters / range) = v
|
||||
*/
|
||||
m_analogSim.SetVoltage(
|
||||
(wpi::RobotController::GetVoltage3V3() *
|
||||
(m_elevatorSim.GetPosition().value() / Robot::kFullHeight))
|
||||
.value());
|
||||
}
|
||||
|
||||
static void CallSimPeriodicBefore(void* param) {
|
||||
static_cast<PotentiometerPIDTest*>(param)->SimPeriodicBefore();
|
||||
}
|
||||
|
||||
void SetUp() override {
|
||||
wpi::sim::PauseTiming();
|
||||
wpi::sim::SetProgramStarted(false);
|
||||
wpi::sim::DriverStationSim::ResetData();
|
||||
|
||||
m_joystickSim.SetButtonsMaximumIndex(12);
|
||||
|
||||
m_callback =
|
||||
HALSIM_RegisterSimPeriodicBeforeCallback(CallSimPeriodicBefore, this);
|
||||
|
||||
m_thread = std::thread([&] { m_robot.StartCompetition(); });
|
||||
wpi::sim::WaitForProgramStart();
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
m_robot.EndCompetition();
|
||||
m_thread->join();
|
||||
|
||||
HALSIM_CancelSimPeriodicBeforeCallback(m_callback);
|
||||
m_analogSim.ResetData();
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(PotentiometerPIDTest, Teleop) {
|
||||
// teleop init
|
||||
{
|
||||
wpi::sim::DriverStationSim::SetRobotMode(HAL_ROBOTMODE_TELEOPERATED);
|
||||
wpi::sim::DriverStationSim::SetEnabled(true);
|
||||
wpi::sim::DriverStationSim::NotifyNewData();
|
||||
|
||||
EXPECT_TRUE(m_analogSim.GetInitialized());
|
||||
}
|
||||
|
||||
// first setpoint
|
||||
{
|
||||
// advance 50 timesteps
|
||||
wpi::sim::StepTiming(1_s);
|
||||
|
||||
EXPECT_NEAR(Robot::kSetpoints[0].value(),
|
||||
m_elevatorSim.GetPosition().value(), 0.1);
|
||||
}
|
||||
|
||||
// second setpoint
|
||||
{
|
||||
// press button to advance setpoint
|
||||
m_joystickSim.SetTrigger(true);
|
||||
m_joystickSim.NotifyNewData();
|
||||
|
||||
// advance 50 timesteps
|
||||
wpi::sim::StepTiming(1_s);
|
||||
|
||||
EXPECT_NEAR(Robot::kSetpoints[1].value(),
|
||||
m_elevatorSim.GetPosition().value(), 0.1);
|
||||
}
|
||||
|
||||
// we need to unpress the button
|
||||
{
|
||||
m_joystickSim.SetTrigger(false);
|
||||
m_joystickSim.NotifyNewData();
|
||||
|
||||
// advance 10 timesteps
|
||||
wpi::sim::StepTiming(0.2_s);
|
||||
}
|
||||
|
||||
// third setpoint
|
||||
{
|
||||
// press button to advance setpoint
|
||||
m_joystickSim.SetTrigger(true);
|
||||
m_joystickSim.NotifyNewData();
|
||||
|
||||
// advance 50 timesteps
|
||||
wpi::sim::StepTiming(1_s);
|
||||
|
||||
EXPECT_NEAR(Robot::kSetpoints[2].value(),
|
||||
m_elevatorSim.GetPosition().value(), 0.1);
|
||||
}
|
||||
|
||||
// we need to unpress the button
|
||||
{
|
||||
m_joystickSim.SetTrigger(false);
|
||||
m_joystickSim.NotifyNewData();
|
||||
|
||||
// advance 10 timesteps
|
||||
wpi::sim::StepTiming(0.2_s);
|
||||
}
|
||||
|
||||
// rollover: first setpoint
|
||||
{
|
||||
// press button to advance setpoint
|
||||
m_joystickSim.SetTrigger(true);
|
||||
m_joystickSim.NotifyNewData();
|
||||
|
||||
// advance 60 timesteps
|
||||
wpi::sim::StepTiming(1.2_s);
|
||||
EXPECT_NEAR(Robot::kSetpoints[0].value(),
|
||||
m_elevatorSim.GetPosition().value(), 0.1);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +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.
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "wpi/hal/HAL.h"
|
||||
|
||||
/**
|
||||
* Runs all unit tests.
|
||||
*/
|
||||
int main(int argc, char** argv) {
|
||||
HAL_Initialize(500, 0);
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
int ret = RUN_ALL_TESTS();
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user