Began shooter integration in superstructure class.
This adds the shooter control loops to the superstructure class.
Change-Id: Ia54550a8c9598349347df7df8e07d81becd1673e
diff --git a/y2017/control_loops/superstructure/shooter/BUILD b/y2017/control_loops/superstructure/shooter/BUILD
index cb9fcf4..e689237 100644
--- a/y2017/control_loops/superstructure/shooter/BUILD
+++ b/y2017/control_loops/superstructure/shooter/BUILD
@@ -1,3 +1,7 @@
+package(default_visibility = ['//visibility:public'])
+
+load('/aos/build/queues', 'queue_library')
+
genrule(
name = 'genrule_shooter',
cmd = '$(location //y2017/control_loops/python:shooter) $(OUTS)',
@@ -27,3 +31,19 @@
'//frc971/control_loops:state_feedback_loop',
],
)
+
+cc_library(
+ name = 'shooter',
+ visibility = ['//visibility:public'],
+ srcs = [
+ 'shooter.cc',
+ ],
+ hdrs = [
+ 'shooter.h',
+ ],
+ deps = [
+ ':shooter_plants',
+ '//aos/common/controls:control_loop',
+ '//y2017/control_loops/superstructure:superstructure_queue',
+ ],
+)
diff --git a/y2017/control_loops/superstructure/shooter/shooter.cc b/y2017/control_loops/superstructure/shooter/shooter.cc
new file mode 100644
index 0000000..c73a008
--- /dev/null
+++ b/y2017/control_loops/superstructure/shooter/shooter.cc
@@ -0,0 +1,147 @@
+#include "y2017/control_loops/superstructure/shooter/shooter.h"
+
+#include <chrono>
+
+#include "aos/common/controls/control_loops.q.h"
+#include "aos/common/logging/logging.h"
+#include "aos/common/logging/queue_logging.h"
+
+#include "y2017/control_loops/superstructure/shooter/shooter.h"
+
+namespace y2017 {
+namespace control_loops {
+namespace superstructure {
+namespace shooter {
+
+namespace chrono = ::std::chrono;
+using ::aos::monotonic_clock;
+
+namespace {
+constexpr double kTolerance = 10.0;
+} // namespace
+
+// TODO(austin): Pseudo current limit?
+
+ShooterController::ShooterController()
+ : loop_(new StateFeedbackLoop<3, 1, 1>(
+ superstructure::shooter::MakeIntegralShooterLoop())) {
+ history_.fill(0);
+ Y_.setZero();
+}
+
+void ShooterController::set_goal(double angular_velocity_goal) {
+ loop_->mutable_next_R() << 0.0, angular_velocity_goal, 0.0;
+}
+
+void ShooterController::set_position(double current_position) {
+ // Update position in the model.
+ Y_ << current_position;
+
+ // Add the position to the history.
+ history_[history_position_] = current_position;
+ history_position_ = (history_position_ + 1) % kHistoryLength;
+
+ dt_velocity_ = (current_position - last_position_) / 0.005;
+ last_position_ = current_position;
+}
+
+double ShooterController::voltage() const { return loop_->U(0, 0); }
+
+void ShooterController::Reset() { reset_ = true; }
+
+void ShooterController::Update(bool disabled) {
+ loop_->mutable_R() = loop_->next_R();
+ if (::std::abs(loop_->R(1, 0)) < 1.0) {
+ // Kill power at low angular velocities.
+ disabled = true;
+ }
+
+ loop_->Correct(Y_);
+
+ // Compute the oldest point in the history.
+ const int oldest_history_position =
+ ((history_position_ == 0) ? kHistoryLength : history_position_) - 1;
+
+ // Compute the distance moved over that time period.
+ average_angular_velocity_ =
+ (history_[oldest_history_position] - history_[history_position_]) /
+ (chrono::duration_cast<chrono::duration<double>>(
+ ::aos::controls::kLoopFrequency)
+ .count() *
+ static_cast<double>(kHistoryLength - 1));
+
+ // Ready if average angular velocity is close to the goal.
+ error_ = average_angular_velocity_ - loop_->next_R(1, 0);
+
+ ready_ = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0;
+
+ if (last_ready_ && !ready_ && loop_->next_R(1, 0) > 1.0 && error_ < 0.0) {
+ needs_reset_ = true;
+ min_velocity_ = loop_->X_hat(1, 0);
+ }
+ if (needs_reset_) {
+ min_velocity_ = ::std::min(min_velocity_, loop_->X_hat(1, 0));
+ if (loop_->X_hat(1, 0) > min_velocity_ + 5.0) {
+ reset_ = true;
+ needs_reset_ = false;
+ }
+ }
+ if (reset_) {
+ loop_->mutable_X_hat(2, 0) = 0.0;
+ loop_->mutable_X_hat(1, 0) = dt_velocity_;
+ loop_->mutable_X_hat(0, 0) = Y_(0, 0);
+ reset_ = false;
+ }
+ last_ready_ = ready_;
+
+ X_hat_current_ = loop_->X_hat();
+ position_error_ = X_hat_current_(0, 0) - Y_(0, 0);
+
+ loop_->Update(disabled);
+}
+
+void ShooterController::SetStatus(ShooterStatus *status) {
+ status->avg_angular_velocity = average_angular_velocity_;
+
+ status->angular_velocity = X_hat_current_(1, 0);
+ status->ready = std::abs(error_) < kTolerance && loop_->next_R(1, 0) > 1.0;
+
+ status->voltage_error = X_hat_current_(2, 0);
+ status->position_error = position_error_;
+ status->instantaneous_velocity = dt_velocity_;
+}
+
+void Shooter::Reset() { wheel_.Reset(); }
+
+void Shooter::Iterate(const control_loops::ShooterGoal *goal,
+ const double *position, double *output,
+ control_loops::ShooterStatus *status) {
+ if (goal) {
+ // Update position/goal for our wheel.
+ wheel_.set_goal(goal->angular_velocity);
+ }
+
+ wheel_.set_position(*position);
+
+ wheel_.Update(output == nullptr);
+
+ wheel_.SetStatus(status);
+
+ if (last_ready_ && !status->ready) {
+ min_ = wheel_.dt_velocity();
+ } else if (!status->ready) {
+ min_ = ::std::min(min_, wheel_.dt_velocity());
+ } else if (!last_ready_ && status->ready) {
+ LOG(INFO, "Shot min was [%f]\n", min_);
+ }
+
+ if (output) {
+ *output = wheel_.voltage();
+ }
+ last_ready_ = status->ready;
+}
+
+} // namespace shooter
+} // namespace superstructure
+} // namespace control_loops
+} // namespace y2017
diff --git a/y2017/control_loops/superstructure/shooter/shooter.h b/y2017/control_loops/superstructure/shooter/shooter.h
new file mode 100644
index 0000000..7f11267
--- /dev/null
+++ b/y2017/control_loops/superstructure/shooter/shooter.h
@@ -0,0 +1,101 @@
+#ifndef Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_
+#define Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_
+
+#include <memory>
+
+#include "aos/common/controls/control_loop.h"
+#include "aos/common/time.h"
+#include "frc971/control_loops/state_feedback_loop.h"
+
+#include "y2017/control_loops/superstructure/shooter/shooter_integral_plant.h"
+#include "y2017/control_loops/superstructure/superstructure.q.h"
+
+namespace y2017 {
+namespace control_loops {
+namespace superstructure {
+namespace shooter {
+
+class ShooterController {
+ public:
+ ShooterController();
+
+ // Sets the velocity goal in radians/sec
+ void set_goal(double angular_velocity_goal);
+ // Sets the current encoder position in radians
+ void set_position(double current_position);
+
+ // Populates the status structure.
+ void SetStatus(control_loops::ShooterStatus *status);
+
+ // Returns the control loop calculated voltage.
+ double voltage() const;
+
+ // Returns the instantaneous velocity.
+ double velocity() const { return loop_->X_hat(1, 0); }
+
+ double dt_velocity() const { return dt_velocity_; }
+
+ double error() const { return error_; }
+
+ // Executes the control loop for a cycle.
+ void Update(bool disabled);
+
+ // Resets the kalman filter and any other internal state.
+ void Reset();
+
+ private:
+ // The current sensor measurement.
+ Eigen::Matrix<double, 1, 1> Y_;
+ // The control loop.
+ ::std::unique_ptr<StateFeedbackLoop<3, 1, 1>> loop_;
+
+ // History array for calculating a filtered angular velocity.
+ static constexpr int kHistoryLength = 5;
+ ::std::array<double, kHistoryLength> history_;
+ ptrdiff_t history_position_ = 0;
+
+ double error_ = 0.0;
+ double dt_velocity_ = 0.0;
+ double last_position_ = 0.0;
+ double average_angular_velocity_ = 0.0;
+ double min_velocity_ = 0.0;
+ double position_error_ = 0.0;
+
+ Eigen::Matrix<double, 3, 1> X_hat_current_;
+
+ bool ready_ = false;
+ bool needs_reset_ = false;
+ bool reset_ = false;
+
+ bool last_ready_ = false;
+ DISALLOW_COPY_AND_ASSIGN(ShooterController);
+};
+
+class Shooter {
+ public:
+ Shooter() {}
+
+ // Iterates the shooter control loop one cycle. position and status must
+ // never be nullptr. goal can be nullptr if no goal exists, and output should
+ // be nullptr if disabled.
+ void Iterate(const control_loops::ShooterGoal *goal, const double *position,
+ double *output, control_loops::ShooterStatus *status);
+
+ // Sets the shooter up to reset the kalman filter next time Iterate is called.
+ void Reset();
+
+ private:
+ ShooterController wheel_;
+
+ bool last_ready_ = false;
+ double min_ = 0.0;
+
+ DISALLOW_COPY_AND_ASSIGN(Shooter);
+};
+
+} // namespace shooter
+} // namespace superstructure
+} // namespace control_loops
+} // namespace y2017
+
+#endif // Y2017_CONTROL_LOOPS_SHOOTER_SHOOTER_H_