mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
Add names to simulator components (#1268)
Makes configuration easier when you can associate the items with a name instead of just a port number. Important if there is a GUI added at some point.
This commit is contained in:
committed by
Peter Johnson
parent
8d8f120cc3
commit
de212a9dd0
@@ -0,0 +1,24 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "lowfisim/SimulatorComponentBase.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
const std::string& SimulatorComponentBase::GetDisplayName() const {
|
||||
return m_name;
|
||||
}
|
||||
|
||||
void SimulatorComponentBase::SetDisplayName(const std::string& displayName) {
|
||||
m_name = displayName;
|
||||
}
|
||||
|
||||
} // namespace lowfi
|
||||
} // namespace sim
|
||||
} // namespace frc
|
||||
@@ -14,21 +14,30 @@ namespace lowfi {
|
||||
ADXLThreeAxisAccelerometerSim::ADXLThreeAxisAccelerometerSim(
|
||||
hal::ThreeAxisAccelerometerData& accelerometerWrapper)
|
||||
: m_accelerometerWrapper(accelerometerWrapper),
|
||||
m_xWrapper(std::function<void(double)>(
|
||||
m_xWrapper(std::function<bool(void)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::GetInitialized,
|
||||
&m_accelerometerWrapper)),
|
||||
std::function<void(double)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::SetX,
|
||||
&m_accelerometerWrapper, std::placeholders::_1)),
|
||||
std::function<double(void)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::GetX,
|
||||
&m_accelerometerWrapper))),
|
||||
|
||||
m_yWrapper(std::function<void(double)>(
|
||||
m_yWrapper(std::function<bool(void)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::GetInitialized,
|
||||
&m_accelerometerWrapper)),
|
||||
std::function<void(double)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::SetY,
|
||||
&m_accelerometerWrapper, std::placeholders::_1)),
|
||||
std::function<double(void)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::GetY,
|
||||
&m_accelerometerWrapper))),
|
||||
|
||||
m_zWrapper(std::function<void(double)>(
|
||||
m_zWrapper(std::function<bool(void)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::GetInitialized,
|
||||
&m_accelerometerWrapper)),
|
||||
std::function<void(double)>(
|
||||
std::bind(&hal::ThreeAxisAccelerometerData::SetZ,
|
||||
&m_accelerometerWrapper, std::placeholders::_1)),
|
||||
std::function<double(void)>(
|
||||
|
||||
@@ -14,6 +14,10 @@ namespace lowfi {
|
||||
ADXRS450_SpiGyroSim::ADXRS450_SpiGyroSim(int spiPort)
|
||||
: m_gyroWrapper(spiPort) {}
|
||||
|
||||
bool ADXRS450_SpiGyroSim::IsWrapperInitialized() const {
|
||||
return m_gyroWrapper.GetInitialized();
|
||||
}
|
||||
|
||||
void ADXRS450_SpiGyroSim::SetAngle(double angle) {
|
||||
m_gyroWrapper.SetAngle(angle);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ namespace lowfi {
|
||||
|
||||
WpiAnalogGyroSim::WpiAnalogGyroSim(int index) : m_gyroSimulator(index) {}
|
||||
|
||||
bool WpiAnalogGyroSim::IsWrapperInitialized() const {
|
||||
return m_gyroSimulator.GetInitialized();
|
||||
}
|
||||
|
||||
void WpiAnalogGyroSim::SetAngle(double angle) {
|
||||
m_gyroSimulator.SetAngle(angle);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ namespace lowfi {
|
||||
|
||||
WpiEncoderSim::WpiEncoderSim(int index) : m_encoderSimulator(index) {}
|
||||
|
||||
bool WpiEncoderSim::IsWrapperInitialized() const {
|
||||
return m_encoderSimulator.GetInitialized();
|
||||
}
|
||||
|
||||
void WpiEncoderSim::SetPosition(double position) {
|
||||
m_encoderSimulator.SetCount(
|
||||
static_cast<int>(position / m_encoderSimulator.GetDistancePerPulse()));
|
||||
|
||||
@@ -20,6 +20,10 @@ void WpiMotorSim::Update(double elapsedTime) {
|
||||
m_motorModelSimulation.Update(elapsedTime);
|
||||
}
|
||||
|
||||
bool WpiMotorSim::IsWrapperInitialized() const {
|
||||
return m_pwmSimulator.GetInitialized();
|
||||
}
|
||||
|
||||
double WpiMotorSim::GetPosition() const {
|
||||
return m_motorModelSimulation.GetPosition();
|
||||
}
|
||||
|
||||
@@ -7,11 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lowfisim/SimulatorComponent.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class AccelerometerSim {
|
||||
class AccelerometerSim : public virtual SimulatorComponent {
|
||||
public:
|
||||
virtual double GetAcceleration() = 0;
|
||||
virtual void SetAcceleration(double acceleration) = 0;
|
||||
|
||||
@@ -7,11 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lowfisim/SimulatorComponent.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class EncoderSim {
|
||||
class EncoderSim : public virtual SimulatorComponent {
|
||||
public:
|
||||
virtual void SetPosition(double position) = 0;
|
||||
virtual void SetVelocity(double velocity) = 0;
|
||||
|
||||
@@ -7,11 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lowfisim/SimulatorComponent.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class GyroSim {
|
||||
class GyroSim : public virtual SimulatorComponent {
|
||||
public:
|
||||
virtual void SetAngle(double angle) = 0;
|
||||
virtual double GetAngle() = 0;
|
||||
|
||||
@@ -7,11 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "lowfisim/SimulatorComponent.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class MotorSim {
|
||||
class MotorSim : public virtual SimulatorComponent {
|
||||
public:
|
||||
virtual double GetPosition() const = 0;
|
||||
virtual double GetVelocity() const = 0;
|
||||
|
||||
@@ -10,16 +10,22 @@
|
||||
#include <functional>
|
||||
|
||||
#include "lowfisim/AccelerometerSim.h"
|
||||
#include "lowfisim/SimulatorComponentBase.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class SimpleAccelerometerSim : public AccelerometerSim {
|
||||
class SimpleAccelerometerSim : public SimulatorComponentBase,
|
||||
public AccelerometerSim {
|
||||
public:
|
||||
SimpleAccelerometerSim(const std::function<void(double)>& setterFunction,
|
||||
using SimulatorComponentBase::GetDisplayName;
|
||||
|
||||
SimpleAccelerometerSim(const std::function<bool(void)>& initializedFunction,
|
||||
const std::function<void(double)>& setterFunction,
|
||||
const std::function<double(void)>& getterFunction)
|
||||
: m_setAccelerationFunction(setterFunction),
|
||||
: m_isInitializedFunction(initializedFunction),
|
||||
m_setAccelerationFunction(setterFunction),
|
||||
m_getAccelerationFunction(getterFunction) {}
|
||||
double GetAcceleration() override { return m_getAccelerationFunction(); }
|
||||
|
||||
@@ -27,7 +33,12 @@ class SimpleAccelerometerSim : public AccelerometerSim {
|
||||
m_setAccelerationFunction(acceleration);
|
||||
}
|
||||
|
||||
bool IsWrapperInitialized() const override {
|
||||
return m_isInitializedFunction();
|
||||
}
|
||||
|
||||
private:
|
||||
std::function<bool(void)> m_isInitializedFunction;
|
||||
std::function<void(double)> m_setAccelerationFunction;
|
||||
std::function<double(void)> m_getAccelerationFunction;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class SimulatorComponent {
|
||||
public:
|
||||
virtual ~SimulatorComponent() = default;
|
||||
|
||||
virtual bool IsWrapperInitialized() const = 0;
|
||||
|
||||
virtual const std::string& GetDisplayName() const = 0;
|
||||
|
||||
virtual void SetDisplayName(const std::string& displayName) = 0;
|
||||
};
|
||||
|
||||
} // namespace lowfi
|
||||
} // namespace sim
|
||||
} // namespace frc
|
||||
@@ -0,0 +1,33 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "lowfisim/SimulatorComponent.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class SimulatorComponentBase : public virtual SimulatorComponent {
|
||||
public:
|
||||
SimulatorComponentBase() = default;
|
||||
virtual ~SimulatorComponentBase() = default;
|
||||
|
||||
const std::string& GetDisplayName() const override;
|
||||
|
||||
void SetDisplayName(const std::string& displayName) override;
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
};
|
||||
|
||||
} // namespace lowfi
|
||||
} // namespace sim
|
||||
} // namespace frc
|
||||
@@ -9,15 +9,18 @@
|
||||
|
||||
#include "ADXRS450_SpiGyroWrapperData.h"
|
||||
#include "lowfisim/GyroSim.h"
|
||||
#include "lowfisim/SimulatorComponentBase.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class ADXRS450_SpiGyroSim : public GyroSim {
|
||||
class ADXRS450_SpiGyroSim : public SimulatorComponentBase, public GyroSim {
|
||||
public:
|
||||
explicit ADXRS450_SpiGyroSim(int spiPort);
|
||||
|
||||
bool IsWrapperInitialized() const override;
|
||||
|
||||
void SetAngle(double angle) override;
|
||||
double GetAngle() override;
|
||||
|
||||
|
||||
@@ -8,16 +8,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "lowfisim/GyroSim.h"
|
||||
#include "lowfisim/SimulatorComponentBase.h"
|
||||
#include "simulation/AnalogGyroSim.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class WpiAnalogGyroSim : public GyroSim {
|
||||
class WpiAnalogGyroSim : public SimulatorComponentBase, public GyroSim {
|
||||
public:
|
||||
explicit WpiAnalogGyroSim(int index);
|
||||
|
||||
bool IsWrapperInitialized() const override;
|
||||
|
||||
void SetAngle(double angle) override;
|
||||
double GetAngle() override;
|
||||
|
||||
|
||||
@@ -8,15 +8,17 @@
|
||||
#pragma once
|
||||
|
||||
#include "lowfisim/EncoderSim.h"
|
||||
#include "lowfisim/SimulatorComponentBase.h"
|
||||
#include "simulation/EncoderSim.h"
|
||||
|
||||
namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class WpiEncoderSim : public EncoderSim {
|
||||
class WpiEncoderSim : public SimulatorComponentBase, public EncoderSim {
|
||||
public:
|
||||
explicit WpiEncoderSim(int index);
|
||||
bool IsWrapperInitialized() const override;
|
||||
|
||||
void SetPosition(double position) override;
|
||||
void SetVelocity(double velocity) override;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "lowfisim/MotorSim.h"
|
||||
#include "lowfisim/SimulatorComponentBase.h"
|
||||
#include "lowfisim/motormodel/MotorModel.h"
|
||||
#include "simulation/PWMSim.h"
|
||||
|
||||
@@ -15,9 +16,10 @@ namespace frc {
|
||||
namespace sim {
|
||||
namespace lowfi {
|
||||
|
||||
class WpiMotorSim : public MotorSim {
|
||||
class WpiMotorSim : public SimulatorComponentBase, public MotorSim {
|
||||
public:
|
||||
explicit WpiMotorSim(int index, MotorModel& motorModelSimulator);
|
||||
bool IsWrapperInitialized() const override;
|
||||
|
||||
void Update(double elapsedTime);
|
||||
double GetPosition() const override;
|
||||
|
||||
@@ -15,17 +15,22 @@ TEST(AccelerometerTests, TestADXL345_I2CAccelerometerWrapper) {
|
||||
|
||||
frc::I2C::Port port = frc::I2C::kOnboard;
|
||||
|
||||
frc::ADXL345_I2C accel{port};
|
||||
|
||||
EXPECT_NEAR(0, accel.GetX(), EPSILON);
|
||||
EXPECT_NEAR(0, accel.GetY(), EPSILON);
|
||||
EXPECT_NEAR(0, accel.GetZ(), EPSILON);
|
||||
|
||||
hal::ADXL345_I2CData rawAdxSim(port);
|
||||
frc::sim::lowfi::ADXLThreeAxisAccelerometerSim accelerometerSim(rawAdxSim);
|
||||
frc::sim::lowfi::AccelerometerSim& xWrapper = accelerometerSim.GetXWrapper();
|
||||
frc::sim::lowfi::AccelerometerSim& yWrapper = accelerometerSim.GetYWrapper();
|
||||
frc::sim::lowfi::AccelerometerSim& zWrapper = accelerometerSim.GetZWrapper();
|
||||
EXPECT_FALSE(xWrapper.IsWrapperInitialized());
|
||||
EXPECT_FALSE(yWrapper.IsWrapperInitialized());
|
||||
EXPECT_FALSE(zWrapper.IsWrapperInitialized());
|
||||
|
||||
frc::ADXL345_I2C accel{port};
|
||||
EXPECT_NEAR(0, accel.GetX(), EPSILON);
|
||||
EXPECT_NEAR(0, accel.GetY(), EPSILON);
|
||||
EXPECT_NEAR(0, accel.GetZ(), EPSILON);
|
||||
EXPECT_TRUE(xWrapper.IsWrapperInitialized());
|
||||
EXPECT_TRUE(yWrapper.IsWrapperInitialized());
|
||||
EXPECT_TRUE(zWrapper.IsWrapperInitialized());
|
||||
|
||||
xWrapper.SetAcceleration(1.45);
|
||||
EXPECT_NEAR(1.45, accel.GetX(), EPSILON);
|
||||
|
||||
@@ -25,16 +25,23 @@ void TestGyro(frc::sim::lowfi::GyroSim& sim, frc::Gyro& gyro) {
|
||||
|
||||
TEST(GyroSimulatorTests, TestAnalogGyro) {
|
||||
int port = 1;
|
||||
frc::AnalogGyro gyro{port};
|
||||
frc::sim::lowfi::WpiAnalogGyroSim sim{port};
|
||||
EXPECT_FALSE(sim.IsWrapperInitialized());
|
||||
|
||||
frc::AnalogGyro gyro{port};
|
||||
EXPECT_TRUE(sim.IsWrapperInitialized());
|
||||
|
||||
TestGyro(sim, gyro);
|
||||
}
|
||||
|
||||
TEST(GyroSimulatorTests, TestSpiGyro) {
|
||||
frc::SPI::Port port = frc::SPI::kOnboardCS0;
|
||||
|
||||
frc::sim::lowfi::ADXRS450_SpiGyroSim sim{port};
|
||||
EXPECT_FALSE(sim.IsWrapperInitialized());
|
||||
|
||||
frc::ADXRS450_Gyro gyro{port};
|
||||
EXPECT_TRUE(sim.IsWrapperInitialized());
|
||||
|
||||
TestGyro(sim, gyro);
|
||||
}
|
||||
|
||||
@@ -14,14 +14,18 @@
|
||||
#include "lowfisim/wpisimulators/WpiMotorSim.h"
|
||||
|
||||
TEST(MotorEncoderConnectorTest, TestWithoutDistancePerPulseFullSpeed) {
|
||||
frc::Talon talon{3};
|
||||
frc::Encoder encoder{3, 1};
|
||||
|
||||
frc::sim::lowfi::SimpleMotorModel motorModelSim(6000);
|
||||
frc::sim::lowfi::WpiMotorSim motorSim(3, motorModelSim);
|
||||
frc::sim::lowfi::WpiEncoderSim encoderSim(0);
|
||||
frc::sim::lowfi::MotorEncoderConnector connector(motorSim, encoderSim);
|
||||
|
||||
EXPECT_FALSE(motorSim.IsWrapperInitialized());
|
||||
EXPECT_FALSE(encoderSim.IsWrapperInitialized());
|
||||
frc::Talon talon{3};
|
||||
frc::Encoder encoder{3, 1};
|
||||
EXPECT_TRUE(motorSim.IsWrapperInitialized());
|
||||
EXPECT_TRUE(encoderSim.IsWrapperInitialized());
|
||||
|
||||
talon.Set(-1);
|
||||
motorSim.Update(1);
|
||||
connector.Update();
|
||||
|
||||
@@ -5,9 +5,12 @@
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include <hal/HAL.h>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
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