Macro Cam Cycloid

Generic macro icon. Create your personal icon with the same name of the macro Cam Cycloid

Description
This macro creates a new part that has a sketch of the profile of a mechanical cam. The input is the radius, the starting-angle and the end-angle of each dwell interval of the cam. The transition intervals of the cam are calculated using the cycloidal function, and approximated by B splines.

Macro version: 0.0.1
Last modified: 2026-06-01
FreeCAD version: All
Download: Text-x-python.png
Author: Garlic
Author
Garlic
Download
Text-x-python.png
Links
Macro Version
0.0.1
Date last modified
2026-06-01
FreeCAD Version(s)
All
Default shortcut
None
See also
None

Installation

Please note that you need to do the following steps only once:

  1. From the menu of FreeCAD, go to Macro → Macros
  2. Find which is the path written in the field User Macros Location
  3. Open this folder with the file explorer (or any other file management application).
  4. Download the file CamCycloid.py from https://git.disroot.org/CrowdDiscussesAlternatives/ScriptsForFreeCAD/src/branch/master/CamCycloid and paste it in this folder.
  5. Close the Execute Macro dialog box.

Here, you can find more info on how to manually install macros.

Usage

The following examples demonstrate how to use this script.

Example 1

Executing the script

In order for the algorithm to run, you need to open the Python Console on FreeCAD, and execute the code below (i.e. copy-paste each line of the code below separately and each time press Enter).

import CamCycloid
camCycloidObj = CamCycloid.CamCycloid()
camCycloidObj.createCamProfile()

Please note that once you open FreeCAD, you need to copy-paste the first two lines of this code only once (the first line imports the script and the second line creates an object). Then, every time you want to execute the algorithm of the script (in order to create a new part with the sketch of the profile of the cam), just copy-paste the third line of the code above and then press Enter.

The following message will appear in the Python Console:

Please enter the number of knots (points) that the B-spline (which will approximate the cycloid curve) will have (i.e. number of steps in discretization). Number must be more than 2 (20 is a good choice):

You can write 20 and press Enter.

Then, the following message appears in the python console:

Please enter the data of the cam in the format:
Radius_Of_1st_Dwell_Interval,Start_Angle_Of_1st_Dwell_Interval,End_Angle_Of_1st_Dwell_Interval,Radius_Of_2nd_Dwell_Interval,Start_Angle_Of_2nd_Dwell_Interval,End_Angle_Of_2nd_Dwell_Interval and so on, until you write as many dwell intervals the cam will have. Angles must be in degrees and radius in mm. Use dot for decimal separator. Positive angles are from X axis (x > 0) turning counterclockwise:

In this example we will write the following input data:

60,20,70,80,200,250

and then, when pressing Enter, a new part that has a body with a sketch is being created.

When you open the sketch, the profile of the cam appears.

The first dwell interval of the profile of the cam has a radius of 60 mm. It starts at an angle of 20° and it ends at an angle of 70° (with respect to x axis). During the rotation of the mechanical cam, when the cam-follower is in contact with the dwell interval of the cam, the stroke of the mechanism does not change (i.e. there is no motion when this part of the profile of the cam is in contact with the cam-follower).

The second dwell interval of the profile of the cam has a radius of 80 mm. It starts at an angle of 200° and it ends at an angle of 250° (with respect to the positive direction of x axis, i.e. y = 0 and x > 0).

Please note that when two dwell intervals are overlapping, then an error message appears in the Python Console:

Error: Dwell intervals are overlapping! Please check the angles of the dwell intervals.

Similarly, the radius of a dwell interval must be always positive, and its end-angle always greater than its starting-angle.

Cycloidal function for calculating the transition intervals

The transition from the first dwell interval to the second dwell interval and from the second dwell interval to the first dwell interval is calculated using the cycloidal function:

yOfCycloid = stroke/math.pi * (math.pi*thetaAngleInRad/angleRangeInRad – 0.5*math.sin(2*math.pi*thetaAngleInRad/angleRangeInRad))

