18 #include <gtest/gtest.h> 22 const std::string& description)
24 std::stringstream stream;
25 stream <<
"<sdf version='" << SDF_VERSION <<
"'>" << std::endl
26 <<
"<model name='test_model'>" << std::endl
27 <<
"<plugin name='test_plugin' filename='test_file.so'>" << std::endl
29 <<
"</plugin>" << std::endl
30 <<
"</model>" << std::endl
31 <<
"</sdf>" << std::endl;
34 sdfParsed.SetFromString(stream.str());
36 sdf::ElementPtr conversion = sdfParsed.Root()->GetElement(
"model")
37 ->GetElement(
"plugin")->GetElement(
"conversion");
39 std::shared_ptr<gazebo::ConversionFunction> func;
41 CreateConversionFunction(conversion));
46 TEST(ThrusterConversionFcn, Basic)
48 std::string description =
50 " <type>Basic</type> \n" 51 " <rotorConstant>0.0049</rotorConstant> \n" 54 std::shared_ptr<gazebo::ConversionFunction> func;
57 EXPECT_TRUE(func != NULL);
58 EXPECT_EQ(func->GetType(),
"Basic");
60 EXPECT_EQ(func->convert(0.0), 0.0);
61 EXPECT_EQ(func->convert(50.), 50.0*50.0*0.0049);
62 EXPECT_EQ(func->convert(-50.), -50.0*50.0*0.0049);
65 TEST(ThrusterConversionFcn, Bessa)
74 std::stringstream stream;
75 stream <<
"<conversion> \n" 76 <<
" <type>Bessa</type> \n" 77 <<
" <rotorConstantL>" << cl <<
"</rotorConstantL> \n" 78 <<
" <rotorConstantR>" << cr <<
"</rotorConstantR> \n" 79 <<
" <deltaL>" << dl <<
"</deltaL> \n" 80 <<
" <deltaR>" << dr <<
"</deltaR> \n" 83 std::shared_ptr<gazebo::ConversionFunction> func;
86 EXPECT_TRUE(func != NULL);
87 EXPECT_EQ(func->GetType(),
"Bessa");
90 EXPECT_EQ(0.0, func->convert(0.0));
91 EXPECT_EQ(0.0, func->convert(sqrt(dr) - delta));
92 EXPECT_EQ(0.0, func->convert(-sqrt(-dl) + delta));
97 EXPECT_EQ(cl*(cmdl*std::abs(cmdl)-dl), func->convert(cmdl));
98 EXPECT_EQ(cr*(cmdr*std::abs(cmdr)-dr), func->convert(cmdr));
101 TEST(ThrusterConversionFcn, LinearInterp)
103 std::vector<double> input = {-5.0, 0, 2.0, 5.0};
104 std::vector<double> output = {-100, -10, 20, 120};
105 std::vector<double> alpha = {0.1, 0.5, 0.9};
107 std::stringstream stream;
108 stream <<
"<conversion> \n" 109 <<
" <type>LinearInterp</type> \n" 111 for (
double d : input)
113 stream <<
"</inputValues> \n" 115 for (
double d : output)
117 stream <<
"</outputValues> \n" 120 std::shared_ptr<gazebo::ConversionFunction> func;
123 EXPECT_TRUE(func != NULL);
124 EXPECT_EQ(func->GetType(),
"LinearInterp");
127 for (
int i = 0; i < input.size(); i++)
129 EXPECT_EQ(output[i], func->convert(input[i]));
133 EXPECT_EQ(output[0], func->convert(input[0] - 0.5));
134 EXPECT_EQ(output.back(), func->convert(input.back() + 0.5));
137 for (
int i = 0; i < input.size()-1; i++)
139 double in = alpha[i]*input[i] + (1-alpha[i])*input[i+1];
140 double out = alpha[i]*output[i] + (1-alpha[i])*output[i+1];
141 EXPECT_NEAR(out, func->convert(in), 1e-7);
145 int main(
int argc,
char **argv)
147 testing::InitGoogleTest(&argc, argv);
148 return RUN_ALL_TESTS();
Description of the conversion function fo a thruster.
std::shared_ptr< gazebo::ConversionFunction > ConversionFromString(const std::string &description)
int main(int argc, char **argv)
TEST(ThrusterConversionFcn, Basic)
static ConversionFunctionFactory & GetInstance()
Return the singleton instance of this factory.