Skip to content

Instantly share code, notes, and snippets.

@SharpCoder
Last active December 5, 2024 15:15
Show Gist options
  • Select an option

  • Save SharpCoder/b1cab020d2d8d7626d8556bdac7069cd to your computer and use it in GitHub Desktop.

Select an option

Save SharpCoder/b1cab020d2d8d7626d8556bdac7069cd to your computer and use it in GitHub Desktop.

Revisions

  1. SharpCoder renamed this gist Jan 28, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. SharpCoder revised this gist Jan 28, 2020. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    # gears.scad

    ![picture of the openscad output](https://i.imgur.com/W8r2YJl.png "A gear generated with this script")

    In order to use this code, you will need to copy gears.scad to your Libraries folder.
    https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Libraries

    @@ -31,4 +29,7 @@ Which takes the following parameters:
    - N2 = Tooth count of the second gear
    - P = Diametral Pitch

    The output of this function will be the mm distance you must space both gears for them to mesh perfectly.
    The output of this function will be the mm distance you must space both gears for them to mesh perfectly.


    ![picture of the openscad output](https://i.imgur.com/W8r2YJl.png "A gear generated with this script")
  3. SharpCoder revised this gist Jan 28, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    # gears.scad

    ![picture of the openscad output](https://i.imgur.com/W8r2YJl.png "A gear generated with this script")

    In order to use this code, you will need to copy gears.scad to your Libraries folder.
    https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Libraries

  4. SharpCoder renamed this gist Jan 25, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. SharpCoder renamed this gist Jan 25, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. SharpCoder renamed this gist Jan 25, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. SharpCoder revised this gist Jan 25, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Untitled.scad
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    include <gears.scad>

    Teeth = 20;
    Teeth = 18;
    Pitch = 12;

    linear_extrude(6)
  8. SharpCoder revised this gist Jan 25, 2020. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,28 @@ https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Libraries

    And then you can run *Untitled.scad* as any other normal scad project file.

    If you don't want to mess around with the library, just combine the two code files together into one.
    If you don't want to mess around with the library, just combine the two code files together into one.

    ## Library Functions

    This code will provide a function

    **spur_gear()**

    Which takes the following parameters:
    - N = Number of teeth
    - P = Diametral Pitch (defaults to 12)
    - pa = Pressure Angle (defaults to 14.5)

    The output of this method will be a 2D gear (converted to mm) which you simply need to *linear_extrude* in order to print on a 3D printer.

    The library also includes a number of other mathematical functions including:

    **calc_center_distance()**

    Which takes the following parameters:
    - N1 = Tooth count of the first gear
    - N2 = Tooth count of the second gear
    - P = Diametral Pitch

    The output of this function will be the mm distance you must space both gears for them to mesh perfectly.
  9. SharpCoder revised this gist Jan 25, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # gears.scad

    In order to use this code, you will need to copy gears.scad to your Libraries folder.
    https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Libraries

    And then you can run *Untitled.scad* as any other normal scad project file.

    If you don't want to mess around with the library, just combine the two code files together into one.
  10. SharpCoder revised this gist Jan 25, 2020. 2 changed files with 29 additions and 20 deletions.
    10 changes: 6 additions & 4 deletions Untitled.scad
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,16 @@ include <gears.scad>
    Teeth = 20;
    Pitch = 12;

    linear_extrude(5)
    linear_extrude(6)
    difference() {
    spur_gear(N=Teeth, P=Pitch);
    circle(d=19.1);
    }

    // Calculate distance between another gear (may need to add 1mm tolerance)
    OtherGearTeeth = 34;
    // Change this variable to calculate how far
    // this gear and another gear need to be spaced
    // in order to mesh properly.
    // (output will display in the console in units mm)
    OtherGearTeeth = 37;
    echo("***");
    echo("center distance (mm)", calc_center_distance(Teeth, OtherGearTeeth, Pitch));
    echo("***");
    39 changes: 23 additions & 16 deletions gears.scad
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ function calc_circular_pitch(P) = (PI / P) * in_to_mm;
    function calc_thickness(P) = (1.5708 / P) * in_to_mm;
    function calc_alpha(dp, db, pa) = ((sqrt(pow(dp,2) - pow(db,2))/db) * rad_to_deg - pa);
    function calc_clearance(P) = calc_dedendum(P) - calc_addendum(P);
    function calc_center_distance(N1, N2, P) = in_to_mm * (N1 + N2) / (2 * P);
    function calc_center_distance(N1, N2, P) = in_to_mm * (N1 + N2) /(2 * P);

    /**
    Modules
    @@ -44,6 +44,7 @@ module spur_gear(N, P = 12, pa = 14.5) {
    db = calc_db(N, P, pa);
    dr = calc_dr(N, P);
    a = calc_addendum(P);
    b = calc_dedendum(P);
    c = calc_clearance(P);
    p = calc_circular_pitch(P);

    @@ -57,30 +58,36 @@ module spur_gear(N, P = 12, pa = 14.5) {
    beta = ((360 / (4*N)) - alpha) * 2;

    module involute_tooth() {
    x = function(t) (r * cos(t*rad_to_deg)) + (r * t * sin(t*rad_to_deg));
    y = function(t) (r * sin(t*rad_to_deg)) - (r * t * cos(t*rad_to_deg));
    x = function(t) (r * (cos(t*rad_to_deg) + t * sin(t*rad_to_deg)));
    y = function(t) (r * (sin(t * rad_to_deg) - t * cos(t * rad_to_deg)));
    x2 = function(t) r * (cos(-t*rad_to_deg - beta) - t * sin(-t * rad_to_deg - beta));
    y2 = function(t) r * (sin(-t*rad_to_deg - beta) + t * cos(-t * rad_to_deg - beta));

    involute_1_points = parametric_points(fx=x, fy=y, t1=1);
    involute_2_points = parametric_points(fx=x2, fy=y2, t1=1);
    involute_1_points = parametric_points(fx=x, fy=y, t1=.68);
    involute_2_points = parametric_points(fx=x2, fy=y2, t1=.68);

    union() {
    polygon(
    concat(
    involute_1_points,
    reverse(involute_2_points)
    )
    );
    difference() {
    union() {
    polygon(
    concat(
    [[ 0, 0 ]],
    involute_1_points,
    reverse(involute_2_points),
    [[ 0, 0]]
    )
    );
    }

    // Use subtraction to extend the invlute curve towards the base
    // circle and then stop it at that point. This will
    // add some square-shaped space at the base of the tooth
    // NOTE: usage of undercut might be overkill.
    circle(d=(dp - 2*b));
    }

    }

    difference() {
    circle(d=(dp + 2*a));

    rotate(-p) // This rotate doesn't really matter
    circular_mirror(d=0, steps=N) involute_tooth();
    }
    }
    @@ -100,4 +107,4 @@ module circular_mirror(x=0, y=0, d, steps) {
    }
    }
    }
    }
    }
  11. SharpCoder revised this gist Jan 25, 2020. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions Untitled.scad
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,16 @@
    include <gears.scad>

    Teeth = 20;
    Pitch = 12;

    linear_extrude(5)
    difference() {
    // Generate spur gear with 20 teeth
    spur_gear(N=20);

    // Add the bore for a shaft.
    spur_gear(N=Teeth, P=Pitch);
    circle(d=19.1);
    }
    }

    // Calculate distance between another gear (may need to add 1mm tolerance)
    OtherGearTeeth = 34;
    echo("***");
    echo("center distance (mm)", calc_center_distance(Teeth, OtherGearTeeth, Pitch));
    echo("***");
  12. SharpCoder created this gist Jan 25, 2020.
    10 changes: 10 additions & 0 deletions Untitled.scad
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    include <gears.scad>

    linear_extrude(5)
    difference() {
    // Generate spur gear with 20 teeth
    spur_gear(N=20);

    // Add the bore for a shaft.
    circle(d=19.1);
    }
    103 changes: 103 additions & 0 deletions gears.scad
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    /**
    Constants
    **/
    in_to_mm = 25.4;
    rad_to_deg = 180 / PI;
    deg_to_rad = PI / 180;

    /**
    Functions
    **/
    function parametric_points(fx, fy, t0=0, t1=10, delta=0.01)
    = [for(i = [t0:delta:t1]) [fx(i), fy(i)]];

    function reverse(vector)
    = [for(i = [1:len(vector)]) vector[len(vector) - i]];

    /**
    Maths
    **/
    function calc_module(P) = in_to_mm / P;
    function calc_addendum(P) = (1/P) * in_to_mm;
    function calc_dedendum(P) = (1.25/P) * in_to_mm;
    function calc_dp(N, P) = (N/P) * in_to_mm;
    function calc_db(N, P, pa) = calc_dp(N,P) * cos(pa);
    function calc_dr(N, P) = calc_dp(N,P) - 2 * calc_dedendum(P);
    function calc_circular_pitch(P) = (PI / P) * in_to_mm;
    function calc_thickness(P) = (1.5708 / P) * in_to_mm;
    function calc_alpha(dp, db, pa) = ((sqrt(pow(dp,2) - pow(db,2))/db) * rad_to_deg - pa);
    function calc_clearance(P) = calc_dedendum(P) - calc_addendum(P);
    function calc_center_distance(N1, N2, P) = in_to_mm * (N1 + N2) / (2 * P);

    /**
    Modules
    **/
    /**
    Given some parameters, this method will generate a spur gear
    with an involute curve. Accepted paramters include:
    - N = How many teeth
    - P = Diametral pitch (all gears should have the same P)
    - pa = pressure angle (recommended to remain at 14.5)
    **/
    module spur_gear(N, P = 12, pa = 14.5) {
    dp = calc_dp(N, P);
    db = calc_db(N, P, pa);
    dr = calc_dr(N, P);
    a = calc_addendum(P);
    c = calc_clearance(P);
    p = calc_circular_pitch(P);

    // Undercut adjustment
    // NOTE: this might not be great? IDK
    undercut = 1 * c;

    // Calculate radius to begin the involute calculations
    r = (db - undercut) * .5;
    alpha = calc_alpha(dp, db, pa);
    beta = ((360 / (4*N)) - alpha) * 2;

    module involute_tooth() {
    x = function(t) (r * cos(t*rad_to_deg)) + (r * t * sin(t*rad_to_deg));
    y = function(t) (r * sin(t*rad_to_deg)) - (r * t * cos(t*rad_to_deg));
    x2 = function(t) r * (cos(-t*rad_to_deg - beta) - t * sin(-t * rad_to_deg - beta));
    y2 = function(t) r * (sin(-t*rad_to_deg - beta) + t * cos(-t * rad_to_deg - beta));

    involute_1_points = parametric_points(fx=x, fy=y, t1=1);
    involute_2_points = parametric_points(fx=x2, fy=y2, t1=1);

    union() {
    polygon(
    concat(
    involute_1_points,
    reverse(involute_2_points)
    )
    );

    }

    }

    difference() {
    circle(d=(dp + 2*a));

    rotate(-p) // This rotate doesn't really matter
    circular_mirror(d=0, steps=N) involute_tooth();
    }
    }

    /**
    Helper modules
    **/
    module circular_mirror(x=0, y=0, d, steps) {
    aps = 360 / steps;
    for (step=[0:steps]) {
    current_angle = step * aps;
    unit_x = cos(current_angle);
    unit_y = sin(current_angle);
    translate([x, y, 0]) {
    translate([unit_x * d, unit_y * d, 0]) {
    rotate(current_angle) children();
    }
    }
    }
    }