1. ## Analog linear motor

Hello all,

I'm new to C programming.
And I have to control an analog linear motor.
That ain't the problem but.

Problem description:
The motor is moving an axis.
So the distance he can travel is limited.
The speed of the motor has to accelerate in the beginning and slow down at the end before stopping.
He has to do this with the priniciple of a sine wave.

The user is able to change the cycle time.

Now my big problem is. When you use a sine wave and you can change cycle time the surface under the sine wave has to be same anytime (because the distance keeps the same).
Probably you have to do this by using integration but I don't know how to do this in C.

Thank you very much for even answering.

Koen

English ain't my native language, so sorry for any grammatical errors

2. Are you in the same class?
How to write programs for PWM in C ?

3. Originally Posted by Salem
Are you in the same class?
How to write programs for PWM in C ?
No, I don't know this person. But if you want the same information as you asked on his question.

actuator: LINAK

programming chip: PICF2580 from microchip.com (http://www.microchip.com/wwwproducts...cName=en020613)

4. Do you mean you have an analog linear positioning drive of some sort, and you want to control its position as a function of time as
Code:
`x(t) = x0 + L sin (2 π  t / t0)`
where x0 is the center point, L is half the total travel, and t0 is the duration of each full cycle of movement?

Or do you mean you want to find out how to calculate the required velocity to move the drive to a new position using a sine acceleration curve? In other words, for each stroke from x0 to x1 in time t = 0 .. t1:
Code:
`x(t) = x0 + (x1 - x0) (1 - cos( π t / t1 ) ) / 2`
which has a velocity curve
Code:
`v(t) = π (x1 - x0) / t1) sin( π t / t1 )`
(a single "hump" of a sine wave, a half wave from zero to max velocity back to zero).

Since you have an analog linear positioning drive, the latter also gives you the PWM duty cycle you need to drive the analog motor:
Code:
`duty(t) = dutymax / vmax |v(t)|`
and the sign of v(t) tells you the direction.
_____________________________________

In real life you cannot control the duty cycle continously, so you need to discretize the algorithm (which replaces a short interval of the curve with a fixed value of the same duration, that has the same effect). Instead of v(t) or duty(t), you need dutyi for i = 0 .. N-1 steps, N = round(t1·rate).

The simplest option is to first do the calculations like I showed above to make sure the curve yields the desired velocity or acceleration profile, then set the duty cycle for each sampling period to the value that yields the desired movement. Assuming for a smooth movement from point x0 to x1 in N steps (t1 time duration), with initial and final velocities zero, you get
Code:
```ti = i / N t1
Δxi = x(ti+1) - x(ti) = (x1 - x0) ( cos(π i / N + π / N) - cos(π i / N) ) / 2
dutyi = dutymax / vmax Δxi```
i.e.
Code:
`dutyi = dutymax / vmax (x1 - x0) ( cos(π i / N + π / N) - cos(π i / N) ) / 2,  i = 0 .. N-1`
To compute the sine and cosine, use the CORDIC algorithm. If computation speed becomes an issue, you can try replacing cos(π i / N + π / N) - cos(π i / N) with the mathematically equivalent, but simpler
Code:
`const1 cos(π i / N) - const2 sin(π i / N)`
where
Code:
```const1 = cos(π / N) - 1
const2 = sin(π / N)```

5. Thank you very much.

I want to regulate the speed of the motor so it moves a bit like this (quick motion study, so no reference to it). motor assembly.avi - Speedy Share - upload your files here

When we regulate the speed we have an output from 0-5v but we use a linear amplifier that will translate it into: 0V = -10V, 2.5V = 0V and 5V = 10V
So its not necessary to use a hump.
The acuator gives feedback at what position he is.

By the way, thank you for giving me a solution for a problem were I didn't even asked for.

6. Originally Posted by Koen
I want to regulate the speed of the motor so it moves a bit like this (quick motion study, so no reference to it).
Look into
Code:
`x(t) = x0 + ( x1 - x0 ) ( 3 t2 - 2 t3 ),  t = 0 .. 1`
The overall feel of the movement is very similar, but the velocity and acceleration curves are
Code:
```v(t) = d x(t) / d t = 6 ( x1 - x0) ( t - t2 )
a(t) = d2 x(t) / d t2 = 6 ( x1 - x0 ) ( 1 - 2 t )```
which means that given just current position and desired position, and maximum velocity and maximum acceleration, you can determine the minimum time you need to do it, and how to do it.

Using the same approach as in my previous post, (but this time t = 0 .. 1),
Code:
```ti = i / N
Δxi = x(ti+1) - x(ti) = (x1 - x0) / N3 ( -6 i2 + 6 (N - 1) i + 3 (N - 1) + 1 )```
where Δxi tells you how far the actuator must move in each time step i, and therefore the duty cycle needed to achieve that -- and in N timesteps, the desired movement using the above velocity and acceleration profile. (You almost always use a fixed-size steps, duration being fixed to some integer multiple of the PWM period you use.)

Because this time you only need one division, and few multiplications, this is very lightweight to compute on a microcontroller. Just note that you need to work with integers up to N3 and (x1-x0)N2, whichever is larger, in magnitude.

Finally, it is possible to incorporate the position feedback on a linear actuator, really turning it into a digitally controlled servo. It gets a bit more complicated, but basically you always use the current position as x0, with x1 being the desired position. Current velocity you know (since it is essentially the PWM duty cycle). In each time step, you simply pick a new velocity towards the target, just one that is within the allowed velocity and acceleration limits. (If you have time steps, acceleration is (new velocity - old velocity) / duration. If you specify acceleration limits by values multiplied by duration, you simply have the maximum speed change allowed per time step. Very simple!) You really only worry about being able to stop at the desired coordinates in time, without exceeding speed or acceleration limits. I think the OpenServo servos do something very similar to this.

7. Thank you very much.
In the next couple of weeks I'll implant this in my program. (don't have that much time now)
So when I've done it you will certainly hear something about it.