Reformat python and c++ files
Change-Id: I7d7d99a2094c2a9181ed882735b55159c14db3b0
diff --git a/y2017/control_loops/python/column.py b/y2017/control_loops/python/column.py
index 1f8bd76..70cd649 100755
--- a/y2017/control_loops/python/column.py
+++ b/y2017/control_loops/python/column.py
@@ -14,383 +14,413 @@
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 ColumnController(control_loop.ControlLoop):
- def __init__(self, name='Column'):
- super(ColumnController, self).__init__(name)
- self.turret = turret.Turret(name + 'Turret')
- self.indexer = indexer.Indexer(name + 'Indexer')
- # Control loop time step
- self.dt = 0.005
+ def __init__(self, name='Column'):
+ super(ColumnController, self).__init__(name)
+ self.turret = turret.Turret(name + 'Turret')
+ self.indexer = indexer.Indexer(name + 'Indexer')
- # State is [position_indexer,
- # velocity_indexer,
- # position_shooter,
- # velocity_shooter]
- # Input is [volts_indexer, volts_shooter]
- self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
- self.B_continuous = numpy.matrix(numpy.zeros((3, 2)))
+ # Control loop time step
+ self.dt = 0.005
- self.A_continuous[0, 0] = -(self.indexer.Kt / self.indexer.Kv / (self.indexer.J * self.indexer.resistance * self.indexer.G * self.indexer.G) +
- self.turret.Kt / self.turret.Kv / (self.indexer.J * self.turret.resistance * self.turret.G * self.turret.G))
- self.A_continuous[0, 2] = self.turret.Kt / self.turret.Kv / (self.indexer.J * self.turret.resistance * self.turret.G * self.turret.G)
- self.B_continuous[0, 0] = self.indexer.Kt / (self.indexer.J * self.indexer.resistance * self.indexer.G)
- self.B_continuous[0, 1] = -self.turret.Kt / (self.indexer.J * self.turret.resistance * self.turret.G)
+ # State is [position_indexer,
+ # velocity_indexer,
+ # position_shooter,
+ # velocity_shooter]
+ # Input is [volts_indexer, volts_shooter]
+ self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
+ self.B_continuous = numpy.matrix(numpy.zeros((3, 2)))
- self.A_continuous[1, 2] = 1
+ self.A_continuous[0, 0] = -(
+ self.indexer.Kt / self.indexer.Kv /
+ (self.indexer.J * self.indexer.resistance * self.indexer.G *
+ self.indexer.G) + self.turret.Kt / self.turret.Kv /
+ (self.indexer.J * self.turret.resistance * self.turret.G *
+ self.turret.G))
+ self.A_continuous[0, 2] = self.turret.Kt / self.turret.Kv / (
+ self.indexer.J * self.turret.resistance * self.turret.G *
+ self.turret.G)
+ self.B_continuous[0, 0] = self.indexer.Kt / (
+ self.indexer.J * self.indexer.resistance * self.indexer.G)
+ self.B_continuous[0, 1] = -self.turret.Kt / (
+ self.indexer.J * self.turret.resistance * self.turret.G)
- self.A_continuous[2, 0] = self.turret.Kt / self.turret.Kv / (self.turret.J * self.turret.resistance * self.turret.G * self.turret.G)
- self.A_continuous[2, 2] = -self.turret.Kt / self.turret.Kv / (self.turret.J * self.turret.resistance * self.turret.G * self.turret.G)
+ self.A_continuous[1, 2] = 1
- self.B_continuous[2, 1] = self.turret.Kt / (self.turret.J * self.turret.resistance * self.turret.G)
+ self.A_continuous[2, 0] = self.turret.Kt / self.turret.Kv / (
+ self.turret.J * self.turret.resistance * self.turret.G *
+ self.turret.G)
+ self.A_continuous[2, 2] = -self.turret.Kt / self.turret.Kv / (
+ self.turret.J * self.turret.resistance * self.turret.G *
+ self.turret.G)
- self.C = numpy.matrix([[1, 0, 0], [0, 1, 0]])
- self.D = numpy.matrix([[0, 0], [0, 0]])
+ self.B_continuous[2, 1] = self.turret.Kt / (
+ self.turret.J * self.turret.resistance * self.turret.G)
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ self.C = numpy.matrix([[1, 0, 0], [0, 1, 0]])
+ self.D = numpy.matrix([[0, 0], [0, 0]])
- q_indexer_vel = 13.0
- q_pos = 0.05
- q_vel = 0.8
- self.Q = numpy.matrix([[(1.0 / (q_indexer_vel ** 2.0)), 0.0, 0.0],
- [0.0, (1.0 / (q_pos ** 2.0)), 0.0],
- [0.0, 0.0, (1.0 / (q_vel ** 2.0))]])
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
- [0.0, (1.0 / (12.0 ** 2.0))]])
- self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
+ q_indexer_vel = 13.0
+ q_pos = 0.05
+ q_vel = 0.8
+ self.Q = numpy.matrix([[(1.0 / (q_indexer_vel**2.0)), 0.0, 0.0],
+ [0.0, (1.0 / (q_pos**2.0)), 0.0],
+ [0.0, 0.0, (1.0 / (q_vel**2.0))]])
- glog.debug('Controller poles are ' + repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
+ self.R = numpy.matrix([[(1.0 / (12.0**2.0)), 0.0],
+ [0.0, (1.0 / (12.0**2.0))]])
+ self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
- q_vel_indexer_ff = 0.000005
- q_pos_ff = 0.0000005
- q_vel_ff = 0.00008
- self.Qff = numpy.matrix([[(1.0 / (q_vel_indexer_ff ** 2.0)), 0.0, 0.0],
- [0.0, (1.0 / (q_pos_ff ** 2.0)), 0.0],
- [0.0, 0.0, (1.0 / (q_vel_ff ** 2.0))]])
+ glog.debug('Controller poles are ' +
+ repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
- self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
+ q_vel_indexer_ff = 0.000005
+ q_pos_ff = 0.0000005
+ q_vel_ff = 0.00008
+ self.Qff = numpy.matrix([[(1.0 / (q_vel_indexer_ff**2.0)), 0.0, 0.0],
+ [0.0, (1.0 / (q_pos_ff**2.0)), 0.0],
+ [0.0, 0.0, (1.0 / (q_vel_ff**2.0))]])
- self.U_max = numpy.matrix([[12.0], [12.0]])
- self.U_min = numpy.matrix([[-12.0], [-12.0]])
+ self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
- self.InitializeState()
+ self.U_max = numpy.matrix([[12.0], [12.0]])
+ self.U_min = numpy.matrix([[-12.0], [-12.0]])
+
+ self.InitializeState()
class Column(ColumnController):
- def __init__(self, name='Column', disable_indexer=False):
- super(Column, self).__init__(name)
- A_continuous = numpy.matrix(numpy.zeros((4, 4)))
- B_continuous = numpy.matrix(numpy.zeros((4, 2)))
- A_continuous[0, 1] = 1
- A_continuous[1:, 1:] = self.A_continuous
- B_continuous[1:, :] = self.B_continuous
+ def __init__(self, name='Column', disable_indexer=False):
+ super(Column, self).__init__(name)
+ A_continuous = numpy.matrix(numpy.zeros((4, 4)))
+ B_continuous = numpy.matrix(numpy.zeros((4, 2)))
- self.A_continuous = A_continuous
- self.B_continuous = B_continuous
+ A_continuous[0, 1] = 1
+ A_continuous[1:, 1:] = self.A_continuous
+ B_continuous[1:, :] = self.B_continuous
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ self.A_continuous = A_continuous
+ self.B_continuous = B_continuous
- self.C = numpy.matrix([[1, 0, 0, 0], [-1, 0, 1, 0]])
- self.D = numpy.matrix([[0, 0], [0, 0]])
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- orig_K = self.K
- self.K = numpy.matrix(numpy.zeros((2, 4)))
- self.K[:, 1:] = orig_K
+ self.C = numpy.matrix([[1, 0, 0, 0], [-1, 0, 1, 0]])
+ self.D = numpy.matrix([[0, 0], [0, 0]])
- glog.debug('K is ' + repr(self.K))
- # TODO(austin): Do we want to damp velocity out or not when disabled?
- #if disable_indexer:
- # self.K[0, 1] = 0.0
- # self.K[1, 1] = 0.0
+ orig_K = self.K
+ self.K = numpy.matrix(numpy.zeros((2, 4)))
+ self.K[:, 1:] = orig_K
- orig_Kff = self.Kff
- self.Kff = numpy.matrix(numpy.zeros((2, 4)))
- self.Kff[:, 1:] = orig_Kff
+ glog.debug('K is ' + repr(self.K))
+ # TODO(austin): Do we want to damp velocity out or not when disabled?
+ #if disable_indexer:
+ # self.K[0, 1] = 0.0
+ # self.K[1, 1] = 0.0
- q_pos = 0.12
- q_vel = 2.00
- self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0],
- [0.0, (q_vel ** 2.0), 0.0, 0.0],
- [0.0, 0.0, (q_pos ** 2.0), 0.0],
- [0.0, 0.0, 0.0, (q_vel ** 2.0)]])
+ orig_Kff = self.Kff
+ self.Kff = numpy.matrix(numpy.zeros((2, 4)))
+ self.Kff[:, 1:] = orig_Kff
- r_pos = 0.05
- self.R = numpy.matrix([[(r_pos ** 2.0), 0.0],
- [0.0, (r_pos ** 2.0)]])
+ q_pos = 0.12
+ q_vel = 2.00
+ self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0, 0.0],
+ [0.0, (q_vel**2.0), 0.0, 0.0],
+ [0.0, 0.0, (q_pos**2.0), 0.0],
+ [0.0, 0.0, 0.0, (q_vel**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), 0.0], [0.0, (r_pos**2.0)]])
- self.InitializeState()
+ 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()
class IntegralColumn(Column):
- def __init__(self, name='IntegralColumn', voltage_error_noise=None,
- disable_indexer=False):
- super(IntegralColumn, self).__init__(name)
- A_continuous = numpy.matrix(numpy.zeros((6, 6)))
- A_continuous[0:4, 0:4] = self.A_continuous
- A_continuous[0:4:, 4:6] = self.B_continuous
+ def __init__(self,
+ name='IntegralColumn',
+ voltage_error_noise=None,
+ disable_indexer=False):
+ super(IntegralColumn, self).__init__(name)
- B_continuous = numpy.matrix(numpy.zeros((6, 2)))
- B_continuous[0:4, :] = self.B_continuous
+ A_continuous = numpy.matrix(numpy.zeros((6, 6)))
+ A_continuous[0:4, 0:4] = self.A_continuous
+ A_continuous[0:4:, 4:6] = self.B_continuous
- self.A_continuous = A_continuous
- self.B_continuous = B_continuous
+ B_continuous = numpy.matrix(numpy.zeros((6, 2)))
+ B_continuous[0:4, :] = self.B_continuous
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ self.A_continuous = A_continuous
+ self.B_continuous = B_continuous
- C = numpy.matrix(numpy.zeros((2, 6)))
- C[0:2, 0:4] = self.C
- self.C = C
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- self.D = numpy.matrix([[0, 0], [0, 0]])
+ C = numpy.matrix(numpy.zeros((2, 6)))
+ C[0:2, 0:4] = self.C
+ self.C = C
- orig_K = self.K
- self.K = numpy.matrix(numpy.zeros((2, 6)))
- self.K[:, 0:4] = orig_K
+ self.D = numpy.matrix([[0, 0], [0, 0]])
- # TODO(austin): I'm not certain this is ideal. If someone spins the bottom
- # at a constant rate, we'll learn a voltage offset. That should translate
- # directly to a voltage on the turret to hold it steady. I'm also not
- # convinced we care that much. If the indexer is off, it'll stop rather
- # quickly anyways, so this is mostly a moot point.
- if not disable_indexer:
- self.K[0, 4] = 1
- self.K[1, 5] = 1
+ orig_K = self.K
+ self.K = numpy.matrix(numpy.zeros((2, 6)))
+ self.K[:, 0:4] = orig_K
- orig_Kff = self.Kff
- self.Kff = numpy.matrix(numpy.zeros((2, 6)))
- self.Kff[:, 0:4] = orig_Kff
+ # TODO(austin): I'm not certain this is ideal. If someone spins the bottom
+ # at a constant rate, we'll learn a voltage offset. That should translate
+ # directly to a voltage on the turret to hold it steady. I'm also not
+ # convinced we care that much. If the indexer is off, it'll stop rather
+ # quickly anyways, so this is mostly a moot point.
+ if not disable_indexer:
+ self.K[0, 4] = 1
+ self.K[1, 5] = 1
- q_pos = 0.40
- q_vel = 2.00
- q_voltage = 8.0
- if voltage_error_noise is not None:
- q_voltage = voltage_error_noise
+ orig_Kff = self.Kff
+ self.Kff = numpy.matrix(numpy.zeros((2, 6)))
+ self.Kff[:, 0:4] = orig_Kff
- self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
- [0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0, 0.0],
- [0.0, 0.0, (q_pos ** 2.0), 0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0, (q_vel ** 2.0), 0.0, 0.0],
- [0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0],
- [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0)]])
+ q_pos = 0.40
+ q_vel = 2.00
+ q_voltage = 8.0
+ if voltage_error_noise is not None:
+ q_voltage = voltage_error_noise
- r_pos = 0.05
- self.R = numpy.matrix([[(r_pos ** 2.0), 0.0],
- [0.0, (r_pos ** 2.0)]])
+ self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
+ [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0],
+ [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0],
+ [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0],
+ [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0],
+ [0.0, 0.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), 0.0], [0.0, (r_pos**2.0)]])
- self.InitializeState()
+ 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()
class ScenarioPlotter(object):
- def __init__(self):
- # Various lists for graphing things.
- self.t = []
- self.xi = []
- self.xt = []
- self.vi = []
- self.vt = []
- self.ai = []
- self.at = []
- self.x_hat = []
- self.ui = []
- self.ut = []
- self.ui_fb = []
- self.ut_fb = []
- self.offseti = []
- self.offsett = []
- self.turret_error = []
- def run_test(self, column, end_goal,
- controller_column,
- observer_column=None,
- iterations=200):
- """Runs the column plant with an initial condition and goal.
+ def __init__(self):
+ # Various lists for graphing things.
+ self.t = []
+ self.xi = []
+ self.xt = []
+ self.vi = []
+ self.vt = []
+ self.ai = []
+ self.at = []
+ self.x_hat = []
+ self.ui = []
+ self.ut = []
+ self.ui_fb = []
+ self.ut_fb = []
+ self.offseti = []
+ self.offsett = []
+ self.turret_error = []
- Args:
- column: column object to use.
- end_goal: end_goal state.
- controller_column: Intake object to get K from, or None if we should
- use column.
- observer_column: Intake object to use for the observer, or None if we should
- use the actual state.
- iterations: Number of timesteps to run the model for.
- """
+ def run_test(self,
+ column,
+ end_goal,
+ controller_column,
+ observer_column=None,
+ iterations=200):
+ """Runs the column plant with an initial condition and goal.
- if controller_column is None:
- controller_column = column
+ Args:
+ column: column object to use.
+ end_goal: end_goal state.
+ controller_column: Intake object to get K from, or None if we should
+ use column.
+ observer_column: Intake object to use for the observer, or None if we should
+ use the actual state.
+ iterations: Number of timesteps to run the model for.
+ """
- vbat = 12.0
+ if controller_column is None:
+ controller_column = column
- if self.t:
- initial_t = self.t[-1] + column.dt
- else:
- initial_t = 0
+ vbat = 12.0
- goal = numpy.concatenate((column.X, numpy.matrix(numpy.zeros((2, 1)))), axis=0)
+ if self.t:
+ initial_t = self.t[-1] + column.dt
+ else:
+ initial_t = 0
- profile = TrapezoidProfile(column.dt)
- profile.set_maximum_acceleration(10.0)
- profile.set_maximum_velocity(3.0)
- profile.SetGoal(goal[2, 0])
+ goal = numpy.concatenate((column.X, numpy.matrix(numpy.zeros((2, 1)))),
+ axis=0)
- U_last = numpy.matrix(numpy.zeros((2, 1)))
- for i in xrange(iterations):
- observer_column.Y = column.Y
- observer_column.CorrectObserver(U_last)
+ profile = TrapezoidProfile(column.dt)
+ profile.set_maximum_acceleration(10.0)
+ profile.set_maximum_velocity(3.0)
+ profile.SetGoal(goal[2, 0])
- self.offseti.append(observer_column.X_hat[4, 0])
- self.offsett.append(observer_column.X_hat[5, 0])
- self.x_hat.append(observer_column.X_hat[0, 0])
+ U_last = numpy.matrix(numpy.zeros((2, 1)))
+ for i in xrange(iterations):
+ observer_column.Y = column.Y
+ observer_column.CorrectObserver(U_last)
- next_goal = numpy.concatenate(
- (end_goal[0:2, :],
- profile.Update(end_goal[2, 0], end_goal[3, 0]),
- end_goal[4:6, :]),
- axis=0)
+ self.offseti.append(observer_column.X_hat[4, 0])
+ self.offsett.append(observer_column.X_hat[5, 0])
+ self.x_hat.append(observer_column.X_hat[0, 0])
- ff_U = controller_column.Kff * (next_goal - observer_column.A * goal)
- fb_U = controller_column.K * (goal - observer_column.X_hat)
- self.turret_error.append((goal[2, 0] - column.X[2, 0]) * 100.0)
- self.ui_fb.append(fb_U[0, 0])
- self.ut_fb.append(fb_U[1, 0])
+ next_goal = numpy.concatenate(
+ (end_goal[0:2, :], profile.Update(
+ end_goal[2, 0], end_goal[3, 0]), end_goal[4:6, :]),
+ axis=0)
- U_uncapped = ff_U + fb_U
+ ff_U = controller_column.Kff * (
+ next_goal - observer_column.A * goal)
+ fb_U = controller_column.K * (goal - observer_column.X_hat)
+ self.turret_error.append((goal[2, 0] - column.X[2, 0]) * 100.0)
+ self.ui_fb.append(fb_U[0, 0])
+ self.ut_fb.append(fb_U[1, 0])
- U = U_uncapped.copy()
- U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
- U[1, 0] = numpy.clip(U[1, 0], -vbat, vbat)
- self.xi.append(column.X[0, 0])
- self.xt.append(column.X[2, 0])
+ U_uncapped = ff_U + fb_U
- if self.vi:
- last_vi = self.vi[-1]
- else:
- last_vi = 0
- if self.vt:
- last_vt = self.vt[-1]
- else:
- last_vt = 0
+ U = U_uncapped.copy()
+ U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
+ U[1, 0] = numpy.clip(U[1, 0], -vbat, vbat)
+ self.xi.append(column.X[0, 0])
+ self.xt.append(column.X[2, 0])
- self.vi.append(column.X[1, 0])
- self.vt.append(column.X[3, 0])
- self.ai.append((self.vi[-1] - last_vi) / column.dt)
- self.at.append((self.vt[-1] - last_vt) / column.dt)
+ if self.vi:
+ last_vi = self.vi[-1]
+ else:
+ last_vi = 0
+ if self.vt:
+ last_vt = self.vt[-1]
+ else:
+ last_vt = 0
- offset = 0.0
- if i > 100:
- offset = 1.0
- column.Update(U + numpy.matrix([[0.0], [offset]]))
+ self.vi.append(column.X[1, 0])
+ self.vt.append(column.X[3, 0])
+ self.ai.append((self.vi[-1] - last_vi) / column.dt)
+ self.at.append((self.vt[-1] - last_vt) / column.dt)
- observer_column.PredictObserver(U)
+ offset = 0.0
+ if i > 100:
+ offset = 1.0
+ column.Update(U + numpy.matrix([[0.0], [offset]]))
- self.t.append(initial_t + i * column.dt)
- self.ui.append(U[0, 0])
- self.ut.append(U[1, 0])
+ observer_column.PredictObserver(U)
- ff_U -= U_uncapped - U
- goal = controller_column.A * goal + controller_column.B * ff_U
+ self.t.append(initial_t + i * column.dt)
+ self.ui.append(U[0, 0])
+ self.ut.append(U[1, 0])
- if U[1, 0] != U_uncapped[1, 0]:
- profile.MoveCurrentState(
- numpy.matrix([[goal[2, 0]], [goal[3, 0]]]))
+ ff_U -= U_uncapped - U
+ goal = controller_column.A * goal + controller_column.B * ff_U
- glog.debug('Time: %f', self.t[-1])
- glog.debug('goal_error %s', repr((end_goal - goal).T))
- glog.debug('error %s', repr((observer_column.X_hat - end_goal).T))
+ if U[1, 0] != U_uncapped[1, 0]:
+ profile.MoveCurrentState(
+ numpy.matrix([[goal[2, 0]], [goal[3, 0]]]))
- def Plot(self):
- pylab.subplot(3, 1, 1)
- pylab.plot(self.t, self.xi, label='x_indexer')
- pylab.plot(self.t, self.xt, label='x_turret')
- pylab.plot(self.t, self.x_hat, label='x_hat')
- pylab.plot(self.t, self.turret_error, label='turret_error * 100')
- pylab.legend()
+ glog.debug('Time: %f', self.t[-1])
+ glog.debug('goal_error %s', repr((end_goal - goal).T))
+ glog.debug('error %s', repr((observer_column.X_hat - end_goal).T))
- pylab.subplot(3, 1, 2)
- pylab.plot(self.t, self.ui, label='u_indexer')
- pylab.plot(self.t, self.ui_fb, label='u_indexer_fb')
- pylab.plot(self.t, self.ut, label='u_turret')
- pylab.plot(self.t, self.ut_fb, label='u_turret_fb')
- pylab.plot(self.t, self.offseti, label='voltage_offset_indexer')
- pylab.plot(self.t, self.offsett, label='voltage_offset_turret')
- pylab.legend()
+ def Plot(self):
+ pylab.subplot(3, 1, 1)
+ pylab.plot(self.t, self.xi, label='x_indexer')
+ pylab.plot(self.t, self.xt, label='x_turret')
+ pylab.plot(self.t, self.x_hat, label='x_hat')
+ pylab.plot(self.t, self.turret_error, label='turret_error * 100')
+ pylab.legend()
- pylab.subplot(3, 1, 3)
- pylab.plot(self.t, self.ai, label='a_indexer')
- pylab.plot(self.t, self.at, label='a_turret')
- pylab.plot(self.t, self.vi, label='v_indexer')
- pylab.plot(self.t, self.vt, label='v_turret')
- pylab.legend()
+ pylab.subplot(3, 1, 2)
+ pylab.plot(self.t, self.ui, label='u_indexer')
+ pylab.plot(self.t, self.ui_fb, label='u_indexer_fb')
+ pylab.plot(self.t, self.ut, label='u_turret')
+ pylab.plot(self.t, self.ut_fb, label='u_turret_fb')
+ pylab.plot(self.t, self.offseti, label='voltage_offset_indexer')
+ pylab.plot(self.t, self.offsett, label='voltage_offset_turret')
+ pylab.legend()
- pylab.show()
+ pylab.subplot(3, 1, 3)
+ pylab.plot(self.t, self.ai, label='a_indexer')
+ pylab.plot(self.t, self.at, label='a_turret')
+ pylab.plot(self.t, self.vi, label='v_indexer')
+ pylab.plot(self.t, self.vt, label='v_turret')
+ pylab.legend()
+
+ pylab.show()
def main(argv):
- scenario_plotter = ScenarioPlotter()
+ scenario_plotter = ScenarioPlotter()
- column = Column()
- column_controller = IntegralColumn()
- observer_column = IntegralColumn()
+ column = Column()
+ column_controller = IntegralColumn()
+ observer_column = IntegralColumn()
- initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
- R = numpy.matrix([[0.0], [10.0], [5.0], [0.0], [0.0], [0.0]])
- scenario_plotter.run_test(column, end_goal=R, controller_column=column_controller,
- observer_column=observer_column, iterations=400)
+ initial_X = numpy.matrix([[0.0], [0.0], [0.0], [0.0]])
+ R = numpy.matrix([[0.0], [10.0], [5.0], [0.0], [0.0], [0.0]])
+ scenario_plotter.run_test(
+ column,
+ end_goal=R,
+ controller_column=column_controller,
+ observer_column=observer_column,
+ iterations=400)
- if FLAGS.plot:
- scenario_plotter.Plot()
+ if FLAGS.plot:
+ scenario_plotter.Plot()
- if len(argv) != 7:
- glog.fatal('Expected .h file name and .cc file names')
- else:
- namespaces = ['y2017', 'control_loops', 'superstructure', 'column']
- column = Column('Column')
- loop_writer = control_loop.ControlLoopWriter('Column', [column],
- namespaces=namespaces)
- loop_writer.AddConstant(control_loop.Constant(
- 'kIndexerFreeSpeed', '%f', column.indexer.free_speed))
- loop_writer.AddConstant(control_loop.Constant(
- 'kIndexerOutputRatio', '%f', column.indexer.G))
- loop_writer.AddConstant(control_loop.Constant(
- 'kTurretFreeSpeed', '%f', column.turret.free_speed))
- loop_writer.AddConstant(control_loop.Constant(
- 'kTurretOutputRatio', '%f', column.turret.G))
- loop_writer.Write(argv[1], argv[2])
+ if len(argv) != 7:
+ glog.fatal('Expected .h file name and .cc file names')
+ else:
+ namespaces = ['y2017', 'control_loops', 'superstructure', 'column']
+ column = Column('Column')
+ loop_writer = control_loop.ControlLoopWriter(
+ 'Column', [column], namespaces=namespaces)
+ loop_writer.AddConstant(
+ control_loop.Constant('kIndexerFreeSpeed', '%f',
+ column.indexer.free_speed))
+ loop_writer.AddConstant(
+ control_loop.Constant('kIndexerOutputRatio', '%f',
+ column.indexer.G))
+ loop_writer.AddConstant(
+ control_loop.Constant('kTurretFreeSpeed', '%f',
+ column.turret.free_speed))
+ loop_writer.AddConstant(
+ control_loop.Constant('kTurretOutputRatio', '%f', column.turret.G))
+ loop_writer.Write(argv[1], argv[2])
- # IntegralColumn controller 1 will disable the indexer.
- integral_column = IntegralColumn('IntegralColumn')
- disabled_integral_column = IntegralColumn('DisabledIntegralColumn',
- disable_indexer=True)
- integral_loop_writer = control_loop.ControlLoopWriter(
- 'IntegralColumn', [integral_column, disabled_integral_column],
- namespaces=namespaces)
- integral_loop_writer.Write(argv[3], argv[4])
+ # IntegralColumn controller 1 will disable the indexer.
+ integral_column = IntegralColumn('IntegralColumn')
+ disabled_integral_column = IntegralColumn(
+ 'DisabledIntegralColumn', disable_indexer=True)
+ integral_loop_writer = control_loop.ControlLoopWriter(
+ 'IntegralColumn', [integral_column, disabled_integral_column],
+ namespaces=namespaces)
+ integral_loop_writer.Write(argv[3], argv[4])
- stuck_integral_column = IntegralColumn('StuckIntegralColumn', voltage_error_noise=8.0)
- stuck_integral_loop_writer = control_loop.ControlLoopWriter(
- 'StuckIntegralColumn', [stuck_integral_column], namespaces=namespaces)
- stuck_integral_loop_writer.Write(argv[5], argv[6])
+ stuck_integral_column = IntegralColumn(
+ 'StuckIntegralColumn', voltage_error_noise=8.0)
+ stuck_integral_loop_writer = control_loop.ControlLoopWriter(
+ 'StuckIntegralColumn', [stuck_integral_column],
+ namespaces=namespaces)
+ stuck_integral_loop_writer.Write(argv[5], argv[6])
if __name__ == '__main__':
- argv = FLAGS(sys.argv)
- glog.init()
- sys.exit(main(argv))
+ argv = FLAGS(sys.argv)
+ glog.init()
+ sys.exit(main(argv))
diff --git a/y2017/control_loops/python/hood.py b/y2017/control_loops/python/hood.py
index fb5aa4e..58bd15e 100755
--- a/y2017/control_loops/python/hood.py
+++ b/y2017/control_loops/python/hood.py
@@ -12,329 +12,339 @@
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 Hood(control_loop.ControlLoop):
- def __init__(self, name='Hood'):
- super(Hood, self).__init__(name)
- # Stall Torque in N m
- self.stall_torque = 0.43
- # Stall Current in Amps
- self.stall_current = 53.0
- self.free_speed_rpm = 13180.0
- # Free Speed in rotations/second.
- self.free_speed = self.free_speed_rpm / 60
- # Free Current in Amps
- self.free_current = 1.8
- # Resistance of the motor
- self.R = 12.0 / self.stall_current
- # Motor velocity constant
- self.Kv = ((self.free_speed * 2.0 * numpy.pi) /
- (12.0 - self.R * self.free_current))
- # Torque constant
- self.Kt = self.stall_torque / self.stall_current
- # First axle gear ratio off the motor
- self.G1 = (12.0 / 60.0)
- # Second axle gear ratio off the motor
- self.G2 = self.G1 * (14.0 / 36.0)
- # Third axle gear ratio off the motor
- self.G3 = self.G2 * (14.0 / 36.0)
- # The last gear reduction (encoder -> hood angle)
- self.last_G = (20.0 / 345.0)
- # Gear ratio
- self.G = (12.0 / 60.0) * (14.0 / 36.0) * (14.0 / 36.0) * self.last_G
+ def __init__(self, name='Hood'):
+ super(Hood, self).__init__(name)
+ # Stall Torque in N m
+ self.stall_torque = 0.43
+ # Stall Current in Amps
+ self.stall_current = 53.0
+ self.free_speed_rpm = 13180.0
+ # Free Speed in rotations/second.
+ self.free_speed = self.free_speed_rpm / 60
+ # Free Current in Amps
+ self.free_current = 1.8
- # 36 tooth gear inertia in kg * m^2
- self.big_gear_inertia = 0.5 * 0.039 * ((36.0 / 40.0 * 0.025) ** 2)
+ # Resistance of the motor
+ self.R = 12.0 / self.stall_current
+ # Motor velocity constant
+ self.Kv = ((self.free_speed * 2.0 * numpy.pi) /
+ (12.0 - self.R * self.free_current))
+ # Torque constant
+ self.Kt = self.stall_torque / self.stall_current
+ # First axle gear ratio off the motor
+ self.G1 = (12.0 / 60.0)
+ # Second axle gear ratio off the motor
+ self.G2 = self.G1 * (14.0 / 36.0)
+ # Third axle gear ratio off the motor
+ self.G3 = self.G2 * (14.0 / 36.0)
+ # The last gear reduction (encoder -> hood angle)
+ self.last_G = (20.0 / 345.0)
+ # Gear ratio
+ self.G = (12.0 / 60.0) * (14.0 / 36.0) * (14.0 / 36.0) * self.last_G
- # Motor inertia in kg * m^2
- self.motor_inertia = 0.000006
- glog.debug(self.big_gear_inertia)
+ # 36 tooth gear inertia in kg * m^2
+ self.big_gear_inertia = 0.5 * 0.039 * ((36.0 / 40.0 * 0.025)**2)
- # Moment of inertia, measured in CAD.
- # Extra mass to compensate for friction is added on.
- self.J = 0.08 + 2.3 + \
- self.big_gear_inertia * ((self.G1 / self.G) ** 2) + \
- self.big_gear_inertia * ((self.G2 / self.G) ** 2) + \
- self.motor_inertia * ((1.0 / self.G) ** 2.0)
- glog.debug('J effective %f', self.J)
+ # Motor inertia in kg * m^2
+ self.motor_inertia = 0.000006
+ glog.debug(self.big_gear_inertia)
- # Control loop time step
- self.dt = 0.005
+ # Moment of inertia, measured in CAD.
+ # Extra mass to compensate for friction is added on.
+ self.J = 0.08 + 2.3 + \
+ self.big_gear_inertia * ((self.G1 / self.G) ** 2) + \
+ self.big_gear_inertia * ((self.G2 / self.G) ** 2) + \
+ self.motor_inertia * ((1.0 / self.G) ** 2.0)
+ glog.debug('J effective %f', self.J)
- # State is [position, velocity]
- # Input is [Voltage]
+ # Control loop time step
+ self.dt = 0.005
- C1 = self.Kt / (self.R * self.J * self.Kv * self.G * self.G)
- C2 = self.Kt / (self.J * self.R * self.G)
+ # State is [position, velocity]
+ # Input is [Voltage]
- self.A_continuous = numpy.matrix(
- [[0, 1],
- [0, -C1]])
+ C1 = self.Kt / (self.R * self.J * self.Kv * self.G * self.G)
+ C2 = self.Kt / (self.J * self.R * self.G)
- # Start with the unmodified input
- self.B_continuous = numpy.matrix(
- [[0],
- [C2]])
+ self.A_continuous = numpy.matrix([[0, 1], [0, -C1]])
- self.C = numpy.matrix([[1, 0]])
- self.D = numpy.matrix([[0]])
+ # Start with the unmodified input
+ self.B_continuous = numpy.matrix([[0], [C2]])
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ self.C = numpy.matrix([[1, 0]])
+ self.D = numpy.matrix([[0]])
- controllability = controls.ctrb(self.A, self.B)
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- glog.debug('Free speed is %f',
- -self.B_continuous[1, 0] / self.A_continuous[1, 1] * 12.0)
- glog.debug(repr(self.A_continuous))
+ controllability = controls.ctrb(self.A, self.B)
- # Calculate the LQR controller gain
- q_pos = 0.015
- q_vel = 0.40
- self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0],
- [0.0, (1.0 / (q_vel ** 2.0))]])
+ glog.debug('Free speed is %f',
+ -self.B_continuous[1, 0] / self.A_continuous[1, 1] * 12.0)
+ glog.debug(repr(self.A_continuous))
- self.R = numpy.matrix([[(5.0 / (12.0 ** 2.0))]])
- self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
+ # Calculate the LQR controller gain
+ q_pos = 0.015
+ q_vel = 0.40
+ self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0],
+ [0.0, (1.0 / (q_vel**2.0))]])
- # Calculate the feed forwards gain.
- q_pos_ff = 0.005
- q_vel_ff = 1.0
- self.Qff = numpy.matrix([[(1.0 / (q_pos_ff ** 2.0)), 0.0],
- [0.0, (1.0 / (q_vel_ff ** 2.0))]])
+ self.R = numpy.matrix([[(5.0 / (12.0**2.0))]])
+ self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
- self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
+ # Calculate the feed forwards gain.
+ q_pos_ff = 0.005
+ q_vel_ff = 1.0
+ self.Qff = numpy.matrix([[(1.0 / (q_pos_ff**2.0)), 0.0],
+ [0.0, (1.0 / (q_vel_ff**2.0))]])
- glog.debug('K %s', repr(self.K))
- glog.debug('Poles are %s',
- repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
+ self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
- q_pos = 0.10
- q_vel = 1.65
- self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0],
- [0.0, (q_vel ** 2.0)]])
+ glog.debug('K %s', repr(self.K))
+ glog.debug('Poles are %s',
+ repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
- r_volts = 0.025
- self.R = numpy.matrix([[(r_volts ** 2.0)]])
+ q_pos = 0.10
+ q_vel = 1.65
+ self.Q = numpy.matrix([[(q_pos**2.0), 0.0], [0.0, (q_vel**2.0)]])
- self.KalmanGain, self.Q_steady = controls.kalman(
- A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
+ r_volts = 0.025
+ self.R = numpy.matrix([[(r_volts**2.0)]])
- glog.debug('Kal %s', repr(self.KalmanGain))
- self.L = self.A * self.KalmanGain
- glog.debug('KalL is %s', repr(self.L))
+ self.KalmanGain, self.Q_steady = controls.kalman(
+ A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
- # 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]])
- self.U_min = numpy.matrix([[-12.0]])
+ glog.debug('Kal %s', repr(self.KalmanGain))
+ self.L = self.A * self.KalmanGain
+ glog.debug('KalL is %s', repr(self.L))
- self.InitializeState()
+ # 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]])
+ self.U_min = numpy.matrix([[-12.0]])
+
+ self.InitializeState()
+
class IntegralHood(Hood):
- def __init__(self, name='IntegralHood'):
- super(IntegralHood, self).__init__(name=name)
- self.A_continuous_unaugmented = self.A_continuous
- self.B_continuous_unaugmented = self.B_continuous
+ def __init__(self, name='IntegralHood'):
+ super(IntegralHood, 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.01
- q_vel = 4.0
- q_voltage = 4.0
- 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.001
- self.R = numpy.matrix([[(r_pos ** 2.0)]])
+ q_pos = 0.01
+ q_vel = 4.0
+ q_voltage = 4.0
+ 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.001
+ 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.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((1, 1)))), axis=1)
+ 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.InitializeState()
+ self.Kff = numpy.concatenate(
+ (self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1)
+
+ self.InitializeState()
+
class ScenarioPlotter(object):
- def __init__(self):
- # Various lists for graphing things.
- self.t = []
- self.x = []
- self.v = []
- self.v_hat = []
- self.a = []
- self.x_hat = []
- self.u = []
- self.offset = []
- def run_test(self, hood, end_goal,
- controller_hood,
- observer_hood=None,
- iterations=200):
- """Runs the hood plant with an initial condition and goal.
+ def __init__(self):
+ # Various lists for graphing things.
+ self.t = []
+ self.x = []
+ self.v = []
+ self.v_hat = []
+ self.a = []
+ self.x_hat = []
+ self.u = []
+ self.offset = []
- Args:
- hood: hood object to use.
- end_goal: end_goal state.
- controller_hood: Hood object to get K from, or None if we should
- use hood.
- observer_hood: Hood object to use for the observer, or None if we should
- use the actual state.
- iterations: Number of timesteps to run the model for.
- """
+ def run_test(self,
+ hood,
+ end_goal,
+ controller_hood,
+ observer_hood=None,
+ iterations=200):
+ """Runs the hood plant with an initial condition and goal.
- if controller_hood is None:
- controller_hood = hood
+ Args:
+ hood: hood object to use.
+ end_goal: end_goal state.
+ controller_hood: Hood object to get K from, or None if we should
+ use hood.
+ observer_hood: Hood object to use for the observer, or None if we should
+ use the actual state.
+ iterations: Number of timesteps to run the model for.
+ """
- vbat = 12.0
+ if controller_hood is None:
+ controller_hood = hood
- if self.t:
- initial_t = self.t[-1] + hood.dt
- else:
- initial_t = 0
+ vbat = 12.0
- goal = numpy.concatenate((hood.X, numpy.matrix(numpy.zeros((1, 1)))), axis=0)
+ if self.t:
+ initial_t = self.t[-1] + hood.dt
+ else:
+ initial_t = 0
- profile = TrapezoidProfile(hood.dt)
- profile.set_maximum_acceleration(10.0)
- profile.set_maximum_velocity(1.0)
- profile.SetGoal(goal[0, 0])
+ goal = numpy.concatenate((hood.X, numpy.matrix(numpy.zeros((1, 1)))),
+ axis=0)
- U_last = numpy.matrix(numpy.zeros((1, 1)))
- for i in xrange(iterations):
- observer_hood.Y = hood.Y
- observer_hood.CorrectObserver(U_last)
+ profile = TrapezoidProfile(hood.dt)
+ profile.set_maximum_acceleration(10.0)
+ profile.set_maximum_velocity(1.0)
+ profile.SetGoal(goal[0, 0])
- self.offset.append(observer_hood.X_hat[2, 0])
- self.x_hat.append(observer_hood.X_hat[0, 0])
+ U_last = numpy.matrix(numpy.zeros((1, 1)))
+ for i in xrange(iterations):
+ observer_hood.Y = hood.Y
+ observer_hood.CorrectObserver(U_last)
- next_goal = numpy.concatenate(
- (profile.Update(end_goal[0, 0], end_goal[1, 0]),
- numpy.matrix(numpy.zeros((1, 1)))),
- axis=0)
+ self.offset.append(observer_hood.X_hat[2, 0])
+ self.x_hat.append(observer_hood.X_hat[0, 0])
- ff_U = controller_hood.Kff * (next_goal - observer_hood.A * goal)
+ next_goal = numpy.concatenate(
+ (profile.Update(end_goal[0, 0], end_goal[1, 0]),
+ numpy.matrix(numpy.zeros((1, 1)))),
+ axis=0)
- U_uncapped = controller_hood.K * (goal - observer_hood.X_hat) + ff_U
- U = U_uncapped.copy()
- U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
- self.x.append(hood.X[0, 0])
+ ff_U = controller_hood.Kff * (next_goal - observer_hood.A * goal)
- if self.v:
- last_v = self.v[-1]
- else:
- last_v = 0
+ U_uncapped = controller_hood.K * (goal - observer_hood.X_hat) + ff_U
+ U = U_uncapped.copy()
+ U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
+ self.x.append(hood.X[0, 0])
- self.v.append(hood.X[1, 0])
- self.a.append((self.v[-1] - last_v) / hood.dt)
- self.v_hat.append(observer_hood.X_hat[1, 0])
+ if self.v:
+ last_v = self.v[-1]
+ else:
+ last_v = 0
- offset = 0.0
- if i > 100:
- offset = 2.0
- hood.Update(U + offset)
+ self.v.append(hood.X[1, 0])
+ self.a.append((self.v[-1] - last_v) / hood.dt)
+ self.v_hat.append(observer_hood.X_hat[1, 0])
- observer_hood.PredictObserver(U)
+ offset = 0.0
+ if i > 100:
+ offset = 2.0
+ hood.Update(U + offset)
- self.t.append(initial_t + i * hood.dt)
- self.u.append(U[0, 0])
+ observer_hood.PredictObserver(U)
- ff_U -= U_uncapped - U
- goal = controller_hood.A * goal + controller_hood.B * ff_U
+ self.t.append(initial_t + i * hood.dt)
+ self.u.append(U[0, 0])
- if U[0, 0] != U_uncapped[0, 0]:
- profile.MoveCurrentState(
- numpy.matrix([[goal[0, 0]], [goal[1, 0]]]))
+ ff_U -= U_uncapped - U
+ goal = controller_hood.A * goal + controller_hood.B * ff_U
- glog.debug('Time: %f', self.t[-1])
- glog.debug('goal_error %s', repr(end_goal - goal))
- glog.debug('error %s', repr(observer_hood.X_hat - end_goal))
+ if U[0, 0] != U_uncapped[0, 0]:
+ profile.MoveCurrentState(
+ numpy.matrix([[goal[0, 0]], [goal[1, 0]]]))
- def Plot(self):
- pylab.subplot(3, 1, 1)
- pylab.plot(self.t, self.x, label='x')
- pylab.plot(self.t, self.x_hat, label='x_hat')
- pylab.plot(self.t, self.v, label='v')
- pylab.plot(self.t, self.v_hat, label='v_hat')
- pylab.legend()
+ glog.debug('Time: %f', self.t[-1])
+ glog.debug('goal_error %s', repr(end_goal - goal))
+ glog.debug('error %s', repr(observer_hood.X_hat - end_goal))
- pylab.subplot(3, 1, 2)
- pylab.plot(self.t, self.u, label='u')
- pylab.plot(self.t, self.offset, label='voltage_offset')
- pylab.legend()
+ def Plot(self):
+ pylab.subplot(3, 1, 1)
+ pylab.plot(self.t, self.x, label='x')
+ pylab.plot(self.t, self.x_hat, label='x_hat')
+ pylab.plot(self.t, self.v, label='v')
+ pylab.plot(self.t, self.v_hat, label='v_hat')
+ pylab.legend()
- pylab.subplot(3, 1, 3)
- pylab.plot(self.t, self.a, label='a')
- 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.show()
+ pylab.subplot(3, 1, 3)
+ pylab.plot(self.t, self.a, label='a')
+ pylab.legend()
+
+ pylab.show()
def main(argv):
- scenario_plotter = ScenarioPlotter()
+ scenario_plotter = ScenarioPlotter()
- hood = Hood()
- hood_controller = IntegralHood()
- observer_hood = IntegralHood()
+ hood = Hood()
+ hood_controller = IntegralHood()
+ observer_hood = IntegralHood()
- # Test moving the hood with constant separation.
- initial_X = numpy.matrix([[0.0], [0.0]])
- R = numpy.matrix([[numpy.pi/4.0], [0.0], [0.0]])
- scenario_plotter.run_test(hood, end_goal=R,
- controller_hood=hood_controller,
- observer_hood=observer_hood, iterations=200)
+ # Test moving the hood with constant separation.
+ initial_X = numpy.matrix([[0.0], [0.0]])
+ R = numpy.matrix([[numpy.pi / 4.0], [0.0], [0.0]])
+ scenario_plotter.run_test(
+ hood,
+ end_goal=R,
+ controller_hood=hood_controller,
+ observer_hood=observer_hood,
+ iterations=200)
- if FLAGS.plot:
- scenario_plotter.Plot()
+ if FLAGS.plot:
+ scenario_plotter.Plot()
- # Write the generated constants out to a file.
- if len(argv) != 5:
- glog.fatal('Expected .h file name and .cc file name for the hood and integral hood.')
- else:
- namespaces = ['y2017', 'control_loops', 'superstructure', 'hood']
- hood = Hood('Hood')
- loop_writer = control_loop.ControlLoopWriter('Hood', [hood],
- namespaces=namespaces)
- loop_writer.AddConstant(control_loop.Constant(
- 'kFreeSpeed', '%f', hood.free_speed))
- loop_writer.AddConstant(control_loop.Constant(
- 'kOutputRatio', '%f', hood.G))
- loop_writer.Write(argv[1], argv[2])
+ # Write the generated constants out to a file.
+ if len(argv) != 5:
+ glog.fatal(
+ 'Expected .h file name and .cc file name for the hood and integral hood.'
+ )
+ else:
+ namespaces = ['y2017', 'control_loops', 'superstructure', 'hood']
+ hood = Hood('Hood')
+ loop_writer = control_loop.ControlLoopWriter(
+ 'Hood', [hood], namespaces=namespaces)
+ loop_writer.AddConstant(
+ control_loop.Constant('kFreeSpeed', '%f', hood.free_speed))
+ loop_writer.AddConstant(
+ control_loop.Constant('kOutputRatio', '%f', hood.G))
+ loop_writer.Write(argv[1], argv[2])
- integral_hood = IntegralHood('IntegralHood')
- integral_loop_writer = control_loop.ControlLoopWriter('IntegralHood', [integral_hood],
- namespaces=namespaces)
- integral_loop_writer.AddConstant(control_loop.Constant('kLastReduction', '%f',
- integral_hood.last_G))
- integral_loop_writer.Write(argv[3], argv[4])
+ integral_hood = IntegralHood('IntegralHood')
+ integral_loop_writer = control_loop.ControlLoopWriter(
+ 'IntegralHood', [integral_hood], namespaces=namespaces)
+ integral_loop_writer.AddConstant(
+ control_loop.Constant('kLastReduction', '%f', integral_hood.last_G))
+ integral_loop_writer.Write(argv[3], argv[4])
if __name__ == '__main__':
- argv = FLAGS(sys.argv)
- glog.init()
- sys.exit(main(argv))
+ argv = FLAGS(sys.argv)
+ glog.init()
+ sys.exit(main(argv))
diff --git a/y2017/control_loops/python/shooter.py b/y2017/control_loops/python/shooter.py
index 1b0ff13..a825ff0 100755
--- a/y2017/control_loops/python/shooter.py
+++ b/y2017/control_loops/python/shooter.py
@@ -16,370 +16,388 @@
def PlotDiff(list1, list2, time):
- pylab.subplot(1, 1, 1)
- pylab.plot(time, numpy.subtract(list1, list2), label='diff')
- pylab.legend()
+ pylab.subplot(1, 1, 1)
+ pylab.plot(time, numpy.subtract(list1, list2), label='diff')
+ pylab.legend()
+
class VelocityShooter(control_loop.HybridControlLoop):
- def __init__(self, name='VelocityShooter'):
- super(VelocityShooter, self).__init__(name)
- # Number of motors
- self.num_motors = 2.0
- # Stall Torque in N m
- self.stall_torque = 0.71 * self.num_motors
- # Stall Current in Amps
- self.stall_current = 134.0 * self.num_motors
- # Free Speed in RPM
- self.free_speed_rpm = 18730.0
- # Free Speed in rotations/second.
- self.free_speed = self.free_speed_rpm / 60.0
- # Free Current in Amps
- self.free_current = 0.7 * self.num_motors
- # Moment of inertia of the shooter wheel in kg m^2
- # 1400.6 grams/cm^2
- # 1.407 *1e-4 kg m^2
- self.J = 0.00120
- # 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 * 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 / 36.0
- # Control loop time step
- self.dt = 0.00505
- # 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)
+ # Number of motors
+ self.num_motors = 2.0
+ # Stall Torque in N m
+ self.stall_torque = 0.71 * self.num_motors
+ # Stall Current in Amps
+ self.stall_current = 134.0 * self.num_motors
+ # Free Speed in RPM
+ self.free_speed_rpm = 18730.0
+ # Free Speed in rotations/second.
+ self.free_speed = self.free_speed_rpm / 60.0
+ # Free Current in Amps
+ self.free_current = 0.7 * self.num_motors
+ # Moment of inertia of the shooter wheel in kg m^2
+ # 1400.6 grams/cm^2
+ # 1.407 *1e-4 kg m^2
+ self.J = 0.00120
+ # 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 * 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 / 36.0
+ # Control loop time step
+ self.dt = 0.00505
- # The states are [unfiltered_velocity]
- 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([.75])
+ # The states are [unfiltered_velocity]
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- glog.debug('K %s', repr(self.K))
- glog.debug('System poles are %s',
- repr(numpy.linalg.eig(self.A_continuous)[0]))
- glog.debug('Poles are %s',
- repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
+ self.PlaceControllerPoles([.75])
- self.PlaceObserverPoles([0.3])
+ glog.debug('K %s', repr(self.K))
+ glog.debug('System poles are %s',
+ repr(numpy.linalg.eig(self.A_continuous)[0]))
+ glog.debug('Poles are %s',
+ repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
- 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)
- self.InitializeState()
+ qff_vel = 8.0
+ self.Qff = numpy.matrix([[1.0 / (qff_vel**2.0)]])
+
+ self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
+ self.InitializeState()
+
class SecondOrderVelocityShooter(VelocityShooter):
- def __init__(self, name='SecondOrderVelocityShooter'):
- super(SecondOrderVelocityShooter, self).__init__(name)
- self.A_continuous_unaugmented = self.A_continuous
- self.B_continuous_unaugmented = self.B_continuous
+ def __init__(self, name='SecondOrderVelocityShooter'):
+ super(SecondOrderVelocityShooter, self).__init__(name)
- self.A_continuous = numpy.matrix(numpy.zeros((2, 2)))
- self.A_continuous[0:1, 0:1] = self.A_continuous_unaugmented
- self.A_continuous[1, 0] = 175.0
- self.A_continuous[1, 1] = -self.A_continuous[1, 0]
+ 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[0:1, 0] = self.B_continuous_unaugmented
+ self.A_continuous = numpy.matrix(numpy.zeros((2, 2)))
+ self.A_continuous[0:1, 0:1] = self.A_continuous_unaugmented
+ self.A_continuous[1, 0] = 175.0
+ self.A_continuous[1, 1] = -self.A_continuous[1, 0]
- self.C = numpy.matrix([[0, 1]])
- self.D = numpy.matrix([[0]])
+ self.B_continuous = numpy.matrix(numpy.zeros((2, 1)))
+ self.B_continuous[0:1, 0] = self.B_continuous_unaugmented
- # The states are [unfiltered_velocity, velocity]
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
+ self.C = numpy.matrix([[0, 1]])
+ self.D = numpy.matrix([[0]])
- self.PlaceControllerPoles([.70, 0.60])
+ # The states are [unfiltered_velocity, velocity]
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- q_vel = 40.0
- q_filteredvel = 30.0
- self.Q = numpy.matrix([[(1.0 / (q_vel ** 2.0)), 0.0],
- [0.0, (1.0 / (q_filteredvel ** 2.0))]])
+ self.PlaceControllerPoles([.70, 0.60])
- self.R = numpy.matrix([[(1.0 / (3.0 ** 2.0))]])
- self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
+ q_vel = 40.0
+ q_filteredvel = 30.0
+ self.Q = numpy.matrix([[(1.0 / (q_vel**2.0)), 0.0],
+ [0.0, (1.0 / (q_filteredvel**2.0))]])
- glog.debug('K %s', repr(self.K))
- glog.debug('System poles are %s',
- repr(numpy.linalg.eig(self.A_continuous)[0]))
- glog.debug('Poles are %s',
- repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
+ self.R = numpy.matrix([[(1.0 / (3.0**2.0))]])
+ self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
- self.PlaceObserverPoles([0.3, 0.3])
+ glog.debug('K %s', repr(self.K))
+ glog.debug('System poles are %s',
+ repr(numpy.linalg.eig(self.A_continuous)[0]))
+ glog.debug('Poles are %s',
+ repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
- self.U_max = numpy.matrix([[12.0]])
- self.U_min = numpy.matrix([[-12.0]])
+ self.PlaceObserverPoles([0.3, 0.3])
- qff_vel = 8.0
- self.Qff = numpy.matrix([[1.0 / (qff_vel ** 2.0), 0.0],
- [0.0, 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)
- self.InitializeState()
+ qff_vel = 8.0
+ self.Qff = numpy.matrix([[1.0 / (qff_vel**2.0), 0.0],
+ [0.0, 1.0 / (qff_vel**2.0)]])
+
+ self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
+ self.InitializeState()
class Shooter(SecondOrderVelocityShooter):
- 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((3, 3)))
- self.A_continuous[1:3, 1:3] = self.A_continuous_unaugmented
- self.A_continuous[0, 2] = 1
+ 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[1:3, 0] = self.B_continuous_unaugmented
+ self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
+ self.A_continuous[1:3, 1:3] = self.A_continuous_unaugmented
+ self.A_continuous[0, 2] = 1
- # State feedback matrices
- # [position, unfiltered_velocity, angular velocity]
- self.C = numpy.matrix([[1, 0, 0]])
- self.D = numpy.matrix([[0]])
+ self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
+ self.B_continuous[1:3, 0] = self.B_continuous_unaugmented
- self.A, self.B = self.ContinuousToDiscrete(
- self.A_continuous, self.B_continuous, self.dt)
- glog.debug(repr(self.A_continuous))
- glog.debug(repr(self.B_continuous))
+ # State feedback matrices
+ # [position, unfiltered_velocity, angular velocity]
+ self.C = numpy.matrix([[1, 0, 0]])
+ self.D = numpy.matrix([[0]])
- observeability = controls.ctrb(self.A.T, self.C.T)
- glog.debug('Rank of augmented observability matrix. %d', numpy.linalg.matrix_rank(
- observeability))
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
+ glog.debug(repr(self.A_continuous))
+ glog.debug(repr(self.B_continuous))
+ observeability = controls.ctrb(self.A.T, self.C.T)
+ glog.debug('Rank of augmented observability matrix. %d',
+ numpy.linalg.matrix_rank(observeability))
- self.PlaceObserverPoles([0.9, 0.8, 0.7])
+ self.PlaceObserverPoles([0.9, 0.8, 0.7])
- self.K_unaugmented = self.K
- self.K = numpy.matrix(numpy.zeros((1, 3)))
- self.K[0, 1:3] = self.K_unaugmented
- self.Kff_unaugmented = self.Kff
- self.Kff = numpy.matrix(numpy.zeros((1, 3)))
- self.Kff[0, 1:3] = self.Kff_unaugmented
+ self.K_unaugmented = self.K
+ self.K = numpy.matrix(numpy.zeros((1, 3)))
+ self.K[0, 1:3] = self.K_unaugmented
+ self.Kff_unaugmented = self.Kff
+ self.Kff = numpy.matrix(numpy.zeros((1, 3)))
+ self.Kff[0, 1:3] = self.Kff_unaugmented
- self.InitializeState()
+ 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((4, 4)))
- self.A_continuous[0:3, 0:3] = self.A_continuous_unaugmented
- self.A_continuous[0:3, 3] = 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((4, 1)))
- self.B_continuous[0:3, 0] = self.B_continuous_unaugmented
+ self.A_continuous = numpy.matrix(numpy.zeros((4, 4)))
+ self.A_continuous[0:3, 0:3] = self.A_continuous_unaugmented
+ self.A_continuous[0:3, 3] = self.B_continuous_unaugmented
- self.C_unaugmented = self.C
- self.C = numpy.matrix(numpy.zeros((1, 4)))
- self.C[0:1, 0:3] = self.C_unaugmented
+ self.B_continuous = numpy.matrix(numpy.zeros((4, 1)))
+ self.B_continuous[0:3, 0] = self.B_continuous_unaugmented
- # The states are [position, unfiltered_velocity, velocity, torque_error]
- 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, 4)))
+ self.C[0:1, 0:3] = self.C_unaugmented
- glog.debug('A: \n%s', repr(self.A_continuous))
- glog.debug('eig(A): \n%s', repr(scipy.linalg.eig(self.A_continuous)))
- glog.debug('schur(A): \n%s', repr(scipy.linalg.schur(self.A_continuous)))
- glog.debug('A_dt(A): \n%s', repr(self.A))
+ # The states are [position, unfiltered_velocity, velocity, torque_error]
+ self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
+ self.B_continuous, self.dt)
- q_pos = 0.01
- q_vel = 5.0
- q_velfilt = 1.5
- q_voltage = 2.0
- self.Q_continuous = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0],
- [0.0, (q_vel ** 2.0), 0.0, 0.0],
- [0.0, 0.0, (q_velfilt ** 2.0), 0.0],
- [0.0, 0.0, 0.0, (q_voltage ** 2.0)]])
+ glog.debug('A: \n%s', repr(self.A_continuous))
+ glog.debug('eig(A): \n%s', repr(scipy.linalg.eig(self.A_continuous)))
+ glog.debug('schur(A): \n%s', repr(
+ scipy.linalg.schur(self.A_continuous)))
+ glog.debug('A_dt(A): \n%s', repr(self.A))
- r_pos = 0.0003
- self.R_continuous = numpy.matrix([[(r_pos ** 2.0)]])
+ q_pos = 0.01
+ q_vel = 5.0
+ q_velfilt = 1.5
+ q_voltage = 2.0
+ self.Q_continuous = numpy.matrix([[(q_pos**2.0), 0.0, 0.0, 0.0],
+ [0.0, (q_vel**2.0), 0.0, 0.0],
+ [0.0, 0.0, (q_velfilt**2.0), 0.0],
+ [0.0, 0.0, 0.0, (q_voltage**2.0)]])
- _, _, self.Q, self.R = controls.kalmd(
- A_continuous=self.A_continuous, B_continuous=self.B_continuous,
- Q_continuous=self.Q_continuous, R_continuous=self.R_continuous,
- dt=self.dt)
+ r_pos = 0.0003
+ self.R_continuous = numpy.matrix([[(r_pos**2.0)]])
- self.KalmanGain, self.P_steady_state = controls.kalman(
- A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
- self.L = self.A * self.KalmanGain
+ _, _, self.Q, self.R = controls.kalmd(
+ A_continuous=self.A_continuous,
+ B_continuous=self.B_continuous,
+ Q_continuous=self.Q_continuous,
+ R_continuous=self.R_continuous,
+ dt=self.dt)
- self.K_unaugmented = self.K
- self.K = numpy.matrix(numpy.zeros((1, 4)))
- self.K[0, 0:3] = self.K_unaugmented
- self.K[0, 3] = 1
- self.Kff_unaugmented = self.Kff
- self.Kff = numpy.matrix(numpy.zeros((1, 4)))
- self.Kff[0, 0:3] = self.Kff_unaugmented
+ self.KalmanGain, self.P_steady_state = 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, 4)))
+ self.K[0, 0:3] = self.K_unaugmented
+ self.K[0, 3] = 1
+ self.Kff_unaugmented = self.Kff
+ self.Kff = numpy.matrix(numpy.zeros((1, 4)))
+ self.Kff[0, 0:3] = 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 = []
- self.diff = []
- def run_test(self, shooter, goal, iterations=200, controller_shooter=None,
- observer_shooter=None, hybrid_obs = False):
- """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 = []
+ self.diff = []
- 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,
+ hybrid_obs=False):
+ """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
- last_U = numpy.matrix([[0.0]])
- for i in xrange(iterations):
- X_hat = shooter.X
-
- if observer_shooter is not None:
- X_hat = observer_shooter.X_hat
- self.x_hat.append(observer_shooter.X_hat[2, 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])
-
- self.diff.append(shooter.X[2, 0] - observer_shooter.X_hat[2, 0])
-
- if self.v:
- last_v = self.v[-1]
- else:
- last_v = 0
-
- self.v.append(shooter.X[2, 0])
- self.a.append((self.v[-1] - last_v) / shooter.dt)
-
- if observer_shooter is not None:
- if i != 0:
- observer_shooter.Y = shooter.Y
- observer_shooter.CorrectObserver(U)
- self.offset.append(observer_shooter.X_hat[3, 0])
-
- applied_U = last_U.copy()
- if i > 60:
- applied_U += 2
- shooter.Update(applied_U)
-
- if observer_shooter is not None:
- if hybrid_obs:
- observer_shooter.PredictHybridObserver(last_U, shooter.dt)
+ if self.t:
+ initial_t = self.t[-1] + shooter.dt
else:
- observer_shooter.PredictObserver(last_U)
- last_U = U.copy()
+ initial_t = 0
+ last_U = numpy.matrix([[0.0]])
+ for i in xrange(iterations):
+ X_hat = shooter.X
- self.t.append(initial_t + i * shooter.dt)
- self.u.append(U[0, 0])
+ if observer_shooter is not None:
+ X_hat = observer_shooter.X_hat
+ self.x_hat.append(observer_shooter.X_hat[2, 0])
- def Plot(self):
- pylab.figure()
- 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()
+ ff_U = controller_shooter.Kff * (goal - observer_shooter.A * goal)
- pylab.subplot(3, 1, 2)
- pylab.plot(self.t, self.u, label='u')
- pylab.plot(self.t, self.offset, label='voltage_offset')
- pylab.legend()
+ 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])
- pylab.subplot(3, 1, 3)
- pylab.plot(self.t, self.a, label='a')
- pylab.legend()
+ self.diff.append(shooter.X[2, 0] - observer_shooter.X_hat[2, 0])
- pylab.draw()
+ if self.v:
+ last_v = self.v[-1]
+ else:
+ last_v = 0
+
+ self.v.append(shooter.X[2, 0])
+ self.a.append((self.v[-1] - last_v) / shooter.dt)
+
+ if observer_shooter is not None:
+ if i != 0:
+ observer_shooter.Y = shooter.Y
+ observer_shooter.CorrectObserver(U)
+ self.offset.append(observer_shooter.X_hat[3, 0])
+
+ applied_U = last_U.copy()
+ if i > 60:
+ applied_U += 2
+ shooter.Update(applied_U)
+
+ if observer_shooter is not None:
+ if hybrid_obs:
+ observer_shooter.PredictHybridObserver(last_U, shooter.dt)
+ else:
+ observer_shooter.PredictObserver(last_U)
+ last_U = U.copy()
+
+ self.t.append(initial_t + i * shooter.dt)
+ self.u.append(U[0, 0])
+
+ def Plot(self):
+ pylab.figure()
+ 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, 3)
+ pylab.plot(self.t, self.a, label='a')
+ pylab.legend()
+
+ pylab.draw()
def main(argv):
- scenario_plotter = ScenarioPlotter()
+ scenario_plotter = ScenarioPlotter()
- if FLAGS.plot:
- iterations = 200
+ if FLAGS.plot:
+ iterations = 200
- initial_X = numpy.matrix([[0.0], [0.0], [0.0]])
- R = numpy.matrix([[0.0], [100.0], [100.0], [0.0]])
+ initial_X = numpy.matrix([[0.0], [0.0], [0.0]])
+ R = numpy.matrix([[0.0], [100.0], [100.0], [0.0]])
- scenario_plotter_int = ScenarioPlotter()
+ scenario_plotter_int = ScenarioPlotter()
- shooter = Shooter()
- shooter_controller = IntegralShooter()
- observer_shooter_hybrid = IntegralShooter()
+ shooter = Shooter()
+ shooter_controller = IntegralShooter()
+ observer_shooter_hybrid = IntegralShooter()
- scenario_plotter_int.run_test(shooter, goal=R, controller_shooter=shooter_controller,
- observer_shooter=observer_shooter_hybrid, iterations=iterations,
- hybrid_obs = True)
+ scenario_plotter_int.run_test(
+ shooter,
+ goal=R,
+ controller_shooter=shooter_controller,
+ observer_shooter=observer_shooter_hybrid,
+ iterations=iterations,
+ hybrid_obs=True)
- scenario_plotter_int.Plot()
+ scenario_plotter_int.Plot()
- pylab.show()
+ pylab.show()
- if len(argv) != 5:
- glog.fatal('Expected .h file name and .cc file name')
- else:
- namespaces = ['y2017', 'control_loops', 'superstructure', 'shooter']
- shooter = Shooter('Shooter')
- loop_writer = control_loop.ControlLoopWriter('Shooter', [shooter],
- namespaces=namespaces)
- loop_writer.AddConstant(control_loop.Constant(
- 'kFreeSpeed', '%f', shooter.free_speed))
- loop_writer.AddConstant(control_loop.Constant(
- 'kOutputRatio', '%f', shooter.G))
- loop_writer.Write(argv[1], argv[2])
+ if len(argv) != 5:
+ glog.fatal('Expected .h file name and .cc file name')
+ else:
+ namespaces = ['y2017', 'control_loops', 'superstructure', 'shooter']
+ shooter = Shooter('Shooter')
+ loop_writer = control_loop.ControlLoopWriter(
+ 'Shooter', [shooter], namespaces=namespaces)
+ loop_writer.AddConstant(
+ control_loop.Constant('kFreeSpeed', '%f', shooter.free_speed))
+ loop_writer.AddConstant(
+ control_loop.Constant('kOutputRatio', '%f', shooter.G))
+ loop_writer.Write(argv[1], argv[2])
- integral_shooter = IntegralShooter('IntegralShooter')
- integral_loop_writer = control_loop.ControlLoopWriter(
- 'IntegralShooter', [integral_shooter], namespaces=namespaces,
- plant_type='StateFeedbackHybridPlant',
- observer_type='HybridKalman')
- integral_loop_writer.Write(argv[3], argv[4])
+ integral_shooter = IntegralShooter('IntegralShooter')
+ integral_loop_writer = control_loop.ControlLoopWriter(
+ 'IntegralShooter', [integral_shooter],
+ namespaces=namespaces,
+ plant_type='StateFeedbackHybridPlant',
+ observer_type='HybridKalman')
+ integral_loop_writer.Write(argv[3], argv[4])
if __name__ == '__main__':
- argv = FLAGS(sys.argv)
- glog.init()
- sys.exit(main(argv))
+ argv = FLAGS(sys.argv)
+ glog.init()
+ sys.exit(main(argv))