Add Climber Constants

Signed-off-by: Filip Kujawa <filip.j.kujawa@gmail.com>
Change-Id: If56eca5a6b7a97ad6c0519d232086d7b0428c9d9
diff --git a/y2024/BUILD b/y2024/BUILD
index 2b2230c..39a023a 100644
--- a/y2024/BUILD
+++ b/y2024/BUILD
@@ -211,6 +211,7 @@
         "//frc971/zeroing:absolute_encoder",
         "//frc971/zeroing:pot_and_absolute_encoder",
         "//y2024/control_loops/drivetrain:polydrivetrain_plants",
+        "//y2024/control_loops/superstructure/climber:climber_plants",
         "//y2024/control_loops/superstructure/intake_pivot:intake_pivot_plants",
         "@com_github_google_glog//:glog",
         "@com_google_absl//absl/base",
diff --git a/y2024/constants/7971.json b/y2024/constants/7971.json
index 7e04438..9e6bafb 100644
--- a/y2024/constants/7971.json
+++ b/y2024/constants/7971.json
@@ -1,4 +1,5 @@
 {% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+{% from 'y2024/constants/common.jinja2' import climber_zero %}
 
 {
   "robot": {
@@ -8,7 +9,16 @@
               "measured_absolute_position" : 0.0
           }
       ) %}
-      "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
+      "zeroing_constants": {{ intake_pivot_zero | tojson(indent=2)}},
+      "potentiometer_offset": 0.0
+    },
+    "climber_constants": {
+      {% set _ = climber_zero.update(
+          {
+              "measured_absolute_position" : 0.0
+          }
+      ) %}
+      "zeroing_constants": {{ climber_zero | tojson(indent=2)}},
       "potentiometer_offset": 0.0
     }
   },
diff --git a/y2024/constants/971.json b/y2024/constants/971.json
index a061f39..641783f 100644
--- a/y2024/constants/971.json
+++ b/y2024/constants/971.json
@@ -1,4 +1,5 @@
 {% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+{% from 'y2024/constants/common.jinja2' import climber_zero %}
 
 {
   "cameras": [
@@ -13,7 +14,16 @@
               "measured_absolute_position" : 0.0
           }
       ) %}
-      "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
+      "zeroing_constants": {{ intake_pivot_zero | tojson(indent=2)}},
+      "potentiometer_offset": 0.0
+    },
+    "climber_constants": {
+      {% set _ = climber_zero.update(
+          {
+              "measured_absolute_position" : 0.0
+          }
+      ) %}
+      "zeroing_constants": {{ climber_zero | tojson(indent=2)}},
       "potentiometer_offset": 0.0
     }
   },
diff --git a/y2024/constants/9971.json b/y2024/constants/9971.json
index 7e04438..9e6bafb 100644
--- a/y2024/constants/9971.json
+++ b/y2024/constants/9971.json
@@ -1,4 +1,5 @@
 {% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+{% from 'y2024/constants/common.jinja2' import climber_zero %}
 
 {
   "robot": {
@@ -8,7 +9,16 @@
               "measured_absolute_position" : 0.0
           }
       ) %}
-      "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
+      "zeroing_constants": {{ intake_pivot_zero | tojson(indent=2)}},
+      "potentiometer_offset": 0.0
+    },
+    "climber_constants": {
+      {% set _ = climber_zero.update(
+          {
+              "measured_absolute_position" : 0.0
+          }
+      ) %}
+      "zeroing_constants": {{ climber_zero | tojson(indent=2)}},
       "potentiometer_offset": 0.0
     }
   },