⇔

yOfCycloid = stroke*thetaAngleInRad/angleRangeInRad – stroke/(2*math.pi)*math.sin(2*math.pi*thetaAngleInRad/angleRangeInRad)

where:

In this example, the stroke of the 1st transition interval is 20mm (i.e. 80mm – 60mm), and the angle range is 130° (i.e. 200° – 70°), which is equal to 2.268928 rad.

For more information about the cycloidal function for a mechanical cam, you can visit the following link:

https://mech.poriyaan.in/topic/cycloidal-follower-motion-31880

The curve of the cycloidal function is approximated by a B-spline. The more knots you choose that the B-spline will have, the more accurate the approximation will be.

Example 2

In the Python Console write:

camCycloidObj.createCamProfile()

and press Enter.

In the first prompt message write 20 and press Enter.

In the second prompt message write:

60,-30,20,80,150,200

and then press Enter.

You can see that in this case the x axis passes through the curve of the first dwell interval (since the starting angle is -30° with respect to the x axis). Keep in mind that the algorithm converts all the angles to positive angles and then reorders the dwell intervals (in ascending order), but this is not important unless you want to alter/improve the algorithm.

Example 3

In the Python Console write:

camCycloidObj.createCamProfile()

and press Enter.

In the first prompt message write 20 and press Enter.

In the second prompt message write:

60,20,50,80.5,110,140,60,220,240,80.5,310,340

and then press Enter.

The profile of the cam with four dwell intervals has been created.

Creating the 3D model of the mechanical cam

You can extrude the sketch and add some other features in order to create the 3D model of the cam. The picture below illustrates a cam with a hole, in order to be mounted to a shaft. You can add more details e.g. by splitting the cam in two parts and mounting them together using screws, in order to clamp the cam to the shaft.

Changing the default parameters of the algorithm

Before you create the new part using the camCycloidObj.createCamProfile() function in the Python Console, you can change some parameters of the algorithm, if you want.

If you write in the Python Console:

camCycloidObj.useAngleOfDiscretization = True

and press Enter, then the algorithm will place a knot point in the B-spline every 10° approximately (you will not be prompted to enter the number of knots).

If you then write in the Python Console:

import math

and then:

camCycloidObj.angleOfDiscretization = math.radians(5)

then the algorithm will place a knot point in the B-spline every 5° approximately (at every new part that you will create using the camCycloidObj.createCamProfile() function).

If you write in the Python Console:

camCycloidObj.allowAuxPoints = True

then the algorithm will create a construction point between every two adjacent knots, which will be a point of the calculated cycloidal curve (i.e. these construction points are exact solutions of the cycloidal function).

You can use these points then, in order to measure the error of the approximation of the cycloidal function by the B-spline curve, by measuring the distance between one of these auxiliary points and the B-spline curve.

If you write in the Python Console:

camCycloidObj.auxPointsAreConstructionGeom = False

then these auxiliary points will be geometry points.

If you write in the Python Console:

camCycloidObj.allowBsplineInternalAlignment = False

then the knots of the B-splines will not be constrained on the B-splines.

If you write in the Python Console:

camCycloidObj.allowBsplineConstraints = False

then the start and the end of the B-splines will not be constrained on the start and the end of the arcs that represent the dwell intervals. You can change these parameters to false if an error occurs when these constraints are automatically placed.

Scripts

You can find the file CamCycloid.py in the following link:

https://git.disroot.org/CrowdDiscussesAlternatives/ScriptsForFreeCAD/src/branch/master/CamCycloid

The version of the script is shown in the first line of the script. The license of the code is shown in the first lines of the script.

Links

The forum discussion: Presenting CamCycloid macro

The documentation of this script in pdf format: Cam Cycloid Documentation

References

  1. https://mech.poriyaan.in/topic/cycloidal-follower-motion-31880
  2. https://en.wikipedia.org/wiki/Cycloid
  3. https://en.wikipedia.org/wiki/Cam_(mechanism)