Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
| 5 | import numpy |
| 6 | import math |
| 7 | import sys |
| 8 | import math |
| 9 | from y2022.control_loops.python import catapult_lib |
| 10 | from matplotlib import pylab |
| 11 | |
| 12 | import gflags |
| 13 | import glog |
| 14 | |
| 15 | FLAGS = gflags.FLAGS |
| 16 | |
| 17 | gflags.DEFINE_bool('plot', True, 'If true, plot the loop response.') |
| 18 | |
| 19 | ball_mass = 0.25 |
| 20 | ball_diameter = 9.5 * 0.0254 |
| 21 | lever = 17.5 * 0.0254 |
| 22 | |
| 23 | G = (14.0 / 72.0) * (12.0 / 33.0) |
| 24 | |
| 25 | |
| 26 | def AddResistance(motor, resistance): |
| 27 | motor.resistance += resistance |
| 28 | return motor |
| 29 | |
| 30 | J_ball = 1.5 * ball_mass * lever * lever |
| 31 | # Assuming carbon fiber, calculate the mass of the bar. |
| 32 | M_bar = (1750 * lever * 0.0254 * 0.0254 * (1.0 - (1 - 0.07)**2.0)) |
| 33 | # And the moment of inertia. |
| 34 | J_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. |
| 37 | M_cup = (1750 * 0.0254 * 0.04 * 2 * math.pi * (ball_diameter / 2.)**2.0) |
| 38 | J_cup = M_cup * lever**2.0 + M_cup * (ball_diameter / 2.)**2.0 |
| 39 | |
| 40 | print("J ball", ball_mass * lever * lever) |
| 41 | print("J bar", J_bar) |
| 42 | print("bar mass", M_bar) |
| 43 | print("J cup", J_cup) |
| 44 | print("cup mass", M_cup) |
| 45 | |
| 46 | J = (J_ball + J_bar + J_cup * 1.5) |
| 47 | print("J", J) |
| 48 | |
| 49 | kFinisher = 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 | |
| 63 | def 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 | |
| 82 | if __name__ == '__main__': |
| 83 | argv = FLAGS(sys.argv) |
| 84 | sys.exit(main(argv)) |