diff --git a/y2024/constants/BUILD b/y2024/constants/BUILD
index 18e506a..1f25bcb 100644
--- a/y2024/constants/BUILD
+++ b/y2024/constants/BUILD
@@ -23,6 +23,7 @@
         "test_data/*.json",
     ]) + [
         "//y2024/control_loops/superstructure/intake_pivot:intake_pivot_json",
+        "//y2024/control_loops/superstructure/climber:climber_json",
         "//y2024/control_loops/drivetrain:drivetrain_config.json",
         "common.json",
         "common.jinja2",
@@ -43,6 +44,7 @@
         "common.json",
         "//y2024/constants/calib_files",
         "//y2024/control_loops/drivetrain:drivetrain_config.json",
+        "//y2024/control_loops/superstructure/climber:climber_json",
         "//y2024/control_loops/superstructure/intake_pivot:intake_pivot_json",
         "//y2024/vision/maps",
     ],
diff --git a/y2024/constants/common.jinja2 b/y2024/constants/common.jinja2
index b260074..21b061c 100644
--- a/y2024/constants/common.jinja2
+++ b/y2024/constants/common.jinja2
@@ -14,3 +14,16 @@
     "allowable_encoder_error": 0.9
 }
 %}
+
+{# TODO(Filip): Update climber values #}
+{% set climber_encoder_ratio = (1.0 / 1.0) %}
+{% set climber_radius = 0.436496 %}
+{%
+set climber_zero = {
+    "average_filter_size": zeroing_sample_size,
+    "one_revolution_distance": pi * 2.0 * climber_encoder_ratio * climber_radius,
+    "zeroing_threshold": 0.0005,
+    "moving_buffer_size": 20,
+    "allowable_encoder_error": 0.9
+}
+%}
diff --git a/y2024/constants/common.json b/y2024/constants/common.json
index 3da88fe..c632afb 100644
--- a/y2024/constants/common.json
+++ b/y2024/constants/common.json
@@ -52,5 +52,29 @@
   "transfer_roller_voltages": {
     "transfer_in": 12.0,
     "transfer_out": -12.0
+  },
+  "climber_set_points": {
+    "full_extend": 0.0,
+    "half_extend": 0.0,
+    "retract": 0.0
+  },
+  "climber": {
+    "zeroing_voltage": 3.0,
+    "operating_voltage": 12.0,
+    "zeroing_profile_params": {
+      "max_velocity": 0.5,
+      "max_acceleration": 3.0
+    },
+    "default_profile_params":{
+      "max_velocity": 6.0,
+      "max_acceleration": 30.0
+    },
+    "range": {
+        "lower_hard": -0.01,
+        "upper_hard": 0.5,
+        "lower": 0.003,
+        "upper": 0.4
+    },
+    "loop": {% include 'y2024/control_loops/superstructure/climber/integral_climber_plant.json' %}
   }
 }
diff --git a/y2024/constants/constants.fbs b/y2024/constants/constants.fbs
index 4a40487..70bcd6d 100644
--- a/y2024/constants/constants.fbs
+++ b/y2024/constants/constants.fbs
@@ -36,9 +36,18 @@
   retracted:double (id: 1);
 }
 
-// Intake Constants
-table IntakeConstants {
-  intake_pivot_zero:frc971.zeroing.PotAndAbsoluteEncoderZeroingConstants (id: 0);
+// Set points for the climber in meters when:
+// fully extended, which represents meters for when ClimberGoal is FULL_EXTEND
+// partially extended, which represents meters for when ClimberGoal is HALF_EXTEND
+// and retracted, which represents meters for when ClimberGoal is RETRACT
+table ClimberSetPoints {
+  full_extend:double (id: 0);
+  half_extend:double (id: 1);
+  retract:double (id: 2);
+}
+
+table PotAndAbsEncoderConstants {
+  zeroing_constants:frc971.zeroing.PotAndAbsoluteEncoderZeroingConstants (id: 0);
   potentiometer_offset:double (id: 1);
 }
 
@@ -60,7 +69,9 @@
 }
 
 table RobotConstants {
-  intake_constants:IntakeConstants (id: 0);
+  intake_constants:PotAndAbsEncoderConstants (id: 0);
+  climber_constants:PotAndAbsEncoderConstants (id: 1);
+
 }
 
 // Common table for constants unrelated to the robot
