Use jinja2 expressions in constants.json
Signed-off-by: Maxwell Henderson <mxwhenderson@gmail.com>
Change-Id: Ic7ce223fdaa2c9578265366abf788d19a21ac5d3
diff --git a/y2024/constants.h b/y2024/constants.h
index 45fd8b0..b90534c 100644
--- a/y2024/constants.h
+++ b/y2024/constants.h
@@ -21,8 +21,6 @@
constexpr uint16_t kCodingRobotTeamNumber = 7971;
struct Values {
- static const int kZeroingSampleSize = 200;
-
static const int kSuperstructureCANWriterPriority = 35;
static const int kDrivetrainWriterPriority = 35;
static const int kDrivetrainTxPriority = 36;
diff --git a/y2024/constants/7971.json b/y2024/constants/7971.json
index 62d4b32..7e04438 100644
--- a/y2024/constants/7971.json
+++ b/y2024/constants/7971.json
@@ -1,10 +1,14 @@
+{% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+
{
"robot": {
"intake_constants": {
- "intake_pivot_zero": {
- "measured_absolute_position": 0.0,
- {% include 'y2024/constants/intake_pivot_common_zeroing.json' %}
- },
+ {% set _ = intake_pivot_zero.update(
+ {
+ "measured_absolute_position" : 0.0
+ }
+ ) %}
+ "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
"potentiometer_offset": 0.0
}
},
diff --git a/y2024/constants/971.json b/y2024/constants/971.json
index 62d4b32..7e04438 100644
--- a/y2024/constants/971.json
+++ b/y2024/constants/971.json
@@ -1,10 +1,14 @@
+{% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+
{
"robot": {
"intake_constants": {
- "intake_pivot_zero": {
- "measured_absolute_position": 0.0,
- {% include 'y2024/constants/intake_pivot_common_zeroing.json' %}
- },
+ {% set _ = intake_pivot_zero.update(
+ {
+ "measured_absolute_position" : 0.0
+ }
+ ) %}
+ "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
"potentiometer_offset": 0.0
}
},
diff --git a/y2024/constants/9971.json b/y2024/constants/9971.json
index 62d4b32..7e04438 100644
--- a/y2024/constants/9971.json
+++ b/y2024/constants/9971.json
@@ -1,10 +1,14 @@
+{% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+
{
"robot": {
"intake_constants": {
- "intake_pivot_zero": {
- "measured_absolute_position": 0.0,
- {% include 'y2024/constants/intake_pivot_common_zeroing.json' %}
- },
+ {% set _ = intake_pivot_zero.update(
+ {
+ "measured_absolute_position" : 0.0
+ }
+ ) %}
+ "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
"potentiometer_offset": 0.0
}
},
diff --git a/y2024/constants/BUILD b/y2024/constants/BUILD
index 9b2b1a6..078d616 100644
--- a/y2024/constants/BUILD
+++ b/y2024/constants/BUILD
@@ -1,5 +1,4 @@
load("//aos/flatbuffers:generate.bzl", "static_flatbuffer")
-load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")
load("//tools/build_rules:template.bzl", "jinja2_template")
cc_library(
@@ -23,10 +22,10 @@
includes = glob([
"test_data/*.json",
]) + [
- ":intake_pivot_common_zeroing.json",
"//y2024/control_loops/superstructure/intake_pivot:intake_pivot_json",
"//y2024/control_loops/drivetrain:drivetrain_config.json",
"common.json",
+ "common.jinja2",
"//y2024/vision/maps",
],
parameters = {},
@@ -40,8 +39,8 @@
"7971.json",
"971.json",
"9971.json",
+ "common.jinja2",
"common.json",
- ":intake_pivot_common_zeroing.json",
"//y2024/control_loops/drivetrain:drivetrain_config.json",
"//y2024/control_loops/superstructure/intake_pivot:intake_pivot_json",
"//y2024/vision/maps",
@@ -95,19 +94,3 @@
"@com_github_google_glog//:glog",
],
)
-
-cc_binary(
- name = "intake_pivot_json_codegen",
- srcs = ["intake_pivot_json_codegen.cc"],
- deps = [
- "//aos/util:file",
- "//y2024:constants",
- ],
-)
-
-run_binary(
- name = "intake_pivot_codegen",
- outs = ["intake_pivot_common_zeroing.json"],
- args = ["$(location :intake_pivot_common_zeroing.json)"],
- tool = ":intake_pivot_json_codegen",
-)
diff --git a/y2024/constants/common.jinja2 b/y2024/constants/common.jinja2
new file mode 100644
index 0000000..b260074
--- /dev/null
+++ b/y2024/constants/common.jinja2
@@ -0,0 +1,16 @@
+{% set pi = 3.14159265 %}
+
+{# we do this here so we keep the encoder ratio in plaintext and also keep the math we're using. #}
+{% set intake_pivot_encoder_ratio = (16.0 / 64.0) * (18.0 / 62.0) %}
+
+{%set zeroing_sample_size = 200 %}
+
+{%
+set intake_pivot_zero = {
+ "average_filter_size": zeroing_sample_size,
+ "one_revolution_distance": pi * 2.0 * intake_pivot_encoder_ratio,
+ "zeroing_threshold": 0.0005,
+ "moving_buffer_size": 20,
+ "allowable_encoder_error": 0.9
+}
+%}
diff --git a/y2024/constants/intake_pivot_json_codegen.cc b/y2024/constants/intake_pivot_json_codegen.cc
deleted file mode 100644
index 9a0b6dd..0000000
--- a/y2024/constants/intake_pivot_json_codegen.cc
+++ /dev/null
@@ -1,24 +0,0 @@
-#include "aos/util/file.h"
-#include "y2024/constants.h"
-
-using namespace y2024::constants;
-
-// This file generates some JSON constants information for the intake that are
-// currently dependent on values that are located in C++ headers and would be
-// obnoxious/inappropriate to pull out. In the future the file may be used to
-// generate constants information for other subsystems as well
-int main(int argc, char *argv[]) {
- CHECK_EQ(argc, 2) << "Must supply file name to output to.";
- std::string output_file = argv[1];
-
- std::stringstream output;
-
- output << "\"average_filter_size\": " << Values::kZeroingSampleSize << ",\n";
- output << "\"one_revolution_distance\": "
- << M_PI * 2.0 * Values::kIntakePivotEncoderRatio() << ",\n";
- output << "\"zeroing_threshold\": 0.0005,\n";
- output << "\"moving_buffer_size\": 20,\n";
- output << "\"allowable_encoder_error\": 0.9\n";
- aos::util::WriteStringToFileOrDie(output_file, output.str());
- return 0;
-}
\ No newline at end of file
diff --git a/y2024/constants/test_data/test_team.json b/y2024/constants/test_data/test_team.json
index bb30395..7e04438 100644
--- a/y2024/constants/test_data/test_team.json
+++ b/y2024/constants/test_data/test_team.json
@@ -1,12 +1,16 @@
+{% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+
{
- "robot": {
- "intake_constants": {
- "intake_pivot_zero": {
- "measured_absolute_position": 0.0,
- {% include 'y2024/constants/intake_pivot_common_zeroing.json' %}
- },
- "potentiometer_offset": 0.0
- }
- },
- {% include 'y2024/constants/common.json' %}
-}
\ No newline at end of file
+ "robot": {
+ "intake_constants": {
+ {% set _ = intake_pivot_zero.update(
+ {
+ "measured_absolute_position" : 0.0
+ }
+ ) %}
+ "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
+ "potentiometer_offset": 0.0
+ }
+ },
+ {% include 'y2024/constants/common.json' %}
+}