/* [Gear Parameters] */ gear_module = 2.0; // [0.5:0.1:10.0] teeth_pinion = 20; // [6:1:100] teeth_gear = 40; // [10:1:150] pressure_angle = 20.0; // [14.5:0.5:25.0] face_width = 8.0; // [2.0:0.5:50.0] /* [Bore & Keyway] */ bore_diameter = 6.0; // [2.0:0.5:25.0] keyway_width = 2.0; // [0.5:0.5:10.0] keyway_depth = 1.2; // [0.2:0.1:5.0] /* [Display & Animation] */ rotation_angle = 0.0; // [0:1:360] show_pitch_circles = false; show_base_plate = false; pinion_color = "Gold"; gear_color = "DodgerBlue"; base_color = "LightSlateGray"; /* [Quality] */ $fn = 64; // --- Calculations --- rp1 = gear_module * teeth_pinion / 2; // Pitch radius 1 rp2 = gear_module * teeth_gear / 2; // Pitch radius 2 center_distance = rp1 + rp2; // Involute math helpers function inv(a) = tan(a) - (a * PI / 180); // Involute function (angle in degrees, result in radians) function inv_angle(inv_val) = // Newton-Raphson approximation for inv_angle in radians let( a0 = pow(3 * inv_val, 1/3), a1 = a0 - (tan(a0 * 180 / PI) - a0 - inv_val) / pow(tan(a0 * 180 / PI), 2), a2 = a1 - (tan(a1 * 180 / PI) - a1 - inv_val) / pow(tan(a1 * 180 / PI), 2), a3 = a2 - (tan(a2 * 180 / PI) - a2 - inv_val) / pow(tan(a2 * 180 / PI), 2) ) a3 * 180 / PI; // Single gear 2D profile module spur_gear_2d(teeth, m, alpha) { pitch_radius = m * teeth / 2; base_radius = pitch_radius * cos(alpha); addendum = 1.0 * m; dedendum = 1.25 * m; tip_radius = pitch_radius + addendum; root_radius = max(pitch_radius - dedendum, 0.1); // Half tooth thickness angle at pitch circle (in degrees) pitch_half_angle = (360 / (4 * teeth)); inv_alpha = (tan(alpha) - (alpha * PI / 180)) * 180 / PI; // Number of sample points along involute profile num_pts = 8; // Build single tooth profile points r_start = max(root_radius, base_radius); // Involute points for right flank right_flank = [ for (i = [0:num_pts]) let( r = r_start + (tip_radius - r_start) * (i / num_pts), alpha_r = acos(base_radius / r), inv_r = (tan(alpha_r) - (alpha_r * PI / 180)) * 180 / PI, theta = pitch_half_angle + inv_alpha - inv_r ) [r * cos(theta), r * sin(theta)] ]; // Root radial extension if root < base right_root = (root_radius < base_radius) ? [ let(theta_base = pitch_half_angle + inv_alpha) [root_radius * cos(theta_base), root_radius * sin(theta_base)] ] : []; // Left flank points (mirrored Y) left_flank = [ for (i = [num_pts:-1:0]) let( r = r_start + (tip_radius - r_start) * (i / num_pts), alpha_r = acos(base_radius / r), inv_r = (tan(alpha_r) - (alpha_r * PI / 180)) * 180 / PI, theta = pitch_half_angle + inv_alpha - inv_r ) [r * cos(-theta), r * sin(-theta)] ]; left_root = (root_radius < base_radius) ? [ let(theta_base = pitch_half_angle + inv_alpha) [root_radius * cos(-theta_base), root_radius * sin(-theta_base)] ] : []; tooth_pts = concat( [[root_radius * cos(0), 0]], right_root, right_flank, left_flank, left_root ); union() { // Root core circle circle(r = root_radius, $fn = teeth * 4); // Teeth array for (k = [0:teeth-1]) { rotate(k * 360 / teeth) polygon(tooth_pts); } } } // 2D Keyway bore cutout module bore_with_keyway_2d(d, kw, kd) { union() { circle(d = d, $fn = 48); translate([-kw / 2, d / 2 - 0.1]) square([kw, kd + 0.1]); } } // Complete 3D Spur Gear module spur_gear_solid(teeth, m, alpha, width, bore_d, kw, kd, gear_col) { color(gear_col) difference() { linear_extrude(height = width, center = true, convexity = 10) difference() { spur_gear_2d(teeth, m, alpha); bore_with_keyway_2d(bore_d, kw, kd); } // Chamfers on rim edges for realistic manufacturing look pitch_r = m * teeth / 2; tip_r = pitch_r + 1.0 * m; translate([0, 0, width / 2]) difference() { cylinder(r = tip_r + 2, h = 0.6, center = false); cylinder(r1 = tip_r - 0.6, r2 = tip_r, h = 0.61, center = false); } translate([0, 0, -width / 2 - 0.01]) difference() { cylinder(r = tip_r + 2, h = 0.6, center = false); cylinder(r1 = tip_r, r2 = tip_r - 0.6, h = 0.61, center = false); } } } // --- Assembly --- // Pinion (Gear 1: 20T) at Origin rotate([0, 0, rotation_angle]) spur_gear_solid( teeth = teeth_pinion, m = gear_module, alpha = pressure_angle, width = face_width, bore_d = bore_diameter, kw = keyway_width, kd = keyway_depth, gear_col = pinion_color ); // Driven Gear (Gear 2: 40T) at Center Distance // Gear ratio is 20/40 = 0.5. Rotation is opposite direction. // Offset phase ensures perfect tooth-in-gap mesh at contact point. gear2_mesh_phase = 180 / teeth_gear; // 4.5 deg shift to align tooth with gap translate([center_distance, 0, 0]) rotate([0, 0, 180 - (rotation_angle * (teeth_pinion / teeth_gear)) + gear2_mesh_phase]) spur_gear_solid( teeth = teeth_gear, m = gear_module, alpha = pressure_angle, width = face_width, bore_d = bore_diameter, kw = keyway_width, kd = keyway_depth, gear_col = gear_color ); // Optional Pitch Circle visualization (dashed/thin) if (show_pitch_circles) { color("LimeGreen") { translate([0, 0, face_width / 2 + 0.2]) difference() { cylinder(r = rp1 + 0.3, h = 0.2, center = true); cylinder(r = rp1 - 0.3, h = 0.3, center = true); } translate([center_distance, 0, face_width / 2 + 0.2]) difference() { cylinder(r = rp2 + 0.3, h = 0.2, center = true); cylinder(r = rp2 - 0.3, h = 0.3, center = true); } } } // Optional Base Plate with Shaft Pins if (show_base_plate) { color(base_color) { translate([center_distance / 2, 0, -face_width / 2 - 3]) difference() { hull() { cylinder(r = bore_diameter + 6, h = 3, center = true); translate([center_distance, 0, 0]) cylinder(r = bore_diameter + 6, h = 3, center = true); } // Mounting holes cylinder(d = 3, h = 5, center = true); translate([center_distance, 0, 0]) cylinder(d = 3, h = 5, center = true); } } }