1
0
mirror of https://github.com/nophead/NopSCADlib.git synced 2025-01-17 21:48:43 +01:00

Merge branch 'open_belt' of https://github.com/SmoothieAq/NopSCADlib into SmoothieAq-open_belt

This commit is contained in:
Chris Palmer 2021-03-14 12:33:46 +00:00
commit 83b4ab2374
4 changed files with 175 additions and 57 deletions

View File

@ -72,6 +72,31 @@ module belt_test() {
layout([for(b = belts) belt_width(b)], 10)
rotate([0, 90, 0])
belt(belts[$i], [[0, 0, 20], [0, 1, 20]], belt_colour = $i%2==0 ? grey(90) : grey(20), tooth_colour = $i%2==0 ? grey(70) : grey(50));
// new example with open loop - this is a simplified example of the style used for example for the BLV 3D printer
pulley = GT2x20ob_pulley;
idler = GT2x16_plain_idler;
corners = [[-75,-50],[75,100]];
carriagepos = [0,0];
carriagew = 80;
points = [
[carriagepos.x - carriagew / 2, carriagepos.y, 0],
[corners[0].x + belt_pulley_pr(belt, pulley) + belt_pulley_pr(belt, idler), carriagepos.y - belt_pulley_pr(belt, idler), idler],
[corners[0].x, corners[0].y, pulley],
[corners[0].x, corners[1].y, idler],
[corners[1].x, corners[1].y, idler],
[corners[1].x, carriagepos.y + belt_pulley_pr(belt, idler), idler],
[carriagepos.x + carriagew / 2, carriagepos.y, 0]
];
translate_z(-30) {
belt(belt, points, open=true, auto_twist=true);
for (p = points)
if (is_list(p.z))
translate([p.x, p.y, 0])
pulley_assembly(p.z);
}
}
if($preview)

View File

@ -88,6 +88,7 @@ function scale(v) = let(s = is_list(v) ? v : [v, v, v]) //! Generate a 4x4 matr
[0, 0, 0, 1]
];
function vec2(v) = [v.x, v.y]; //! Return a 2 vector with the first two elements of `v`
function vec3(v) = [v.x, v.y, v.z]; //! Return a 3 vector with the first three elements of `v`
function vec4(v) = [v.x, v.y, v.z, 1]; //! Return a 4 vector with the first three elements of `v`
function transform(v, m) = vec3(m * [v.x, v.y, v.z, 1]); //! Apply 4x4 transform to a 3 vector by extending it and cropping it again
@ -153,3 +154,10 @@ function circle_intersect(c1, r1, c2, r2) = //! Calculate one point where tw
d = norm(v), // Distance between centres
a = atan2(v.z, v.x) - acos((sqr(d) + sqr(r2) - sqr(r1)) / (2 * d * r2)) // Cosine rule to find angle from c2
) c2 + r2 * [cos(a), 0, sin(a)]; // Point on second circle
function map(v, func) = [ for (e = v) func(e) ]; //! make a new vector where the func function argument is applied to each element of the vector v
function mapi(v, func) = [ for (i = [0:len(v)-1]) func(i,v[i]) ]; //! make a new vector where the func function argument is applied to each element of the vector v. The func will get the index number as first argument, and the element as second argument.
function reduce(v, func, unity) = let ( r = function(i,val) i == len(v) ? val : r(i + 1, func(val, v[i])) ) r(0, unity); //! reduce a vector v to a single entity by applying the func function recursivly to the reduced value so far and the next element, starting with unity as the inital reduced value
function sumv(v) = reduce(v, function(a, b) a + b, 0); //! sum a vector of values that can be added with "+"
function xor(a,b) = (a && !b) || (!a && b);

View File

