blob: d75d43831241c6b5899ebd45c7327a1d8ac7f31f [file] [log] [blame]
Michael Schuh10dd1e02018-01-20 13:19:44 -08001#!/usr/bin/python3
2
3# This code was used to select the gear ratio for the intake.
4# Run it from the command line and it displays the time required
5# to rotate the intake 180 degrees.
6#
7# Michael Schuh
8# January 20, 2018
9
10import math
11import numpy
12import scipy.integrate
13
14# apt-get install python-scipy python3-scipy python-numpy python3-numpy
15
16pi = math.pi
17pi2 = 2.0*pi
18rad_to_deg = 180.0/pi
19inches_to_meters = 0.0254
20lbs_to_kg = 1.0/2.2
21newton_to_lbf = 0.224809
22newton_meters_to_ft_lbs = 0.73756
23run_count = 0
24theta_travel = 0.0
25
26def to_deg(angle):
27 return (angle*rad_to_deg)
28
29def to_rad(angle):
30 return (angle/rad_to_deg)
31
32def to_rotations(angle):
33 return (angle/pi2)
34
35def time_derivative(x, t, voltage, c1, c2, c3):
36 global run_count
37 theta, omega = x
38 dxdt = [omega, -c1*omega + c3*math.sin(theta) + c2*voltage]
39 run_count = run_count + 1
40
41 #print ('dxdt = ',dxdt,' repr(dxdt) = ', repr(dxdt))
42 return dxdt
43
44def get_distal_angle(theta_proximal):
45 # For the proximal angle = -50 degrees, the distal angle is -180 degrees
46 # For the proximal angle = 10 degrees, the distal angle is -90 degrees
47 distal_angle = to_rad(-180.0 - (-50.0-to_deg(theta_proximal))*(180.0-90.0)/(50.0+10.0))
48 return distal_angle
49
50
51def get_180_degree_time(c1,c2,c3,voltage,gear_ratio,motor_free_speed):
52 #print ("# step time theta angular_speed angular_acceleration theta angular_speed motor_speed motor_speed_fraction")
53 #print ("# (sec) (rad) (rad/sec) (rad/sec^2) (rotations) (rotations/sec) (rpm) (fraction)")
54 global run_count
55 global theta_travel
56
57 if ( True ):
58 # Gravity is assisting the motion.
59 theta_start = 0.0
60 theta_target = pi
61 elif ( False ):
62 # Gravity is assisting the motion.
63 theta_start = 0.0
64 theta_target = -pi
65 elif ( False ):
66 # Gravity is slowing the motion.
67 theta_start = pi
68 theta_target = 0.0
69 elif ( False ):
70 # Gravity is slowing the motion.
71 theta_start = -pi
72 theta_target = 0.0
73 elif ( False ):
74 # This is for the proximal arm motion.
75 theta_start = to_rad(-50.0)
76 theta_target = to_rad(10.0)
77
78 theta_half = 0.5*(theta_start + theta_target)
79 if (theta_start > theta_target):
80 voltage = -voltage
81 theta = theta_start
82 theta_travel = theta_start - theta_target
83 if ( run_count == 0 ):
84 print ("# Theta Start = %.2f radians End = %.2f Theta travel %.2f Theta half = %.2f Voltage = %.2f" % (theta_start,theta_target,theta_travel,theta_half, voltage))
85 print ("# Theta Start = %.2f degrees End = %.2f Theta travel %.2f Theta half = %.2f Voltage = %.2f" % (to_deg(theta_start),to_deg(theta_target),to_deg(theta_travel),to_deg(theta_half), voltage))
86 omega = 0.0
87 time = 0.0
88 delta_time = 0.01 # time step in seconds
89 for step in range(1, 5000):
90 t = numpy.array([time, time + delta_time])
91 time = time + delta_time
92 x = [theta, omega]
93 angular_acceleration = -c1*omega + c2*voltage
94 x_n_plus_1 = scipy.integrate.odeint(time_derivative,x,t,args=(voltage,c1,c2,c3))
95 #print ('x_n_plus_1 = ',x_n_plus_1)
96 #print ('repr(x_n_plus_1) = ',repr(x_n_plus_1))
97 theta, omega = x_n_plus_1[1]
98 #theta= x_n_plus_1[0]
99 #omega = x_n_plus_1[1]
100 if ( False ):
101 print ("%4d %8.4f %8.2f %8.4f %8.4f %8.3f %8.3f %8.3f %8.3f" % \
102 (step, time, theta, omega, angular_acceleration, to_rotations(theta), \
103 to_rotations(omega), omega*gear_ratio*60.0/pi2, omega*gear_ratio/motor_free_speed ))
104 if (theta_start < theta_target):
105 # Angle is increasing through the motion.
106 if (theta > theta_half):
107 break
108 else:
109 # Angle is decreasing through the motion.
110 if (theta < theta_half):
111 break
112
113 #print ("# step time theta angular_speed angular_acceleration theta angular_speed motor_speed motor_speed_fraction")
114 #print ("# (sec) (rad) (rad/sec) (rad/sec^2) (rotations) (rotations/sec) (rpm) (fraction)")
115 #print ("# Total time for 1/2 rotation of arm is %0.2f" % (time*2))
116 return (2.0*time)
117
118def main():
119 gravity = 9.8 # m/sec^2 Gravity Constant
120 gravity = 0.0 # m/sec^2 Gravity Constant - Use 0.0 for the intake. It is horizontal.
121 voltage_nominal = 12 # Volts
122
123 # Vex 775 Pro motor specs from http://banebots.com/p/M2-RS550-120
124 motor_name = "Vex 775 Pro motor specs from http://banebots.com/p/M2-RS550-120"
125 current_stall = 134 # amps stall current
126 current_no_load = 0.7 # amps no load current
127 torque_stall = 710/1000.0 # N-m Stall Torque
128 speed_no_load_rpm = 18730 # RPM no load speed
129
130 if ( True ):
131 # Bag motor from https://www.vexrobotics.com/217-3351.html
132 motor_name = "Bag motor from https://www.vexrobotics.com/217-3351.html"
133 current_stall = 53.0 # amps stall current
134 current_no_load = 1.8 # amps no load current
135 torque_stall = 0.4 # N-m Stall Torque
136 speed_no_load_rpm = 13180.0 # RPM no load speed
137
138 if ( False ):
139 # Mini CIM motor from https://www.vexrobotics.com/217-3371.html
140 motor_name = "Mini CIM motor from https://www.vexrobotics.com/217-3371.html"
141 current_stall = 89.0 # amps stall current
142 current_no_load = 3.0 # amps no load current
143 torque_stall = 1.4 # N-m Stall Torque
144 speed_no_load_rpm = 5840.0 # RPM no load speed
145
146 # How many motors are we using?
147 num_motors = 1
148
149 # Motor values
150 print ("# Motor: %s" % (motor_name))
151 print ("# Number of motors: %d" % (num_motors))
152 print ("# Stall torque: %.1f n-m" % (torque_stall))
153 print ("# Stall current: %.1f amps" % (current_stall))
154 print ("# No load current: %.1f amps" % (current_no_load))
155 print ("# No load speed: %.0f rpm" % (speed_no_load_rpm))
156
157 # Constants from motor values
158 resistance_motor = voltage_nominal/current_stall
159 speed_no_load_rps = speed_no_load_rpm/60.0 # Revolutions per second no load speed
160 speed_no_load = speed_no_load_rps*2.0*pi
161 Kt = num_motors*torque_stall/current_stall # N-m/A torque constant
162 Kv_rpm = speed_no_load_rpm /(voltage_nominal - resistance_motor*current_no_load) # rpm/V
163 Kv = Kv_rpm*2.0*pi/60.0 # rpm/V
164
165 # Robot Geometry and physics
166 length_proximal_arm = inches_to_meters*47.34 # m Length of arm connected to the robot base
167 length_distal_arm = inches_to_meters*44.0 # m Length of arm that holds the cube
168 length_intake_arm = inches_to_meters*9.0 # m Length of intake arm from the pivot point to where the big roller contacts a cube.
169 mass_cube = 6.0*lbs_to_kg # Weight of the cube in Kgrams
170 mass_proximal_arm = 5.5*lbs_to_kg # Weight of proximal arm
171 mass_distal_arm = 3.5*lbs_to_kg # Weight of distal arm
172 mass_distal = mass_cube + mass_distal_arm
173 mass_proximal = mass_proximal_arm + mass_distal
174 radius_to_proximal_arm_cg = 22.0*inches_to_meters # m Length from arm pivot point to arm CG
175 radius_to_distal_arm_cg = 10.0*inches_to_meters # m Length from arm pivot point to arm CG
176
177 radius_to_distal_cg = ( length_distal_arm*mass_cube + radius_to_distal_arm_cg*mass_distal_arm)/mass_distal
178 radius_to_proximal_cg = ( length_proximal_arm*mass_distal + radius_to_proximal_arm_cg*mass_proximal_arm)/mass_proximal
179 J_cube = length_distal_arm*length_distal_arm*mass_cube
180 # Kg m^2 Moment of inertia of the proximal arm
181 J_proximal_arm = radius_to_proximal_arm_cg*radius_to_proximal_arm_cg*mass_distal_arm
182 # Kg m^2 Moment of inertia distal arm and cube at end of proximal arm.
183 J_distal_arm_and_cube_at_end_of_proximal_arm = length_proximal_arm*length_proximal_arm*mass_distal
184 J_distal_arm = radius_to_distal_arm_cg*radius_to_distal_arm_cg*mass_distal_arm # Kg m^2 Moment of inertia of the distal arm
185 J = J_distal_arm_and_cube_at_end_of_proximal_arm + J_proximal_arm # Moment of inertia of the arm with the cube on the end
186 # Intake claw
187 J_intake = 0.295 # Kg m^2 Moment of inertia of intake
188 J = J_intake
189
190 gear_ratio = 140.0 # Guess at the gear ratio
191 gear_ratio = 100.0 # Guess at the gear ratio
192 gear_ratio = 90.0 # Guess at the gear ratio
193
194 error_margine = 1.0
195 voltage = 10.0 # voltage for the motor. Assuming a loaded robot so not using 12 V.
196 # It might make sense to use a lower motor frees peed when the voltage is not a full 12 Volts.
197 # motor_free_speed = Kv*voltage
198 motor_free_speed = speed_no_load
199
200 print ("# Kt = %f N-m/A\n# Kv_rpm = %f rpm/V\n# Kv = %f radians/V" % (Kt, Kv_rpm, Kv))
201 print ("# %.2f Ohms Resistance of the motor " % (resistance_motor))
202 print ("# %.2f kg Cube weight" % (mass_cube))
203 print ("# %.2f kg Proximal Arm mass" % (mass_proximal_arm))
204 print ("# %.2f kg Distal Arm mass" % (mass_distal_arm))
205 print ("# %.2f kg Distal Arm and Cube weight" % (mass_distal))
206 print ("# %.2f m Length from distal arm pivot point to arm CG" % (radius_to_distal_arm_cg))
207 print ("# %.2f m Length from distal arm pivot point to arm and cube cg" % (radius_to_distal_cg))
208 print ("# %.2f kg-m^2 Moment of inertia of the cube about the arm pivot point" % (J_cube))
209 print ("# %.2f m Length from proximal arm pivot point to arm CG" % (radius_to_proximal_arm_cg))
210 print ("# %.2f m Length from proximal arm pivot point to arm and cube cg" % (radius_to_proximal_cg))
211 print ("# %.2f m Proximal arm length" % (length_proximal_arm))
212 print ("# %.2f m Distal arm length" % (length_distal_arm))
213
214 print ("# %.2f kg-m^2 Moment of inertia of the intake about the intake pivot point" % (J_intake))
215 print ("# %.2f kg-m^2 Moment of inertia of the distal arm about the arm pivot point" % (J_distal_arm))
216 print ("# %.2f kg-m^2 Moment of inertia of the proximal arm about the arm pivot point" % (J_proximal_arm))
217 print ("# %.2f kg-m^2 Moment of inertia of the distal arm and cube mass about the proximal arm pivot point" % (J_distal_arm_and_cube_at_end_of_proximal_arm))
218 print ("# %.2f kg-m^2 Moment of inertia of the intake the intake pivot point (J value used in simulation)" % (J))
219 print ("# %d Number of motors" % (num_motors))
220
221 print ("# %.2f V Motor voltage" % (voltage))
222 for gear_ratio in range(60, 241, 10):
223 c1 = Kt*gear_ratio*gear_ratio/(Kv*resistance_motor*J)
224 c2 = gear_ratio*Kt/(J*resistance_motor)
225 c3 = radius_to_proximal_cg*mass_proximal*gravity/J
226
227 if ( False ):
228 print ("# %.8f 1/sec C1 constant" % (c1))
229 print ("# %.2f 1/sec C2 constant" % (c2))
230 print ("# %.2f 1/(V sec^2) C3 constant" % (c3))
231 print ("# %.2f RPM Free speed at motor voltage" % (voltage*Kv_rpm))
232
233 torque_90_degrees = radius_to_distal_cg*mass_distal*gravity
234 voltage_90_degrees = resistance_motor*torque_90_degrees/(gear_ratio*Kt)
235 torque_peak = gear_ratio*num_motors*torque_stall
236 torque_peak_ft_lbs = torque_peak * newton_meters_to_ft_lbs
237 normal_force = torque_peak/length_intake_arm
238 normal_force_lbf = newton_to_lbf*normal_force
239 time_required = get_180_degree_time(c1,c2,c3,voltage,gear_ratio,motor_free_speed)
240 print ("Time for %.1f degrees for gear ratio %3.0f is %.2f seconds. Peak (stall) torque %3.0f nm %3.0f ft-lb Normal force at intake end %3.0f N %2.0f lbf" % \
241 (to_deg(theta_travel),gear_ratio,time_required,
242 torque_peak,torque_peak_ft_lbs,normal_force,normal_force_lbf))
243
244if __name__ == '__main__':
245 main()