blob: bb8d74dc0be592b0ad5499e6062666987ee703c4 [file] [log] [blame]
Austin Schuh82162452022-02-07 22:01:45 -08001#!/usr/bin/python3
2
3from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
5import numpy
6import math
7import sys
8import math
9from y2022.control_loops.python import catapult_lib
10from matplotlib import pylab
11
12import gflags
13import glog
14
15FLAGS = gflags.FLAGS
16
17gflags.DEFINE_bool('plot', True, 'If true, plot the loop response.')
18
19ball_mass = 0.25
20ball_diameter = 9.5 * 0.0254
21lever = 17.5 * 0.0254
22
23G = (14.0 / 72.0) * (12.0 / 33.0)
24
25
26def AddResistance(motor, resistance):
27 motor.resistance += resistance
28 return motor
29
30J_ball = 1.5 * ball_mass * lever * lever
31# Assuming carbon fiber, calculate the mass of the bar.
32M_bar = (1750 * lever * 0.0254 * 0.0254 * (1.0 - (1 - 0.07)**2.0))
33# And the moment of inertia.
34J_bar = 1.0 / 3.0 * M_bar * lever**2.0
35
36# Do the same for a theoretical cup. Assume a 40 thou thick carbon cup.
37M_cup = (1750 * 0.0254 * 0.04 * 2 * math.pi * (ball_diameter / 2.)**2.0)
38J_cup = M_cup * lever**2.0 + M_cup * (ball_diameter / 2.)**2.0
39
40print("J ball", ball_mass * lever * lever)
41print("J bar", J_bar)
42print("bar mass", M_bar)
43print("J cup", J_cup)
44print("cup mass", M_cup)
45
46J = (J_ball + J_bar + J_cup * 1.5)
47print("J", J)
48
49kFinisher = catapult_lib.CatapultParams(
50 name='Finisher',
51 motor=AddResistance(control_loop.NMotor(control_loop.Falcon(), 2), 0.03),
52 G=G,
53 J=J,
54 lever=lever,
55 q_pos=0.01,
56 q_vel=10.0,
57 q_voltage=4.0,
58 r_pos=0.01,
59 controller_poles=[.93],
60 dt=0.0005)
61
62
63def main(argv):
64 # Do all our math with a lower voltage so we have headroom.
65 U = numpy.matrix([[9.0]])
66 print("For G:", G, " max speed ", catapult_lib.MaxSpeed(params=kFinisher, U=U, final_position = math.pi / 2.0))
67
68 if FLAGS.plot:
69 catapult_lib.PlotShot(kFinisher, U, final_position = math.pi / 4.0)
70
71 gs = []
72 speed = []
73 for i in numpy.linspace(0.01, 0.15, 150):
74 kFinisher.G = i
75 gs.append(kFinisher.G)
76 speed.append(catapult_lib.MaxSpeed(params=kFinisher, U=U, final_position = math.pi / 2.0))
77 pylab.plot(gs, speed, label = "max_speed")
78 pylab.show()
79 return 0
80
81
82if __name__ == '__main__':
83 argv = FLAGS(sys.argv)
84 sys.exit(main(argv))