Make initialization of 2020 constants explicit
By doing this, we will remember to initialize the constants
before RT so we don't malloc in RT.
Later we will replace this with the constants in a json file.
Signed-off-by: milind-u <milind.upadhyay@gmail.com>
Change-Id: I283f4accc3c180815abbc424d39c84fdd4c47d92
diff --git a/y2020/constants.cc b/y2020/constants.cc
index 81c9cd4..d174b57 100644
--- a/y2020/constants.cc
+++ b/y2020/constants.cc
@@ -174,36 +174,25 @@
return r;
}
-void DoGetValues(const Values **result) {
+const Values *values = nullptr;
+
+void DoGetValues() {
uint16_t team = ::aos::network::GetTeamNumber();
AOS_LOG(INFO, "creating a Constants for team %" PRIu16 "\n", team);
- *result = DoGetValuesForTeam(team);
+ values = DoGetValuesForTeam(team);
}
} // namespace
-const Values &GetValues() {
+void InitValues() {
static absl::once_flag once;
- static const Values *result;
- absl::call_once(once, DoGetValues, &result);
- return *result;
+ absl::call_once(once, DoGetValues);
}
-const Values &GetValuesForTeam(uint16_t team_number) {
- static aos::stl_mutex mutex;
- std::unique_lock<aos::stl_mutex> locker(mutex);
-
- // IMPORTANT: This declaration has to stay after the mutex is locked to
- // avoid race conditions.
- static ::std::map<uint16_t, const Values *> values;
-
- if (values.count(team_number) == 0) {
- values[team_number] = DoGetValuesForTeam(team_number);
-#if __has_feature(address_sanitizer)
- __lsan_ignore_object(values[team_number]);
-#endif
- }
- return *values[team_number];
+const Values &GetValues() {
+ CHECK(values)
+ << "Values are uninitialized. Call InitValues before accessing them.";
+ return *values;
}
} // namespace constants
diff --git a/y2020/constants.h b/y2020/constants.h
index 4483948..30fb553 100644
--- a/y2020/constants.h
+++ b/y2020/constants.h
@@ -223,13 +223,14 @@
InterpolationTable<FlywheelShotParams> flywheel_shot_interpolation_table;
};
-// Creates (once) a Values instance for ::aos::network::GetTeamNumber() and
-// returns a reference to it.
-const Values &GetValues();
+// Creates (once) a Values instance for ::aos::network::GetTeamNumber(). Should
+// be called before realtime because this allocates memory.
+void InitValues();
-// Creates Values instances for each team number it is called with and
-// returns them.
-const Values &GetValuesForTeam(uint16_t team_number);
+// Returns a reference to the Values instance for
+// ::aos::network::GetTeamNumber(). Values must be initialized through
+// InitValues() before calling this.
+const Values &GetValues();
} // namespace constants
} // namespace y2020
diff --git a/y2020/control_loops/superstructure/superstructure_lib_test.cc b/y2020/control_loops/superstructure/superstructure_lib_test.cc
index 029475a..26402f4 100644
--- a/y2020/control_loops/superstructure/superstructure_lib_test.cc
+++ b/y2020/control_loops/superstructure/superstructure_lib_test.cc
@@ -421,6 +421,16 @@
float climber_voltage_ = 0.0f;
};
+class SuperstructureTestEnvironment : public ::testing::Environment {
+ public:
+ void SetUp() override { constants::InitValues(); }
+};
+
+namespace {
+const auto kTestEnv =
+ ::testing::AddGlobalTestEnvironment(new SuperstructureTestEnvironment());
+}
+
class SuperstructureTest : public ::frc971::testing::ControlLoopTest {
protected:
SuperstructureTest()
diff --git a/y2020/control_loops/superstructure/superstructure_main.cc b/y2020/control_loops/superstructure/superstructure_main.cc
index a9f071d..a10237d 100644
--- a/y2020/control_loops/superstructure/superstructure_main.cc
+++ b/y2020/control_loops/superstructure/superstructure_main.cc
@@ -1,3 +1,4 @@
+#include "y2020/constants.h"
#include "y2020/control_loops/superstructure/superstructure.h"
#include "aos/events/shm_event_loop.h"
@@ -10,6 +11,7 @@
aos::configuration::ReadConfig("config.json");
::aos::ShmEventLoop event_loop(&config.message());
+ ::y2020::constants::InitValues();
::y2020::control_loops::superstructure::Superstructure superstructure(
&event_loop);
diff --git a/y2020/wpilib_interface.cc b/y2020/wpilib_interface.cc
index fb46d71..217c6b7 100644
--- a/y2020/wpilib_interface.cc
+++ b/y2020/wpilib_interface.cc
@@ -133,6 +133,8 @@
// we should ever see.
UpdateFastEncoderFilterHz(kMaxFastEncoderPulsesPerSecond);
UpdateMediumEncoderFilterHz(kMaxMediumEncoderPulsesPerSecond);
+
+ constants::InitValues();
}
// Hood