Implement sim devices for ADXL345, ADXL362, ADXRS450, Ultrasonic

This makes the halsim_adx_gyro_accelerometer simulation plugin and
the accelerometer part of lowfi_simulation obsolete.
This commit is contained in:
Peter Johnson
2019-09-29 00:57:16 -07:00
parent aa90645865
commit a9f0e46680
42 changed files with 371 additions and 1337 deletions

View File

@@ -1,46 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-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/wpisimulators/ADXLThreeAxisAccelerometerSim.h"
namespace frc {
namespace sim {
namespace lowfi {
ADXLThreeAxisAccelerometerSim::ADXLThreeAxisAccelerometerSim(
hal::ThreeAxisAccelerometerData& accelerometerWrapper)
: m_accelerometerWrapper(accelerometerWrapper),
m_xWrapper(
[data = &m_accelerometerWrapper] { return data->GetInitialized(); },
[data = &m_accelerometerWrapper](double x) { data->x = x; },
[data = &m_accelerometerWrapper] { return data->GetX(); }),
m_yWrapper(
[data = &m_accelerometerWrapper] { return data->GetInitialized(); },
[data = &m_accelerometerWrapper](double y) { data->y = y; },
[data = &m_accelerometerWrapper] { return data->GetY(); }),
m_zWrapper(
[data = &m_accelerometerWrapper] { return data->GetInitialized(); },
[data = &m_accelerometerWrapper](double z) { data->z = z; },
[data = &m_accelerometerWrapper] { return data->GetZ(); }) {}
AccelerometerSim& ADXLThreeAxisAccelerometerSim::GetXWrapper() {
return m_xWrapper;
}
AccelerometerSim& ADXLThreeAxisAccelerometerSim::GetYWrapper() {
return m_yWrapper;
}
AccelerometerSim& ADXLThreeAxisAccelerometerSim::GetZWrapper() {
return m_zWrapper;
}
} // namespace lowfi
} // namespace sim
} // namespace frc

View File

@@ -1,29 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-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/wpisimulators/ADXRS450_SpiGyroSim.h"
namespace frc {
namespace sim {
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);
}
double ADXRS450_SpiGyroSim::GetAngle() { return m_gyroWrapper.GetAngle(); }
} // namespace lowfi
} // namespace sim
} // namespace frc

View File

@@ -1,35 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-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 "ThreeAxisAccelerometerData.h"
#include "lowfisim/SimpleAccelerometerSim.h"
namespace frc {
namespace sim {
namespace lowfi {
class ADXLThreeAxisAccelerometerSim {
public:
ADXLThreeAxisAccelerometerSim(
hal::ThreeAxisAccelerometerData& accelerometerWrapper);
AccelerometerSim& GetXWrapper();
AccelerometerSim& GetYWrapper();
AccelerometerSim& GetZWrapper();
protected:
hal::ThreeAxisAccelerometerData& m_accelerometerWrapper;
SimpleAccelerometerSim m_xWrapper;
SimpleAccelerometerSim m_yWrapper;
SimpleAccelerometerSim m_zWrapper;
};
} // namespace lowfi
} // namespace sim
} // namespace frc

View File

@@ -1,32 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 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 "ADXRS450_SpiGyroWrapperData.h"
#include "lowfisim/GyroSim.h"
namespace frc {
namespace sim {
namespace lowfi {
class ADXRS450_SpiGyroSim : public GyroSim {
public:
explicit ADXRS450_SpiGyroSim(int spiPort);
bool IsWrapperInitialized() const override;
void SetAngle(double angle) override;
double GetAngle() override;
protected:
hal::ADXRS450_SpiGyroWrapper m_gyroWrapper;
};
} // namespace lowfi
} // namespace sim
} // namespace frc

View File

@@ -1,58 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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 "ADXL345_I2CAccelerometerData.h"
#include "frc/ADXL345_I2C.h"
#include "gtest/gtest.h"
#include "lowfisim/wpisimulators/ADXLThreeAxisAccelerometerSim.h"
TEST(AccelerometerTests, TestADXL345_I2CAccelerometerWrapper) {
const double EPSILON = 1 / 256.0;
frc::I2C::Port port = frc::I2C::kOnboard;
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);
EXPECT_NEAR(0, accel.GetY(), EPSILON);
EXPECT_NEAR(0, accel.GetZ(), EPSILON);
EXPECT_NEAR(1.45, xWrapper.GetAcceleration(), EPSILON);
EXPECT_NEAR(0, yWrapper.GetAcceleration(), EPSILON);
EXPECT_NEAR(0, zWrapper.GetAcceleration(), EPSILON);
yWrapper.SetAcceleration(-.67);
EXPECT_NEAR(1.45, accel.GetX(), EPSILON);
EXPECT_NEAR(-.67, accel.GetY(), EPSILON);
EXPECT_NEAR(0, accel.GetZ(), EPSILON);
EXPECT_NEAR(1.45, xWrapper.GetAcceleration(), EPSILON);
EXPECT_NEAR(-.67, yWrapper.GetAcceleration(), EPSILON);
EXPECT_NEAR(0, zWrapper.GetAcceleration(), EPSILON);
zWrapper.SetAcceleration(2.42);
EXPECT_NEAR(1.45, accel.GetX(), EPSILON);
EXPECT_NEAR(-.67, accel.GetY(), EPSILON);
EXPECT_NEAR(2.42, accel.GetZ(), EPSILON);
EXPECT_NEAR(1.45, xWrapper.GetAcceleration(), EPSILON);
EXPECT_NEAR(-.67, yWrapper.GetAcceleration(), EPSILON);
EXPECT_NEAR(2.42, zWrapper.GetAcceleration(), EPSILON);
}

View File

@@ -1,47 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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 "ADXRS450_SpiGyroWrapperData.h"
#include "frc/ADXRS450_Gyro.h"
#include "frc/AnalogGyro.h"
#include "gtest/gtest.h"
#include "lowfisim/wpisimulators/ADXRS450_SpiGyroSim.h"
#include "lowfisim/wpisimulators/WpiAnalogGyroSim.h"
void TestGyro(frc::sim::lowfi::GyroSim& sim, frc::Gyro& gyro) {
const double EPSILON = .00001;
EXPECT_NEAR(0, gyro.GetAngle(), EPSILON);
EXPECT_NEAR(0, sim.GetAngle(), EPSILON);
sim.SetAngle(45.13);
EXPECT_NEAR(45.13, gyro.GetAngle(), EPSILON);
EXPECT_NEAR(45.13, sim.GetAngle(), EPSILON);
}
TEST(GyroSimulatorTests, TestAnalogGyro) {
int port = 1;
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);
}