@@ -73,6 +84,8 @@
   drivetrain:frc971.control_loops.drivetrain.fbs.DrivetrainConfig (id: 5);
   current_limits:CurrentLimits (id: 6);
   transfer_roller_voltages:TransferRollerVoltages (id: 7);
+  climber:frc971.control_loops.StaticZeroingSingleDOFProfiledSubsystemCommonParams (id: 8);
+  climber_set_points:ClimberSetPoints (id: 9);
 }
 
 table Constants {
diff --git a/y2024/constants/test_data/test_team.json b/y2024/constants/test_data/test_team.json
index 7e04438..9e6bafb 100644
--- a/y2024/constants/test_data/test_team.json
+++ b/y2024/constants/test_data/test_team.json
@@ -1,4 +1,5 @@
 {% from 'y2024/constants/common.jinja2' import intake_pivot_zero %}
+{% from 'y2024/constants/common.jinja2' import climber_zero %}
 
 {
   "robot": {
@@ -8,7 +9,16 @@
               "measured_absolute_position" : 0.0
           }
       ) %}
-      "intake_pivot_zero": {{ intake_pivot_zero | tojson(indent=2)}},
+      "zeroing_constants": {{ intake_pivot_zero | tojson(indent=2)}},
+      "potentiometer_offset": 0.0
+    },
+    "climber_constants": {
+      {% set _ = climber_zero.update(
+          {
+              "measured_absolute_position" : 0.0
+          }
+      ) %}
+      "zeroing_constants": {{ climber_zero | tojson(indent=2)}},
       "potentiometer_offset": 0.0
     }
   },
diff --git a/y2024/control_loops/python/BUILD b/y2024/control_loops/python/BUILD
index f92b484..da80d87 100644
--- a/y2024/control_loops/python/BUILD
+++ b/y2024/control_loops/python/BUILD
@@ -61,6 +61,22 @@
     ],
 )
 
