Sketch Feature to create linkage mechanism simulator

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
NewbieSimulator
Posts: 7
Joined: Tue Apr 11, 2017 1:47 am

Sketch Feature to create linkage mechanism simulator

Post by NewbieSimulator »

The sketch feature can be used to create linkage kinematics simulation using constraints such as lock, coincide, length, angle, etc...
And using the python script you can make an animation out of it.
It's good enough that you can make an animated jansen mechanism using it.

I'm new at python scripting, by the way.

Here I used a simple four-bar linkage.
1st.png
1st.png (177.59 KiB) Viewed 4180 times
The 30 mm crank is constrained angularly with the x-axis. That constraint is what I used to move the mechanism.
You only need to rotate the crank in order to move the entire mechanism.


editing the python script in macro editor, you can manipulate the angle constraint to change value at every instant of time, as well as the interval " i += 0.1"

from PySide import QtCore

i = 0
def update():
global i
# use the "Constraint Number minus 1" for the # in the (#.App.Units.Quantity(i)) in the line below
App.ActiveDocument.Sketch.setDatum(8,App.Units.Quantity(i))
App.ActiveDocument.recompute()
i += 0.1

timer = QtCore.QTimer()
timer.timeout.connect( update )
timer.start( 1 )
this code is the one responsible for changing the value of the selected constraint,
in this case, my angular constraint is number 9, but you must always subtract 1.
I honestly don't know why.
App.ActiveDocument.Sketch.setDatum(8,App.Units.Quantity(i))

you can copy the edited script from the macro editor and paste it in the python console, or execute it directly from the macros.
After that you can exit the sketcher and the animation will still continue.
Just type timer.stop() to stop the animation
3rd.png
3rd.png (130.17 KiB) Viewed 4171 times
Last edited by NewbieSimulator on Fri Apr 21, 2017 1:14 pm, edited 1 time in total.
saiteja
Posts: 1
Joined: Tue Apr 18, 2017 7:40 am

Re: Sketch Feature to create linkage mechanism simulator

Post by saiteja »

Hello,

I'm new to FreeCad and the forum. Could you walk me through step by step on the procedure. I've tried what you did but it did not work for me. I'm hoping to simulate a Five Bar Mechanism one day similar to Dexter Robot.

Thank You
Sai
User avatar
r-frank
Veteran
Posts: 2180
Joined: Thu Jan 24, 2013 6:26 pm
Location: Möckmühl, Germany
Contact:

Re: Sketch Feature to create linkage mechanism simulator

Post by r-frank »

Hello sai.

Welcome to FreeCAD and the forum.
For more convenience i would use the animation workbench (extra add-on, Needs to be installed).
I made a tutorial for a four bar linkage with german audio.

Since the pace is slow i am hoping that it will give you a first start.

You may also have a look at the macro animated constraint.

Roland
Deutsche FreeCAD Tutorials auf Youtube
My GrabCAD FreeCAD-Projects
FreeCAD lessons for beginners in english

Native german speaker - so apologies for my english, no offense intended :)
NewbieSimulator
Posts: 7
Joined: Tue Apr 11, 2017 1:47 am

Re: Sketch Feature to create linkage mechanism simulator

Post by NewbieSimulator »

saiteja wrote:Hello,

I'm new to FreeCad and the forum. Could you walk me through step by step on the procedure. I've tried what you did but it did not work for me. I'm hoping to simulate a Five Bar Mechanism one day similar to Dexter Robot.

Thank You
Sai
This 2D technique is only limited to studying kinematics of linkage mechanisms.
If you want to animate an assembly you should follow Roland's advice.

You should study making constraints in the sketch mode, check it on youtube.

Constraints are used to limit the parameters of an element, for example, a line in sketch mode consists of 2 points, and 1 edge. You can constrain a point to a specific position, which means you lock it to that position. Or you constrain its length to a specific length.

You can use the concepts of mechanisms to choose which constraint you'll use. Since linkage mechanisms have links which are connected, you have to use coincident constraint at each intersection. And they also have constant lengths, so you have to constrain the lengths of each link. If you successfully constrained all the necessary elements you can interactively move the mechanism with your mouse. The animation us just extra.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Sketch Feature to create linkage mechanism simulator

Post by mario52 »

hi

you must use the expression for create constraint and animate all "ensemble"

here example :
only the circle rotation create the movement for all objects connected

see here for + explanation Macro_Constraint_Draft

Image

the file
Constraint_Draft00.FCStd
(11.96 KiB) Downloaded 552 times
+ 1 For the same sketcher constraint system in the DraftWorkbench

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
NewbieSimulator
Posts: 7
Joined: Tue Apr 11, 2017 1:47 am

Re: Sketch Feature to create linkage mechanism simulator

Post by NewbieSimulator »

I made a video regarding making a simple four-bar linkage and sliding linkage.

https://www.youtube.com/watch?v=8llFZxxloXg

there is no python scripting in the video , since the animation script doesn't seem to work in the version of freeCAD I'm using.
User avatar
wandererfan
Veteran
Posts: 6265
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Sketch Feature to create linkage mechanism simulator

Post by wandererfan »

NewbieSimulator wrote:this code is the one responsible for changing the value of the selected constraint,
in this case, my angular constraint is number 9, but you must always subtract 1.
I honestly don't know why.
The OCC geometry kernel indexes arrays [1..n] rather than the usual C++/Python convention of [0..n-1]. The naming of Edges, Vertices, Faces, etc reflects the OCC convention, while the actual Python lists in FreeCAD follow the Python convention.

Now you know.

wf
NewbieSimulator
Posts: 7
Joined: Tue Apr 11, 2017 1:47 am

Re: Sketch Feature to create linkage mechanism simulator

Post by NewbieSimulator »

wandererfan wrote:
NewbieSimulator wrote:this code is the one responsible for changing the value of the selected constraint,
in this case, my angular constraint is number 9, but you must always subtract 1.
I honestly don't know why.
The OCC geometry kernel indexes arrays [1..n] rather than the usual C++/Python convention of [0..n-1]. The naming of Edges, Vertices, Faces, etc reflects the OCC convention, while the actual Python lists in FreeCAD follow the Python convention.

Now you know.

wf
Thanks for the extra info! I'll keep that in mind next time I do some coding.
Post Reply