Reformat python and c++ files
Change-Id: I7d7d99a2094c2a9181ed882735b55159c14db3b0
diff --git a/y2016/control_loops/python/arm.py b/y2016/control_loops/python/arm.py
index 5c5793b..96975bb 100755
--- a/y2016/control_loops/python/arm.py
+++ b/y2016/control_loops/python/arm.py
@@ -18,414 +18,439 @@
FLAGS = gflags.FLAGS
try:
- gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
+ gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
except gflags.DuplicateFlagError:
- pass
+ pass
class Arm(control_loop.ControlLoop):
- def __init__(self, name="Arm", J=None):
- super(Arm, self).__init__(name=name)
- self._shoulder = Shoulder(name=name, J=J)
- self._shooter = Wrist(name=name)
- self.shoulder_Kv = self._shoulder.Kv / self._shoulder.G
- # Do a coordinate transformation.
- # X_shooter_grounded = X_shooter + X_shoulder
- # dX_shooter_grounded/dt = A_shooter * X_shooter + A_shoulder * X_shoulder +
- # B_shoulder * U_shoulder + B_shooter * U_shooter
- # dX_shooter_grounded/dt = A_shooter * (X_shooter_grounded - X_shoulder) +
- # A_shoulder * X_shoulder + B_shooter * U_shooter + B_shoulder * U_shoulder
- # X = [X_shoulder; X_shooter + X_shoulder]
- # dX/dt = [A_shoulder 0] [X_shoulder ] + [B_shoulder 0] [U_shoulder]
- # [(A_shoulder - A_shooter) A_shooter] [X_shooter_grounded] + [B_shoulder B_shooter] [ U_shooter]
- # Y_shooter_grounded = Y_shooter + Y_shoulder
+ def __init__(self, name="Arm", J=None):
+ super(Arm, self).__init__(name=name)
+ self._shoulder = Shoulder(name=name, J=J)
+ self._shooter = Wrist(name=name)
+ self.shoulder_Kv = self._shoulder.Kv / self._shoulder.G
- self.A_continuous = numpy.matrix(numpy.zeros((4, 4)))
- self.A_continuous[0:2, 0:2] = self._shoulder.A_continuous
- self.A_continuous[2:4, 0:2] = (self._shoulder.A_continuous -
- self._shooter.A_continuous)
- self.A_continuous[2:4, 2:4] = self._shooter.A_continuous
+ # Do a coordinate transformation.
+ # X_shooter_grounded = X_shooter + X_shoulder
+ # dX_shooter_grounded/dt = A_shooter * X_shooter + A_shoulder * X_shoulder +
+ # B_shoulder * U_shoulder + B_shooter * U_shooter
+ # dX_shooter_grounded/dt = A_shooter * (X_shooter_grounded - X_shoulder) +
+ # A_shoulder * X_shoulder + B_shooter * U_shooter + B_shoulder * U_shoulder
+ # X = [X_shoulder; X_shooter + X_shoulder]
+ # dX/dt = [A_shoulder 0] [X_shoulder ] + [B_shoulder 0] [U_shoulder]
+ # [(A_shoulder - A_shooter) A_shooter] [X_shooter_grounded] + [B_shoulder B_shooter] [ U_shooter]
+ # Y_shooter_grounded = Y_shooter + Y_shoulder
- self.B_continuous = numpy.matrix(numpy.zeros((4, 2)))
- self.B_continuous[0:2, 0:1] = self._shoulder.B_continuous
- self.B_continuous[2:4, 1:2] = self._shooter.B_continuous
- self.B_continuous[2:4, 0:1] = self._shoulder.B_continuous
+ self.A_continuous = numpy.matrix(numpy.zeros((4, 4)))
+ self.A_continuous[0:2, 0:2] = self._shoulder.A_continuous
+ self.A_continuous[2:4, 0:2] = (
+ self._shoulder.A_continuous - self._shooter.A_continuous)
+ self.A_continuous[2:4, 2:4] = self._shooter.A_continuous
- self.C = numpy.matrix(numpy.zeros((2, 4)))
- self.C[0:1, 0:2] = self._shoulder.C
- self.C[1:2, 0:2] = -self._shoulder.C
- self.C[1:2, 2:4] = self._shooter.C
+ self.B_continuous = numpy.matrix(numpy.zeros((4, 2)))
+ self.B_continuous[0:2, 0:1] = self._shoulder.B_continuous
+ self.B_continuous[2:4, 1:2] = self._shooter.B_continuous
+ self.B_continuous[2:4, 0:1] = self._shoulder.B_continuous
- # D is 0 for all our loops.
- self.D = numpy.matrix(numpy.zeros((2, 2)))
+ self.C = numpy.matrix(numpy.zeros((2, 4)))
+ self.C[0:1, 0:2] = self._shoulder.C
+ self.C[1:2, 0:2] = -self._shoulder.C
+ self.C[1:2, 2:4] = self._shooter.C
- self.dt = 0.005
+ # D is 0 for all our loops.
+ self.D = numpy.matrix(numpy.zeros((2, 2)))
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ self.dt = 0.005
- # Cost of error
- self.Q = numpy.matrix(numpy.zeros((4, 4)))
- q_pos_shoulder = 0.014
- q_vel_shoulder = 4.00
- q_pos_shooter = 0.014
- q_vel_shooter = 4.00
- self.Q[0, 0] = 1.0 / q_pos_shoulder ** 2.0
- self.Q[1, 1] = 1.0 / q_vel_shoulder ** 2.0
- self.Q[2, 2] = 1.0 / q_pos_shooter ** 2.0
- self.Q[3, 3] = 1.0 / q_vel_shooter ** 2.0
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- self.Qff = numpy.matrix(numpy.zeros((4, 4)))
- qff_pos_shoulder = 0.005
- qff_vel_shoulder = 1.00
- qff_pos_shooter = 0.005
- qff_vel_shooter = 1.00
- self.Qff[0, 0] = 1.0 / qff_pos_shoulder ** 2.0
- self.Qff[1, 1] = 1.0 / qff_vel_shoulder ** 2.0
- self.Qff[2, 2] = 1.0 / qff_pos_shooter ** 2.0
- self.Qff[3, 3] = 1.0 / qff_vel_shooter ** 2.0
+ # Cost of error
+ self.Q = numpy.matrix(numpy.zeros((4, 4)))
+ q_pos_shoulder = 0.014
+ q_vel_shoulder = 4.00
+ q_pos_shooter = 0.014
+ q_vel_shooter = 4.00
+ self.Q[0, 0] = 1.0 / q_pos_shoulder**2.0
+ self.Q[1, 1] = 1.0 / q_vel_shoulder**2.0
+ self.Q[2, 2] = 1.0 / q_pos_shooter**2.0
+ self.Q[3, 3] = 1.0 / q_vel_shooter**2.0
- # Cost of control effort
- self.R = numpy.matrix(numpy.zeros((2, 2)))
- r_voltage = 1.0 / 12.0
- self.R[0, 0] = r_voltage ** 2.0
- self.R[1, 1] = r_voltage ** 2.0
+ self.Qff = numpy.matrix(numpy.zeros((4, 4)))
+ qff_pos_shoulder = 0.005
+ qff_vel_shoulder = 1.00
+ qff_pos_shooter = 0.005
+ qff_vel_shooter = 1.00
+ self.Qff[0, 0] = 1.0 / qff_pos_shoulder**2.0
+ self.Qff[1, 1] = 1.0 / qff_vel_shoulder**2.0
+ self.Qff[2, 2] = 1.0 / qff_pos_shooter**2.0
+ self.Qff[3, 3] = 1.0 / qff_vel_shooter**2.0
- self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
+ # Cost of control effort
+ self.R = numpy.matrix(numpy.zeros((2, 2)))
+ r_voltage = 1.0 / 12.0
+ self.R[0, 0] = r_voltage**2.0
+ self.R[1, 1] = r_voltage**2.0
- glog.debug('Shoulder K')
- glog.debug(repr(self._shoulder.K))
- glog.debug('Poles are %s',
- repr(numpy.linalg.eig(self._shoulder.A -
- self._shoulder.B * self._shoulder.K)[0]))
+ self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
- # Compute controller gains.
- # self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
- self.K = numpy.matrix(numpy.zeros((2, 4)))
- self.K[0:1, 0:2] = self._shoulder.K
- self.K[1:2, 0:2] = (
- -self.Kff[1:2, 2:4] * self.B[2:4, 0:1] * self._shoulder.K
- + self.Kff[1:2, 2:4] * self.A[2:4, 0:2])
- self.K[1:2, 2:4] = self._shooter.K
+ glog.debug('Shoulder K')
+ glog.debug(repr(self._shoulder.K))
+ glog.debug(
+ 'Poles are %s',
+ repr(
+ numpy.linalg.eig(self._shoulder.A -
+ self._shoulder.B * self._shoulder.K)[0]))
- glog.debug('Arm controller %s', repr(self.K))
+ # Compute controller gains.
+ # self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
+ self.K = numpy.matrix(numpy.zeros((2, 4)))
+ self.K[0:1, 0:2] = self._shoulder.K
+ self.K[1:2, 0:2] = (
+ -self.Kff[1:2, 2:4] * self.B[2:4, 0:1] * self._shoulder.K +
+ self.Kff[1:2, 2:4] * self.A[2:4, 0:2])
+ self.K[1:2, 2:4] = self._shooter.K
- # Cost of error
- self.Q = numpy.matrix(numpy.zeros((4, 4)))
- q_pos_shoulder = 0.05
- q_vel_shoulder = 2.65
- q_pos_shooter = 0.05
- q_vel_shooter = 2.65
- self.Q[0, 0] = q_pos_shoulder ** 2.0
- self.Q[1, 1] = q_vel_shoulder ** 2.0
- self.Q[2, 2] = q_pos_shooter ** 2.0
- self.Q[3, 3] = q_vel_shooter ** 2.0
+ glog.debug('Arm controller %s', repr(self.K))
- # Cost of control effort
- self.R = numpy.matrix(numpy.zeros((2, 2)))
- r_voltage = 0.025
- self.R[0, 0] = r_voltage ** 2.0
- self.R[1, 1] = r_voltage ** 2.0
+ # Cost of error
+ self.Q = numpy.matrix(numpy.zeros((4, 4)))
+ q_pos_shoulder = 0.05
+ q_vel_shoulder = 2.65
+ q_pos_shooter = 0.05
+ q_vel_shooter = 2.65
+ self.Q[0, 0] = q_pos_shoulder**2.0
+ self.Q[1, 1] = q_vel_shoulder**2.0
+ self.Q[2, 2] = q_pos_shooter**2.0
+ self.Q[3, 3] = q_vel_shooter**2.0
- self.KalmanGain, self.Q_steady = controls.kalman(
- A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
- self.L = self.A * self.KalmanGain
+ # Cost of control effort
+ self.R = numpy.matrix(numpy.zeros((2, 2)))
+ r_voltage = 0.025
+ self.R[0, 0] = r_voltage**2.0
+ self.R[1, 1] = r_voltage**2.0
- self.U_max = numpy.matrix([[12.0], [12.0]])
- self.U_min = numpy.matrix([[-12.0], [-12.0]])
+ self.KalmanGain, self.Q_steady = controls.kalman(
+ A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
+ self.L = self.A * self.KalmanGain
- self.InitializeState()
+ self.U_max = numpy.matrix([[12.0], [12.0]])
+ self.U_min = numpy.matrix([[-12.0], [-12.0]])
+
+ self.InitializeState()
class IntegralArm(Arm):
- def __init__(self, name="IntegralArm", J=None):
- super(IntegralArm, self).__init__(name=name, J=J)
- self.A_continuous_unaugmented = self.A_continuous
- self.B_continuous_unaugmented = self.B_continuous
+ def __init__(self, name="IntegralArm", J=None):
+ super(IntegralArm, self).__init__(name=name, J=J)
- self.A_continuous = numpy.matrix(numpy.zeros((6, 6)))
- self.A_continuous[0:4, 0:4] = self.A_continuous_unaugmented
- self.A_continuous[0:4, 4:6] = self.B_continuous_unaugmented
+ self.A_continuous_unaugmented = self.A_continuous
+ self.B_continuous_unaugmented = self.B_continuous
- self.B_continuous = numpy.matrix(numpy.zeros((6, 2)))
- self.B_continuous[0:4, 0:2] = self.B_continuous_unaugmented
+ self.A_continuous = numpy.matrix(numpy.zeros((6, 6)))
+ self.A_continuous[0:4, 0:4] = self.A_continuous_unaugmented
+ self.A_continuous[0:4, 4:6] = self.B_continuous_unaugmented
- self.C_unaugmented = self.C
- self.C = numpy.matrix(numpy.zeros((2, 6)))
- self.C[0:2, 0:4] = self.C_unaugmented
+ self.B_continuous = numpy.matrix(numpy.zeros((6, 2)))
+ self.B_continuous[0:4, 0:2] = self.B_continuous_unaugmented
- self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, self.B_continuous, self.dt)
+ self.C_unaugmented = self.C
+ self.C = numpy.matrix(numpy.zeros((2, 6)))
+ self.C[0:2, 0:4] = self.C_unaugmented
- q_pos_shoulder = 0.10
- q_vel_shoulder = 0.005
- q_voltage_shoulder = 3.5
- q_pos_shooter = 0.08
- q_vel_shooter = 2.00
- q_voltage_shooter = 1.0
- self.Q = numpy.matrix(numpy.zeros((6, 6)))
- self.Q[0, 0] = q_pos_shoulder ** 2.0
- self.Q[1, 1] = q_vel_shoulder ** 2.0
- self.Q[2, 2] = q_pos_shooter ** 2.0
- self.Q[3, 3] = q_vel_shooter ** 2.0
- self.Q[4, 4] = q_voltage_shoulder ** 2.0
- self.Q[5, 5] = q_voltage_shooter ** 2.0
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- self.R = numpy.matrix(numpy.zeros((2, 2)))
- r_pos = 0.05
- self.R[0, 0] = r_pos ** 2.0
- self.R[1, 1] = r_pos ** 2.0
+ q_pos_shoulder = 0.10
+ q_vel_shoulder = 0.005
+ q_voltage_shoulder = 3.5
+ q_pos_shooter = 0.08
+ q_vel_shooter = 2.00
+ q_voltage_shooter = 1.0
+ self.Q = numpy.matrix(numpy.zeros((6, 6)))
+ self.Q[0, 0] = q_pos_shoulder**2.0
+ self.Q[1, 1] = q_vel_shoulder**2.0
+ self.Q[2, 2] = q_pos_shooter**2.0
+ self.Q[3, 3] = q_vel_shooter**2.0
+ self.Q[4, 4] = q_voltage_shoulder**2.0
+ self.Q[5, 5] = q_voltage_shooter**2.0
- self.KalmanGain, self.Q_steady = controls.kalman(
- A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
- self.L = self.A * self.KalmanGain
+ self.R = numpy.matrix(numpy.zeros((2, 2)))
+ r_pos = 0.05
+ self.R[0, 0] = r_pos**2.0
+ self.R[1, 1] = r_pos**2.0
- self.K_unaugmented = self.K
- self.K = numpy.matrix(numpy.zeros((2, 6)))
- self.K[0:2, 0:4] = self.K_unaugmented
- self.K[0, 4] = 1
- self.K[1, 5] = 1
+ self.KalmanGain, self.Q_steady = controls.kalman(
+ A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
+ self.L = self.A * self.KalmanGain
- self.Kff = numpy.concatenate((self.Kff, numpy.matrix(numpy.zeros((2, 2)))), axis=1)
+ self.K_unaugmented = self.K
+ self.K = numpy.matrix(numpy.zeros((2, 6)))
+ self.K[0:2, 0:4] = self.K_unaugmented
+ self.K[0, 4] = 1
+ self.K[1, 5] = 1
- self.InitializeState()
+ self.Kff = numpy.concatenate(
+ (self.Kff, numpy.matrix(numpy.zeros((2, 2)))), axis=1)
+
+ self.InitializeState()
class ScenarioPlotter(object):
- def __init__(self):
- # Various lists for graphing things.
- self.t = []
- self.x_shoulder = []
- self.v_shoulder = []
- self.a_shoulder = []
- self.x_hat_shoulder = []
- self.u_shoulder = []
- self.offset_shoulder = []
- self.x_shooter = []
- self.v_shooter = []
- self.a_shooter = []
- self.x_hat_shooter = []
- self.u_shooter = []
- self.offset_shooter = []
- self.goal_x_shoulder = []
- self.goal_v_shoulder = []
- self.goal_x_shooter = []
- self.goal_v_shooter = []
- def run_test(self, arm, end_goal,
- iterations=200, controller=None, observer=None):
- """Runs the plant with an initial condition and goal.
+ def __init__(self):
+ # Various lists for graphing things.
+ self.t = []
+ self.x_shoulder = []
+ self.v_shoulder = []
+ self.a_shoulder = []
+ self.x_hat_shoulder = []
+ self.u_shoulder = []
+ self.offset_shoulder = []
+ self.x_shooter = []
+ self.v_shooter = []
+ self.a_shooter = []
+ self.x_hat_shooter = []
+ self.u_shooter = []
+ self.offset_shooter = []
+ self.goal_x_shoulder = []
+ self.goal_v_shoulder = []
+ self.goal_x_shooter = []
+ self.goal_v_shooter = []
- Args:
- arm: Arm object to use.
- end_goal: numpy.Matrix[6, 1], end goal state.
- iterations: Number of timesteps to run the model for.
- controller: Arm object to get K from, or None if we should
- use arm.
- observer: Arm object to use for the observer, or None if we should
- use the actual state.
- """
+ def run_test(self,
+ arm,
+ end_goal,
+ iterations=200,
+ controller=None,
+ observer=None):
+ """Runs the plant with an initial condition and goal.
- if controller is None:
- controller = arm
+ Args:
+ arm: Arm object to use.
+ end_goal: numpy.Matrix[6, 1], end goal state.
+ iterations: Number of timesteps to run the model for.
+ controller: Arm object to get K from, or None if we should
+ use arm.
+ observer: Arm object to use for the observer, or None if we should
+ use the actual state.
+ """
- vbat = 12.0
+ if controller is None:
+ controller = arm
- if self.t:
- initial_t = self.t[-1] + arm.dt
- else:
- initial_t = 0
+ vbat = 12.0
- goal = numpy.concatenate((arm.X, numpy.matrix(numpy.zeros((2, 1)))), axis=0)
+ if self.t:
+ initial_t = self.t[-1] + arm.dt
+ else:
+ initial_t = 0
- shoulder_profile = TrapezoidProfile(arm.dt)
- shoulder_profile.set_maximum_acceleration(12.0)
- shoulder_profile.set_maximum_velocity(10.0)
- shoulder_profile.SetGoal(goal[0, 0])
- shooter_profile = TrapezoidProfile(arm.dt)
- shooter_profile.set_maximum_acceleration(50.0)
- shooter_profile.set_maximum_velocity(10.0)
- shooter_profile.SetGoal(goal[2, 0])
+ goal = numpy.concatenate((arm.X, numpy.matrix(numpy.zeros((2, 1)))),
+ axis=0)
- U_last = numpy.matrix(numpy.zeros((2, 1)))
- for i in xrange(iterations):
- X_hat = arm.X
+ shoulder_profile = TrapezoidProfile(arm.dt)
+ shoulder_profile.set_maximum_acceleration(12.0)
+ shoulder_profile.set_maximum_velocity(10.0)
+ shoulder_profile.SetGoal(goal[0, 0])
+ shooter_profile = TrapezoidProfile(arm.dt)
+ shooter_profile.set_maximum_acceleration(50.0)
+ shooter_profile.set_maximum_velocity(10.0)
+ shooter_profile.SetGoal(goal[2, 0])
- if observer is not None:
- observer.Y = arm.Y
- observer.CorrectObserver(U_last)
- self.offset_shoulder.append(observer.X_hat[4, 0])
- self.offset_shooter.append(observer.X_hat[5, 0])
+ U_last = numpy.matrix(numpy.zeros((2, 1)))
+ for i in xrange(iterations):
+ X_hat = arm.X
- X_hat = observer.X_hat
- self.x_hat_shoulder.append(observer.X_hat[0, 0])
- self.x_hat_shooter.append(observer.X_hat[2, 0])
+ if observer is not None:
+ observer.Y = arm.Y
+ observer.CorrectObserver(U_last)
+ self.offset_shoulder.append(observer.X_hat[4, 0])
+ self.offset_shooter.append(observer.X_hat[5, 0])
- next_shoulder_goal = shoulder_profile.Update(end_goal[0, 0], end_goal[1, 0])
- next_shooter_goal = shooter_profile.Update(end_goal[2, 0], end_goal[3, 0])
+ X_hat = observer.X_hat
+ self.x_hat_shoulder.append(observer.X_hat[0, 0])
+ self.x_hat_shooter.append(observer.X_hat[2, 0])
- next_goal = numpy.concatenate(
- (next_shoulder_goal,
- next_shooter_goal,
- numpy.matrix(numpy.zeros((2, 1)))),
- axis=0)
- self.goal_x_shoulder.append(goal[0, 0])
- self.goal_v_shoulder.append(goal[1, 0])
- self.goal_x_shooter.append(goal[2, 0])
- self.goal_v_shooter.append(goal[3, 0])
+ next_shoulder_goal = shoulder_profile.Update(
+ end_goal[0, 0], end_goal[1, 0])
+ next_shooter_goal = shooter_profile.Update(end_goal[2, 0],
+ end_goal[3, 0])
- ff_U = controller.Kff * (next_goal - observer.A * goal)
+ next_goal = numpy.concatenate(
+ (next_shoulder_goal, next_shooter_goal,
+ numpy.matrix(numpy.zeros((2, 1)))),
+ axis=0)
+ self.goal_x_shoulder.append(goal[0, 0])
+ self.goal_v_shoulder.append(goal[1, 0])
+ self.goal_x_shooter.append(goal[2, 0])
+ self.goal_v_shooter.append(goal[3, 0])
- U_uncapped = controller.K * (goal - X_hat) + ff_U
- U = U_uncapped.copy()
+ ff_U = controller.Kff * (next_goal - observer.A * goal)
- U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
- U[1, 0] = numpy.clip(U[1, 0], -vbat, vbat)
- self.x_shoulder.append(arm.X[0, 0])
- self.x_shooter.append(arm.X[2, 0])
+ U_uncapped = controller.K * (goal - X_hat) + ff_U
+ U = U_uncapped.copy()
- if self.v_shoulder:
- last_v_shoulder = self.v_shoulder[-1]
- else:
- last_v_shoulder = 0
- self.v_shoulder.append(arm.X[1, 0])
- self.a_shoulder.append(
- (self.v_shoulder[-1] - last_v_shoulder) / arm.dt)
+ U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
+ U[1, 0] = numpy.clip(U[1, 0], -vbat, vbat)
+ self.x_shoulder.append(arm.X[0, 0])
+ self.x_shooter.append(arm.X[2, 0])
- if self.v_shooter:
- last_v_shooter = self.v_shooter[-1]
- else:
- last_v_shooter = 0
- self.v_shooter.append(arm.X[3, 0])
- self.a_shooter.append(
- (self.v_shooter[-1] - last_v_shooter) / arm.dt)
+ if self.v_shoulder:
+ last_v_shoulder = self.v_shoulder[-1]
+ else:
+ last_v_shoulder = 0
+ self.v_shoulder.append(arm.X[1, 0])
+ self.a_shoulder.append(
+ (self.v_shoulder[-1] - last_v_shoulder) / arm.dt)
- if i % 40 == 0:
- # Test that if we move the shoulder, the shooter stays perfect.
- #observer.X_hat[0, 0] += 0.20
- #arm.X[0, 0] += 0.20
- pass
- U_error = numpy.matrix([[2.0], [2.0]])
- # Kick it and see what happens.
- #if (initial_t + i * arm.dt) % 0.4 > 0.2:
- #U_error = numpy.matrix([[4.0], [0.0]])
- #else:
- #U_error = numpy.matrix([[-4.0], [0.0]])
+ if self.v_shooter:
+ last_v_shooter = self.v_shooter[-1]
+ else:
+ last_v_shooter = 0
+ self.v_shooter.append(arm.X[3, 0])
+ self.a_shooter.append(
+ (self.v_shooter[-1] - last_v_shooter) / arm.dt)
- arm.Update(U + U_error)
+ if i % 40 == 0:
+ # Test that if we move the shoulder, the shooter stays perfect.
+ #observer.X_hat[0, 0] += 0.20
+ #arm.X[0, 0] += 0.20
+ pass
+ U_error = numpy.matrix([[2.0], [2.0]])
+ # Kick it and see what happens.
+ #if (initial_t + i * arm.dt) % 0.4 > 0.2:
+ #U_error = numpy.matrix([[4.0], [0.0]])
+ #else:
+ #U_error = numpy.matrix([[-4.0], [0.0]])
- if observer is not None:
- observer.PredictObserver(U)
+ arm.Update(U + U_error)
- self.t.append(initial_t + i * arm.dt)
- self.u_shoulder.append(U[0, 0])
- self.u_shooter.append(U[1, 0])
+ if observer is not None:
+ observer.PredictObserver(U)
- ff_U -= U_uncapped - U
- goal = controller.A * goal + controller.B * ff_U
+ self.t.append(initial_t + i * arm.dt)
+ self.u_shoulder.append(U[0, 0])
+ self.u_shooter.append(U[1, 0])
- if U[0, 0] != U_uncapped[0, 0]:
- glog.debug('Moving shoulder %s', repr(initial_t + i * arm.dt))
- glog.debug('U error %s', repr(U_uncapped - U))
- glog.debug('goal change is %s',
- repr(next_shoulder_goal -
- numpy.matrix([[goal[0, 0]], [goal[1, 0]]])))
- shoulder_profile.MoveCurrentState(
- numpy.matrix([[goal[0, 0]], [goal[1, 0]]]))
- if U[1, 0] != U_uncapped[1, 0]:
- glog.debug('Moving shooter %s', repr(initial_t + i * arm.dt))
- glog.debug('U error %s', repr(U_uncapped - U))
- shooter_profile.MoveCurrentState(
- numpy.matrix([[goal[2, 0]], [goal[3, 0]]]))
- U_last = U
- glog.debug('goal_error %s', repr(end_goal - goal))
- glog.debug('error %s', repr(observer.X_hat - end_goal))
+ ff_U -= U_uncapped - U
+ goal = controller.A * goal + controller.B * ff_U
+ if U[0, 0] != U_uncapped[0, 0]:
+ glog.debug('Moving shoulder %s', repr(initial_t + i * arm.dt))
+ glog.debug('U error %s', repr(U_uncapped - U))
+ glog.debug(
+ 'goal change is %s',
+ repr(next_shoulder_goal -
+ numpy.matrix([[goal[0, 0]], [goal[1, 0]]])))
+ shoulder_profile.MoveCurrentState(
+ numpy.matrix([[goal[0, 0]], [goal[1, 0]]]))
+ if U[1, 0] != U_uncapped[1, 0]:
+ glog.debug('Moving shooter %s', repr(initial_t + i * arm.dt))
+ glog.debug('U error %s', repr(U_uncapped - U))
+ shooter_profile.MoveCurrentState(
+ numpy.matrix([[goal[2, 0]], [goal[3, 0]]]))
+ U_last = U
+ glog.debug('goal_error %s', repr(end_goal - goal))
+ glog.debug('error %s', repr(observer.X_hat - end_goal))
- def Plot(self):
- pylab.subplot(3, 1, 1)
- pylab.plot(self.t, self.x_shoulder, label='x shoulder')
- pylab.plot(self.t, self.goal_x_shoulder, label='goal x shoulder')
- pylab.plot(self.t, self.x_hat_shoulder, label='x_hat shoulder')
+ def Plot(self):
+ pylab.subplot(3, 1, 1)
+ pylab.plot(self.t, self.x_shoulder, label='x shoulder')
+ pylab.plot(self.t, self.goal_x_shoulder, label='goal x shoulder')
+ pylab.plot(self.t, self.x_hat_shoulder, label='x_hat shoulder')
- pylab.plot(self.t, self.x_shooter, label='x shooter')
- pylab.plot(self.t, self.x_hat_shooter, label='x_hat shooter')
- pylab.plot(self.t, self.goal_x_shooter, label='goal x shooter')
- pylab.plot(self.t, map(operator.add, self.x_shooter, self.x_shoulder),
- label='x shooter ground')
- pylab.plot(self.t, map(operator.add, self.x_hat_shooter, self.x_hat_shoulder),
- label='x_hat shooter ground')
- pylab.legend()
+ pylab.plot(self.t, self.x_shooter, label='x shooter')
+ pylab.plot(self.t, self.x_hat_shooter, label='x_hat shooter')
+ pylab.plot(self.t, self.goal_x_shooter, label='goal x shooter')
+ pylab.plot(
+ self.t,
+ map(operator.add, self.x_shooter, self.x_shoulder),
+ label='x shooter ground')
+ pylab.plot(
+ self.t,
+ map(operator.add, self.x_hat_shooter, self.x_hat_shoulder),
+ label='x_hat shooter ground')
+ pylab.legend()
- pylab.subplot(3, 1, 2)
- pylab.plot(self.t, self.u_shoulder, label='u shoulder')
- pylab.plot(self.t, self.offset_shoulder, label='voltage_offset shoulder')
- pylab.plot(self.t, self.u_shooter, label='u shooter')
- pylab.plot(self.t, self.offset_shooter, label='voltage_offset shooter')
- pylab.legend()
+ pylab.subplot(3, 1, 2)
+ pylab.plot(self.t, self.u_shoulder, label='u shoulder')
+ pylab.plot(
+ self.t, self.offset_shoulder, label='voltage_offset shoulder')
+ pylab.plot(self.t, self.u_shooter, label='u shooter')
+ pylab.plot(self.t, self.offset_shooter, label='voltage_offset shooter')
+ pylab.legend()
- pylab.subplot(3, 1, 3)
- pylab.plot(self.t, self.a_shoulder, label='a_shoulder')
- pylab.plot(self.t, self.a_shooter, label='a_shooter')
- pylab.legend()
+ pylab.subplot(3, 1, 3)
+ pylab.plot(self.t, self.a_shoulder, label='a_shoulder')
+ pylab.plot(self.t, self.a_shooter, label='a_shooter')
+ pylab.legend()
- pylab.show()
+ pylab.show()
def main(argv):
- argv = FLAGS(argv)
- glog.init()
+ argv = FLAGS(argv)
+ glog.init()
- scenario_plotter = ScenarioPlotter()
+ scenario_plotter = ScenarioPlotter()
- J_accelerating = 18
- J_decelerating = 7
+ J_accelerating = 18
+ J_decelerating = 7
- arm = Arm(name='AcceleratingArm', J=J_accelerating)
- arm_integral_controller = IntegralArm(
- name='AcceleratingIntegralArm', J=J_accelerating)
- arm_observer = IntegralArm()
+ arm = Arm(name='AcceleratingArm', J=J_accelerating)
+ arm_integral_controller = IntegralArm(
+ name='AcceleratingIntegralArm', J=J_accelerating)
+ arm_observer = IntegralArm()
- # Test moving the shoulder with constant separation.
- initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0]])
- R = numpy.matrix([[numpy.pi / 2.0],
- [0.0],
- [0.0], #[numpy.pi / 2.0],
- [0.0],
- [0.0],
- [0.0]])
- arm.X = initial_X[0:4, 0]
- arm_observer.X = initial_X
+ # Test moving the shoulder with constant separation.
+ initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0]])
+ R = numpy.matrix([
+ [numpy.pi / 2.0],
+ [0.0],
+ [0.0], #[numpy.pi / 2.0],
+ [0.0],
+ [0.0],
+ [0.0]
+ ])
+ arm.X = initial_X[0:4, 0]
+ arm_observer.X = initial_X
- scenario_plotter.run_test(arm=arm,
- end_goal=R,
- iterations=300,
- controller=arm_integral_controller,
- observer=arm_observer)
+ scenario_plotter.run_test(
+ arm=arm,
+ end_goal=R,
+ iterations=300,
+ controller=arm_integral_controller,
+ observer=arm_observer)
- if len(argv) != 5:
- glog.fatal('Expected .h file name and .cc file name for the wrist and integral wrist.')
- else:
- namespaces = ['y2016', 'control_loops', 'superstructure']
- decelerating_arm = Arm(name='DeceleratingArm', J=J_decelerating)
- loop_writer = control_loop.ControlLoopWriter(
- 'Arm', [arm, decelerating_arm], namespaces=namespaces)
- loop_writer.Write(argv[1], argv[2])
+ if len(argv) != 5:
+ glog.fatal(
+ 'Expected .h file name and .cc file name for the wrist and integral wrist.'
+ )
+ else:
+ namespaces = ['y2016', 'control_loops', 'superstructure']
+ decelerating_arm = Arm(name='DeceleratingArm', J=J_decelerating)
+ loop_writer = control_loop.ControlLoopWriter(
+ 'Arm', [arm, decelerating_arm], namespaces=namespaces)
+ loop_writer.Write(argv[1], argv[2])
- decelerating_integral_arm_controller = IntegralArm(
- name='DeceleratingIntegralArm', J=J_decelerating)
+ decelerating_integral_arm_controller = IntegralArm(
+ name='DeceleratingIntegralArm', J=J_decelerating)
- integral_loop_writer = control_loop.ControlLoopWriter(
- 'IntegralArm',
- [arm_integral_controller, decelerating_integral_arm_controller],
- namespaces=namespaces)
- integral_loop_writer.AddConstant(control_loop.Constant("kV_shoulder", "%f",
- arm_integral_controller.shoulder_Kv))
- integral_loop_writer.Write(argv[3], argv[4])
+ integral_loop_writer = control_loop.ControlLoopWriter(
+ 'IntegralArm',
+ [arm_integral_controller, decelerating_integral_arm_controller],
+ namespaces=namespaces)
+ integral_loop_writer.AddConstant(
+ control_loop.Constant("kV_shoulder", "%f",
+ arm_integral_controller.shoulder_Kv))
+ integral_loop_writer.Write(argv[3], argv[4])
- if FLAGS.plot:
- scenario_plotter.Plot()
+ if FLAGS.plot:
+ scenario_plotter.Plot()
+
if __name__ == '__main__':
- sys.exit(main(sys.argv))
+ sys.exit(main(sys.argv))
diff --git a/y2016/control_loops/python/shooter.py b/y2016/control_loops/python/shooter.py
index 53793d0..b3c988c 100755
--- a/y2016/control_loops/python/shooter.py
+++ b/y2016/control_loops/python/shooter.py
@@ -13,262 +13,274 @@
gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
+
class VelocityShooter(control_loop.ControlLoop):
- def __init__(self, name='VelocityShooter'):
- super(VelocityShooter, self).__init__(name)
- # Stall Torque in N m
- self.stall_torque = 0.71
- # Stall Current in Amps
- self.stall_current = 134
- # Free Speed in RPM
- self.free_speed = 18730.0
- # Free Current in Amps
- self.free_current = 0.7
- # Moment of inertia of the shooter wheel in kg m^2
- self.J = 0.00032
- # Resistance of the motor, divided by 2 to account for the 2 motors
- self.R = 12.0 / self.stall_current
- # Motor velocity constant
- self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
- (12.0 - self.R * self.free_current))
- # Torque constant
- self.Kt = self.stall_torque / self.stall_current
- # Gear ratio
- self.G = 12.0 / 18.0
- # Control loop time step
- self.dt = 0.005
- # State feedback matrices
- # [angular velocity]
- self.A_continuous = numpy.matrix(
- [[-self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
- self.B_continuous = numpy.matrix(
- [[self.Kt / (self.J * self.G * self.R)]])
- self.C = numpy.matrix([[1]])
- self.D = numpy.matrix([[0]])
+ def __init__(self, name='VelocityShooter'):
+ super(VelocityShooter, self).__init__(name)
+ # Stall Torque in N m
+ self.stall_torque = 0.71
+ # Stall Current in Amps
+ self.stall_current = 134
+ # Free Speed in RPM
+ self.free_speed = 18730.0
+ # Free Current in Amps
+ self.free_current = 0.7
+ # Moment of inertia of the shooter wheel in kg m^2
+ self.J = 0.00032
+ # Resistance of the motor, divided by 2 to account for the 2 motors
+ self.R = 12.0 / self.stall_current
+ # Motor velocity constant
+ self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
+ (12.0 - self.R * self.free_current))
+ # Torque constant
+ self.Kt = self.stall_torque / self.stall_current
+ # Gear ratio
+ self.G = 12.0 / 18.0
+ # Control loop time step
+ self.dt = 0.005
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ # State feedback matrices
+ # [angular velocity]
+ self.A_continuous = numpy.matrix(
+ [[-self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
+ self.B_continuous = numpy.matrix(
+ [[self.Kt / (self.J * self.G * self.R)]])
+ self.C = numpy.matrix([[1]])
+ self.D = numpy.matrix([[0]])
- self.PlaceControllerPoles([.87])
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- self.PlaceObserverPoles([0.3])
+ self.PlaceControllerPoles([.87])
- self.U_max = numpy.matrix([[12.0]])
- self.U_min = numpy.matrix([[-12.0]])
+ self.PlaceObserverPoles([0.3])
- qff_vel = 8.0
- self.Qff = numpy.matrix([[1.0 / (qff_vel ** 2.0)]])
+ self.U_max = numpy.matrix([[12.0]])
+ self.U_min = numpy.matrix([[-12.0]])
- self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
+ qff_vel = 8.0
+ self.Qff = numpy.matrix([[1.0 / (qff_vel**2.0)]])
+
+ self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
class Shooter(VelocityShooter):
- def __init__(self, name='Shooter'):
- super(Shooter, self).__init__(name)
- self.A_continuous_unaugmented = self.A_continuous
- self.B_continuous_unaugmented = self.B_continuous
+ def __init__(self, name='Shooter'):
+ super(Shooter, self).__init__(name)
- self.A_continuous = numpy.matrix(numpy.zeros((2, 2)))
- self.A_continuous[1:2, 1:2] = self.A_continuous_unaugmented
- self.A_continuous[0, 1] = 1
+ self.A_continuous_unaugmented = self.A_continuous
+ self.B_continuous_unaugmented = self.B_continuous
- self.B_continuous = numpy.matrix(numpy.zeros((2, 1)))
- self.B_continuous[1:2, 0] = self.B_continuous_unaugmented
+ self.A_continuous = numpy.matrix(numpy.zeros((2, 2)))
+ self.A_continuous[1:2, 1:2] = self.A_continuous_unaugmented
+ self.A_continuous[0, 1] = 1
- # State feedback matrices
- # [position, angular velocity]
- self.C = numpy.matrix([[1, 0]])
- self.D = numpy.matrix([[0]])
+ self.B_continuous = numpy.matrix(numpy.zeros((2, 1)))
+ self.B_continuous[1:2, 0] = self.B_continuous_unaugmented
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ # State feedback matrices
+ # [position, angular velocity]
+ self.C = numpy.matrix([[1, 0]])
+ self.D = numpy.matrix([[0]])
- self.rpl = .45
- self.ipl = 0.07
- self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
- self.rpl - 1j * self.ipl])
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- self.K_unaugmented = self.K
- self.K = numpy.matrix(numpy.zeros((1, 2)))
- self.K[0, 1:2] = self.K_unaugmented
- self.Kff_unaugmented = self.Kff
- self.Kff = numpy.matrix(numpy.zeros((1, 2)))
- self.Kff[0, 1:2] = self.Kff_unaugmented
+ self.rpl = .45
+ self.ipl = 0.07
+ self.PlaceObserverPoles(
+ [self.rpl + 1j * self.ipl, self.rpl - 1j * self.ipl])
- self.InitializeState()
+ self.K_unaugmented = self.K
+ self.K = numpy.matrix(numpy.zeros((1, 2)))
+ self.K[0, 1:2] = self.K_unaugmented
+ self.Kff_unaugmented = self.Kff
+ self.Kff = numpy.matrix(numpy.zeros((1, 2)))
+ self.Kff[0, 1:2] = self.Kff_unaugmented
+
+ self.InitializeState()
class IntegralShooter(Shooter):
- def __init__(self, name="IntegralShooter"):
- super(IntegralShooter, self).__init__(name=name)
- self.A_continuous_unaugmented = self.A_continuous
- self.B_continuous_unaugmented = self.B_continuous
+ def __init__(self, name="IntegralShooter"):
+ super(IntegralShooter, self).__init__(name=name)
- self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
- self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented
- self.A_continuous[0:2, 2] = self.B_continuous_unaugmented
+ self.A_continuous_unaugmented = self.A_continuous
+ self.B_continuous_unaugmented = self.B_continuous
- self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
- self.B_continuous[0:2, 0] = self.B_continuous_unaugmented
+ self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
+ self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented
+ self.A_continuous[0:2, 2] = self.B_continuous_unaugmented
- self.C_unaugmented = self.C
- self.C = numpy.matrix(numpy.zeros((1, 3)))
- self.C[0:1, 0:2] = self.C_unaugmented
+ self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
+ self.B_continuous[0:2, 0] = self.B_continuous_unaugmented
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ self.C_unaugmented = self.C
+ self.C = numpy.matrix(numpy.zeros((1, 3)))
+ self.C[0:1, 0:2] = self.C_unaugmented
- q_pos = 0.08
- q_vel = 4.00
- q_voltage = 0.3
- self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0],
- [0.0, (q_vel ** 2.0), 0.0],
- [0.0, 0.0, (q_voltage ** 2.0)]])
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- r_pos = 0.05
- self.R = numpy.matrix([[(r_pos ** 2.0)]])
+ q_pos = 0.08
+ q_vel = 4.00
+ q_voltage = 0.3
+ self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0],
+ [0.0, (q_vel**2.0), 0.0],
+ [0.0, 0.0, (q_voltage**2.0)]])
- self.KalmanGain, self.Q_steady = controls.kalman(
- A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
- self.L = self.A * self.KalmanGain
+ r_pos = 0.05
+ self.R = numpy.matrix([[(r_pos**2.0)]])
- self.K_unaugmented = self.K
- self.K = numpy.matrix(numpy.zeros((1, 3)))
- self.K[0, 0:2] = self.K_unaugmented
- self.K[0, 2] = 1
- self.Kff_unaugmented = self.Kff
- self.Kff = numpy.matrix(numpy.zeros((1, 3)))
- self.Kff[0, 0:2] = self.Kff_unaugmented
+ self.KalmanGain, self.Q_steady = controls.kalman(
+ A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
+ self.L = self.A * self.KalmanGain
- self.InitializeState()
+ self.K_unaugmented = self.K
+ self.K = numpy.matrix(numpy.zeros((1, 3)))
+ self.K[0, 0:2] = self.K_unaugmented
+ self.K[0, 2] = 1
+ self.Kff_unaugmented = self.Kff
+ self.Kff = numpy.matrix(numpy.zeros((1, 3)))
+ self.Kff[0, 0:2] = self.Kff_unaugmented
+
+ self.InitializeState()
class ScenarioPlotter(object):
- def __init__(self):
- # Various lists for graphing things.
- self.t = []
- self.x = []
- self.v = []
- self.a = []
- self.x_hat = []
- self.u = []
- self.offset = []
- def run_test(self, shooter, goal, iterations=200, controller_shooter=None,
- observer_shooter=None):
- """Runs the shooter plant with an initial condition and goal.
+ def __init__(self):
+ # Various lists for graphing things.
+ self.t = []
+ self.x = []
+ self.v = []
+ self.a = []
+ self.x_hat = []
+ self.u = []
+ self.offset = []
- Args:
- shooter: Shooter object to use.
- goal: goal state.
- iterations: Number of timesteps to run the model for.
- controller_shooter: Shooter object to get K from, or None if we should
- use shooter.
- observer_shooter: Shooter object to use for the observer, or None if we
- should use the actual state.
- """
+ def run_test(self,
+ shooter,
+ goal,
+ iterations=200,
+ controller_shooter=None,
+ observer_shooter=None):
+ """Runs the shooter plant with an initial condition and goal.
- if controller_shooter is None:
- controller_shooter = shooter
+ Args:
+ shooter: Shooter object to use.
+ goal: goal state.
+ iterations: Number of timesteps to run the model for.
+ controller_shooter: Shooter object to get K from, or None if we should
+ use shooter.
+ observer_shooter: Shooter object to use for the observer, or None if we
+ should use the actual state.
+ """
- vbat = 12.0
+ if controller_shooter is None:
+ controller_shooter = shooter
- if self.t:
- initial_t = self.t[-1] + shooter.dt
- else:
- initial_t = 0
+ vbat = 12.0
- for i in xrange(iterations):
- X_hat = shooter.X
+ if self.t:
+ initial_t = self.t[-1] + shooter.dt
+ else:
+ initial_t = 0
- if observer_shooter is not None:
- X_hat = observer_shooter.X_hat
- self.x_hat.append(observer_shooter.X_hat[1, 0])
+ for i in xrange(iterations):
+ X_hat = shooter.X
- ff_U = controller_shooter.Kff * (goal - observer_shooter.A * goal)
+ if observer_shooter is not None:
+ X_hat = observer_shooter.X_hat
+ self.x_hat.append(observer_shooter.X_hat[1, 0])
- U = controller_shooter.K * (goal - X_hat) + ff_U
- U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
- self.x.append(shooter.X[0, 0])
+ ff_U = controller_shooter.Kff * (goal - observer_shooter.A * goal)
+ U = controller_shooter.K * (goal - X_hat) + ff_U
+ U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
+ self.x.append(shooter.X[0, 0])
- if self.v:
- last_v = self.v[-1]
- else:
- last_v = 0
+ if self.v:
+ last_v = self.v[-1]
+ else:
+ last_v = 0
- self.v.append(shooter.X[1, 0])
- self.a.append((self.v[-1] - last_v) / shooter.dt)
+ self.v.append(shooter.X[1, 0])
+ self.a.append((self.v[-1] - last_v) / shooter.dt)
- if observer_shooter is not None:
- observer_shooter.Y = shooter.Y
- observer_shooter.CorrectObserver(U)
- self.offset.append(observer_shooter.X_hat[2, 0])
+ if observer_shooter is not None:
+ observer_shooter.Y = shooter.Y
+ observer_shooter.CorrectObserver(U)
+ self.offset.append(observer_shooter.X_hat[2, 0])
- applied_U = U.copy()
- if i > 30:
- applied_U += 2
- shooter.Update(applied_U)
+ applied_U = U.copy()
+ if i > 30:
+ applied_U += 2
+ shooter.Update(applied_U)
- if observer_shooter is not None:
- observer_shooter.PredictObserver(U)
+ if observer_shooter is not None:
+ observer_shooter.PredictObserver(U)
- self.t.append(initial_t + i * shooter.dt)
- self.u.append(U[0, 0])
+ self.t.append(initial_t + i * shooter.dt)
+ self.u.append(U[0, 0])
- glog.debug('Time: %f', self.t[-1])
+ glog.debug('Time: %f', self.t[-1])
- def Plot(self):
- pylab.subplot(3, 1, 1)
- pylab.plot(self.t, self.v, label='x')
- pylab.plot(self.t, self.x_hat, label='x_hat')
- pylab.legend()
+ def Plot(self):
+ pylab.subplot(3, 1, 1)
+ pylab.plot(self.t, self.v, label='x')
+ pylab.plot(self.t, self.x_hat, label='x_hat')
+ pylab.legend()
- pylab.subplot(3, 1, 2)
- pylab.plot(self.t, self.u, label='u')
- pylab.plot(self.t, self.offset, label='voltage_offset')
- pylab.legend()
+ pylab.subplot(3, 1, 2)
+ pylab.plot(self.t, self.u, label='u')
+ pylab.plot(self.t, self.offset, label='voltage_offset')
+ pylab.legend()
- pylab.subplot(3, 1, 3)
- pylab.plot(self.t, self.a, label='a')
- pylab.legend()
+ pylab.subplot(3, 1, 3)
+ pylab.plot(self.t, self.a, label='a')
+ pylab.legend()
- pylab.show()
+ pylab.show()
def main(argv):
- scenario_plotter = ScenarioPlotter()
+ scenario_plotter = ScenarioPlotter()
- shooter = Shooter()
- shooter_controller = IntegralShooter()
- observer_shooter = IntegralShooter()
+ shooter = Shooter()
+ shooter_controller = IntegralShooter()
+ observer_shooter = IntegralShooter()
- initial_X = numpy.matrix([[0.0], [0.0]])
- R = numpy.matrix([[0.0], [100.0], [0.0]])
- scenario_plotter.run_test(shooter, goal=R, controller_shooter=shooter_controller,
- observer_shooter=observer_shooter, iterations=200)
+ initial_X = numpy.matrix([[0.0], [0.0]])
+ R = numpy.matrix([[0.0], [100.0], [0.0]])
+ scenario_plotter.run_test(
+ shooter,
+ goal=R,
+ controller_shooter=shooter_controller,
+ observer_shooter=observer_shooter,
+ iterations=200)
- if FLAGS.plot:
- scenario_plotter.Plot()
+ if FLAGS.plot:
+ scenario_plotter.Plot()
- if len(argv) != 5:
- glog.fatal('Expected .h file name and .cc file name')
- else:
- namespaces = ['y2016', 'control_loops', 'shooter']
- shooter = Shooter('Shooter')
- loop_writer = control_loop.ControlLoopWriter('Shooter', [shooter],
- namespaces=namespaces)
- loop_writer.Write(argv[1], argv[2])
+ if len(argv) != 5:
+ glog.fatal('Expected .h file name and .cc file name')
+ else:
+ namespaces = ['y2016', 'control_loops', 'shooter']
+ shooter = Shooter('Shooter')
+ loop_writer = control_loop.ControlLoopWriter(
+ 'Shooter', [shooter], namespaces=namespaces)
+ loop_writer.Write(argv[1], argv[2])
- integral_shooter = IntegralShooter('IntegralShooter')
- integral_loop_writer = control_loop.ControlLoopWriter(
- 'IntegralShooter', [integral_shooter], namespaces=namespaces)
- integral_loop_writer.Write(argv[3], argv[4])
+ integral_shooter = IntegralShooter('IntegralShooter')
+ integral_loop_writer = control_loop.ControlLoopWriter(
+ 'IntegralShooter', [integral_shooter], namespaces=namespaces)
+ integral_loop_writer.Write(argv[3], argv[4])
if __name__ == '__main__':
- argv = FLAGS(sys.argv)
- sys.exit(main(argv))
+ argv = FLAGS(sys.argv)
+ sys.exit(main(argv))