Merge "Add down_estimater and EKF state to DrivetrainStatus"
diff --git a/y2020/control_loops/python/BUILD b/y2020/control_loops/python/BUILD
index 492326b..2d1af66 100644
--- a/y2020/control_loops/python/BUILD
+++ b/y2020/control_loops/python/BUILD
@@ -81,6 +81,21 @@
     ],
 )
 
+py_binary(
+    name = "turret",
+    srcs = [
+        "turret.py",
+    ],
+    legacy_create_init = False,
+    restricted_to = ["//tools:k8"],
+    deps = [
+        ":python_init",
+        "//external:python-gflags",
+        "//external:python-glog",
+        "//frc971/control_loops/python:angular_system",
+        "//frc971/control_loops/python:controls",
+    ],
+)
 
 py_library(
     name = "python_init",
diff --git a/y2020/control_loops/python/turret.py b/y2020/control_loops/python/turret.py
new file mode 100644
index 0000000..58ea7d8
--- /dev/null
+++ b/y2020/control_loops/python/turret.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python
+
+from aos.util.trapezoid_profile import TrapezoidProfile
+from frc971.control_loops.python import control_loop
+from frc971.control_loops.python import angular_system
+from frc971.control_loops.python import controls
+import copy
+import numpy
+import sys
+from matplotlib import pylab
+import gflags
+import glog
+
+FLAGS = gflags.FLAGS
+
+try:
+    gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
+except gflags.DuplicateFlagError:
+    pass
+
+kTurret = angular_system.AngularSystemParams(
+    name='Turret',
+    motor=control_loop.Vex775Pro(),
+    #TODO: Update Gear Ratios when they are ready
+    G=(6.0 / 60.0) * (20.0 / 100.0) * (24.0 / 84.0),
+    #TODO: Get number from Bryan (moment of inertia)
+    J=0.30,
+    q_pos=0.20,
+    q_vel=5.0,
+    kalman_q_pos=0.12,
+    kalman_q_vel=2.0,
+    kalman_q_voltage=4.0,
+    kalman_r_position=0.05)
+
+def main(argv):
+    # Write the generated constants out to a file.
+    if len(argv) != 5:
+        glog.fatal(
+            'Expected .h file name and .cc file name for the turret.'
+        )
+    else:
+        namespaces = ['y2020', 'control_loops', 'superstructure', 'turret']
+        angular_system.WriteAngularSystem([kTurret],
+                                          argv[1:3], argv[3:5], namespaces)
+
+
+if __name__ == '__main__':
+    argv = FLAGS(sys.argv)
+    glog.init()
+    sys.exit(main(argv))
diff --git a/y2020/control_loops/superstructure/superstructure_goal.fbs b/y2020/control_loops/superstructure/superstructure_goal.fbs
index c1b3b5a..4b1d8e9 100644
--- a/y2020/control_loops/superstructure/superstructure_goal.fbs
+++ b/y2020/control_loops/superstructure/superstructure_goal.fbs
@@ -2,15 +2,47 @@
 
 namespace y2020.control_loops.superstructure;
 
+table ShooterGoal {
+  // Angular velocity in rad/s of the slowest (lowest) wheel in the kicker.
+  // Positive is shooting the ball.
+  velocity_kicker:double;
+
+  // Angular velocity in rad/s of the flywheel. Positive is shooting.
+  velocity_flywheel:double;
+}
+
 table Goal {
   // Zero is at the horizontal, positive towards the front (meters on the lead screw).
+  // Only applies if hood_tracking = false.
   hood:frc971.control_loops.StaticZeroingSingleDOFProfiledSubsystemGoal;
 
   //0 = Linkage on sprocket is pointing straight up
   //Positive = forward
   intake:frc971.control_loops.StaticZeroingSingleDOFProfiledSubsystemGoal;
+
   //Positive is rollers intaking to Washing Machine.
   roller_voltage:float;
+
+  // 0 = facing the front of the robot. Positive rotates counterclockwise.
+  // TODO(Kai): Define which wrap of the shooter is 0 if it can rotate past
+  // forward more than once.
+  turret:frc971.control_loops.StaticZeroingSingleDOFProfiledSubsystemGoal;
+
+  // Only applies if shooter_tracking = false.
+  shooter:ShooterGoal;
+
+  // Whether the robot should be shooting balls. Waits until hood, turret, and
+  // shooter are at goal (as determined by auto-tracking or override).
+  shooting:bool;
+
+  // Whether the hood should adjust its position automatically.
+  hood_tracking:bool;
+
+  // Whether the turret should follow the target automatically.
+  turret_tracking:bool;
+
+  // Whether the kicker and flywheel should choose a velocity automatically.
+  shooter_tracking:bool;
 }
 
 root_type Goal;
diff --git a/y2020/control_loops/superstructure/superstructure_output.fbs b/y2020/control_loops/superstructure/superstructure_output.fbs
index e887cf5..9048b33 100644
--- a/y2020/control_loops/superstructure/superstructure_output.fbs
+++ b/y2020/control_loops/superstructure/superstructure_output.fbs
@@ -9,6 +9,25 @@
 
   // Voltage sent to rollers on intake. Positive rolls inward.
   intake_roller_voltage:double;
+
+  //Voltage sent to the motors.
+  //Positive rotates counterclockwise from a birds eye view.
+  turret_voltage:double;
+
+  // Voltage sent to the feeder belt. Positive is feeding.
+  feeder_voltage:double;
+
+  // Voltage sent to the washing_machine and control panel spinner.
+  // Positive runs the washing machine CCW facing the front of the robot, and
+  // the spinner runs CCW from a top down view.
+  washing_machine_spinner_voltage:double;
+
+  // Voltage sent to the kicker. Positive is shooting.
+  kicker_left_voltage:double;
+  kicker_right_voltage:double;
+
+  // Voltage sent to the flywheel. Positive is shooting.
+  flywheel_voltage:double;
 }
 
 root_type Output;