@ -36,36 +36,44 @@ function circle_tangent(p1, p2) = //! Compute the clockwise tangent between two
v = [cos(theta), sin(theta)]
)[ p1 + r1 * v, p2 + r2 * v ];
function rounded_polygon_tangents(points) = //! Compute the straight sections needed to draw and to compute the lengths
let(len = len(points))
[for(i = [0 : len - 1])
let(ends = circle_tangent(points[i], points[(i + 1) % len]))
for(end = [0, 1])
ends[end]];
function rounded_polygon_arcs(points, tangents) = //! Compute the arcs at the points, for each point [angle,rotate_angle,length]
let(
len = len(points)
) [ for (i = [0: len-1])
let(
p1 = tangents[(i - 1 + len) % len][1],
p2 = tangents[i][0],
p = points[i],
v1 = p1 - p,
v2 = p2 - p,
r = abs(p.z),
a = let( aa = acos((v1 * v2) / sqr(r)) ) cross(v1, v2)*sign(p.z) <= 0 ? aa : 360 - aa,
l = PI * a * r / 180,
v0 = [r, 0],
v = let (
vv = norm(v0-v2) < 0.001 ? 0 : abs(v2.y) < 0.001 ? 180 :
let( aa = acos((v0 * v2) / sqr(r)) ) cross(v0, v2)*sign(p.z) <= 0 ? aa : 360 - aa
) p.z > 0 ? 360 - vv : vv - a
) [a, v, l]
];
function sumv(v, i = 0, sum = 0) = i == len(v) ? sum : sumv(v, i + 1, sum + v[i]);
function rounded_polygon_tangents(points) = //! Compute the straight sections between a point and the next point, for each section [start_point, end_point, length]
let(len = len(points))
[ for(i = [0 : len - 1])
let(ends = circle_tangent(points[i], points[(i + 1) % len]))
[ends[0], ends[1], norm(ends[0] - ends[1])]
];
// the cross product of 2D vectors is the area of the parallelogram between them. We use the sign of this to decide if the angle is bigger than 180.
function rounded_polygon_length(points, tangents) = //! Calculate the length given the point list and the list of tangents computed by ` rounded_polygon_tangents`
let(
len = len(points),
indices = [0 : len - 1],
straights = [for(i = indices) norm(tangents[2 * i] - tangents[2 * i + 1])],
arcs = [for(i = indices) let(p1 = tangents[2 * i + 1],
p2 = tangents[(2 * i + 2) % (2 * len)],
corner = points[(i + 1) % len],
c = [corner.x, corner.y],
v1 = p1 - c,
v2 = p2 - c,
r = abs(corner.z),
a = acos((v1 * v2) / sqr(r))) r ? PI * (cross(v1, v2) <= 0 ? a : 360 - a) * r / 180 : 0]
)
sumv(concat(straights, arcs));
arcs = rounded_polygon_arcs(points, tangents)
) sumv( map( concat(tangents, arcs), function(e) e[2] ) );
module rounded_polygon(points, _tangents = undef) { //! Draw the rounded polygon from the point list, can pass the tangent list to save it being calculated
len = len(points);
indices = [0 : len - 1];
tangents = _tangents ? _tangents : rounded_polygon_tangents(points);
tangents = [ for (t = _tangents ? _tangents : rounded_polygon_tangents(points)) each [t.x, t.y] ];
difference(convexity = points) {
union() {

View File

@ -18,19 +18,31 @@
//
//
//! Models timing belt running over toothed or smooth pulleys and calculates an accurate length.
//! Only models 2D paths, so not crossed belt core XY!
//! Models timing belt running in a path over toothed or smooth pulleys and calculates an accurate length.
//! Only models 2D paths, belt may twist to support crossed belt core XY and other designes where the belt twists!
//!
//! To make the back of the belt run against a smooth pulley on the outside of the loop specify a negative pitch radius.
//! By default the path is a closed loop. An open loop can be specified in two different ways:
//! 1) If a gap length and position i specified, you can make some forms of open loops.
//! To draw the gap its XY position is specified by `gap_pos`. `gap_pos.z` can be used to specify a rotation if the gap is not at the bottom of the loop.
//! 2) Alternativly, at open loop can more flexible be drawn by specifying `open=true`, and in that case the start and end points are not connected, leaving the loop open.
//!
//! By default the path is a closed loop but a gap length and position can be specified to make open loops.
//! To draw the gap its XY position is specified by `gap_pos`. `gap_pos.z` can be used to specify a rotation if the gap is not at the bottom of the loop.
//! To get a 180 degree twist of the loop, you can use the `twist` argument. `Twist` can be a single number, and in that case the belt will twist after
//! the position with that number. Alternatively `twist` can be a list of boolean values with a boolean for each position; the belt will then twist after
//! the position that have a `true` value in the `twist` list. If the path is specified with pulley/idler types, then you can use `auto_twist=true`; in
//! that case the belt will automatically twist so the back of the belt always runs against idlers and the tooth side runs against pullies. If you use
//! `open=true` then you might also use `start_twist=true` to let the belt start the part with the back side out.
//!
//! The path must be specified as a list of positions. Each position should be either a vector with `[x, y, pulley]` or `[x, y, r]`. A pully is a type from
//! `pulleys.scad`, and correct radius and angle will automatically be calculated. Alternativly a radius can be specifed directly.
//! To make the back of the belt run against a smooth pulley on the outside of the loop specify a negative pitch radius. Alternativly you can just specify
//! smooth pulleys in the path, and it will then happen automaticall.
//!
//! Individual teeth are not drawn, instead they are represented by a lighter colour.
//
include <../utils/core/core.scad>
use <../utils/rounded_polygon.scad>
use <../utils/maths.scad>
use <pulley.scad>
function belt_pitch(type) = type[1]; //! Pitch in mm
function belt_width(type) = type[2]; //! Width in mm
@ -41,47 +53,112 @@ function belt_pitch_height(type) = type[5] + belt_tooth_height(type); //! Offset
function belt_pitch_to_back(type) = belt_thickness(type) - belt_pitch_height(type); //! Offset of the back from the pitch radius
//
// We model the belt path at the pitch radius of the pulleys and the pitch line of the belt to get an accurate length.
// The belt is then drawn by offseting each side from the pitch line.
//
module belt(type, points, gap = 0, gap_pos = undef, belt_colour = grey(20), tooth_colour = grey(50)) { //! Draw a belt path given a set of points and pitch radii where the pulleys are. Closed loop unless a gap is specified
module belt(type, points, gap = 0, gap_pos = undef, belt_colour = grey(20), tooth_colour = grey(50), open = false, twist = undef, auto_twist = false, start_twist = false) { //! Draw a belt path given a set of points and pitch radii where the pulleys are. Closed loop unless a gap is specified
width = belt_width(type);
pitch = belt_pitch(type);
thickness = belt_thickness(type);
info = _belt_points_info(type, points, open, twist, auto_twist, start_twist);
dotwist = info[0]; // array of booleans, true if a twist happen after the position
twisted = info[1]; // array of booleans, true if the belt is twisted at the position
pointsx = info[2]; // array of [x,y,r], r is negative if left-angle (points may have pulleys as third element, but pointsx have radi)
tangents = info[3];
arcs = info[4];
length = ceil(_belt_length(type, info, open, gap) / pitch) * pitch;
part = str(type[0],pitch);
vitamin(str("belt(", no_point(part), "x", width, ", ", points, arg(gap, 0), arg(gap_pos, undef), "): Belt ", part," x ", width, "mm x ", length, "mm"));
vitamin(str("xbelt(", no_point(part), "x", width, ", ", points, "): Belt ", part," x ", width, "mm x ", length, "mm"));
len = len(points);
tangents = rounded_polygon_tangents(points);
length = ceil((rounded_polygon_length(points, tangents) - (is_list(gap) ? gap.x + gap.y : gap)) / pitch) * pitch;
module shape() rounded_polygon(points, tangents);
ph = belt_pitch_height(type);
th = belt_tooth_height(type);
module gap()
ph = belt_pitch_height(type);
module beltp() translate([ph-th,-width/2]) square([th,width]);
module beltb() translate([ph-thickness,-width/2]) square([thickness-th,width]);
difference() {
for (i = [0:len-(open?2:1)]) {
p1 = tangents[i].x;
p2 = tangents[i].y;
v = p2-p1;
a = atan(v.y/v.x) - (v.x < 0 ? 180 : 0);//a2(p2-p1);
l = norm(v);
translate(p1) rotate([-90, 0, a-90]) {
twist = dotwist[i] ? 180 : 0;
mirrored = twisted[i] ? 1 : 0;
color(tooth_colour) linear_extrude(l, twist = twist) mirror([mirrored, 0, 0]) beltp();
color(belt_colour) linear_extrude(l, twist = twist) mirror([mirrored, 0, 0]) beltb();
}
}
if(gap)
translate([gap_pos.x, gap_pos.y])
rotate(is_undef(gap_pos.z) ? 0 : gap_pos.z)
translate([0, ph - thickness / 2])
square(is_list(gap) ? [gap.x, gap.y + thickness + eps] : [gap, thickness + eps], center = true);
linear_extrude(width + eps, center = true)
translate([gap_pos.x, gap_pos.y])
rotate(is_undef(gap_pos.z) ? 0 : gap_pos.z)
translate([0, ph - thickness / 2])
square(is_list(gap) ? [gap.x, gap.y + thickness + eps] : [gap, thickness + eps], center = true);
}
for (i = [(open?1:0):len-(open?2:1)]) {
p = pointsx[i];
arc = arcs[i];
translate([p.x,p.y,0]) rotate([0,0,arc[1]]) {
mirrored = xor(twisted[i], p[2] < 0) ? 0 : 1;
color(tooth_colour) rotate_extrude(angle=arc[0]) translate([abs(p[2]),0,0]) mirror([mirrored,0,0]) beltp();
color(belt_colour) rotate_extrude(angle=arc[0]) translate([abs(p[2]),0,0]) mirror([mirrored,0,0]) beltb();
}
}
color(belt_colour)
linear_extrude(width, center = true)
difference() {
offset(-ph + thickness ) shape();
offset(-ph + th) shape();
gap();
}
color(tooth_colour)
linear_extrude(width, center = true)
difference() {
offset(-ph + th) shape();
offset(-ph) shape();
gap();
}
}
function belt_length(points, gap = 0) = rounded_polygon_length(points, rounded_polygon_tangents(points)) - gap; //! Compute belt length given path and optional gap
function _belt_points_info(type, points, open, twist, auto_twist, start_twist) = //! Helper function that calulates [twist, istwisted, points, tangents, arcs]
let(
len = len(points),
isleft = function(i) let(
p = vec2(points[i]),
p0 = vec2(points[(i - 1 + len) % len]),
p1 = vec2(points[(i + 1) % len])
) cross(p-p0,p1-p) > 0,
dotwist = function(i,istwisted) let( in = (i + 1) % len )
is_list(twist) ? twist[i] :
!is_undef(twist) ? i == twist :
open && is_list(points[in][2]) && auto_twist ? !pulley_teeth(points[in][2]) && !xor(isleft(in),istwisted) :
false,
twisted = [ for (
i = 0,
istwisted = start_twist,
twist = dotwist(i,istwisted),
nexttwisted = xor(twist,istwisted);
i < len;
i = i + 1,
istwisted = nexttwisted,
twist = dotwist(i,istwisted),
nexttwisted = xor(twist,istwisted)
) [twist,istwisted] ],
pointsx = mapi(points, function(i, p) !is_list(p[2]) ? p : [p.x, p.y, let( // if p[2] is not a list it is just r, otherwise it is taken to be a pulley and we calculate r
isleft = isleft(i),
r = belt_pulley_pr(type, p[2], twisted=!xor(pulley_teeth(p[2]),xor(isleft, twisted[i][1])))
) isleft ? -r : r ] ),
tangents = rounded_polygon_tangents(pointsx),
arcs = rounded_polygon_arcs(pointsx, tangents)
) [ [ for (t = twisted) t[0] ], [ for (t = twisted) t[1] ], pointsx, tangents, arcs];
function belt_pulley_pr(type, pulley, twisted=false) = //! Pitch radius. Default it expects the belt tooth to be against a toothed pulley an the backside to be against a smooth pulley (an idler). If `twisted` is true, the the belt is the other way around.
let(
thickness = belt_thickness(type),
ph = belt_pitch_height(type)
) pulley_teeth(pulley)
? pulley_pr(pulley) + (twisted ? thickness - ph : 0 )
: pulley_ir(pulley) + (twisted ? ph : thickness - ph );
function belt_length(type, points, open = false, gap = 0) = _belt_length(type, _belt_points_info(type, points, open), open, gap); //! Compute belt length given path and optional gap
function _belt_length(type, info, open, gap) = let(
len = len(info[0]),
pitch = belt_pitch(type),
d = open ? 1 : 0,
tangents = slice(info[3], 0, len - d) ,
arcs = slice(info[4], d, len - d),
beltl = sumv( map( concat(tangents, arcs), function(e) e[2] ) ),
gapl = is_list(gap) ? gap.x : is_undef(gap) ? 0 : gap
) beltl - gapl;