mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Add simple motor simulation classes (#1117)
This commit is contained in:
committed by
Peter Johnson
parent
57fc614074
commit
76c901ce78
@@ -0,0 +1,106 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* 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 "Encoder.h"
|
||||
#include "LowFiSim/MotorEncoderConnector.h"
|
||||
#include "LowFiSim/MotorModel/SimpleMotorModel.h"
|
||||
#include "LowFiSim/WpiSimulators/WpiEncoderSim.h"
|
||||
#include "LowFiSim/WpiSimulators/WpiMotorSim.h"
|
||||
#include "Talon.h"
|
||||
#include "gtest/gtest.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);
|
||||
|
||||
talon.Set(-1);
|
||||
motorSim.Update(1);
|
||||
connector.Update();
|
||||
|
||||
// Position
|
||||
EXPECT_EQ(-6000, encoder.Get());
|
||||
EXPECT_DOUBLE_EQ(-6000, encoder.GetDistance());
|
||||
|
||||
// Velocity
|
||||
EXPECT_DOUBLE_EQ(-1.0 / 6000, encoder.GetPeriod());
|
||||
EXPECT_DOUBLE_EQ(-6000, encoder.GetRate());
|
||||
}
|
||||
|
||||
TEST(MotorEncoderConnectorTest, TestWithoutDistancePerPulseRealisitcUpdate) {
|
||||
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);
|
||||
|
||||
talon.Set(0.5);
|
||||
motorSim.Update(.02);
|
||||
connector.Update();
|
||||
|
||||
// Position
|
||||
EXPECT_EQ(60, encoder.Get());
|
||||
EXPECT_DOUBLE_EQ(60, encoder.GetDistance());
|
||||
|
||||
// Velocity
|
||||
EXPECT_DOUBLE_EQ(1.0 / 3000, encoder.GetPeriod());
|
||||
EXPECT_DOUBLE_EQ(3000, encoder.GetRate());
|
||||
}
|
||||
|
||||
TEST(MotorEncoderConnectorTest, TestWithDistancePerPulseFullSpeed) {
|
||||
frc::Talon talon{3};
|
||||
frc::Encoder encoder{3, 1};
|
||||
encoder.SetDistancePerPulse(.001);
|
||||
|
||||
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);
|
||||
|
||||
talon.Set(-1);
|
||||
|
||||
motorSim.Update(1);
|
||||
connector.Update();
|
||||
|
||||
// Position
|
||||
EXPECT_EQ(-6000000, encoder.Get());
|
||||
EXPECT_DOUBLE_EQ(-6000, encoder.GetDistance());
|
||||
|
||||
// Velocity
|
||||
EXPECT_EQ(-1.0 / 6000, encoder.GetPeriod());
|
||||
EXPECT_DOUBLE_EQ(-6, encoder.GetRate());
|
||||
}
|
||||
|
||||
TEST(MotorEncoderConnectorTest, TestWithDistancePerPulseRealistic) {
|
||||
frc::Talon talon{3};
|
||||
frc::Encoder encoder{3, 1};
|
||||
encoder.SetDistancePerPulse(.001);
|
||||
|
||||
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);
|
||||
|
||||
talon.Set(0.5);
|
||||
|
||||
motorSim.Update(.02);
|
||||
connector.Update();
|
||||
|
||||
// Position
|
||||
EXPECT_EQ(60000, encoder.Get());
|
||||
EXPECT_DOUBLE_EQ(60, encoder.GetDistance());
|
||||
|
||||
// Velocity
|
||||
EXPECT_EQ(1.0 / 3000, encoder.GetPeriod());
|
||||
EXPECT_DOUBLE_EQ(3, encoder.GetRate());
|
||||
}
|
||||
@@ -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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "LowFiSim/MotorModel/SimpleMotorModel.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(SimpleMotorModelSimulationTest, TestSimpleModel) {
|
||||
frc::sim::lowfi::SimpleMotorModel motorModelSim(200);
|
||||
|
||||
// Test forward voltage
|
||||
motorModelSim.SetVoltage(6);
|
||||
motorModelSim.Update(.5);
|
||||
|
||||
EXPECT_DOUBLE_EQ(50, motorModelSim.GetPosition());
|
||||
EXPECT_DOUBLE_EQ(100, motorModelSim.GetVelocity());
|
||||
|
||||
// Test Reset
|
||||
motorModelSim.Reset();
|
||||
EXPECT_DOUBLE_EQ(0, motorModelSim.GetPosition());
|
||||
EXPECT_DOUBLE_EQ(0, motorModelSim.GetVelocity());
|
||||
|
||||
// Test negative voltage
|
||||
motorModelSim.Reset();
|
||||
motorModelSim.SetVoltage(-3);
|
||||
motorModelSim.Update(.06);
|
||||
|
||||
EXPECT_DOUBLE_EQ(-3, motorModelSim.GetPosition());
|
||||
EXPECT_DOUBLE_EQ(-50, motorModelSim.GetVelocity());
|
||||
}
|
||||
14
simulation/lowfi_simulation/src/test/native/cpp/main.cpp
Normal file
14
simulation/lowfi_simulation/src/test/native/cpp/main.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2015-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 "gtest/gtest.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
int ret = RUN_ALL_TESTS();
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user