# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

hello everyone,

This is a difficult algorithm, I must say at first. At least it is for me.
I have an AC servo motor. I calculated one complete revolution of the motor and mapped it between 0-360 degrees.The motor goes to the given position in the -/+ direction.

If I give -/+ values between 0 and 360 degrees to the motor  after mapping, it will act accordingly.
For example:
If I give -20 degrees the Motor will move in CCW direction and go up to 20 degrees.
If I give +20 degrees, the motor goes 20 degrees to the right.
Note: Here it goes to the absolute position of the digit sent.

The situation I want to create and have a problem with is as follows.
No matter how many turns the motor turns, it should go to the real compass position entered between 0-360 degrees and calculate the shortest path with compass logic.

It's just a question of creating algorithms.

Below is an example that is causing the problem I created.

Can anyone help with this?

Any opinion given is worthy of respect.

Message 1 of 67
(1,967 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

@constructionworker wrote:

For example:
If I give -20 degrees the Motor will move in CCW direction and go up to 20 degrees.
If I give +20 degrees, the motor goes 20 degrees to the right.
Note: Here it goes to the absolute position of the digit sent.

That's quite confusing...

CCW is one direction, to the right the other (CW?). I'd stick with CCW and CW, to the left\right is ambiguous with angles (unless you specify if the bottom or top goes to the left\right).

This calculates the difference between the 2 angles, and gives the shortest dAngle to that position.

Message 2 of 67
(1,961 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

Yes, let me explain the situation again in some detail, as sticking to the CW and CCW would be a bit more confusing.
This is the AC servo I am currently using. The motor moves with absolute position information.
If I send a 20 degree rotation position pulse to the motor it will rotate 20 degrees in the "CW" direction. And again if I send -10 it will return and after reaching 0 it will turn in CCW direction and it will actually go to -10' which corresponds to 350 degrees.
What I want to create is to write numbers between 0-360 degrees, but it doesn't matter whether - position or + position goes to the motor, what matters is that it goes to real compass degrees.

The example you gave does not meet the situation I described above.
because it sends a positive number again after it passes -179 degrees.If I send 180 degrees to the motor after -170, -179 degrees, the motor will return from the CCW position it went to, and after reaching the 0 position, it will go 180 degrees in the CW direction.

I hope I explained well.

Message 3 of 67
(1,929 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

Your example does not make a lot of sense, especially since you are using a greedy loop and only one indicator and one control.

You need a control for the desired new position

You need an indicator (or control when testing) for the current position. (In real life, the current position is given by the hardware)

You need an indicator that shows the +/- angle required to go to the desired position.

I am sure that you can start with Wiebe's example and get it to do what you want with a few tweaks. I am sure you don't need that birds nest of code of your original.

Message 4 of 67
(1,923 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

@constructionworker wrote:

Yes, let me explain the situation again in some detail, as sticking to the CW and CCW would be a bit more confusing.
This is the AC servo I am currently using. The motor moves with absolute position information.
If I send a 20 degree rotation position pulse to the motor it will rotate 20 degrees in the "CW" direction. And again if I send -10 it will return and after reaching 0 it will turn in CCW direction and it will actually go to -10' which corresponds to 350 degrees.
What I want to create is to write numbers between 0-360 degrees, but it doesn't matter whether - position or + position goes to the motor, what matters is that it goes to real compass degrees.

If you just want to convert an angle, + or - to a 0-360 deg. angle, simply do the modulus 360...

Message 5 of 67
(1,897 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

@constructionworker wrote:

Yes, let me explain the situation again in some detail, as sticking to the CW and CCW would be a bit more confusing.
This is the AC servo I am currently using. The motor moves with absolute position information.
If I send a 20 degree rotation position pulse to the motor it will rotate 20 degrees in the "CW" direction. And again if I send -10 it will return and after reaching 0 it will turn in CCW direction and it will actually go to -10' which corresponds to 350 degrees.
What I want to create is to write numbers between 0-360 degrees, but it doesn't matter whether - position or + position goes to the motor, what matters is that it goes to real compass degrees.

The example you gave does not meet the situation I described above.
because it sends a positive number again after it passes -179 degrees.If I send 180 degrees to the motor after -170, -179 degrees, the motor will return from the CCW position it went to, and after reaching the 0 position, it will go 180 degrees in the CW direction.

I hope I explained well.

Maybe this is what you are looking for, give it a try . Thanks to  for most of the code.

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
Message 6 of 67
(1,874 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

wiebe@CARYA wrote:

This calculates the difference between the 2 angles, and gives the shortest dAngle to that position.

Careful there. The difference can be anywhere between -359 and +359.

Message 7 of 67
(1,869 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

@altenbach wrote:

Your example does not make a lot of sense, especially since you are using a greedy loop and only one indicator and one control.

You need a control for the desired new position

You need an indicator (or control when testing) for the current position. (In real life, the current position is given by the hardware)

You need an indicator that shows the +/- angle required to go to the desired position.

I am sure that you can start with Wiebe's example and get it to do what you want with a few tweaks. I am sure you don't need that birds nest of code of your original.

Actually, I'm sure there is no need for this bird's nest, but I have tried every possible way. I probably tried 200 algorithm methods, but I couldn't get exactly what I wanted 🙂

Jay14159265

I have examined and tested the example you have created here, but here it sends the engine how far it needs to move from the normal position. What is required is absolute position information to the engine.
This is what I tried to explain above. So if I send -350 to the motor, the motor goes to -350 degrees in the CCW direction and stops. The motor is doing absolute positioning.

The main problem here is not the difference, I need to send the location directly.

I've been testing it thoroughly but couldn't find a way. That's why I can't answer very quickly.

Message 8 of 67
(1,838 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

As wiebe already answered back in msg #5, the modulus function can be used to map any positive or negative value into your desired compass degree range [0,360).

-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 9 of 67
(1,815 Views)

## Re: Algorithm that can calculate the shortest path between 0-360 degrees for an AC servo motor.

@constructionworker wrote:

Jay14159265

I have examined and tested the example you have created here, but here it sends the engine how far it needs to move from the normal position. What is required is absolute position information to the engine.
This is what I tried to explain above. So if I send -350 to the motor, the motor goes to -350 degrees in the CCW direction and stops. The motor is doing absolute positioning.

So now I am confused, if the servo is at 0deg and you want it to go to 270deg do you send it 270 or -90, or something else?

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
Message 10 of 67
(1,807 Views)