Need some help with the math

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
sliptonic
Veteran
Posts: 3460
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Need some help with the math

Post by sliptonic »

The attached file has a sketch with a single degree of freedom which is the angle of the long blue line. The angle is currently a reference angle but if you turn it into a regular constraint, the sketch becomes locked. With it set as a reference angle, the smaller circle can roll around the larger one.

I need a function that, given this arrangement, returns the coordinates of the center of the smaller circle for a given input angle. The sketch is just to demonstrate the setup.

I posted the problem on stackexchange and got some answers but I still don't understand how to solve the system of equations in python.

2020-04-27_12-37.png
2020-04-27_12-37.png (26.54 KiB) Viewed 1654 times
Attachments
bendingexperiment.FCStd
(8.46 KiB) Downloaded 37 times
User avatar
mpetrasinovic
Posts: 106
Joined: Sat Feb 22, 2020 10:19 am
Location: Belgrade, Serbia
Contact:

Re: Need some help with the math

Post by mpetrasinovic »

From the following image, you can see that there are two possible solutions. You need to decide which one is right using additional condition.

img1.png
img1.png (37.98 KiB) Viewed 1634 times

In python, you can use the following code.

Code: Select all

R1 = 20
R2 = 11
alpha = 50

alpha_r = alpha*math.pi/180
k = math.tan(math.pi/2+alpha_r)
n = R1*math.sin(alpha_r)-k*R1*math.cos(alpha_r)

a = 1+k**2
b = 2*k*(n+R2)
c = (n+R2)**2-(R1+R2)**2

x1 = (-b+math.sqrt(b**2-4*a*c))/(2*a)
y1 = k*x1+n+R2

x2 = (-b-math.sqrt(b**2-4*a*c))/(2*a)
y2 = k*x2+n+R2
Last edited by mpetrasinovic on Mon Apr 27, 2020 8:55 pm, edited 1 time in total.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Need some help with the math

Post by openBrain »

mpetrasinovic wrote: Mon Apr 27, 2020 8:09 pm In python, you can use the following code.
'alpha' should be used to compute 'alpha_r'. ;)
User avatar
mpetrasinovic
Posts: 106
Joined: Sat Feb 22, 2020 10:19 am
Location: Belgrade, Serbia
Contact:

Re: Need some help with the math

Post by mpetrasinovic »

openBrain wrote: Mon Apr 27, 2020 8:48 pm
mpetrasinovic wrote: Mon Apr 27, 2020 8:09 pm In python, you can use the following code.
'alpha' should be used to compute 'alpha_r'. ;)
That's right, thank you.
User avatar
mpetrasinovic
Posts: 106
Joined: Sat Feb 22, 2020 10:19 am
Location: Belgrade, Serbia
Contact:

Re: Need some help with the math

Post by mpetrasinovic »

sliptonic wrote: Mon Apr 27, 2020 9:51 pm Perfect! Thank you :D
Glad to help. I can provide some details if you need them.
kisolre
Veteran
Posts: 4166
Joined: Wed Nov 21, 2018 1:13 pm

Re: Need some help with the math

Post by kisolre »

sliptonic wrote: Mon Apr 27, 2020 9:51 pmPerfect!
Could you provide some background for that kinematic?
User avatar
sliptonic
Veteran
Posts: 3460
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Need some help with the math

Post by sliptonic »

kisolre wrote: Tue Apr 28, 2020 7:39 am
sliptonic wrote: Mon Apr 27, 2020 9:51 pmPerfect!
Could you provide some background for that kinematic?
Sure!
And if you guys want to help with more math, there's one more area described below that I haven't finished.

Short version.
I started playing with wire bending. I thought that adding a wire-bending capability to the Path workbench would be a nice feature.

Much Longer Version
(Background Thread and some videos)
Wire bending is pretty different from regular CNC milling but there's enough overlap to be interesting. I'm not sure exactly what this would look like in Path but I have some ideas.

Lenny
For my own experimentation, fun, and to make some content for my youtube channel, I decided to turn Lenny The Big Dumb Bot into the world's dumbest wire bender. Now this is absolutely the WRONG way to build a wire bender. If I actually needed a wire bender I wouldn't want this one. In fact, no one in their right mind would do it this way but Lenny is what I have so that's what I'm going to do.

Instead of a moving mechanism that grabs the wire and bends it around the tip, Lenny is going to move the tip around a fixed bending mandrel (green). The math problem in this thread is needed to calculate the ending position of the tip for a given wire bend angle.

LBDB1.png
LBDB1.png (28.9 KiB) Viewed 1481 times
LBDB2.png
LBDB2.png (45.63 KiB) Viewed 1481 times
LBDB3.png
LBDB3.png (15.88 KiB) Viewed 1481 times

What's Needed:
Besides building the bending head and getting Lenny to do his thing, I still need to start with a wire in FreeCAD and calculate the bends and rotations. The process will take a wire as input. It will extract the vertices of sharp bends and discretize arcs into a segments of desired length. That's easy and I've done it already.

The next thing is to take those vertices and turn it into "B-code". B-code is industry standard for wire benders. It's just a table of three values. Length, Rotation, and bend Angle. You start at one end of the wire:
Length is the distance from one vertex to the next and the amount of wire Lenny will Feed out.
Rotation is the angle to rotate to bring the next vertex into the bending plane.
Angle is how much to bend.
Repeat for each vertex in the list.

I found a decent paper that describes it very well. The math is included but no python code. So if someone wants to help, this would be a great place to jump in.

Once I have B-code, a custom postprocessor will turn it into G-code that Lenny can use.
User avatar
mpetrasinovic
Posts: 106
Joined: Sat Feb 22, 2020 10:19 am
Location: Belgrade, Serbia
Contact:

Re: Need some help with the math

Post by mpetrasinovic »

sliptonic wrote: Tue Apr 28, 2020 4:26 pm I found a decent paper that describes it very well. The math is included but no python code. So if someone wants to help, this would be a great place to jump in.
In python, you can use the following code to obtain the same results as in the paper.

Code: Select all

import numpy as np

P = np.array([[39.92, -54.48, 7.7],
        [24.8, 3.586, 7.69],
        [-4.71, 9.002, 0],
        [-4.35, -18.32, -12.39],
        [-5.32, 20.99, -19.75],
        [ -8.53, 39.59, -92.33],
        [ -13.22, 25.77, -165.9],
        [-14.9, -12.96, -175.75],
        [-15.01, 15.11, -186.33],
        [14.5, 9.43, -186.36],
        [29.05, -48.76, -187.38]])

L = np.linalg.norm(P[:-1,:]-P[1:,:] ,axis=1)
p = np.cross(P[2:-1,:]-P[:-3,:],P[1:-2,:]-P[:-3,:])
q = np.cross(P[3:,:]-P[1:-2,:],P[2:-1,:]-P[1:-2,:])
beta = np.insert(np.degrees(np.arccos(np.sum(np.divide(p,np.linalg.norm(p,axis=1)[:,None])*np.divide(q,np.linalg.norm(q,axis=1)[:,None]),axis=1))),0,0)
v1 = P[:-2,:]-P[1:-1,:]
v2 = P[2:,:]-P[1:-1,:]
alpha = np.degrees(np.arccos(np.sum(np.divide(v1,np.linalg.norm(v1,axis=1)[:,None])*np.divide(v2,np.linalg.norm(v2,axis=1)[:,None]),axis=1)))
theta = 180-alpha
User avatar
sliptonic
Veteran
Posts: 3460
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Need some help with the math

Post by sliptonic »

That's fantastic! Thank you again.

I'll report back when I've got the big dummy bending wires :D
Post Reply