blob: d40ec8ae07a13e786b29016dc1a94022e28bfece [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 proximal arm.
4# Run it from the command line and it displays the time required
5# to move the proximal arm 180 degrees from straight down to straight up.
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
50def get_distal_omega(omega_proximal):
51 # For the proximal angle = -50 degrees, the distal angle is -180 degrees
52 # For the proximal angle = 10 degrees, the distal angle is -90 degrees
53 distal_angle = omega_proximal*( (180.0-90.0) / (50.0+10.0) )
54 return distal_angle
55
56
57def get_180_degree_time(c1,c2,c3,voltage,gear_ratio,motor_free_speed):
58 #print ("# step time theta angular_speed angular_acceleration theta angular_speed motor_speed motor_speed_fraction")
59 #print ("# (sec) (rad) (rad/sec) (rad/sec^2) (rotations) (rotations/sec) (rpm) (fraction)")
60 global run_count
61 global theta_travel
62
63 if ( False ):
64 # Gravity is assisting the motion.
65 theta_start = 0.0
66 theta_target = pi
67 elif ( False ):
68 # Gravity is assisting the motion.
69 theta_start = 0.0
70 theta_target = -pi
71 elif ( False ):
72 # Gravity is slowing the motion.
73 theta_start = pi
74 theta_target = 0.0
75 elif ( False ):
76 # Gravity is slowing the motion.
77 theta_start = -pi
78 theta_target = 0.0
79 elif ( True ):
80 # This is for the proximal arm motion.
81 theta_start = to_rad(-50.0)
82 theta_target = to_rad(10.0)
83
84 theta_half = 0.5*(theta_start + theta_target)
85 if (theta_start > theta_target):
86 voltage = -voltage
87 theta = theta_start
88 theta_travel = theta_start - theta_target
89 if ( run_count == 0 ):
90 print ("# Theta Start = %.2f radians End = %.2f Theta travel %.2f Theta half = %.2f Voltage = %.2f" % (theta_start,theta_target,theta_travel,theta_half, voltage))
91 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))
92 omega = 0.0
93 time = 0.0
94 delta_time = 0.01 # time step in seconds
95 for step in range(1, 5000):
96 t = numpy.array([time, time + delta_time])
97 time = time + delta_time
98 x = [theta, omega]
99 angular_acceleration = -c1*omega + c2*voltage
100 x_n_plus_1 = scipy.integrate.odeint(time_derivative,x,t,args=(voltage,c1,c2,c3))
101 #print ('x_n_plus_1 = ',x_n_plus_1)
102 #print ('repr(x_n_plus_1) = ',repr(x_n_plus_1))
103 theta, omega = x_n_plus_1[1]
104 #theta= x_n_plus_1[0]
105 #omega = x_n_plus_1[1]
106 if ( False ):
107 print ("%4d %8.4f %8.2f %8.4f %8.4f %8.3f %8.3f %8.3f %8.3f" % \
108 (step, time, theta, omega, angular_acceleration, to_rotations(theta), \
109 to_rotations(omega), omega*gear_ratio*60.0/pi2, omega*gear_ratio/motor_free_speed ))
110 if (theta_start < theta_target):
111 # Angle is increasing through the motion.
112 if (theta > theta_half):
113 break
114 else:
115 # Angle is decreasing through the motion.
116 if (theta < theta_half):
117 break
118
119 #print ("# step time theta angular_speed angular_acceleration theta angular_speed motor_speed motor_speed_fraction")
120 #print ("# (sec) (rad) (rad/sec) (rad/sec^2) (rotations) (rotations/sec) (rpm) (fraction)")
121 #print ("# Total time for 1/2 rotation of arm is %0.2f" % (time*2))
122 return (2.0*time)
123
124def main():
125 global run_count
126 gravity = 9.8 # m/sec^2 Gravity Constant
127 voltage_nominal = 12 # Volts
128
129 # Vex 775 Pro motor specs from http://banebots.com/p/M2-RS550-120
130 motor_name = "Vex 775 Pro motor specs from http://banebots.com/p/M2-RS550-120"
131 current_stall = 134 # amps stall current
132 current_no_load = 0.7 # amps no load current
133 torque_stall = 710/1000.0 # N-m Stall Torque
134 speed_no_load_rpm = 18730 # RPM no load speed
135
136 if ( False ):
137 # Bag motor from https://www.vexrobotics.com/217-3351.html
138 motor_name = "Bag motor from https://www.vexrobotics.com/217-3351.html"
139 current_stall = 53.0 # amps stall current
140 current_no_load = 1.8 # amps no load current
141 torque_stall = 0.4 # N-m Stall Torque
142 speed_no_load_rpm = 13180.0 # RPM no load speed
143
144 if ( True ):
145 # Mini CIM motor from https://www.vexrobotics.com/217-3371.html
146 motor_name = "Mini CIM motor from https://www.vexrobotics.com/217-3371.html"
147 current_stall = 89.0 # amps stall current
148 current_no_load = 3.0 # amps no load current
149 torque_stall = 1.4 # N-m Stall Torque
150 speed_no_load_rpm = 5840.0 # RPM no load speed
151
152 # How many motors are we using?
153 num_motors = 1
154
155 # Motor values
156 print ("# Motor: %s" % (motor_name))
157 print ("# Number of motors: %d" % (num_motors))
158 print ("# Stall torque: %.1f n-m" % (torque_stall))
159 print ("# Stall current: %.1f amps" % (current_stall))
160 print ("# No load current: %.1f amps" % (current_no_load))
161 print ("# No load speed: %.0f rpm" % (speed_no_load_rpm))
162
163 # Constants from motor values
164 resistance_motor = voltage_nominal/current_stall
165 speed_no_load_rps = speed_no_load_rpm/60.0 # Revolutions per second no load speed
166 speed_no_load = speed_no_load_rps*2.0*pi
167 Kt = num_motors*torque_stall/current_stall # N-m/A torque constant
168 Kv_rpm = speed_no_load_rpm /(voltage_nominal - resistance_motor*current_no_load) # rpm/V
169 Kv = Kv_rpm*2.0*pi/60.0 # rpm/V
170
171 # Robot Geometry and physics
172 length_proximal_arm = inches_to_meters*47.34 # m Length of arm connected to the robot base
173 length_distal_arm = inches_to_meters*44.0 # m Length of arm that holds the cube
174 mass_cube = 6.0*lbs_to_kg # Weight of the cube in Kgrams
175 mass_proximal_arm = 5.5*lbs_to_kg # Weight of proximal arm
176 mass_distal_arm = 3.5*lbs_to_kg # Weight of distal arm
177 mass_distal = mass_cube + mass_distal_arm
178 mass_proximal = mass_proximal_arm + mass_distal
179 radius_to_proximal_arm_cg = 22.0*inches_to_meters # m Length from arm pivot point to arm CG
180 radius_to_distal_arm_cg = 10.0*inches_to_meters # m Length from arm pivot point to arm CG
181
182 radius_to_distal_cg = ( length_distal_arm*mass_cube + radius_to_distal_arm_cg*mass_distal_arm)/mass_distal
183 radius_to_proximal_cg = ( length_proximal_arm*mass_distal + radius_to_proximal_arm_cg*mass_proximal_arm)/mass_proximal
184 J_cube = length_distal_arm*length_distal_arm*mass_cube
185 # Kg m^2 Moment of inertia of the proximal arm
186 J_proximal_arm = radius_to_proximal_arm_cg*radius_to_proximal_arm_cg*mass_distal_arm
187 # Kg m^2 Moment of inertia distal arm and cube at end of proximal arm.
188 J_distal_arm_and_cube_at_end_of_proximal_arm = length_proximal_arm*length_proximal_arm*mass_distal
189 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
190 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
191
192 error_margine = 1.0
193 voltage = 10.0 # voltage for the motor. Assuming a loaded robot so not using 12 V.
194 # It might make sense to use a lower motor frees peed when the voltage is not a full 12 Volts.
195 # motor_free_speed = Kv*voltage
196 motor_free_speed = speed_no_load
197
198 print ("# Kt = %f N-m/A\n# Kv_rpm = %f rpm/V\n# Kv = %f radians/V" % (Kt, Kv_rpm, Kv))
199 print ("# %.2f Ohms Resistance of the motor " % (resistance_motor))
200 print ("# %.2f kg Cube weight" % (mass_cube))
201 print ("# %.2f kg Proximal Arm mass" % (mass_proximal_arm))
202 print ("# %.2f kg Distal Arm mass" % (mass_distal_arm))
203 print ("# %.2f kg Distal Arm and Cube weight" % (mass_distal))
204 print ("# %.2f m Length from distal arm pivot point to arm CG" % (radius_to_distal_arm_cg))
205 print ("# %.2f m Length from distal arm pivot point to arm and cube cg" % (radius_to_distal_cg))
206 print ("# %.2f kg-m^2 Moment of inertia of the cube about the arm pivot point" % (J_cube))
207 print ("# %.2f m Length from proximal arm pivot point to arm CG" % (radius_to_proximal_arm_cg))
208 print ("# %.2f m Length from proximal arm pivot point to arm and cube cg" % (radius_to_proximal_cg))
209 print ("# %.2f m Proximal arm length" % (length_proximal_arm))
210 print ("# %.2f m Distal arm length" % (length_distal_arm))
211
212 print ("# %.2f kg-m^2 Moment of inertia of the distal arm about the arm pivot point" % (J_distal_arm))
213 print ("# %.2f kg-m^2 Moment of inertia of the proximal arm about the arm pivot point" % (J_proximal_arm))
214 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))
215 print ("# %.2f kg-m^2 Moment of inertia of the proximal arm and distal arm and cube about the arm pivot point" % (J))
216 print ("# %d Number of motors" % (num_motors))
217
218 print ("# %.2f V Motor voltage" % (voltage))
219
220 print ("\n# Min time is for proximal arm without any forces from distal arm. Max time puts all distal arm mass at the end of proximal arm.")
221
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 c1_proximal_only = Kt*gear_ratio*gear_ratio/(Kv*resistance_motor*J_proximal_arm)
227 c2_proximal_only = gear_ratio*Kt/(J_proximal_arm*resistance_motor)
228 c3_proximal_only = radius_to_proximal_arm_cg*mass_proximal_arm*gravity/J_proximal_arm
229
230 if ( False and run_count < 1 ):
231 print ("# %.8f 1/sec C1 constant" % (c1))
232 print ("# %.2f 1/sec C2 constant" % (c2))
233 print ("# %.2f 1/(V sec^2) C3 constant" % (c3))
234 print ("# %.8f 1/sec C1 proximal only constant" % (c1_proximal_only))
235 print ("# %.2f 1/sec C2 proximal only constant" % (c2_proximal_only))
236 print ("# %.2f 1/(V sec^2) C3 proximal only constant" % (c3_proximal_only))
237 print ("# %.2f RPM Free speed at motor voltage" % (voltage*Kv_rpm))
238
239 torque_90_degrees = radius_to_proximal_cg*mass_proximal*gravity
240 voltage_90_degrees = resistance_motor*torque_90_degrees/(gear_ratio*Kt)
241 torque_peak = gear_ratio*num_motors*torque_stall
242 torque_peak_ft_lbs = torque_peak * newton_meters_to_ft_lbs
243 normal_force = torque_peak/length_proximal_arm
244 normal_force_lbf = newton_to_lbf*normal_force
245 normal_distal_arm_end_force = torque_peak/(length_proximal_arm + length_distal_arm)
246 normal_distal_arm_end_force_lbf = newton_to_lbf*normal_distal_arm_end_force
247 time_required = get_180_degree_time(c1,c2,c3,voltage,gear_ratio,motor_free_speed)
248 time_required_proximal_only = get_180_degree_time(c1_proximal_only,c2_proximal_only,c3_proximal_only,voltage,gear_ratio,motor_free_speed)
249 print ("Time for %.1f degrees for gear ratio %3.0f is %.2f (min) %.2f (max) seconds. 90 degree torque %.1f N-m and voltage %.1f Volts. Peak torque %3.0f nm %3.0f ft-lb Normal force at proximal end %3.0f N %2.0f lbf distal end %3.0f N %2.0f lbf" % \
250 (to_deg(theta_travel),gear_ratio,time_required_proximal_only,time_required,torque_90_degrees,voltage_90_degrees,
251 torque_peak,torque_peak_ft_lbs,normal_force,normal_force_lbf,normal_distal_arm_end_force,normal_distal_arm_end_force_lbf))
252
253if __name__ == '__main__':
254 main()