Cheetah Software  1.0
test_ControlParameters.cpp
Go to the documentation of this file.
1 
6 #include "gmock/gmock.h"
7 #include "gtest/gtest.h"
8 
12 #include "Math/MathUtilities.h"
13 
15  public:
17  : ControlParameters("test-parameters"),
18  test_double_param("test_double", test_double, collection),
19  test_float_param("test_float", test_float, collection),
20  test_integer_param("test_integer", test_integer, collection) {}
21 
22  double test_double;
24 
25  float test_float;
27 
30 };
31 
33  public:
34  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
36  : ControlParameters("vector-params"),
37  test_double_param("test_double", test_double, collection),
38  test_float_param("test_float", test_float, collection) {}
39 
42 
45 };
46 
47 TEST(ControlParams, testSimple) {
48  TestControlParameters settings;
49 
50  EXPECT_FALSE(settings.isFullyInitialized());
51 
52  EXPECT_THROW(settings.initializeInteger("test_double", 1),
53  std::runtime_error);
54  EXPECT_THROW(settings.initializeDouble("not-a-real-thing", 1.2),
55  std::runtime_error);
56 
57  settings.initializeDouble("test_double", 1.2);
58 
59  EXPECT_FALSE(settings.isFullyInitialized());
60  EXPECT_TRUE(1.2 == *settings.collection.lookup("test_double")._value.d);
62  settings.collection.lookup("test_double")._kind);
63  EXPECT_TRUE("test_double" == settings.collection.lookup("test_double")._name);
64  EXPECT_TRUE(settings.collection.lookup("test_double")._set);
65 
66  settings.initializeFloat("test_float", 1);
67  EXPECT_FALSE(settings.isFullyInitialized());
68  EXPECT_TRUE(1 == *settings.collection.lookup("test_float")._value.f);
70  settings.collection.lookup("test_float")._kind);
71  EXPECT_TRUE("test_float" == settings.collection.lookup("test_float")._name);
72  EXPECT_TRUE(settings.collection.lookup("test_float")._set);
73 
74  settings.initializeInteger("test_integer", 8);
75  EXPECT_TRUE(settings.isFullyInitialized());
76  EXPECT_TRUE(8 == *settings.collection.lookup("test_integer")._value.i);
77  EXPECT_TRUE(ControlParameterValueKind::S64 ==
78  settings.collection.lookup("test_integer")._kind);
79  EXPECT_TRUE("test_integer" ==
80  settings.collection.lookup("test_integer")._name);
81  EXPECT_TRUE(settings.collection.lookup("test_integer")._set);
82 }
83 
84 TEST(ControlParams, testIni) {
85  // create and initialize some parameters
86  TestControlParameters settings;
87  EXPECT_FALSE(settings.isFullyInitialized());
88  settings.initializeDouble("test_double", 2e-9);
89  settings.initializeFloat("test_float", 1);
90  settings.initializeInteger("test_integer", 8);
91  EXPECT_TRUE(settings.isFullyInitialized());
92 
93  // write to ini:
94  settings.writeToIniFile("control-params-test-file.ini");
95 
96  // read from ini
97  TestControlParameters settingsFromIni;
98  EXPECT_FALSE(settingsFromIni.isFullyInitialized());
99  settingsFromIni.initializeFromIniFile("control-params-test-file.ini");
100  printf("%s\n", settingsFromIni.generateUnitializedList().c_str());
101  EXPECT_TRUE(settingsFromIni.isFullyInitialized());
102 
103  EXPECT_TRUE(
104  fpEqual(settings.test_double, settingsFromIni.test_double, 1e-10));
105  EXPECT_TRUE(fpEqual(settings.test_float, settingsFromIni.test_float, 1e-5f));
106  EXPECT_TRUE(settings.test_integer == settingsFromIni.test_integer);
107 }
108 
109 TEST(ControlParams, testVector) {
111  EXPECT_FALSE(settings.isFullyInitialized());
112  Vec3<float> v3f(1, 2.2, 54.45);
113  Vec3<double> v3d(.01, 1e-3, -2.23e-7);
114  settings.initializeVec3f("test_float", v3f);
115  settings.initializeVec3d("test_double", v3d);
116  EXPECT_TRUE(settings.isFullyInitialized());
117 
118  settings.writeToYamlFile("test-yaml.yaml");
119 
120  TestVectorControlParameters settingsFromFile;
121  EXPECT_FALSE(settingsFromFile.isFullyInitialized());
122  settingsFromFile.initializeFromYamlFile("test-yaml.yaml");
123  EXPECT_TRUE(settingsFromFile.isFullyInitialized());
124 
125  EXPECT_TRUE(almostEqual(v3f, settingsFromFile.test_float, 1e-6f));
126  EXPECT_TRUE(almostEqual(v3d, settingsFromFile.test_double, 1e-10));
127 }
128 
129 TEST(ControlParams, testYaml) {
130  // create and initialize some parameters
131  TestControlParameters settings;
132  EXPECT_FALSE(settings.isFullyInitialized());
133  settings.initializeDouble("test_double", 2e-9);
134  settings.initializeFloat("test_float", 1);
135  settings.initializeInteger("test_integer", 8);
136  EXPECT_TRUE(settings.isFullyInitialized());
137 
138  // write to yaml
139  settings.writeToYamlFile("control-params-test-file.yaml");
140 
141  // read from yaml
142  TestControlParameters settingsFromYaml;
143  EXPECT_FALSE(settingsFromYaml.isFullyInitialized());
144  settingsFromYaml.initializeFromYamlFile("control-params-test-file.yaml");
145  EXPECT_TRUE(settingsFromYaml.isFullyInitialized());
146 
147  EXPECT_TRUE(
148  fpEqual(settings.test_double, settingsFromYaml.test_double, 1e-10));
149  EXPECT_TRUE(fpEqual(settings.test_float, settingsFromYaml.test_float, 1e-5f));
150  EXPECT_TRUE(settings.test_integer == settingsFromYaml.test_integer);
151 }
152 
153 // check to see that the simulator default settings file contains all the
154 // simulator settings.
155 TEST(ControlParams, CheckSimulatorDefaults) {
156  SimulatorControlParameters simParams;
159  if (!simParams.isFullyInitialized()) {
160  printf("Missing parameters:\n%s\n",
161  simParams.generateUnitializedList().c_str());
162  }
163  EXPECT_TRUE(simParams.isFullyInitialized());
164 }
165 
166 // check to see that the simulator default settings file contains all the
167 // simulator settings.
168 TEST(ControlParams, CheckMiniCheetahDefaults) {
169  RobotControlParameters robotParams;
172  if (!robotParams.isFullyInitialized()) {
173  printf("Missing parameters:\n%s\n",
174  robotParams.generateUnitializedList().c_str());
175  }
176  EXPECT_TRUE(robotParams.isFullyInitialized());
177 }
178 
179 // check to see that the simulator default settings file contains all the
180 // simulator settings.
181 TEST(ControlParams, CheckCheetah3Defulats) {
182  RobotControlParameters robotParams;
185  if (!robotParams.isFullyInitialized()) {
186  printf("Missing parameters:\n%s\n",
187  robotParams.generateUnitializedList().c_str());
188  }
189  EXPECT_TRUE(robotParams.isFullyInitialized());
190 }
191 
192 
193 TEST(ControlParams, CheckTypeRecognition) {
199 }
200 
201 TEST(ControlParams, DefineFromYaml) {
202  ControlParameters params("robot-parameters");
205 
206  params.collection.deleteAll();
208 
209  //printf("Result: \n %s\n", params.collection.printToIniString().c_str());
210 }
bool fpEqual(T a, T b, T tol)
Definition: utilities.h:15
ControlParameterCollection collection
ControlParameterValueKind _kind
#define MINI_CHEETAH_DEFAULT_PARAMETERS
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
#define SIMULATOR_DEFAULT_PARAMETERS
ControlParameterValuePtr _value
TEST(ControlParams, testSimple)
int64_t s64
Definition: cTypes.h:24
void initializeDouble(const std::string &name, double d)
std::string getConfigDirectoryPath()
Definition: utilities.cpp:30
void writeToYamlFile(const std::string &path)
void initializeVec3d(const std::string &name, Vec3< double > &v)
void initializeInteger(const std::string &name, s64 i)
ControlParameter & lookup(const std::string &name)
void writeToIniFile(const std::string &path)
ControlParameter test_double_param
EIGEN_MAKE_ALIGNED_OPERATOR_NEW TestVectorControlParameters()
#define CHEETAH_3_DEFAULT_PARAMETERS
bool almostEqual(const Eigen::MatrixBase< T > &a, const Eigen::MatrixBase< T > &b, T2 tol)
Definition: MathUtilities.h:23
Utility functions for math.
void initializeFloat(const std::string &name, float f)
void defineAndInitializeFromYamlFile(const std::string &path)
void initializeVec3f(const std::string &name, Vec3< float > &v)
Interface to set gains/control parameters for simulator and robot These are designed to be updated in...
void initializeFromIniFile(const std::string &path)
ControlParameter test_integer_param
ControlParameterValueKind getControlParameterValueKindFromString(const std::string &str)
void initializeFromYamlFile(const std::string &path)
std::string generateUnitializedList()
MX f(const MX &x, const MX &u)