diff --git a/simulation/halsim_adx_gyro_accelerometer/build.gradle b/simulation/halsim_adx_gyro_accelerometer/build.gradle index a65f70280c..7d3c3d4f9d 100644 --- a/simulation/halsim_adx_gyro_accelerometer/build.gradle +++ b/simulation/halsim_adx_gyro_accelerometer/build.gradle @@ -1,14 +1,35 @@ -description = "A simulation library for the ADXL345 I2C and SPI Accelerometer, ADXL362 SPI Accelerometer, and ADXRS450 SPI Gyro" +apply plugin: 'cpp' +apply plugin: 'google-test-test-suite' +apply plugin: 'visual-studio' +apply plugin: 'edu.wpi.first.NativeUtils' +apply plugin: SingleNativeBuild +apply plugin: ExtraTasks ext { - includeWpiutil = true - pluginName = 'halsim_adx_gyro_accelerometer' + nativeName = 'halsim_adx_gyro_accelerometer' } -apply from: "${rootDir}/shared/plugins/setupBuild.gradle" - +apply from: "${rootDir}/shared/config.gradle" if (!project.hasProperty('onlyAthena')) { + + ext { + sharedCvConfigs = [halsim_adx_gyro_accelerometerTest: []] + staticCvConfigs = [:] + useJava = false + useCpp = true + } + apply from: "${rootDir}/shared/opencv.gradle" + + ext { + staticGtestConfigs = [:] + } + staticGtestConfigs["${nativeName}Test"] = [] + apply from: "${rootDir}/shared/googletest.gradle" + + project(':').libraryBuild.dependsOn build + + model { exportsConfigs { halsim_adx_gyro_accelerometer(ExportsConfig) { @@ -22,5 +43,142 @@ if (!project.hasProperty('onlyAthena')) { '_TI5?AVfailure'] } } + components { + "${nativeName}Base"(NativeLibrarySpec) { + sources { + cpp { + source { + srcDirs = ['src/main/native/cpp'] + include '**/*.cpp' + } + exportedHeaders { + srcDirs 'src/main/native/include' + } + } + } + binaries.all { + if (it instanceof SharedLibraryBinarySpec) { + it.buildable = false + return + } + if (it.targetPlatform.architecture.name == 'athena') { + it.buildable = false + return + } + lib project: ':hal', library: 'hal', linkage: 'shared' + lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' + } + } + "${nativeName}"(NativeLibrarySpec) { + sources { + cpp { + source { + srcDirs "${rootDir}/shared/singlelib" + include '**/*.cpp' + } + exportedHeaders { + srcDirs 'src/main/native/include' + } + } + } + binaries.all { + if (it.targetPlatform.architecture.name == 'athena') { + it.buildable = false + return + } + lib project: ':hal', library: 'hal', linkage: 'shared' + lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' + } + } + // By default, a development executable will be generated. This is to help the case of + // testing specific functionality of the library. + "${nativeName}Dev"(NativeExecutableSpec) { + sources { + cpp { + source { + srcDirs 'src/dev/native/cpp' + include '**/*.cpp' + lib library: "${nativeName}" + } + exportedHeaders { + srcDirs 'src/dev/native/include' + } + } + } + binaries.all { + if (it.targetPlatform.architecture.name == 'athena') { + it.buildable = false + return + } + } + } + } + binaries { + withType(GoogleTestTestSuiteBinarySpec) { + lib project: ':ntcore', library: 'ntcore', linkage: 'shared' + lib project: ':cscore', library: 'cscore', linkage: 'shared' + lib project: ':hal', library: 'hal', linkage: 'shared' + lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' + lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared' + lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared' + project(':ni-libraries').addNiLibrariesToLinker(it) + lib library: nativeName, linkage: 'shared' + } + } + } + + apply from: "publish.gradle" +} + +model { + + testSuites { + if (!project.hasProperty('onlyAthena')) { + "${nativeName}Test"(GoogleTestTestSuiteSpec) { + for(NativeComponentSpec c : $.components) { + if (c.name == nativeName) { + testing c + break + } + } + sources { + cpp { + source { + srcDirs 'src/test/native/cpp' + include '**/*.cpp' + } + exportedHeaders { + srcDirs 'src/test/native/include', 'src/main/native/cpp' + } + } + } + } + } + } + tasks { + def c = $.components + project.tasks.create('runCpp', Exec) { + def found = false + c.each { + if (it in NativeExecutableSpec && it.name == "${nativeName}Dev") { + it.binaries.each { + if (!found) { + def arch = it.targetPlatform.architecture.name + if (arch == 'x86-64' || arch == 'x86') { + dependsOn it.tasks.install + commandLine it.tasks.install.runScriptFile.get().asFile.toString() + + found = true + } + } + } + } + } + } } } + +tasks.withType(RunTestExecutable) { + args "--gtest_output=xml:test_detail.xml" + outputs.dir outputDir +} diff --git a/simulation/halsim_adx_gyro_accelerometer/publish.gradle b/simulation/halsim_adx_gyro_accelerometer/publish.gradle new file mode 100644 index 0000000000..d292bb72d7 --- /dev/null +++ b/simulation/halsim_adx_gyro_accelerometer/publish.gradle @@ -0,0 +1,80 @@ +apply plugin: 'maven-publish' + +def pubVersion = '' +if (project.hasProperty("publishVersion")) { + pubVersion = project.publishVersion +} else { + pubVersion = WPILibVersion.version +} + +def baseArtifactId = nativeName +def artifactGroupId = 'edu.wpi.first.halsim' +def zipBaseName = "_GROUP_edu_wpi_first_halsim_ID_${nativeName}_CLS" + +def outputsFolder = file("$project.buildDir/outputs") + +task cppSourcesZip(type: Zip) { + destinationDir = outputsFolder + baseName = zipBaseName + classifier = "sources" + + from(licenseFile) { + into '/' + } + + from('src/main/native/cpp') { + into '/' + } +} + +task cppHeadersZip(type: Zip) { + destinationDir = outputsFolder + baseName = zipBaseName + classifier = "headers" + + from(licenseFile) { + into '/' + } + + from('src/main/native/include') { + into '/' + } +} + +build.dependsOn cppSourcesZip +build.dependsOn cppHeadersZip + +addTaskToCopyAllOutputs(cppSourcesZip) +addTaskToCopyAllOutputs(cppHeadersZip) + +model { + publishing { + def halsim_adx_gyro_accelerometerTaskList = createComponentZipTasks($.components, nativeName, zipBaseName, Zip, project, includeStandardZipFormat) + def allTask + if (!project.hasProperty('jenkinsBuild')) { + allTask = createAllCombined(halsim_adx_gyro_accelerometerTaskList, nativeName, zipBaseName, Zip, project) + } + + + + publications { + cpp(MavenPublication) { + halsim_adx_gyro_accelerometerTaskList.each { + artifact it + } + + if (!project.hasProperty('jenkinsBuild')) { + artifact allTask + } + + artifact cppHeadersZip + artifact cppSourcesZip + + + artifactId = baseArtifactId + groupId artifactGroupId + version pubVersion + } + } + } +} diff --git a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_I2CAccelerometerTest.cpp b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_I2CAccelerometerTest.cpp index a432901fd6..aa5886180f 100644 --- a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_I2CAccelerometerTest.cpp +++ b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_I2CAccelerometerTest.cpp @@ -5,9 +5,8 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "ADXL345_I2C.h" #include "ADXL345_I2CAccelerometerData.h" -#include "Talon.h" +#include "frc/ADXL345_I2C.h" #include "gtest/gtest.h" class ADXL345_I2CAccelerometerTest @@ -44,4 +43,5 @@ TEST_P(ADXL345_I2CAccelerometerTest, TestAccelerometer) { INSTANTIATE_TEST_CASE_P(ADXL345_I2CAccelerometerTest, ADXL345_I2CAccelerometerTest, - ::testing::Values(frc::I2C::kOnboard, frc::I2C::kMXP)); + ::testing::Values(frc::I2C::kOnboard, + frc::I2C::kMXP), ); diff --git a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_SpiAccelerometerTest.cpp b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_SpiAccelerometerTest.cpp index 1a437d7527..548240d106 100644 --- a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_SpiAccelerometerTest.cpp +++ b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL345_SpiAccelerometerTest.cpp @@ -5,9 +5,8 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "ADXL345_SPI.h" #include "ADXL345_SpiAccelerometerData.h" -#include "Talon.h" +#include "frc/ADXL345_SPI.h" #include "gtest/gtest.h" class ADXL345_SpiAccelerometerTest @@ -46,4 +45,4 @@ INSTANTIATE_TEST_CASE_P( ADXL345_SpiAccelerometerTest, ADXL345_SpiAccelerometerTest, ::testing::Values(frc::SPI::kOnboardCS0, frc::SPI::kOnboardCS1, frc::SPI::kOnboardCS2, frc::SPI::kOnboardCS3, - frc::SPI::kMXP)); + frc::SPI::kMXP), ); diff --git a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL362_SpiAccelerometerTest.cpp b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL362_SpiAccelerometerTest.cpp index 6e445ffc1f..67efd18b22 100644 --- a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL362_SpiAccelerometerTest.cpp +++ b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXL362_SpiAccelerometerTest.cpp @@ -5,9 +5,8 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "ADXL362.h" #include "ADXL362_SpiAccelerometerData.h" -#include "Talon.h" +#include "frc/ADXL362.h" #include "gtest/gtest.h" class ADXL362_SpiAccelerometerTest @@ -46,4 +45,4 @@ INSTANTIATE_TEST_CASE_P( ADXL362_SpiAccelerometerTest, ADXL362_SpiAccelerometerTest, ::testing::Values(frc::SPI::kOnboardCS0, frc::SPI::kOnboardCS1, frc::SPI::kOnboardCS2, frc::SPI::kOnboardCS3, - frc::SPI::kMXP)); + frc::SPI::kMXP), ); diff --git a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXRS450_SpiGyroWrapperTest.cpp b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXRS450_SpiGyroWrapperTest.cpp index ebb42fa297..3a99ee45c7 100644 --- a/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXRS450_SpiGyroWrapperTest.cpp +++ b/simulation/halsim_adx_gyro_accelerometer/src/test/native/cpp/ADXRS450_SpiGyroWrapperTest.cpp @@ -5,9 +5,8 @@ /* the project. */ /*----------------------------------------------------------------------------*/ -#include "ADXRS450_Gyro.h" #include "ADXRS450_SpiGyroWrapperData.h" -#include "Talon.h" +#include "frc/ADXRS450_Gyro.h" #include "gtest/gtest.h" class ADXRS450_SpiGyroWrapperTest @@ -33,4 +32,4 @@ INSTANTIATE_TEST_CASE_P( ADXRS450_SpiGyroWrapperTest, ADXRS450_SpiGyroWrapperTest, ::testing::Values(frc::SPI::kOnboardCS0, frc::SPI::kOnboardCS1, frc::SPI::kOnboardCS2, frc::SPI::kOnboardCS3, - frc::SPI::kMXP)); + frc::SPI::kMXP), ); diff --git a/simulation/lowfi_simulation/build.gradle b/simulation/lowfi_simulation/build.gradle index 83f45d7d94..50e0002f18 100644 --- a/simulation/lowfi_simulation/build.gradle +++ b/simulation/lowfi_simulation/build.gradle @@ -49,7 +49,7 @@ if (!project.hasProperty('onlyAthena')) { sources { cpp { source { - srcDirs = ['src/main/native/cpp', "$buildDir/generated/cpp"] + srcDirs = ['src/main/native/cpp'] include '**/*.cpp' } exportedHeaders { @@ -68,7 +68,7 @@ if (!project.hasProperty('onlyAthena')) { } lib project: ':hal', library: 'hal', linkage: 'shared' lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' - project(':ni-libraries').addNiLibrariesToLinker(it) + lib project: ':simulation:halsim_adx_gyro_accelerometer', library: 'halsim_adx_gyro_accelerometer', linkage: 'shared' } } "${nativeName}"(NativeLibrarySpec) { @@ -90,7 +90,7 @@ if (!project.hasProperty('onlyAthena')) { } lib project: ':hal', library: 'hal', linkage: 'shared' lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' - project(':ni-libraries').addNiLibrariesToLinker(it) + lib project: ':simulation:halsim_adx_gyro_accelerometer', library: 'halsim_adx_gyro_accelerometer', linkage: 'shared' } } // By default, a development executable will be generated. This is to help the case of @@ -101,7 +101,7 @@ if (!project.hasProperty('onlyAthena')) { source { srcDirs 'src/dev/native/cpp' include '**/*.cpp' - lib library: 'lowfi_sim' + lib library: "${nativeName}" } exportedHeaders { srcDirs 'src/dev/native/include' @@ -115,7 +115,6 @@ if (!project.hasProperty('onlyAthena')) { } lib project: ':hal', library: 'hal', linkage: 'shared' lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' - project(':ni-libraries').addNiLibrariesToLinker(it) } } } @@ -127,7 +126,7 @@ if (!project.hasProperty('onlyAthena')) { lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared' lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared' lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared' - project(':ni-libraries').addNiLibrariesToLinker(it) + lib project: ':simulation:halsim_adx_gyro_accelerometer', library: 'halsim_adx_gyro_accelerometer', linkage: 'shared' lib library: nativeName, linkage: 'shared' } } @@ -172,6 +171,7 @@ model { def arch = it.targetPlatform.architecture.name if (arch == 'x86-64' || arch == 'x86') { dependsOn it.tasks.install + commandLine it.tasks.install.runScriptFile.get().asFile.toString() found = true } diff --git a/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/ADXLThreeAxisAccelerometerSim.cpp b/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/ADXLThreeAxisAccelerometerSim.cpp new file mode 100644 index 0000000000..50ed3361e8 --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/ADXLThreeAxisAccelerometerSim.cpp @@ -0,0 +1,52 @@ +/*----------------------------------------------------------------------------*/ +/* 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(std::function( + std::bind(&hal::ThreeAxisAccelerometerData::SetX, + &m_accelerometerWrapper, std::placeholders::_1)), + std::function( + std::bind(&hal::ThreeAxisAccelerometerData::GetX, + &m_accelerometerWrapper))), + + m_yWrapper(std::function( + std::bind(&hal::ThreeAxisAccelerometerData::SetY, + &m_accelerometerWrapper, std::placeholders::_1)), + std::function( + std::bind(&hal::ThreeAxisAccelerometerData::GetY, + &m_accelerometerWrapper))), + + m_zWrapper(std::function( + std::bind(&hal::ThreeAxisAccelerometerData::SetZ, + &m_accelerometerWrapper, std::placeholders::_1)), + std::function( + std::bind(&hal::ThreeAxisAccelerometerData::GetZ, + &m_accelerometerWrapper))) {} + +AccelerometerSim& ADXLThreeAxisAccelerometerSim::GetXWrapper() { + return m_xWrapper; +} + +AccelerometerSim& ADXLThreeAxisAccelerometerSim::GetYWrapper() { + return m_yWrapper; +} + +AccelerometerSim& ADXLThreeAxisAccelerometerSim::GetZWrapper() { + return m_zWrapper; +} + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/ADXRS450_SpiGyroSim.cpp b/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/ADXRS450_SpiGyroSim.cpp new file mode 100644 index 0000000000..1f02a41a74 --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/ADXRS450_SpiGyroSim.cpp @@ -0,0 +1,25 @@ +/*----------------------------------------------------------------------------*/ +/* 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) {} + +void ADXRS450_SpiGyroSim::SetAngle(double angle) { + m_gyroWrapper.SetAngle(angle); +} + +double ADXRS450_SpiGyroSim::GetAngle() { return m_gyroWrapper.GetAngle(); } + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/WpiAnalogGyroSim.cpp b/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/WpiAnalogGyroSim.cpp new file mode 100644 index 0000000000..b31e011c2d --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/cpp/lowfisim/wpisimulators/WpiAnalogGyroSim.cpp @@ -0,0 +1,24 @@ +/*----------------------------------------------------------------------------*/ +/* 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/WpiAnalogGyroSim.h" + +namespace frc { +namespace sim { +namespace lowfi { + +WpiAnalogGyroSim::WpiAnalogGyroSim(int index) : m_gyroSimulator(index) {} + +void WpiAnalogGyroSim::SetAngle(double angle) { + m_gyroSimulator.SetAngle(angle); +} + +double WpiAnalogGyroSim::GetAngle() { return m_gyroSimulator.GetAngle(); } + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/include/lowfisim/AccelerometerSim.h b/simulation/lowfi_simulation/src/main/native/include/lowfisim/AccelerometerSim.h new file mode 100644 index 0000000000..17b16f39a7 --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/include/lowfisim/AccelerometerSim.h @@ -0,0 +1,22 @@ +/*----------------------------------------------------------------------------*/ +/* 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 + +namespace frc { +namespace sim { +namespace lowfi { + +class AccelerometerSim { + public: + virtual double GetAcceleration() = 0; + virtual void SetAcceleration(double acceleration) = 0; +}; + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/include/lowfisim/GyroSim.h b/simulation/lowfi_simulation/src/main/native/include/lowfisim/GyroSim.h new file mode 100644 index 0000000000..41a44c4eac --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/include/lowfisim/GyroSim.h @@ -0,0 +1,22 @@ +/*----------------------------------------------------------------------------*/ +/* 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 + +namespace frc { +namespace sim { +namespace lowfi { + +class GyroSim { + public: + virtual void SetAngle(double angle) = 0; + virtual double GetAngle() = 0; +}; + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/include/lowfisim/SimpleAccelerometerSim.h b/simulation/lowfi_simulation/src/main/native/include/lowfisim/SimpleAccelerometerSim.h new file mode 100644 index 0000000000..9c8ef09078 --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/include/lowfisim/SimpleAccelerometerSim.h @@ -0,0 +1,37 @@ +/*----------------------------------------------------------------------------*/ +/* 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 + +#include "lowfisim/AccelerometerSim.h" + +namespace frc { +namespace sim { +namespace lowfi { + +class SimpleAccelerometerSim : public AccelerometerSim { + public: + SimpleAccelerometerSim(const std::function& setterFunction, + const std::function& getterFunction) + : m_setAccelerationFunction(setterFunction), + m_getAccelerationFunction(getterFunction) {} + double GetAcceleration() override { return m_getAccelerationFunction(); } + + void SetAcceleration(double acceleration) override { + m_setAccelerationFunction(acceleration); + } + + private: + std::function m_setAccelerationFunction; + std::function m_getAccelerationFunction; +}; + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/include/lowfisim/SimpleGyroSim.h b/simulation/lowfi_simulation/src/main/native/include/lowfisim/SimpleGyroSim.h new file mode 100644 index 0000000000..ea69247adb --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/include/lowfisim/SimpleGyroSim.h @@ -0,0 +1,35 @@ +/*----------------------------------------------------------------------------*/ +/* 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 + +#include "lowfisim/GyroSim.h" + +namespace frc { +namespace sim { +namespace lowfi { + +class SimpleGyroSim : public GyroSim { + public: + SimpleGyroSim(std::function& setterFunction, + std::function& getterFunction) + : m_setAngleFunction(setterFunction), + m_getAngleFunction(getterFunction) {} + double GetAngle() override { return m_getAngleFunction(); } + + void SetAngle(double angle) override { m_setAngleFunction(angle); } + + private: + std::function m_setAngleFunction; + std::function m_getAngleFunction; +}; + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/ADXLThreeAxisAccelerometerSim.h b/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/ADXLThreeAxisAccelerometerSim.h new file mode 100644 index 0000000000..0b63f2e737 --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/ADXLThreeAxisAccelerometerSim.h @@ -0,0 +1,35 @@ +/*----------------------------------------------------------------------------*/ +/* 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 diff --git a/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/ADXRS450_SpiGyroSim.h b/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/ADXRS450_SpiGyroSim.h new file mode 100644 index 0000000000..989602e3e1 --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/ADXRS450_SpiGyroSim.h @@ -0,0 +1,30 @@ +/*----------------------------------------------------------------------------*/ +/* 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 "ADXRS450_SpiGyroWrapperData.h" +#include "lowfisim/GyroSim.h" + +namespace frc { +namespace sim { +namespace lowfi { + +class ADXRS450_SpiGyroSim : public GyroSim { + public: + explicit ADXRS450_SpiGyroSim(int spiPort); + + void SetAngle(double angle) override; + double GetAngle() override; + + protected: + hal::ADXRS450_SpiGyroWrapper m_gyroWrapper; +}; + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/WpiAnalogGyroSim.h b/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/WpiAnalogGyroSim.h new file mode 100644 index 0000000000..37c3c3ca7f --- /dev/null +++ b/simulation/lowfi_simulation/src/main/native/include/lowfisim/wpisimulators/WpiAnalogGyroSim.h @@ -0,0 +1,30 @@ +/*----------------------------------------------------------------------------*/ +/* 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 "lowfisim/GyroSim.h" +#include "simulation/AnalogGyroSim.h" + +namespace frc { +namespace sim { +namespace lowfi { + +class WpiAnalogGyroSim : public GyroSim { + public: + explicit WpiAnalogGyroSim(int index); + + void SetAngle(double angle) override; + double GetAngle() override; + + protected: + frc::sim::AnalogGyroSim m_gyroSimulator; +}; + +} // namespace lowfi +} // namespace sim +} // namespace frc diff --git a/simulation/lowfi_simulation/src/test/native/cpp/lowfisim/AccelerometerSimulatorTest.cpp b/simulation/lowfi_simulation/src/test/native/cpp/lowfisim/AccelerometerSimulatorTest.cpp new file mode 100644 index 0000000000..c55dc7c2b6 --- /dev/null +++ b/simulation/lowfi_simulation/src/test/native/cpp/lowfisim/AccelerometerSimulatorTest.cpp @@ -0,0 +1,53 @@ +/*----------------------------------------------------------------------------*/ +/* 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; + + 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(); + + 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); +} diff --git a/simulation/lowfi_simulation/src/test/native/cpp/lowfisim/GyroSimulatorTest.cpp b/simulation/lowfi_simulation/src/test/native/cpp/lowfisim/GyroSimulatorTest.cpp new file mode 100644 index 0000000000..ac1f12d4be --- /dev/null +++ b/simulation/lowfi_simulation/src/test/native/cpp/lowfisim/GyroSimulatorTest.cpp @@ -0,0 +1,40 @@ +/*----------------------------------------------------------------------------*/ +/* 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::AnalogGyro gyro{port}; + frc::sim::lowfi::WpiAnalogGyroSim sim{port}; + + TestGyro(sim, gyro); +} + +TEST(GyroSimulatorTests, TestSpiGyro) { + frc::SPI::Port port = frc::SPI::kOnboardCS0; + frc::sim::lowfi::ADXRS450_SpiGyroSim sim{port}; + frc::ADXRS450_Gyro gyro{port}; + + TestGyro(sim, gyro); +}