LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using Formula Node for Vector Subtraction

Formula Node for subtracting vectors.  Works fine for:

Running Vector 1.4 @ 75 degrees, Slow Roll 0.35 @ 65 degrees

Will not give correct answer for:

Running Vector 1.1 @ 265 degrees, Slow Roll 0.35 @ 65 degrees

I know the Formula Node expects radians, but is there also an issue with angles greater than 180 degrees?

Answer for second example should be 1.43 @ 258.

Thanks in advance.

0 Kudos
Message 1 of 12
(4,121 Views)

Not sure why it doesnt work. I have no experience of the formula node.

For vector subtraction I would use the inbuilt complex datatype like this.

 

Complex.png

 

Not sure if this helps or not.

 

0xDEAD

Message 2 of 12
(4,098 Views)

Sometimes it helps to just stop and think about the problem.  Your "vector" is a polar representation of a 2D Vector, which can also be expressed as a complex number.  In either the 2D Vector formulation (an array of X and Y components) or the complex number formulation, "Vector subtraction" is simply subtracting the two representations.

 

Your first step is to convert your representation to a true Vector representation.  The simplest method may be to use complex numbers, as there is a function, Polar to Complex, that does this (theta, of course, needs to be in radians).  Once you have two complex numbers, subtract them, use Complex to Polar, and convert theta back to degrees (if you wish).

 

The Complex functions are found as a sub-Palette on the Numeric Palette.  No Snippet, as the code is so simple, and you will "learn by doing".

 

Bob Schor

0 Kudos
Message 3 of 12
(4,094 Views)

You answered my question with your example.  Yes, if the angle is greater than 180 degrees, the answer is negative and requires the addition of 2 pi radians!  I just adopted your example.  Much easier code!

0 Kudos
Message 4 of 12
(4,073 Views)

The polar space spans -pi to pi radians (rather than 0 to 2pi).

The polar to complex block in labview is nice. It doesnt care that 265 degrees is 4.62512 radians. It converts it behind the scenes to -1.65806 radians for you.

 

On the output side, there is no conversion it can help with, if the angle is greater than 180 then you will receive a negative angle in radians (since the angle must live in the -pi to pi range). If you want it between 0 and 360 degrees (rather than -180 to 180) you must do that extra conversion step yourself.

 

0xDEAD

 

0 Kudos
Message 5 of 12
(4,067 Views)

@Coryman wrote:

Formula Node for subtracting vectors.  Works fine for:

Running Vector 1.4 @ 75 degrees, Slow Roll 0.35 @ 65 degrees

Will not give correct answer for:

Running Vector 1.1 @ 265 degrees, Slow Roll 0.35 @ 65 degrees

I know the Formula Node expects radians, but is there also an issue with angles greater than 180 degrees?

Answer for second example should be 1.43 @ 258.

Thanks in advance.


Just to answer your original question, the reason your formula node did not give the correct result is that you need to use the atan2(x,y) expression instead.

 

I would still recommend using the complex numbers solution as provided above.

 

formula node.PNG

atan2.PNG

Message 6 of 12
(4,048 Views)

If you use built in support for physical quantities, you don't need to convert units.  Also, you can use the Quotient & Remainder function to keep the angle positive

va.png

"If you weren't supposed to push it, it wouldn't be a button."
Message 7 of 12
(4,030 Views)

I keep saying I learn new things in the Forums -- I know I've heard about "Units", but never have used them, and never looked into them.  What a useful example, Paul -- thanks.

 

Bob Schor

0 Kudos
Message 8 of 12
(4,026 Views)

@paul_cardinale wrote:

If you use built in support for physical quantities, you don't need to convert units.  Also, you can use the Quotient & Remainder function to keep the angle positive

va.png


I am wary of using LabVIEW units because I was always finding bugs with it in the past.

For example, the square primitive wouldn't square the units (you had to multiply the value by itself as the workaround).

I see that's now fixed in the newer versions, but there were so many issues I had to start manually keeping track of units.

 

I just made a control with units Deg, used the "convert units" primitive to convert to Rad.

The value is correctly modified but the unit type doesn't propagate to the indicator.

 

Its a nice trick for the conversion though. Kudos.

 

0xDEAD

0 Kudos
Message 9 of 12
(4,016 Views)

Hi deceased,

 

I just made a control with units Deg, used the "convert units" primitive to convert to Rad.

The value is correctly modified but the unit type doesn't propagate to the indicator.

The "convert unit" does not work this way…

Instead it will convert the value and remove/add the unit. Examples:

When your input is "180 deg" and you convert to "rad" the result will be a value of pi (no unit).

When your input is "180" and you convert to "deg" then the result is "180 deg".

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 10 of 12
(4,010 Views)