diff --git a/y2020/control_loops/superstructure/superstructure_position.fbs b/y2020/control_loops/superstructure/superstructure_position.fbs
index 62754df..ed73e52 100644
--- a/y2020/control_loops/superstructure/superstructure_position.fbs
+++ b/y2020/control_loops/superstructure/superstructure_position.fbs
@@ -2,12 +2,28 @@
 
 namespace y2020.control_loops.superstructure;
 
+table ShooterPosition {
+  // Flywheel angle in radians, positive is shooting.
+  theta_flywheel:double;
+
+  // Kicker angle in radians of the slowest (lowest) wheel, positive is
+  // accelerating the ball toward the shooter.
+  theta_kicker_left:double;
+  theta_kicker_right:double;
+}
+
 table Position {
   // Zero is at the horizontal, positive towards the front (meters on the lead screw).
   hood:frc971.AbsolutePosition;
 
   // Position of the intake. 0 when four-bar is vertical, positive extended.
   intake_joint:frc971.AbsolutePosition;
+
+  // See goal for definition of 0
+  turret:frc971.PotAndAbsolutePosition;
+
+  // Position of the kicker and flywheel
+  shooter:ShooterPosition;
 }
 
 root_type Position;
diff --git a/y2020/control_loops/superstructure/superstructure_status.fbs b/y2020/control_loops/superstructure/superstructure_status.fbs
index a230528..45dbba8 100644
--- a/y2020/control_loops/superstructure/superstructure_status.fbs
+++ b/y2020/control_loops/superstructure/superstructure_status.fbs
@@ -3,6 +3,29 @@
 
 namespace y2020.control_loops.superstructure;
 
+table ShooterSegmentStatus {
+  // The current average velocity in radians/second over the last kHistoryLength
+  // in shooter.h
+  avg_angular_velocity:double;
+
+  // The current instantaneous filtered velocity in radians/second.
+  angular_velocity:double;
+
+  // The target speed selected by the lookup table or from manual override
+  // Can be compared to velocity to determine if ready.
+  angular_velocity_goal:double;
+}
+
+table ShooterStatus {
+  // The final wheel shooting the ball
+  flywheel:ShooterSegmentStatus;
+
+  // The subsystem to accelerate the ball before the flywheel
+  // Velocity is the slowest (lowest) wheel
+  kicker_left:ShooterSegmentStatus;
+  kicker_right:ShooterSegmentStatus;
+}
+
 table Status {
   // All subsystems know their location.
   zeroed:bool;
@@ -13,6 +36,10 @@
   //Subsystem status.
   hood:frc971.control_loops.AbsoluteEncoderProfiledJointStatus;
   intake:frc971.control_loops.AbsoluteEncoderProfiledJointStatus;
+  turret:frc971.control_loops.PotAndAbsoluteEncoderProfiledJointStatus;
+
+  // Shooter subsystem status.
+  shooter:ShooterStatus;
 }
 
 root_type Status;
diff --git a/y2020/control_loops/superstructure/turret/BUILD b/y2020/control_loops/superstructure/turret/BUILD
new file mode 100644
index 0000000..894d418
--- /dev/null
+++ b/y2020/control_loops/superstructure/turret/BUILD
@@ -0,0 +1,32 @@
+package(default_visibility = ["//y2020:__subpackages__"])
+
+genrule(
+    name = "genrule_turret",
+    outs = [
+        "turret_plant.h",
+        "turret_plant.cc",
+        "integral_turret_plant.h",
+        "integral_turret_plant.cc",
+    ],
+    cmd = "$(location //y2020/control_loops/python:turret) $(OUTS)",
+    tools = [
+        "//y2020/control_loops/python:turret",
+    ],
+)
+
+cc_library(
+    name = "turret_plants",
+    srcs = [
+        "integral_turret_plant.cc",
+        "turret_plant.cc",
+    ],
+    hdrs = [
+        "integral_turret_plant.h",
+        "turret_plant.h",
+    ],
+    visibility = ["//visibility:public"],
+    deps = [
+        "//frc971/control_loops:hybrid_state_feedback_loop",
+        "//frc971/control_loops:state_feedback_loop",
+    ],
+)