Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 2 | |
| 3 | from frc971.control_loops.python import control_loop |
Maxwell Henderson | 3424299 | 2024-01-07 12:39:11 -0800 | [diff] [blame] | 4 | from frc971.control_loops.python import flywheel |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 5 | import numpy |
| 6 | |
| 7 | import sys |
| 8 | |
| 9 | import gflags |
| 10 | import glog |
| 11 | |
| 12 | FLAGS = gflags.FLAGS |
| 13 | |
| 14 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
| 15 | |
Austin Schuh | c1c957a | 2020-02-20 17:47:58 -0800 | [diff] [blame] | 16 | # Gear ratio to the final wheel. |
| 17 | # 40 tooth on the flywheel |
| 18 | # 48 for the falcon. |
| 19 | # 60 tooth on the outer wheel. |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 20 | G = 44.0 / 40.0 |
Austin Schuh | c1c957a | 2020-02-20 17:47:58 -0800 | [diff] [blame] | 21 | # Overall flywheel inertia. |
Austin Schuh | 0ad31d7 | 2021-03-06 17:07:04 -0800 | [diff] [blame] | 22 | J = 0.00507464 |
Austin Schuh | eb240f6 | 2021-11-07 19:57:06 -0800 | [diff] [blame] | 23 | J = 0.0035 |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 24 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 25 | |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 26 | def AddResistance(motor, resistance): |
| 27 | motor.resistance += resistance |
| 28 | return motor |
| 29 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 30 | |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 31 | def ScaleKv(motor, scale): |
| 32 | motor.Kv *= scale |
| 33 | return motor |
Austin Schuh | c1c957a | 2020-02-20 17:47:58 -0800 | [diff] [blame] | 34 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 35 | |
Austin Schuh | c1c957a | 2020-02-20 17:47:58 -0800 | [diff] [blame] | 36 | # The position and velocity are measured for the final wheel. |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 37 | kFinisher = flywheel.FlywheelParams( |
| 38 | name='Finisher', |
Austin Schuh | 5c40ea4 | 2021-09-26 13:28:03 -0700 | [diff] [blame] | 39 | motor=AddResistance(control_loop.NMotor(control_loop.Falcon(), 2), 0.03), |
Austin Schuh | c1c957a | 2020-02-20 17:47:58 -0800 | [diff] [blame] | 40 | G=G, |
| 41 | J=J, |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 42 | q_pos=0.01, |
Austin Schuh | 8047677 | 2021-03-06 20:17:36 -0800 | [diff] [blame] | 43 | q_vel=10.0, |
| 44 | q_voltage=4.0, |
| 45 | r_pos=0.01, |
Austin Schuh | eb240f6 | 2021-11-07 19:57:06 -0800 | [diff] [blame] | 46 | controller_poles=[.93]) |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 47 | |
| 48 | |
| 49 | def main(argv): |
| 50 | if FLAGS.plot: |
Austin Schuh | c1c957a | 2020-02-20 17:47:58 -0800 | [diff] [blame] | 51 | R = numpy.matrix([[0.0], [500.0], [0.0]]) |
Austin Schuh | 43b9ae9 | 2020-02-29 23:08:38 -0800 | [diff] [blame] | 52 | flywheel.PlotSpinup(params=kFinisher, goal=R, iterations=400) |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 53 | return 0 |
| 54 | |
| 55 | if len(argv) != 5: |
| 56 | glog.fatal('Expected .h file name and .cc file name') |
| 57 | else: |
| 58 | namespaces = ['y2020', 'control_loops', 'superstructure', 'finisher'] |
| 59 | flywheel.WriteFlywheel(kFinisher, argv[1:3], argv[3:5], namespaces) |
| 60 | |
| 61 | |
| 62 | if __name__ == '__main__': |
| 63 | argv = FLAGS(sys.argv) |
Austin Schuh | eb240f6 | 2021-11-07 19:57:06 -0800 | [diff] [blame] | 64 | glog.init() |
Sabina Davis | edf8947 | 2020-02-17 15:27:37 -0800 | [diff] [blame] | 65 | sys.exit(main(argv)) |