Algorithmcore
Cam profile synthesis
Also called cam contour generation, cam layout by inversion.
Computes the cam contour as the envelope of the follower positions for a given displacement law, in closed form for flat-faced and roller followers.
- Time
- Θ(m + N log m) worst · average · best
- Space
- Θ(m + N) extra
- Where
- N = the number of cam angles sampled; m = the number of segments of the cam program
- Model
- Real RAM
- Properties
- deterministiciterative
- Evidence
- Proof sketchCode in 1 languages
Cam profile synthesis computes the outline of a disk cam from the motion that its follower must make. A disk cam is a plate that turns on a shaft. Its follower is a part that slides in a fixed guide and is kept against the edge of the cam by a spring, so the shape of the edge drives the follower. The input is the displacement of the follower as a function of the cam angle, built from cam follower motion laws. The method is inversion: hold the cam still and turn the ground and the follower the other way. The follower then takes one position for each cam angle, and the cam outline is the envelope of these positions, the curve that touches each of them. For a roller follower and for a flat-faced follower the envelope has a closed form, so each point of the outline costs a fixed number of operations, with no search and no root finding.
In plain words
Draw the cam standing still in the middle of the page. Walk the follower around it, and at each step put the follower where the motion plan says it must be at that angle. The cam edge must touch the follower at each step and must not cut into it. So the edge is the curve that just touches all the drawn followers. For a round roller, the edge is a fixed distance inside the path of the roller center. For a flat foot, the edge is the curve that all the drawn foot lines touch. The same formulas also tell you how steep the push is and how sharp the edge is, so you can see at once if the cam is too small.
The problem
Input.
- A cam program: segments , each with a motion law on (, , or for a dwell), a cam angle and a lift (negative for a return), with and .
- The base circle radius : the radius of the smallest circle about the cam center that touches the cam profile.
- The follower. A translating roller follower has a roller of radius , and the line of its motion is at the offset from the cam center. A translating flat-faced follower has a flat face square to its line of motion.
- The number of samples .
Output. For , , in the frame of the cam: the point of the cam profile that touches the follower, and the checks that decide if the cam works, the pressure angle and the radius of curvature.
For the roller follower, the prime circle has the radius . It is the circle through the roller center at zero lift. The pitch curve is the path of the roller center relative to the cam. The cam turns counterclockwise. The fixed frame has the cam center at the origin and the follower moving along the line in the direction .
The algorithm
Here is the rotation by . A point fixed to the ground is at in the frame of a cam that has turned by .
CAM-PROFILE(P, R_b, R_f, ε, N) ▷ P = segments (f_i, β_i, L_i); translating roller follower- 1
check Σ β_i = 2π and Σ L_i = 0; θ_1 ← 0, h_1 ← 0, θ_{i+1} ← θ_i + β_i, h_{i+1} ← h_i + L_i - 2
R_p ← R_b + R_f, d_0 ← √(R_p² − ε²) - 3
for k ← 0 to N − 1, with θ ← 2πk/N, do - 4
i ← the segment with θ_i ≤ θ < θ_{i+1}, x ← (θ − θ_i)/β_i - 5
s ← h_i + L_i f_i(x), s′ ← L_i f_i′(x)/β_i, s″ ← L_i f_i″(x)/β_i² - 6
u ← d_0 + s, w ← s′ − ε, D ← √(u² + w²) - 7
C ← R(−θ)(ε, u) ▷ roller center: a point of the pitch curve - 8
K ← R(−θ)(ε + R_f w/D, u − R_f u/D) ▷ contact: a point of the cam profile - 9
φ ← atan2(w, u) ▷ pressure angle - 10
ρ ← D³/(u² + w(w + s′) − u s″) ▷ radius of curvature of the pitch curve - 11
output (θ, s, C, K, φ, ρ), marked undercut if 0 < ρ ≤ R_f FLAT-FACE-PROFILE(P, R_b, N) ▷ translating follower, flat face square to its axis- 12
for k ← 0 to N − 1, with θ ← 2πk/N, do - 13
s, s′, s″ as in lines L4 and L5 - 14
Q ← R(−θ)(s′, R_b + s) ▷ contact: a point of the cam profile - 15
ρ ← R_b + s + s″ ▷ radius of curvature; it must be positive - 16
output (θ, s, Q, ρ); the contact is at the distance s′ from the axis along the face
Line L5 is the chain rule on . Line L7 is the inversion: in the fixed frame the roller center is at , and puts it on the prime circle at zero lift. The pitch curve has the tangent . Line L8 steps from the roller center by along the normal toward the cam, so the cam profile is the inner offset of the pitch curve at the distance , as on the offset curve page. Line L9 is the angle between the common normal and the line of motion. Line L10 is the radius of curvature of the pitch curve, positive where the curve is convex. With it is the formula of Norton, Section 8.6. The cam profile then has the radius at . For the flat face, line L14 is the envelope of the face lines, and line L15 is the radius of the profile itself.
Example
Take a rise of mm with the cycloidal law over , a dwell of , a return of mm with the 3-4-5 polynomial over , and a dwell of . Take mm, a roller of mm and no offset, so mm. In the rise, and the cycloidal law gives and . Lengths are in mm, and , are per radian of cam angle.
| pitch point | cam point | ||||||
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | (0, 50) | (0, 40) | 50.00 | ||
| 1.8169 | 12.7324 | 50.9296 | (19.8295, 47.8726) | (18.3178, 37.9875) | 410.36 | ||
| 10 | 25.4648 | 0 | (42.4264, 42.4264) | (38.6799, 33.1548) | 56.55 | ||
| 18.1831 | 12.7324 | (62.9930, 26.0925) | (54.6136, 20.6348) | 39.51 | |||
| 20 | 0 | 0 | (70, 0) | (60, 0) | 70.00 | ||
| 10 | 0 | (, ) | (, ) | 56.81 |
At , line L6 gives , and , and line L9 gives . Line L7 turns by to . Line L10 gives .
With samples, the largest pressure angle is at in the rise and at in the return. The smallest radius of the pitch curve is mm at , so the cam itself has the radius mm there, and no sample is marked as undercut. An offset of mm lowers the peak in the rise to but raises the peak in the return to . An offset helps one direction of motion at the cost of the other.
The same program with a flat-faced follower and mm has the smallest radius mm at . The smallest value of over the cycle is mm, so line L15 needs mm. The contact moves along the face from mm to mm, so the face must be at least mm wide.
A small cam shows the undercut. Take a cycloidal rise of mm over , a dwell of , a cycloidal return over and a dwell of , with mm and mm. The pitch curve then has a convex radius of only mm, less than the roller, and line L11 marks 30 of 720 samples, near and near . The pressure angle reaches , far above the usual limits on the cam pressure angle page. With mm no sample is marked.
Correctness
Four facts make the output right. Line L5 gives , and by the chain rule. In the frame of the cam, the roller center at the angle is the fixed point turned by , which is line L7. The roller circles have the envelope , where is the unit normal of the pitch curve, because a point of the envelope satisfies . The face lines , with , have the envelope , which is line L14.
These facts show that each output point is on the envelope. The proof also shows that the follower does not cut the cam. If at every angle, the tangent of the profile turns once in one direction, and the whole cam lies on the inner side of the tangent line at each contact point. So the roller is at the distance from the cam, and touches it at . If at an angle, the roller at nearby angles covers the point , which is the undercut of line L11. For the flat face the same argument works when at every angle. The full proof is below.
Proof: The computed points lie on the cam profile, and the follower touches the cam without cutting itfull
We use the notation of the page. The cam turns counterclockwise by about the origin . is the rotation by , and , so . The program is a list of segments with laws , angles and lifts , with and . We assume that each is on and that and are continuous across the segment ends, so is on the cycle and is piecewise continuous. All derivatives below are with respect to . At a segment end where jumps, the statements about , and hold on each side.
Lemma 1 (the program). Line L5 returns , and .
Proof. Line L1 sets and , so segment covers and starts at the height . Line L4 finds the segment that holds and sets , so . On that segment , and the chain rule gives and . Since and , the end of the last segment has height at the angle , so is -periodic.
Lemma 2 (inversion). In the frame of the cam, the roller center at the angle is with , which is line L7. Its derivative is with .
Proof. In the fixed frame the follower slides on the line , and at the roller center is on the prime circle of radius , so it is at with . That is line L2. At lift it is at . A point with cam coordinates is at in the fixed frame, so the fixed point has the cam coordinates . Since ,
Put , the unit tangent and the unit normal . We assume , which holds when , for example when . Then and the pitch curve is regular.
Lemma 3 (the turning of the tangent). The angle of is , where is the value of line L9, and the signed curvature of the pitch curve is . Line L10 computes .
Proof. The vector has the angle , and subtracts . The curvature of a plane curve is the rate of turn of its tangent per unit length, and the length grows at the rate , so . With and ,
so , which is line L10.
As grows the pitch curve runs clockwise about , so a convex arc has and , and is then the ordinary radius of curvature. On a circle of radius about (a dwell with ) we get . Since is periodic, changes by exactly over one cycle. The normal is to the left of the direction of travel, which is the outer side of a clockwise convex arc.
Lemma 4 (the contact point). The roller circles have the envelope . Line L8 computes , the branch on the side of the cam, and .
Proof. Write the family as . A point of the envelope also satisfies , so is a multiple of of length . In the fixed frame, is , so , which is line L8. For the derivative, , so . With this is .
At the roller circle and the profile have the common normal . The force between them, without friction, acts along . In the fixed frame is and the follower moves along , so the angle between the force and the motion has the tangent . This is the pressure angle of line L9.
Theorem (no interference). Assume for every . Then the profile is a closed convex curve with radius of curvature . For every , every point of the region bounded by satisfies , with equality at . So at each angle the roller touches the cam at and does not cut into it.
Proof. By Lemma 4, is a positive multiple of , so has the same tangent angle as the pitch curve, and its speed is . Its radius of curvature is . Fix and write and . Let on . Since and ,
The factor in front is positive. Since and the total change is , the difference falls strictly from to . It is in up to an angle and in after it, so falls on and rises on . As and by periodicity, everywhere. So the whole curve lies in the closed half-plane . A closed curve whose tangent turns once in one direction is a simple convex curve, so is well defined. It lies in the convex hull of , and so in . For , since ,
with equality at .
Proposition (the undercut flag). If and is continuous at , then the roller at angles close to covers the point . So the cam cannot have on its boundary, and the follower does not follow the program near . This is the case that line L11 marks.
Proof. Let . Then and , which is at because is normal to . Next, , so
So has a strict local maximum at , and for close to .
Theorem (flat-faced follower). Let and . In the frame of the cam the face is the line . Line L14 computes , the envelope of these lines, and line L15 computes its radius of curvature . If for every , then the whole profile lies in the half-plane for every , and touches the face at , at the distance from the follower axis.
Proof. In the fixed frame the face is the line . A cam point is at in the fixed frame, and its height is . This gives the line. Here and . The envelope of the lines satisfies also , and since and are orthonormal, , which is line L14. The fixed-frame point is on the face at the distance from the axis. Then , and the tangent turns at the unit rate, so is the radius of curvature. For the half-plane, let . Then
which is negative on and positive on . As , , so .
The algorithm. Lines L3 to L11 apply Lemmas 1 to 4 at each angle , and lines L12 to L16 apply the last theorem. So every output point is a point of the true profile, with no error from the sampling at the sample angles. When the check of line L11 finds no sample with and every is positive, the conditions of the no-interference theorem hold at the samples. Between samples, the check is only as fine as the step .
Complexity
Line L1 takes time. Line L4 finds the segment by binary search on the start angles in time, and lines L5 to L11 take time, with one square root, one arc tangent and a few products. So the time is . The samples come in increasing order of , so a pointer that moves forward through the segments replaces the search, and the time drops to . The output has points, and the start angles and heights of line L1 take more space, so the extra space is .
The number follows from the machining tolerance. Between two samples the profile is replaced by a chord, and a chord of length on an arc of radius is at most from the arc. For the example, the cam has a perimeter of mm and a smallest radius of mm. With the longest chord is mm, so the bound is mm, and the largest distance from a chord to the true profile is mm.
Properties
- Exact at the samples. Each output point is a point of the true profile up to rounding. Only the curve between samples is an approximation.
- Independent samples. Each angle is computed alone, so the loop runs in parallel and a single angle can be evaluated on demand.
- Needs but not . The profile needs and , and the checks need . A jump in , as with the harmonic law next to a dwell, is a jump in the radius of curvature, so the cam has a visible change of curvature there.
- Deterministic and closed form. There is no iteration, so there is no convergence test and no tolerance to choose.
Variants and improvements
- Oscillating followers. A follower arm that turns about a fixed pivot needs the same inversion with the arm angle in place of . The formulas change, and the pressure angle is measured against the direction of motion of the roller center, which is square to the arm.
- The cutter path. A milling cutter of radius that cuts the cam must follow the offset of the profile by . If , the cutter center follows the pitch curve itself. For other cutters, offset the pitch curve by with curve offsetting.
- General follower shapes. For a follower of any convex outline, the largest cam that no follower position overlaps is the complement of the union of the follower positions in the cam frame, which is the swept volume of the follower in two dimensions. The contact points are the points of the follower outline where the relative velocity is square to the normal.
- Fitted spline cams. Instead of fixed laws, fit with a spline that meets the end conditions and limits on , and , then run the same loop on the spline.
- Three-dimensional cams. Barrel and globoidal cams use the same inversion in space, with an envelope of surfaces in place of curves.
When to use it
Use it whenever the motion of the follower is known and the cam outline must be made, which is the normal order of cam design. Run the checks of lines L9, L10 and L15 first, at a coarse , and change , or the motion laws until the pressure angle and the radius of curvature are acceptable. Then run it at the fine that the machining tolerance needs.
Pitfalls
- The direction of rotation. The profile for a cam that turns clockwise is the mirror image. Using in place of gives the profile of a cam that turns the wrong way. The sign of a useful offset also changes with the direction.
- Derivatives in time or in angle. Lines L5 to L15 use derivatives with respect to , in units of length per radian. The velocity in time is , not . Mixing them, or using degrees, gives a wrong pressure angle and a wrong radius.
- Undercut. If , the inner offset forms a cusp and a loop, and a cutter of the roller size removes the loop. The cam then has a sharp corner and the follower does not follow the program. Trimming the loop, as on the offset trimming page, does not cure this. Increase or use a smaller roller.
- The flat face needs a positive radius. If somewhere, the envelope folds back, and no flat-faced follower can follow the program. A large negative at a high lift causes this. The face must also be wide enough for the full range of .
- The offset limit. The offset needs , and the formulas assume .
- Too few samples near sharp curvature. Equal angle steps give long chords where the cam is large and few points where is small. Choose from the smallest radius, or sample by arc length.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cam-profile-synthesis/. The authors are listed in prov/59Q72X.jsonl.
"""Cam profile synthesis for translating followers by the inversion (envelope) method.
The cam turns counterclockwise by the angle theta. In the fixed frame the follower
moves along the line x = eps, and its roller center is at (eps, d0 + s(theta)). In
the frame of the cam, a fixed point p appears at R(-theta) p, so the roller centers
trace the pitch curve C(theta) = R(-theta) (eps, d0 + s), and the cam profile is the
envelope of the roller circles. A flat-faced follower has its face on the line
y = Rb + s, and the profile is the envelope of those lines.
"""
from __future__ import annotations
import bisect
import math
from dataclasses import dataclass
from typing import Callable, List, Sequence, Tuple
Vec = Tuple[float, float]
Law = Callable[[float], Tuple[float, float, float, float]]
TWO_PI = 2.0 * math.pi
def dwell(x):
"""The dwell: f = 0 and all derivatives zero. Cost O(1)."""
z = x - x
return z, z, z, z
def harmonic(x):
"""Simple harmonic rise f = (1 - cos(pi x)) / 2 and its first three derivatives. Cost O(1)."""
c, s, p = math.cos(math.pi * x), math.sin(math.pi * x), math.pi
return (1 - c) / 2, p / 2 * s, p * p / 2 * c, -p ** 3 / 2 * s
def cycloidal(x):
"""Cycloidal rise f = x - sin(2 pi x) / (2 pi) and its first three derivatives. Cost O(1)."""
t = 2 * math.pi * x
return x - math.sin(t) / (2 * math.pi), 1 - math.cos(t), 2 * math.pi * math.sin(t), 4 * math.pi ** 2 * math.cos(t)
def poly345(x):
"""The 3-4-5 polynomial f = 10x^3 - 15x^4 + 6x^5. Exact for Fraction input. Cost O(1)."""
return (10 * x ** 3 - 15 * x ** 4 + 6 * x ** 5, 30 * x ** 2 * (1 - x) ** 2,
60 * x * (1 - x) * (1 - 2 * x), 60 - 360 * x + 360 * x * x)
def poly4567(x):
"""The 4-5-6-7 polynomial f = 35x^4 - 84x^5 + 70x^6 - 20x^7. Exact for Fraction input. Cost O(1)."""
return (35 * x ** 4 - 84 * x ** 5 + 70 * x ** 6 - 20 * x ** 7, 140 * x ** 3 * (1 - x) ** 3,
420 * x ** 2 * (1 - x) ** 2 * (1 - 2 * x), 840 * x * (1 - x) * (1 - 5 * x + 5 * x * x))
LAWS = {"dwell": dwell, "harmonic": harmonic, "cycloidal": cycloidal, "poly345": poly345, "poly4567": poly4567}
@dataclass(frozen=True)
class Segment:
"""One segment of the cam program: a law, the cam angle beta > 0 and the lift (negative for a return)."""
law: str
beta: float
lift: float
class CamProgram:
"""A full cycle of segments whose angles sum to 2 pi and whose lifts sum to 0.
Construction costs O(m) for m segments; svaj costs O(log m).
"""
def __init__(self, segments: Sequence[Segment], tol: float = 1e-9):
if not segments or any(g.beta <= 0 or g.law not in LAWS for g in segments):
raise ValueError("each segment needs a known law and a positive angle")
if abs(sum(g.beta for g in segments) - TWO_PI) > tol: # @line L1
raise ValueError("the segment angles must sum to 2 pi")
if abs(sum(g.lift for g in segments)) > tol * (1 + sum(abs(g.lift) for g in segments)):
raise ValueError("the lifts must return the follower to its start")
self.segments = list(segments)
self.starts: List[float] = []
self.heights: List[float] = []
theta, h = 0.0, 0.0
for g in segments:
self.starts.append(theta)
self.heights.append(h)
theta += g.beta
h += g.lift
def segment_at(self, theta: float) -> int:
"""Index i of the segment with start_i <= theta < start_{i+1}, theta taken mod 2 pi."""
return max(0, bisect.bisect_right(self.starts, theta % TWO_PI) - 1)
def eval_segment(self, i: int, x: float) -> Tuple[float, float, float, float]:
"""s, ds/dtheta, d2s/dtheta2 and d3s/dtheta3 of segment i at the local parameter x in [0, 1]."""
g = self.segments[i]
f, f1, f2, f3 = LAWS[g.law](x)
b = g.beta
return self.heights[i] + g.lift * f, g.lift * f1 / b, g.lift * f2 / (b * b), g.lift * f3 / (b ** 3) # @line L5
def svaj(self, theta: float) -> Tuple[float, float, float, float]:
"""Displacement and its first three derivatives with respect to the cam angle."""
t = theta % TWO_PI
i = self.segment_at(t) # @line L4
return self.eval_segment(i, (t - self.starts[i]) / self.segments[i].beta)
def rot_minus(theta: float, p: Vec) -> Vec:
"""R(-theta) p: a point fixed in the ground, seen from the cam turned by theta."""
c, s = math.cos(theta), math.sin(theta)
return (c * p[0] + s * p[1], -s * p[0] + c * p[1])
@dataclass(frozen=True)
class RollerSample:
theta: float
s: float
pitch: Vec
contact: Vec
pressure_angle: float
rho: float
undercut: bool
def roller_sample(prog: CamProgram, theta: float, rb: float, rf: float, eps: float) -> RollerSample:
"""Pitch point, cam point, pressure angle and pitch radius of curvature at one cam angle.
Precondition: rb > 0, rf >= 0, |eps| < rb + rf. Cost O(log m).
rho is the signed radius of curvature of the pitch curve (positive where it is convex);
the sample is flagged as undercut when 0 < rho <= rf.
"""
rp = rb + rf # @line L2
if not (rb > 0 and rf >= 0 and abs(eps) < rp):
raise ValueError("bad cam size")
d0 = math.sqrt(rp * rp - eps * eps)
s, s1, s2, _ = prog.svaj(theta)
u, w = d0 + s, s1 - eps # @line L6
d = math.hypot(u, w)
pitch = rot_minus(theta, (eps, u)) # @line L7
contact = rot_minus(theta, (eps + rf * w / d, u - rf * u / d)) # @line L8
phi = math.atan2(w, u) # @line L9
den = u * u + w * (w + s1) - u * s2
rho = d ** 3 / den if den != 0 else math.inf # @line L10
return RollerSample(theta, s, pitch, contact, phi, rho, 0 < rho <= rf) # @line L11
def roller_profile(prog: CamProgram, rb: float, rf: float, eps: float, n: int) -> List[RollerSample]:
"""Cam profile for a translating roller follower with offset eps, at n equal cam angles.
Precondition: rb > 0, rf >= 0, |eps| < rb + rf, n >= 1. Cost O(m + n log m).
"""
if n < 1:
raise ValueError("n must be positive")
return [roller_sample(prog, TWO_PI * k / n, rb, rf, eps) for k in range(n)] # @line L3
@dataclass(frozen=True)
class FlatSample:
theta: float
s: float
contact: Vec
rho: float
face_offset: float
def flat_sample(prog: CamProgram, theta: float, rb: float) -> FlatSample:
"""Cam point, radius of curvature and face offset of a flat-faced follower at one cam angle.
Precondition: rb > 0. Cost O(log m).
"""
if not rb > 0:
raise ValueError("bad cam size")
s, s1, s2, _ = prog.svaj(theta) # @line L13
q = rot_minus(theta, (s1, rb + s)) # @line L14
rho = rb + s + s2 # @line L15
return FlatSample(theta, s, q, rho, s1) # @line L16
def flat_faced_profile(prog: CamProgram, rb: float, n: int) -> List[FlatSample]:
"""Cam profile for a translating flat-faced follower with its face square to its axis.
Precondition: rb > 0, n >= 1. Cost O(m + n log m). The profile is valid when every rho > 0.
"""
if n < 1:
raise ValueError("n must be positive")
return [flat_sample(prog, TWO_PI * k / n, rb) for k in range(n)] # @line L12
test_cam_profile_synthesis.py 257 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cam-profile-synthesis/. The authors are listed in prov/59Q72X.jsonl.
import math
import random
import unittest
from fractions import Fraction as F
from cam_profile_synthesis import (
TWO_PI,
CamProgram,
Segment,
flat_faced_profile,
flat_sample,
poly345,
poly4567,
roller_profile,
roller_sample,
rot_minus,
)
SMOOTH = ["cycloidal", "poly345", "poly4567"]
ALL = SMOOTH + ["harmonic"]
def random_program(rng, laws, max_lift=15.0):
"""Rise, optional dwell, return, optional dwell, each motion at least about 50 degrees."""
lift = rng.uniform(3.0, max_lift)
parts = [("rise", rng.uniform(1.0, 2.0)), ("dwell", rng.uniform(0.0, 1.5)),
("return", rng.uniform(1.0, 2.0)), ("dwell", rng.uniform(0.0, 1.5))]
parts = [p for p in parts if p[0] != "dwell" or p[1] > 0.3]
total = sum(w for _, w in parts)
segs = []
for kind, w in parts:
beta = TWO_PI * w / total
if kind == "dwell":
segs.append(Segment("dwell", beta, 0.0))
else:
segs.append(Segment(rng.choice(laws), beta, lift if kind == "rise" else -lift))
# make the angles sum to 2 pi in floating point
last = segs[-1]
segs[-1] = Segment(last.law, TWO_PI - sum(g.beta for g in segs[:-1]), last.lift)
return CamProgram(segs)
def poly_eval(c, x):
return sum(ci * x ** i for i, ci in enumerate(c))
def poly_der(c):
return [i * ci for i, ci in enumerate(c)][1:]
def dist(p, q):
return math.hypot(p[0] - q[0], p[1] - q[1])
def signed_circumradius(a, b, c):
"""Radius of the circle through three points, positive when they turn clockwise."""
cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
return -dist(a, b) * dist(b, c) * dist(a, c) / (2.0 * cross)
class TestLaws(unittest.TestCase):
def test_polynomial_laws_exact(self):
rng = random.Random(1)
for law, coef in ((poly345, [0, 0, 0, 10, -15, 6]), (poly4567, [0, 0, 0, 0, 35, -84, 70, -20])):
ders = [coef]
for _ in range(3):
ders.append(poly_der(ders[-1]))
for _ in range(200):
x = F(rng.randint(0, 1000), 1000)
self.assertEqual(law(x), tuple(poly_eval(d, x) for d in ders))
self.assertEqual(poly345(F(0)), (0, 0, 0, 60))
self.assertEqual(poly345(F(1)), (1, 0, 0, 60))
self.assertEqual(poly4567(F(0)), (0, 0, 0, 0))
self.assertEqual(poly4567(F(1)), (1, 0, 0, 0))
def test_derivatives_against_finite_differences(self):
rng = random.Random(2)
cases = 0
while cases < 600:
prog = random_program(rng, ALL)
theta = rng.uniform(0, TWO_PI)
ends = prog.starts + [TWO_PI]
if min(abs(theta - e) for e in ends) < 1e-3:
continue
i = prog.segment_at(theta)
g = prog.segments[i]
h = 1e-5
lo, mid, hi = prog.svaj(theta - h), prog.svaj(theta), prog.svaj(theta + h)
for k in range(3):
fd = (hi[k] - lo[k]) / (2 * h)
scale = abs(g.lift) / g.beta ** (k + 1) + 1.0
self.assertLess(abs(fd - mid[k + 1]), 1e-6 * scale, (g.law, k))
fd2 = (hi[0] - 2 * mid[0] + lo[0]) / (h * h)
self.assertLess(abs(fd2 - mid[2]), 1e-3 * (abs(g.lift) / g.beta ** 2 + 1.0))
cases += 1
def test_continuity_at_segment_ends(self):
rng = random.Random(3)
for _ in range(500):
prog = random_program(rng, SMOOTH)
m = len(prog.segments)
for i in range(m):
left = prog.eval_segment(i, 1.0)
right = prog.eval_segment((i + 1) % m, 0.0)
scale = 1 + sum(abs(g.lift) / g.beta ** 2 for g in prog.segments)
for k in range(3):
self.assertLess(abs(left[k] - right[k]), 1e-9 * scale, (i, k))
def test_harmonic_acceleration_jump_at_dwell(self):
rng = random.Random(4)
for _ in range(500):
b1, b2, b3 = rng.uniform(0.5, 2.0), rng.uniform(0.3, 1.5), rng.uniform(0.5, 2.0)
lift = rng.uniform(1, 20)
prog = CamProgram([Segment("harmonic", b1, lift), Segment("dwell", b2, 0.0),
Segment("harmonic", b3, -lift), Segment("dwell", TWO_PI - b1 - b2 - b3, 0.0)])
left, right = prog.eval_segment(0, 1.0), prog.eval_segment(1, 0.0)
self.assertAlmostEqual(left[0], right[0], places=9)
self.assertAlmostEqual(left[1], right[1], places=9)
jump = right[2] - left[2]
self.assertAlmostEqual(jump, math.pi ** 2 * lift / (2 * b1 * b1), places=7)
start = prog.eval_segment(0, 0.0)
self.assertAlmostEqual(start[2], math.pi ** 2 * lift / (2 * b1 * b1), places=7)
def test_rejects_open_programs(self):
with self.assertRaises(ValueError):
CamProgram([Segment("cycloidal", math.pi, 10.0), Segment("dwell", math.pi, 0.0)])
with self.assertRaises(ValueError):
CamProgram([Segment("cycloidal", 1.0, 10.0), Segment("cycloidal", 1.0, -10.0)])
class TestRollerProfile(unittest.TestCase):
def test_base_circle(self):
prog = CamProgram([Segment("dwell", TWO_PI, 0.0)])
for smp in roller_profile(prog, 30.0, 8.0, 5.0, 64):
self.assertAlmostEqual(math.hypot(*smp.contact), 30.0, places=9)
self.assertAlmostEqual(math.hypot(*smp.pitch), 38.0, places=9)
def test_contact_at_roller_distance_from_pitch_curve(self):
"""Brute force: the nearest point of a dense pitch curve to each cam point is at distance rf."""
rng = random.Random(5)
used = 0
for _ in range(6):
prog = random_program(rng, SMOOTH)
rb, rf, eps = rng.uniform(40, 60), rng.uniform(5, 12), rng.uniform(-8, 8)
n = 180
prof = roller_profile(prog, rb, rf, eps, n)
if min(smp.rho for smp in prof) <= rf:
continue
dense = roller_profile(prog, rb, rf, eps, 4 * n)
for smp in prof:
self.assertAlmostEqual(dist(smp.contact, smp.pitch), rf, places=9)
near = min(dist(smp.contact, d.pitch) for d in dense)
self.assertAlmostEqual(near, rf, places=7)
used += 1
self.assertGreaterEqual(used, 4)
def test_contact_is_on_pitch_normal(self):
rng = random.Random(6)
for _ in range(500):
prog = random_program(rng, ALL)
rb, rf, eps = rng.uniform(30, 60), rng.uniform(3, 12), rng.uniform(-8, 8)
theta = rng.uniform(0, TWO_PI)
if min(abs(theta - e) for e in prog.starts + [TWO_PI]) < 1e-4:
continue
a = roller_sample(prog, theta - 1e-6, rb, rf, eps)
b = roller_sample(prog, theta + 1e-6, rb, rf, eps)
c = roller_sample(prog, theta, rb, rf, eps)
tangent = (b.pitch[0] - a.pitch[0], b.pitch[1] - a.pitch[1])
radial = (c.contact[0] - c.pitch[0], c.contact[1] - c.pitch[1])
cosang = (tangent[0] * radial[0] + tangent[1] * radial[1]) / (math.hypot(*tangent) * rf)
self.assertLess(abs(cosang), 1e-5)
# pressure angle: angle of the pitch tangent, turned back to the fixed frame, from the x axis
back = rot_minus(-theta, tangent)
self.assertLess(abs(math.atan2(back[1], back[0]) - c.pressure_angle), 1e-6)
def test_radius_of_curvature_against_three_points(self):
rng = random.Random(7)
for _ in range(500):
prog = random_program(rng, ALL)
rb, rf, eps = rng.uniform(20, 60), rng.uniform(3, 12), rng.uniform(-8, 8)
theta = rng.uniform(0, TWO_PI)
if min(abs(theta - e) for e in prog.starts + [TWO_PI]) < 1e-2:
continue
d = 1e-4
pts = [roller_sample(prog, theta + k * d, rb, rf, eps).pitch for k in (-1, 0, 1)]
rho = roller_sample(prog, theta, rb, rf, eps).rho
ref = signed_circumradius(*pts)
self.assertLess(abs(1 / ref - 1 / rho), 1e-4 / abs(rho) + 1e-6)
def test_undercut_flag_matches_interference(self):
"""A small cam with a large roller and a fast rise: flagged samples cut into the roller."""
prog = CamProgram([Segment("cycloidal", math.radians(40), 20.0), Segment("dwell", math.radians(140), 0.0),
Segment("cycloidal", math.radians(40), -20.0), Segment("dwell", math.radians(140), 0.0)])
rb, rf = 10.0, 9.0
prof = roller_profile(prog, rb, rf, 0.0, 720)
flagged = [smp for smp in prof if smp.undercut]
self.assertTrue(flagged)
worst = min(min(dist(smp.contact, d.pitch) for d in prof) for smp in flagged)
self.assertLess(worst, rf - 1e-3)
# a large base circle removes the undercut
big = roller_profile(prog, 60.0, 9.0, 0.0, 720)
self.assertFalse(any(smp.undercut for smp in big))
class TestFlatFaced(unittest.TestCase):
def test_eccentric_circle(self):
e, rb = 7.0, 25.0
prog = CamProgram([Segment("harmonic", math.pi, 2 * e), Segment("harmonic", math.pi, -2 * e)])
for smp in flat_faced_profile(prog, rb, 360):
self.assertAlmostEqual(dist(smp.contact, (0.0, -e)), rb + e, places=9)
self.assertAlmostEqual(smp.rho, rb + e, places=9)
def test_face_supports_cam(self):
rng = random.Random(8)
used = 0
for _ in range(8):
prog = random_program(rng, ALL)
rb = rng.uniform(30, 60)
prof = flat_faced_profile(prog, rb, 720)
if min(smp.rho for smp in prof) <= 0:
continue
for smp in prof[::12]:
e = rot_minus(smp.theta, (0.0, 1.0))
h = rb + smp.s
top = max(q.contact[0] * e[0] + q.contact[1] * e[1] for q in prof)
self.assertAlmostEqual(smp.contact[0] * e[0] + smp.contact[1] * e[1], h, places=9)
self.assertLessEqual(top, h + 1e-9)
used += 1
self.assertGreaterEqual(used, 5)
def test_tangency_and_curvature(self):
rng = random.Random(9)
done = 0
while done < 500:
prog = random_program(rng, ALL)
rb = rng.uniform(30, 60)
theta = rng.uniform(0, TWO_PI)
if min(abs(theta - e) for e in prog.starts + [TWO_PI]) < 1e-2:
continue
d = 1e-4
pts = [flat_sample(prog, theta + k * d, rb).contact for k in (-1, 0, 1)]
s, s1, s2, _ = prog.svaj(theta)
e = rot_minus(theta, (0.0, 1.0))
tang = (pts[2][0] - pts[0][0], pts[2][1] - pts[0][1])
self.assertLess(abs(tang[0] * e[0] + tang[1] * e[1]), 1e-6 * (1 + math.hypot(*tang)))
rho = flat_sample(prog, theta, rb).rho
self.assertAlmostEqual(rho, rb + s + s2, places=9)
if abs(rho) > 1.0:
self.assertLess(abs(signed_circumradius(*pts) - rho), 1e-3 * abs(rho))
done += 1
if __name__ == "__main__":
unittest.main()
History
The layout of a cam by inversion, in which the follower is drawn at each angle around a fixed cam and the cam outline is drawn to touch every position, is the classic graphical method of the mechanism textbooks. The closed forms for the pitch curve, the profile, the pressure angle and the radius of curvature are the analytic form of the same construction. Norton gives the pressure angle and the radius of curvature for translating followers in Section 8.6 of Design of Machinery, and his handbook also treats the manufacture of cams. Uicker, Pennock and Shigley derive the profile equations for flat-faced and roller followers.
References
- Robert L. Norton, Design of Machinery, 6th edition, McGraw-Hill, 2019, Section 8.6 (p. 460, sizing the cam, with the pressure angle of translating roller followers on p. 461, the overturning moment of flat-faced followers on p. 465, the radius of curvature of roller followers on p. 466 and of flat-faced followers on p. 470) and Section 8.7 (p. 475, practical design considerations)
- Robert L. Norton, Cam Design and Manufacturing Handbook, 2nd edition, Industrial Press, 2009, the chapters on cam size, pressure angle and radius of curvature, and on cam manufacturing (general locator)
- John J. Uicker, Gordon R. Pennock and Joseph E. Shigley, Theory of Machines and Mechanisms, 5th edition, Oxford University Press, 2017, the chapter on cam design, the layout of cam profiles by inversion and the analytic profile equations (general locator)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 59Q72X Edit this page History Report an error Markdown