blob: ac527de2adc1332375d09faf7442ba388f0a0d63 [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Philipp Schrader9ffe2982016-12-07 20:51:08 -08002
3from frc971.control_loops.python import control_loop
4import numpy
5
Tyler Chatow6738c362019-02-16 14:12:30 -08006
Philipp Schrader9ffe2982016-12-07 20:51:08 -08007class CIM(control_loop.ControlLoop):
Tyler Chatow6738c362019-02-16 14:12:30 -08008 def __init__(self):
9 super(CIM, self).__init__("CIM")
10 # Stall Torque in N m
11 self.stall_torque = 2.42
12 # Stall Current in Amps
13 self.stall_current = 133
14 # Free Speed in RPM
15 self.free_speed = 4650.0
16 # Free Current in Amps
17 self.free_current = 2.7
18 # Moment of inertia of the CIM in kg m^2
19 self.J = 0.0001
20 # Resistance of the motor, divided by 2 to account for the 2 motors
21 self.resistance = 12.0 / self.stall_current
22 # Motor velocity constant
23 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
24 (12.0 - self.resistance * self.free_current))
25 # Torque constant
26 self.Kt = self.stall_torque / self.stall_current
27 # Control loop time step
28 self.dt = 0.005
Philipp Schrader9ffe2982016-12-07 20:51:08 -080029
Tyler Chatow6738c362019-02-16 14:12:30 -080030 # State feedback matrices
31 self.A_continuous = numpy.matrix(
32 [[-self.Kt / self.Kv / (self.J * self.resistance)]])
33 self.B_continuous = numpy.matrix(
34 [[self.Kt / (self.J * self.resistance)]])
35 self.C = numpy.matrix([[1]])
36 self.D = numpy.matrix([[0]])
Philipp Schrader9ffe2982016-12-07 20:51:08 -080037
Tyler Chatow6738c362019-02-16 14:12:30 -080038 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
39 self.B_continuous, self.dt)
Philipp Schrader9ffe2982016-12-07 20:51:08 -080040
Tyler Chatow6738c362019-02-16 14:12:30 -080041 self.PlaceControllerPoles([0.01])
42 self.PlaceObserverPoles([0.01])
Philipp Schrader9ffe2982016-12-07 20:51:08 -080043
Tyler Chatow6738c362019-02-16 14:12:30 -080044 self.U_max = numpy.matrix([[12.0]])
45 self.U_min = numpy.matrix([[-12.0]])
46
47 self.InitializeState()