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