+py_binary(
+    name = "climber",
+    srcs = [
+        "climber.py",
+    ],
+    legacy_create_init = False,
+    target_compatible_with = ["@platforms//cpu:x86_64"],
+    deps = [
+        ":python_init",
+        "//frc971/control_loops/python:controls",
+        "//frc971/control_loops/python:linear_system",
+        "@pip//glog",
+        "@pip//python_gflags",
+    ],
+)
+
 py_library(
     name = "python_init",
     srcs = ["__init__.py"],
diff --git a/y2024/control_loops/python/climber.py b/y2024/control_loops/python/climber.py
new file mode 100644
index 0000000..10aeff9
--- /dev/null
+++ b/y2024/control_loops/python/climber.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python3
+
+from frc971.control_loops.python import control_loop
+from frc971.control_loops.python import linear_system
+import numpy
+import sys
+import gflags
+import glog
+
+FLAGS = gflags.FLAGS
+
+try:
+    gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
+except gflags.DuplicateFlagError:
+    pass
+
+# TODO(Filip): Update information the climber when design is finalized.
+kClimber = linear_system.LinearSystemParams(
+    name='Climber',
+    motor=control_loop.Falcon(),
+    G=(1.0 / 4.0) * (1.0 / 3.0) * (1.0 / 3.0),
+    radius=22 * 0.25 / numpy.pi / 2.0 * 0.0254,
+    mass=2.0,
+    q_pos=0.10,
+    q_vel=1.35,
+    kalman_q_pos=0.12,
+    kalman_q_vel=2.00,
+    kalman_q_voltage=35.0,
+    kalman_r_position=0.05)
+
+
+def main(argv):
+    if FLAGS.plot:
+        R = numpy.matrix([[0.2], [0.0]])
+        linear_system.PlotKick(kClimber, R, plant_params=kClimber)
+        linear_system.PlotMotion(kClimber,
+                                 R,
+                                 max_velocity=5.0,
+                                 plant_params=kClimber)
+
+    # Write the generated constants out to a file.
+    if len(argv) != 7:
+        glog.fatal(
+            'Expected .h file name and .cc file name for the climber and integral climber.'
+        )
+    else:
+        namespaces = ['y2024', 'control_loops', 'superstructure', 'climber']
+    linear_system.WriteLinearSystem(kClimber, argv[1:4], argv[4:7], namespaces)
+
+
+if __name__ == '__main__':
+    argv = FLAGS(sys.argv)
+    glog.init()
+    sys.exit(main(argv))
diff --git a/y2024/control_loops/superstructure/climber/BUILD b/y2024/control_loops/superstructure/climber/BUILD
new file mode 100644
index 0000000..4a044d2
--- /dev/null
+++ b/y2024/control_loops/superstructure/climber/BUILD
@@ -0,0 +1,42 @@
+package(default_visibility = ["//y2024:__subpackages__"])
+
+genrule(
+    name = "genrule_climber",
+    outs = [
+        "climber_plant.h",
+        "climber_plant.cc",
+        "climber_plant.json",
+        "integral_climber_plant.h",
+        "integral_climber_plant.cc",
+        "integral_climber_plant.json",
+    ],
+    cmd = "$(location //y2024/control_loops/python:climber) $(OUTS)",
+    target_compatible_with = ["@platforms//os:linux"],
+    tools = [
+        "//y2024/control_loops/python:climber",
+    ],
+)
+
+cc_library(
+    name = "climber_plants",
+    srcs = [
+        "climber_plant.cc",
+        "integral_climber_plant.cc",
+    ],
+    hdrs = [
+        "climber_plant.h",
+        "integral_climber_plant.h",
+    ],
+    target_compatible_with = ["@platforms//os:linux"],
+    visibility = ["//visibility:public"],
+    deps = [
+        "//frc971/control_loops:hybrid_state_feedback_loop",
+        "//frc971/control_loops:state_feedback_loop",
+    ],
+)
+
+filegroup(
+    name = "climber_json",
+    srcs = ["integral_climber_plant.json"],
+    visibility = ["//visibility:public"],
+)
diff --git a/y2024/control_loops/superstructure/superstructure.cc b/y2024/control_loops/superstructure/superstructure.cc
index 9a703e3..566c402 100644
--- a/y2024/control_loops/superstructure/superstructure.cc
+++ b/y2024/control_loops/superstructure/superstructure.cc
@@ -33,7 +33,7 @@
       transfer_goal_(TransferRollerGoal::NONE),
       intake_pivot_(
           robot_constants_->common()->intake_pivot(),
-          robot_constants_->robot()->intake_constants()->intake_pivot_zero()) {
+          robot_constants_->robot()->intake_constants()->zeroing_constants()) {
   event_loop->SetRuntimeRealtimePriority(30);
 }
 
diff --git a/y2024/control_loops/superstructure/superstructure_lib_test.cc b/y2024/control_loops/superstructure/superstructure_lib_test.cc
index 26e75e4..710de4b 100644
--- a/y2024/control_loops/superstructure/superstructure_lib_test.cc
+++ b/y2024/control_loops/superstructure/superstructure_lib_test.cc
@@ -56,13 +56,13 @@
             new CappedTestPlant(intake_pivot::MakeIntakePivotPlant()),
             PositionSensorSimulator(simulated_robot_constants->robot()
                                         ->intake_constants()
-                                        ->intake_pivot_zero()
+                                        ->zeroing_constants()
                                         ->one_revolution_distance()),
             {.subsystem_params =
                  {simulated_robot_constants->common()->intake_pivot(),
                   simulated_robot_constants->robot()
                       ->intake_constants()
-                      ->intake_pivot_zero()},
+                      ->zeroing_constants()},
              .potentiometer_offset = simulated_robot_constants->robot()
                                          ->intake_constants()
                                          ->potentiometer_offset()},
@@ -70,7 +70,7 @@
                 simulated_robot_constants->common()->intake_pivot()->range()),
             simulated_robot_constants->robot()
                 ->intake_constants()
-                ->intake_pivot_zero()
+                ->zeroing_constants()
                 ->measured_absolute_position(),
             dt_) {
     intake_pivot_.InitializePosition(