Merge remote-tracking branch 'austin/claw' into claw
diff --git a/frc971/control_loops/claw/claw.cc b/frc971/control_loops/claw/claw.cc
old mode 100755
new mode 100644
index dd81ecb..11719ee
--- a/frc971/control_loops/claw/claw.cc
+++ b/frc971/control_loops/claw/claw.cc
@@ -49,30 +49,41 @@
static const double kMaxVoltage = 12.0;
void ClawLimitedLoop::CapU() {
- uncapped_average_voltage_ = U(0, 0) + U(1, 0) / 2.0;
- if (is_zeroing_) {
- LOG(DEBUG, "zeroing\n");
- const frc971::constants::Values &values = constants::GetValues();
- if (uncapped_average_voltage_ > values.claw.max_zeroing_voltage) {
- const double difference =
- uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
- U(0, 0) -= difference;
- } else if (uncapped_average_voltage_ < -values.claw.max_zeroing_voltage) {
- const double difference =
- -uncapped_average_voltage_ - values.claw.max_zeroing_voltage;
- U(0, 0) += difference;
- }
- }
-
- double max_value =
- ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0) + U(0, 0)));
+ uncapped_average_voltage_ = (U(0, 0) + U(1, 0)) / 2.0;
const double k_max_voltage = is_zeroing_ ? kZeroingVoltage : kMaxVoltage;
- if (max_value > k_max_voltage) {
- LOG(DEBUG, "Capping U because max is %f\n", max_value);
- U = U * k_max_voltage / max_value;
- LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0));
+ double max_value =
+ ::std::max(::std::abs(U(0, 0)), ::std::abs(U(1, 0)));
+ double scalar = k_max_voltage / max_value;
+ bool bottom_big = (::std::abs(U(0, 0)) > k_max_voltage) &&
+ (::std::abs(U(0, 0)) > ::std::abs(U(1, 0)));
+ bool top_big = (::std::abs(U(1, 0)) > k_max_voltage) && (!bottom_big);
+ double separation_voltage = U(1, 0) - U(0, 0) * kClawMomentOfInertiaRatio;
+ double u_top = U(1, 0);
+ double u_bottom = U(0, 0);
+
+ if (bottom_big) {
+ LOG(DEBUG, "Capping U because bottom is %f\n", max_value);
+ u_bottom *= scalar;
+ u_top = separation_voltage + u_bottom * kClawMomentOfInertiaRatio;
+ // If we can't maintain the separation, just clip it.
+ if (u_top > k_max_voltage) u_top = k_max_voltage;
+ else if (u_top < -k_max_voltage) u_top = -k_max_voltage;
}
+ else if (top_big) {
+ LOG(DEBUG, "Capping U because top is %f\n", max_value);
+ u_top *= scalar;
+ u_bottom = (u_top - separation_voltage) / kClawMomentOfInertiaRatio;
+ if (u_bottom > k_max_voltage) u_bottom = k_max_voltage;
+ else if (u_bottom < -k_max_voltage) u_bottom = -k_max_voltage;
+ }
+
+ U(0, 0) = u_bottom;
+ U(1, 0) = u_top;
+
+ LOG(DEBUG, "Capping U is now %f %f\n", U(0, 0), U(1, 0));
+ LOG(DEBUG, "Separation Voltage was %f, is now %f\n", separation_voltage,
+ U(1, 0) - U(0, 0) * kClawMomentOfInertiaRatio);
}
ClawMotor::ClawMotor(control_loops::ClawGroup *my_claw)
@@ -536,12 +547,20 @@
case FINE_TUNE_BOTTOM:
case FINE_TUNE_TOP:
case UNKNOWN_LOCATION: {
+ Eigen::Matrix<double, 2, 1> U = claw_.K() * (claw_.R - claw_.X_hat);
if (claw_.uncapped_average_voltage() > values.claw.max_zeroing_voltage) {
- double dx = (claw_.uncapped_average_voltage() -
+ double dx_bot = (U(0, 0) -
values.claw.max_zeroing_voltage) /
claw_.K(0, 0);
+ double dx_top = (U(1, 0) -
+ values.claw.max_zeroing_voltage) /
+ claw_.K(0, 0);
+ double dx = ::std::max(dx_top, dx_bot);
bottom_claw_goal_ -= dx;
top_claw_goal_ -= dx;
+ Eigen::Matrix<double, 4, 1> R;
+ R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0), claw_.R(3, 0);
+ U = claw_.K() * (R - claw_.X_hat);
capped_goal_ = true;
LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
LOG(DEBUG, "Uncapped is %f, max is %f, difference is %f\n",
@@ -550,11 +569,18 @@
values.claw.max_zeroing_voltage));
} else if (claw_.uncapped_average_voltage() <
-values.claw.max_zeroing_voltage) {
- double dx = (claw_.uncapped_average_voltage() +
+ double dx_bot = (U(0, 0) +
values.claw.max_zeroing_voltage) /
claw_.K(0, 0);
+ double dx_top = (U(1, 0) +
+ values.claw.max_zeroing_voltage) /
+ claw_.K(0, 0);
+ double dx = ::std::min(dx_top, dx_bot);
bottom_claw_goal_ -= dx;
top_claw_goal_ -= dx;
+ Eigen::Matrix<double, 4, 1> R;
+ R << bottom_claw_goal_, top_claw_goal_ - bottom_claw_goal_, claw_.R(2, 0), claw_.R(3, 0);
+ U = claw_.K() * (R - claw_.X_hat);
capped_goal_ = true;
LOG(DEBUG, "Moving the goal by %f to prevent windup\n", dx);
}
@@ -562,7 +588,7 @@
}
if (output) {
- output->top_claw_voltage = claw_.U(1, 0) + claw_.U(0, 0);
+ output->top_claw_voltage = claw_.U(1, 0);
output->bottom_claw_voltage = claw_.U(0, 0);
if (output->top_claw_voltage > kMaxVoltage) {
diff --git a/frc971/control_loops/claw/claw_lib_test.cc b/frc971/control_loops/claw/claw_lib_test.cc
index 3fa9079..3ec3b25 100644
--- a/frc971/control_loops/claw/claw_lib_test.cc
+++ b/frc971/control_loops/claw/claw_lib_test.cc
@@ -82,9 +82,14 @@
return GetAbsolutePosition(type) - initial_position_[type];
}
- // Makes sure pos is inside range (inclusive)
+ // Makes sure pos is inside range (exclusive)
bool CheckRange(double pos, const constants::Values::Claws::AnglePair &pair) {
- return (pos >= pair.lower_angle && pos <= pair.upper_angle);
+ // Note: If the >= and <= signs are used, then the there exists a case
+ // where the wrist starts precisely on the edge and because initial
+ // position and the *edge_value_ are the same, then the comparison
+ // in ZeroedStateFeedbackLoop::DoGetPositionOfEdge will return that
+ // the lower, rather than upper, edge of the hall effect was passed.
+ return (pos > pair.lower_angle && pos < pair.upper_angle);
}
void SetHallEffect(double pos,
@@ -212,8 +217,7 @@
EXPECT_TRUE(claw_queue_group.output.FetchLatest());
claw_plant_->U << claw_queue_group.output->bottom_claw_voltage,
- claw_queue_group.output->top_claw_voltage -
- claw_queue_group.output->bottom_claw_voltage;
+ claw_queue_group.output->top_claw_voltage;
claw_plant_->Update();
// Check that the claw is within the limits.
@@ -454,7 +458,6 @@
::std::make_pair(1.1, 1.0),
::std::make_pair(1.15, 1.05),
::std::make_pair(1.05, 0.95),
- ::std::make_pair(1.1, 1.0),
::std::make_pair(1.2, 1.1),
::std::make_pair(1.3, 1.2),
::std::make_pair(1.4, 1.3),
@@ -462,7 +465,8 @@
::std::make_pair(1.6, 1.5),
::std::make_pair(1.7, 1.6),
::std::make_pair(1.8, 1.7),
- ::std::make_pair(2.015, 2.01)));
+ ::std::make_pair(2.015, 2.01)
+));
/*
// Tests that loosing the encoder for a second triggers a re-zero.
@@ -547,7 +551,7 @@
const frc971::constants::Values& values = constants::GetValues();
bool kicked = false;
bool measured = false;
- for (int i = 0; i < 600; ++i) {
+ for (int i = 0; i < 700; ++i) {
claw_motor_plant_.SendPositionMessage();
if (i >= start_time && mode == claw_motor_.mode() && !kicked) {
EXPECT_EQ(mode, claw_motor_.mode());
diff --git a/frc971/control_loops/claw/claw_motor_plant.cc b/frc971/control_loops/claw/claw_motor_plant.cc
index 196b62c..babbb04 100644
--- a/frc971/control_loops/claw/claw_motor_plant.cc
+++ b/frc971/control_loops/claw/claw_motor_plant.cc
@@ -9,25 +9,25 @@
StateFeedbackPlantCoefficients<4, 2, 2> MakeClawPlantCoefficients() {
Eigen::Matrix<double, 4, 4> A;
- A << 1.0, 0.0, 0.00740659366663, 0.0, 0.0, 1.0, 0.0, 0.00740659366663, 0.0, 0.0, 0.530576083967, 0.0, 0.0, 0.0, 0.0, 0.530576083967;
+ A << 1.0, 0.0, 0.00737284608086, 0.0, 0.0, 1.0, -0.00294667339472, 0.00442617268614, 0.0, 0.0, 0.525184383468, 0.0, 0.0, 0.0, -0.380328742836, 0.144855640632;
Eigen::Matrix<double, 4, 2> B;
- B << 0.00101390984157, 0.0, 0.0, 0.00101390984157, 0.183524472124, 0.0, 0.0, 0.183524472124;
+ B << 0.00102145540588, 0.0, -0.00102145540588, 0.00216714216844, 0.184611558069, 0.0, -0.184611558069, 0.332485973629;
Eigen::Matrix<double, 2, 4> C;
C << 1, 0, 0, 0, 1, 1, 0, 0;
Eigen::Matrix<double, 2, 2> D;
D << 0, 0, 0, 0;
Eigen::Matrix<double, 2, 1> U_max;
- U_max << 12.0, 24.0;
+ U_max << 12.0, 12.0;
Eigen::Matrix<double, 2, 1> U_min;
- U_min << -12.0, -24.0;
+ U_min << -12.0, -12.0;
return StateFeedbackPlantCoefficients<4, 2, 2>(A, B, C, D, U_max, U_min);
}
StateFeedbackController<4, 2, 2> MakeClawController() {
Eigen::Matrix<double, 4, 2> L;
- L << 1.43057608397, -4.48948312405e-16, -1.43057608397, 1.43057608397, 31.1907717473, -9.79345171104e-15, -31.1907717473, 31.1907717473;
+ L << 1.42518438347, 4.71027737605e-16, -1.42518438347, 1.04485564063, 30.6346010502, 1.00485917356e-14, -30.6346010502, 2.04727497147;
Eigen::Matrix<double, 2, 4> K;
- K << 110.395400642, 0.0, 2.50425726274, 0.0, 0.0, 170.435941688, 0.0, 2.89797614353;
+ K << 50.0, 0.0, 1.0, 0.0, 23.5668757858, 300.0, -0.88836718554, 1.1;
return StateFeedbackController<4, 2, 2>(L, K, MakeClawPlantCoefficients());
}
diff --git a/frc971/control_loops/claw/claw_motor_plant.h b/frc971/control_loops/claw/claw_motor_plant.h
index 988cc20..80164d8 100644
--- a/frc971/control_loops/claw/claw_motor_plant.h
+++ b/frc971/control_loops/claw/claw_motor_plant.h
@@ -14,6 +14,8 @@
StateFeedbackLoop<4, 2, 2> MakeClawLoop();
+const double kClawMomentOfInertiaRatio = 0.333333;
+
} // namespace control_loops
} // namespace frc971
diff --git a/frc971/control_loops/python/claw.py b/frc971/control_loops/python/claw.py
index 73039db..f059b45 100755
--- a/frc971/control_loops/python/claw.py
+++ b/frc971/control_loops/python/claw.py
@@ -13,15 +13,16 @@
self.stall_torque = 2.42
# Stall Current in Amps
self.stall_current = 133
- # Free Speed in RPM, pulled from drivetrain
+ # Free Speed in RPM
self.free_speed = 5500.0
# Free Current in Amps
self.free_current = 2.7
# Moment of inertia of the claw in kg m^2
- # approzimately 0.76 (without ball) in CAD
- self.J = 0.70
+ # measured from CAD
+ self.J_top = 0.3
+ self.J_bottom = 0.9
# Resistance of the motor
- self.R = 12.0 / self.stall_current + 0.024 + .003
+ self.R = 12.0 / self.stall_current
# Motor velocity constant
self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
(13.5 - self.R * self.free_current))
@@ -32,25 +33,58 @@
# Control loop time step
self.dt = 0.01
- # State is [bottom position, top - bottom position,
- # bottom velocity, top - bottom velocity]
- # Input is [bottom power, top power]
- # Motor time constant.
- self.motor_timeconstant = self.Kt / self.Kv / (self.J * self.G * self.G * self.R)
+ # State is [bottom position, bottom velocity, top - bottom position,
+ # top - bottom velocity]
+ # Input is [bottom power, top power - bottom power * J_top / J_bottom]
+ # Motor time constants. difference_bottom refers to the constant for how the
+ # bottom velocity affects the difference of the top and bottom velocities.
+ self.common_motor_constant = -self.Kt / self.Kv / (self.G * self.G * self.R)
+ self.bottom_bottom = self.common_motor_constant / self.J_bottom
+ self.difference_bottom = -self.common_motor_constant * (1 / self.J_bottom
+ - 1 / self.J_top)
+ self.difference_difference = self.common_motor_constant / self.J_top
# State feedback matrices
+
self.A_continuous = numpy.matrix(
[[0, 0, 1, 0],
[0, 0, 0, 1],
- [0, 0, -self.motor_timeconstant, 0],
- [0, 0, 0, -self.motor_timeconstant]])
+ [0, 0, self.bottom_bottom, 0],
+ [0, 0, self.difference_bottom, self.difference_difference]])
- self.motor_feedforward = self.Kt / (self.J * self.G * self.R)
+ self.A_bottom_cont = numpy.matrix(
+ [[0, 1],
+ [0, self.bottom_bottom]])
+ self.A_diff_cont = numpy.matrix(
+ [[0, 1],
+ [0, self.difference_difference]])
+
+ # self.A_continuous[0:2, 0:2] = self.A_bottom_cont
+ # self.A_continuous[2:4, 2:4] = self.A_diff_cont
+ # self.A_continuous[3, 1] = self.difference_bottom
+
+ self.motor_feedforward = self.Kt / (self.G * self.R)
+ self.motor_feedforward_bottom = self.motor_feedforward / self.J_bottom
+ self.motor_feedforward_difference = self.motor_feedforward / self.J_top
+ self.motor_feedforward_difference_bottom = (
+ self.motor_feedforward * (1 / self.J_bottom - 1 / self.J_top))
self.B_continuous = numpy.matrix(
[[0, 0],
[0, 0],
- [self.motor_feedforward, 0],
- [0.0, self.motor_feedforward]])
+ [self.motor_feedforward_bottom, 0],
+ [-self.motor_feedforward_bottom,
+ self.motor_feedforward_difference]])
+
+ print "Cont X_ss", self.motor_feedforward / -self.common_motor_constant
+
+ self.B_bottom_cont = numpy.matrix(
+ [[0],
+ [self.motor_feedforward_bottom]])
+
+ self.B_diff_cont = numpy.matrix(
+ [[0],
+ [self.motor_feedforward_difference]])
+
self.C = numpy.matrix([[1, 0, 0, 0],
[1, 1, 0, 0]])
self.D = numpy.matrix([[0, 0],
@@ -59,6 +93,32 @@
self.A, self.B = self.ContinuousToDiscrete(
self.A_continuous, self.B_continuous, self.dt)
+ self.A_bottom, self.B_bottom = controls.c2d(
+ self.A_bottom_cont, self.B_bottom_cont, self.dt)
+ self.A_diff, self.B_diff = controls.c2d(
+ self.A_diff_cont, self.B_diff_cont, self.dt)
+
+ print "A"
+ print self.A
+ print "B"
+ print self.B
+
+ X_ss = numpy.matrix([[0], [0], [0.0], [0]])
+
+ U = numpy.matrix([[1.0],[1.0]])
+ A = self.A
+ B = self.B
+ #X_ss[2, 0] = X_ss[2, 0] * A[2, 2] + B[2, 0] * U[0, 0]
+ X_ss[2, 0] = 1 / (1 - A[2, 2]) * B[2, 0] * U[0, 0]
+ #X_ss[3, 0] = X_ss[3, 0] * A[3, 3] + X_ss[2, 0] * A[3, 2] + B[3, 0] * U[0, 0] + B[3, 1] * U[1, 0]
+ #X_ss[3, 0] * (1 - A[3, 3]) = X_ss[2, 0] * A[3, 2] + B[3, 0] * U[0, 0] + B[3, 1] * U[1, 0]
+ X_ss[3, 0] = 1 / (1 - A[3, 3]) * (X_ss[2, 0] * A[3, 2] + B[3, 1] * U[1, 0] + B[3, 0] * U[0, 0])
+ #X_ss[3, 0] = 1 / (1 - A[3, 3]) / (1 - A[2, 2]) * B[2, 0] * U[0, 0] * A[3, 2] + B[3, 0] * U[0, 0] + B[3, 1] * U[1, 0]
+ X_ss[0, 0] = A[0, 2] * X_ss[2, 0] + B[0, 0] * U[0, 0]
+ X_ss[1, 0] = A[1, 2] * X_ss[2, 0] + A[1, 3] * X_ss[3, 0] + B[1, 0] * U[0, 0] + B[1, 1] * U[1, 0]
+
+ print "X_ss", X_ss
+
#controlability = controls.ctrb(self.A, self.B);
#print "Rank of controlability matrix.", numpy.linalg.matrix_rank(controlability)
@@ -67,24 +127,46 @@
[0.0, 0.0, 0.10, 0.0],
[0.0, 0.0, 0.0, 0.1]])
- self.R = numpy.matrix([[(1.0 / (20.0 ** 2.0)), 0.0],
- [0.0, (1.0 / (20.0 ** 2.0))]])
- self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
+ self.R = numpy.matrix([[(1.0 / (40.0 ** 2.0)), 0.0],
+ [0.0, (1.0 / (5.0 ** 2.0))]])
+ #self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
+
+ self.K = numpy.matrix([[50, 0.0, 1, 0.0],
+ [0.0, 300, 0.0, 1.1]])
+ lstsq_A = numpy.identity(2)
+ lstsq_A[0] = self.B[1]
+ lstsq_A[1] = self.B[3]
+ print "System of Equations coefficients:"
+ print lstsq_A
+ print "det", numpy.linalg.det(lstsq_A)
+ self.K[1, 0] = -lstsq_A[0, 0] * self.K[0, 0] / lstsq_A[0, 1]
+ #self.K[0:2, 0] = numpy.linalg.lstsq(lstsq_A, numpy.matrix([[0.0], [0.0]]))[0]
+ out_x = numpy.linalg.lstsq(
+ lstsq_A,
+ numpy.matrix([[self.A[1, 2]], [self.A[3, 2]]]))[0]
+ self.K[1, 2] = -lstsq_A[0, 0] * (self.K[0, 2] - out_x[0]) / lstsq_A[0, 1] + out_x[1]
print "K unaugmented"
print self.K
- print "Placed controller poles"
- print numpy.linalg.eig(self.A - self.B * self.K)[0]
+ print "B * K unaugmented"
+ print self.B * self.K
+ F = self.A - self.B * self.K
+ print "A - B * K unaugmented"
+ print F
+ print "eigenvalues"
+ print numpy.linalg.eig(F)[0]
self.rpl = .05
self.ipl = 0.008
self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
- self.rpl - 1j * self.ipl,
self.rpl + 1j * self.ipl,
+ self.rpl - 1j * self.ipl,
self.rpl - 1j * self.ipl])
- self.U_max = numpy.matrix([[12.0], [24.0]])
- self.U_min = numpy.matrix([[-12.0], [-24.0]])
+ # The box formed by U_min and U_max must encompass all possible values,
+ # or else Austin's code gets angry.
+ self.U_max = numpy.matrix([[12.0], [12.0]])
+ self.U_min = numpy.matrix([[-12.0], [-12.0]])
self.InitializeState()
@@ -196,18 +278,47 @@
return numpy.matrix([[bottom_u], [top_u - bottom_u]])
-def AverageUFix(claw, U):
- bottom_u = U[0, 0]
- top_u = U[1, 0] + bottom_u
+def AverageUFix(claw, U, preserve_v3=False):
+ """Clips U as necessary.
- #print "Bottom is", new_unclipped_bottom_u, "Top is", top_u
- if (bottom_u > claw.U_max[0, 0] or top_u > claw.U_max[1, 0] or
- top_u < claw.U_min[1, 0] or bottom_u < claw.U_min[0, 0]):
- scalar = 12.0 / max(numpy.abs(top_u), numpy.abs(bottom_u))
+ Args:
+ claw: claw object containing moments of inertia and U limits.
+ U: Input matrix to clip as necessary.
+ preserve_v3: There are two ways to attempt to clip the voltages:
+ -If you preserve the imaginary v3, then it will attempt to
+ preserve the effect on the separation of the two claws.
+ If it is not able to do this, then it calls itself with preserve_v3
+ set to False.
+ -If you preserve the ratio of the voltage of the bottom and the top,
+ then it will attempt to preserve the ratio of those two. This is
+ equivalent to preserving the ratio of v3 and the bottom voltage.
+ """
+ bottom_u = U[0, 0]
+ top_u = U[1, 0]
+ seperation_u = top_u - bottom_u * claw.J_top / claw.J_bottom
+
+ top_big = top_u > claw.U_max[0, 0]
+ top_small = top_u < claw.U_min[0, 0]
+ bot_big = bottom_u > claw.U_max[0, 0]
+ bot_small = top_u < claw.U_min[0, 0]
+ bottom_bad = bot_big or bot_small
+ top_bad = top_big or top_small
+ scalar = claw.U_max[0, 0] / max(numpy.abs(top_u), numpy.abs(bottom_u))
+ if bottom_bad and preserve_v3:
+ bottom_u *= scalar
+ top_u = seperation_u + bottom_u * claw.J_top / claw.J_bottom
+ if abs(top_u) > claw.U_max[0, 0]:
+ return AverageUFix(claw, U, preserve_v3=False)
+ elif top_bad and preserve_v3:
+ top_u *= scalar
+ bottom_u = (top_u - seperation_u) * claw.J_bottom / claw.J_top
+ if abs(bottom_u) > claw.U_max[0, 0]:
+ return AverageUFix(claw, U, preserve_v3=False)
+ elif (bottom_bad or top_bad) and not preserve_v3:
top_u *= scalar
bottom_u *= scalar
- return numpy.matrix([[bottom_u], [top_u - bottom_u]])
+ return numpy.matrix([[bottom_u], [top_u]])
def ClipDeltaU(claw, U):
delta_u = U[0, 0]
@@ -248,33 +359,47 @@
#pylab.plot(range(100), simulated_x)
#pylab.show()
- # Simulate the closed loop response of the system to a step input.
+ # Simulate the closed loop response of the system.
claw = Claw("TopClaw")
t = []
close_loop_x_bottom = []
close_loop_x_sep = []
+ actual_sep = []
+ actual_x_bottom = []
+ close_loop_x_top = []
close_loop_u_bottom = []
close_loop_u_top = []
- R = numpy.matrix([[1.0], [1.0], [0.0], [0.0]])
- claw.X[0, 0] = 0
+ R = numpy.matrix([[0.0], [0.00], [0.0], [0.0]])
+ claw.X[0, 0] = 1
+ claw.X[1, 0] = .0
+ claw.X_hat = claw.X
+ #X_actual = claw.X
for i in xrange(100):
#print "Error is", (R - claw.X_hat)
- U = claw.K * (R - claw.X_hat)
+ U = claw.K * (R - claw.X)
#U = numpy.clip(claw.K * (R - claw.X_hat), claw.U_min, claw.U_max)
#U = FullSeparationPriority(claw, U)
- U = AverageUFix(claw, U)
+ #U = AverageUFix(claw, U, preserve_v3=True)
#U = claw.K * (R - claw.X_hat)
#U = ClipDeltaU(claw, U)
claw.UpdateObserver(U)
claw.Update(U)
+ #X_actual = claw.A_actual * X_actual + claw.B_actual * U
+ #claw.Y = claw.C * X_actual
close_loop_x_bottom.append(claw.X[0, 0] * 10)
close_loop_u_bottom.append(U[0, 0])
- close_loop_x_sep.append(claw.X[1, 0] * 10)
- close_loop_u_top.append(U[1, 0] + U[0, 0])
+ #actual_sep.append(X_actual[2, 0] * 100)
+ #actual_x_bottom.append(X_actual[0, 0] * 10)
+ close_loop_x_sep.append(claw.X[1, 0] * 100)
+ close_loop_x_top.append((claw.X[1, 0] + claw.X[0, 0]) * 10)
+ close_loop_u_top.append(U[1, 0])
t.append(0.01 * i)
pylab.plot(t, close_loop_x_bottom, label='x bottom')
pylab.plot(t, close_loop_x_sep, label='separation')
+ #pylab.plot(t, actual_x_bottom, label='true x bottom')
+ #pylab.plot(t, actual_sep, label='true separation')
+ pylab.plot(t, close_loop_x_top, label='x top')
pylab.plot(t, close_loop_u_bottom, label='u bottom')
pylab.plot(t, close_loop_u_top, label='u top')
pylab.legend()
diff --git a/frc971/control_loops/python/control_loop.py b/frc971/control_loops/python/control_loop.py
index 4b63aec..90faf9f 100644
--- a/frc971/control_loops/python/control_loop.py
+++ b/frc971/control_loops/python/control_loop.py
@@ -62,8 +62,10 @@
"""Returns a template name for StateFeedbackPlantCoefficients."""
return self._GenericType('StateFeedbackPlantCoefficients')
- def WriteHeader(self, header_file):
- """Writes the header file to the file named header_file."""
+ def WriteHeader(self, header_file, double_appendage=False, MoI_ratio=0.0):
+ """Writes the header file to the file named header_file.
+ Set double_appendage to true in order to include a ratio of
+ moments of inertia constant. Currently, only used for 2014 claw."""
with open(header_file, 'w') as fd:
header_guard = self._HeaderGuard(header_file)
fd.write('#ifndef %s\n'
@@ -85,6 +87,10 @@
fd.write('%s Make%sLoop();\n\n' %
(self._LoopType(), self._gain_schedule_name))
+ fd.write('const double k%sMomentOfInertiaRatio = %f;\n\n' %
+ (self._gain_schedule_name,
+ self._loops[0].J_top / self._loops[0].J_bottom))
+
fd.write(self._namespace_end)
fd.write('\n\n')
fd.write("#endif // %s\n" % header_guard)
@@ -183,7 +189,7 @@
def Update(self, U):
"""Simulates one time step with the provided U."""
- U = numpy.clip(U, self.U_min, self.U_max)
+ #U = numpy.clip(U, self.U_min, self.U_max)
self.X = self.A * self.X + self.B * U
self.Y = self.C * self.X + self.D * U