mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[hal, wpilib] PWM Rewrite (#7845)
The HAL will only contain the output period and the raw microseconds. Higher level things such as SimDevice can handle everything else.
This commit is contained in:
@@ -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 "frc/simulation/PWMMotorControllerSim.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <hal/HAL.h>
|
||||
|
||||
#include "frc/motorcontrol/Spark.h"
|
||||
|
||||
namespace frc::sim {
|
||||
TEST(PWMMotorControllerSimTest, TestMotor) {
|
||||
frc::Spark spark{0};
|
||||
frc::sim::PWMMotorControllerSim sim{spark};
|
||||
|
||||
spark.Set(0);
|
||||
EXPECT_EQ(0, sim.GetSpeed());
|
||||
|
||||
spark.Set(0.354);
|
||||
EXPECT_EQ(0.354, sim.GetSpeed());
|
||||
|
||||
spark.Set(-0.785);
|
||||
EXPECT_EQ(-0.785, sim.GetSpeed());
|
||||
}
|
||||
} // namespace frc::sim
|
||||
@@ -12,8 +12,6 @@
|
||||
|
||||
namespace frc::sim {
|
||||
|
||||
constexpr double kPWMStepSize = 1.0 / 2000.0;
|
||||
|
||||
TEST(PWMSimTest, Initialize) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
@@ -46,115 +44,7 @@ TEST(PWMSimTest, SetPulseTime) {
|
||||
EXPECT_EQ(2290, callback.GetLastValue());
|
||||
}
|
||||
|
||||
TEST(PWMSimTest, SetSpeed) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
PWMSim sim{0};
|
||||
sim.ResetData();
|
||||
EXPECT_FALSE(sim.GetInitialized());
|
||||
|
||||
DoubleCallback callback;
|
||||
|
||||
auto cb = sim.RegisterSpeedCallback(callback.GetCallback(), false);
|
||||
PWM pwm{0};
|
||||
double kTestValue = 0.3504;
|
||||
pwm.SetSpeed(kTestValue);
|
||||
|
||||
EXPECT_NEAR(kTestValue, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_TRUE(callback.WasTriggered());
|
||||
EXPECT_NEAR(kTestValue, callback.GetLastValue(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue / 2 + 0.5, sim.GetPosition(), kPWMStepSize * 2);
|
||||
EXPECT_NEAR(kTestValue / 2 + 0.5, pwm.GetPosition(), kPWMStepSize * 2);
|
||||
|
||||
kTestValue = -1.0;
|
||||
pwm.SetSpeed(kTestValue);
|
||||
|
||||
EXPECT_NEAR(kTestValue, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(0.0, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(0.0, pwm.GetPosition(), kPWMStepSize);
|
||||
|
||||
kTestValue = 0.0;
|
||||
pwm.SetSpeed(kTestValue);
|
||||
|
||||
EXPECT_NEAR(kTestValue, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(0.5, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(0.5, pwm.GetPosition(), kPWMStepSize);
|
||||
|
||||
kTestValue = 1.0;
|
||||
pwm.SetSpeed(kTestValue);
|
||||
|
||||
EXPECT_NEAR(kTestValue, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetPosition(), kPWMStepSize);
|
||||
|
||||
kTestValue = 1.1;
|
||||
pwm.SetSpeed(kTestValue);
|
||||
|
||||
EXPECT_NEAR(1.0, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(1.0, pwm.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(1.0, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(1.0, pwm.GetPosition(), kPWMStepSize);
|
||||
}
|
||||
|
||||
TEST(PWMSimTest, SetPosition) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
PWMSim sim{0};
|
||||
sim.ResetData();
|
||||
EXPECT_FALSE(sim.GetInitialized());
|
||||
|
||||
DoubleCallback callback;
|
||||
|
||||
auto cb = sim.RegisterPositionCallback(callback.GetCallback(), false);
|
||||
PWM pwm{0};
|
||||
double kTestValue = 0.3504;
|
||||
pwm.SetPosition(kTestValue);
|
||||
|
||||
EXPECT_NEAR(kTestValue, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetPosition(), kPWMStepSize);
|
||||
EXPECT_TRUE(callback.WasTriggered());
|
||||
EXPECT_NEAR(kTestValue, callback.GetLastValue(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue * 2 - 1.0, sim.GetSpeed(), kPWMStepSize * 2);
|
||||
EXPECT_NEAR(kTestValue * 2 - 1.0, pwm.GetSpeed(), kPWMStepSize * 2);
|
||||
|
||||
kTestValue = -1.0;
|
||||
pwm.SetPosition(kTestValue);
|
||||
|
||||
EXPECT_NEAR(0.0, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(0.0, pwm.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetSpeed(), kPWMStepSize);
|
||||
|
||||
kTestValue = 0.0;
|
||||
pwm.SetPosition(kTestValue);
|
||||
|
||||
EXPECT_NEAR(kTestValue, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(-1.0, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(-1.0, pwm.GetSpeed(), kPWMStepSize);
|
||||
|
||||
kTestValue = 1.0;
|
||||
pwm.SetPosition(kTestValue);
|
||||
|
||||
EXPECT_NEAR(kTestValue, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(kTestValue, pwm.GetSpeed(), kPWMStepSize);
|
||||
|
||||
kTestValue = 1.1;
|
||||
pwm.SetPosition(kTestValue);
|
||||
|
||||
EXPECT_NEAR(1.0, sim.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(1.0, pwm.GetPosition(), kPWMStepSize);
|
||||
EXPECT_NEAR(1.0, sim.GetSpeed(), kPWMStepSize);
|
||||
EXPECT_NEAR(1.0, pwm.GetSpeed(), kPWMStepSize);
|
||||
}
|
||||
|
||||
TEST(PWMSimTest, SetPeriodScale) {
|
||||
TEST(PWMSimTest, SetOutputPeriod) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
PWMSim sim{0};
|
||||
@@ -163,28 +53,12 @@ TEST(PWMSimTest, SetPeriodScale) {
|
||||
|
||||
IntCallback callback;
|
||||
|
||||
auto cb = sim.RegisterPeriodScaleCallback(callback.GetCallback(), false);
|
||||
auto cb = sim.RegisterOutputPeriodCallback(callback.GetCallback(), false);
|
||||
PWM pwm{0};
|
||||
sim.SetPeriodScale(3504);
|
||||
EXPECT_EQ(3504, sim.GetPeriodScale());
|
||||
sim.SetOutputPeriod(3504);
|
||||
EXPECT_EQ(3504, sim.GetOutputPeriod());
|
||||
EXPECT_TRUE(callback.WasTriggered());
|
||||
EXPECT_EQ(3504, callback.GetLastValue());
|
||||
}
|
||||
|
||||
TEST(PWMSimTest, SetZeroLatch) {
|
||||
HAL_Initialize(500, 0);
|
||||
|
||||
PWMSim sim{0};
|
||||
sim.ResetData();
|
||||
EXPECT_FALSE(sim.GetInitialized());
|
||||
|
||||
BooleanCallback callback;
|
||||
|
||||
auto cb = sim.RegisterZeroLatchCallback(callback.GetCallback(), false);
|
||||
PWM pwm{0};
|
||||
pwm.SetZeroLatch();
|
||||
|
||||
EXPECT_TRUE(callback.WasTriggered());
|
||||
}
|
||||
|
||||
} // namespace frc::sim
|
||||
|
||||
32
wpilibc/src/test/native/cpp/simulation/ServoSimTest.cpp
Normal file
32
wpilibc/src/test/native/cpp/simulation/ServoSimTest.cpp
Normal 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 "frc/simulation/ServoSim.h" // NOLINT(build/include_order)
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <hal/HAL.h>
|
||||
|
||||
#include "frc/Servo.h"
|
||||
|
||||
namespace frc::sim {
|
||||
TEST(ServoSimTest, TestServo) {
|
||||
frc::Servo servo{0};
|
||||
frc::sim::ServoSim sim{servo};
|
||||
|
||||
servo.Set(0);
|
||||
EXPECT_EQ(0, sim.GetPosition());
|
||||
|
||||
servo.Set(0.354);
|
||||
EXPECT_EQ(0.354, sim.GetPosition());
|
||||
|
||||
servo.SetAngle(10);
|
||||
EXPECT_EQ(10, sim.GetAngle());
|
||||
|
||||
servo.SetAngle(90);
|
||||
EXPECT_EQ(90, sim.GetAngle());
|
||||
|
||||
servo.SetAngle(170);
|
||||
EXPECT_EQ(170, sim.GetAngle());
|
||||
}
|
||||
} // namespace frc::sim
|
||||
Reference in New Issue
Block a user