blob: f8a6e9abc3886a1f678df444cf5886fc7a009854 [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):
Philipp Schrader9ffe2982016-12-07 20:51:08 -08008
Tyler Chatow6738c362019-02-16 14:12:30 -08009 def __init__(self):
10 super(CIM, self).__init__("CIM")
11 # Stall Torque in N m
12 self.stall_torque = 2.42
13 # Stall Current in Amps
14 self.stall_current = 133
15 # Free Speed in RPM
16 self.free_speed = 4650.0
17 # Free Current in Amps
18 self.free_current = 2.7
19 # Moment of inertia of the CIM in kg m^2
20 self.J = 0.0001
21 # Resistance of the motor, divided by 2 to account for the 2 motors
22 self.resistance = 12.0 / self.stall_current
23 # Motor velocity constant
24 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
25 (12.0 - self.resistance * self.free_current))
26 # Torque constant
27 self.Kt = self.stall_torque / self.stall_current
28 # Control loop time step
29 self.dt = 0.005
Philipp Schrader9ffe2982016-12-07 20:51:08 -080030
Tyler Chatow6738c362019-02-16 14:12:30 -080031 # State feedback matrices
32 self.A_continuous = numpy.matrix(
33 [[-self.Kt / self.Kv / (self.J * self.resistance)]])
34 self.B_continuous = numpy.matrix(
35 [[self.Kt / (self.J * self.resistance)]])
36 self.C = numpy.matrix([[1]])
37 self.D = numpy.matrix([[0]])
Philipp Schrader9ffe2982016-12-07 20:51:08 -080038
Tyler Chatow6738c362019-02-16 14:12:30 -080039 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
40 self.B_continuous, self.dt)
Philipp Schrader9ffe2982016-12-07 20:51:08 -080041
Tyler Chatow6738c362019-02-16 14:12:30 -080042 self.PlaceControllerPoles([0.01])
43 self.PlaceObserverPoles([0.01])
Philipp Schrader9ffe2982016-12-07 20:51:08 -080044
Tyler Chatow6738c362019-02-16 14:12:30 -080045 self.U_max = numpy.matrix([[12.0]])
46 self.U_min = numpy.matrix([[-12.0]])
47
48 self.InitializeState()