07-16-2020 09:23 AM - edited 07-16-2020 10:17 AM
I cannot think this morning... what I need is super simple I feel like, but I can't figure out an easy way to implement it. Please halp.
Inputs: angles c and d:
current angle = c
desired angle = d
Output: angle z that is closest angle to c but is also in the set {d, d+-360, d+-720, ...}
examples:
c=0
d=270
z=-90
c=765
d=0
z=720
c=-361
d=-1
z=-361
c=0
d=3,602
z=2
Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.
Solved! Go to Solution.
07-16-2020 09:56 AM - edited 07-16-2020 09:56 AM
Hi Andrew,
@FireFist-Redhawk wrote:
inputs: angles c and d:
current angle=c
desired angle = doutput: angle z that is closest angle
examples:c=765
d=0
z=720
Please explain this example!
Why should z be 720° when c=765° (=720°+45°) and d=0°???
07-16-2020 10:13 AM
Hi GerdW,
I'm not sure I understand you're query. If my current angle shows 765, and I want to go to 0 (same place for me as +-360,+-720), then z is 720 because its the member of that set whose value is closest to my current angle, 765.
Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.
07-16-2020 10:19 AM
That description was initially a comment label in LV, I went back and cleaned it up to possibly get rid of the confusion, and added a couple more examples.
Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.
07-16-2020 10:24 AM - edited 07-16-2020 10:26 AM
I think this does everything you need.
The idea here is to find the differences and keep that difference in a shift register. Add (or subtract) 360 until the absolute value of the difference is greater. The previous value is therefore the closest value.
07-16-2020 10:25 AM
Can you explain it graphically instead?
07-16-2020 10:31 AM - edited 07-16-2020 10:32 AM
07-16-2020 10:48 AM - edited 07-16-2020 10:54 AM
Let's say
e=(c-d)
and z'=(z-d)
then, you are looking for a z' in {..., -720, 360, 0, 360, 720,...} for which the difference to e is minimal.
Dividing e by 360 using the quotient & remainder VI gives a quotient which, multiplied with 360 again, is the next lower multible of 360 to e. If the remainder is >180, add 360 to get the nearest value. This is z'. Add d to get z.
In LabVIEW:
EDIT:
c=0d=3,602
z=2
z must be in {..., d-360, d, d+360,...}, so it must be d=3,602 to be next to c....
07-16-2020 10:57 AM
@crossrulz wrote:
I think this does everything you need.
That's very dangerous code because it will run forever if any of the inputs is NaN. 😮 Maybe you want to reverse some logic so it stops on FALSE instead.
(I am pretty sure there is a loop-free solution ;))
07-16-2020 12:08 PM
@altenbach wrote:
Can you explain it graphically instead?
I think we all have the idea at this point but it took me a few mins to make, so may as well post.
Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.