How to control a linear drive motor using PWM

Thread Starter

ponas.jonas

Joined Feb 28, 2023
36
I am trying to control a linear drive motor with a built-in encoder. I am using a motor driver to move the linear motor. I have calculated a PWM signal for a PWM frequency of 1kHz and a duty percent of 50% with the following values:
  1. Internal Clock = 180 MHz
  2. Prescaler = 180
  3. Arr register (calculated) 1MHz
  4. CCRx = 500
After I ran my code and checked with an oscilloscope to confirm that it is doing what was calculated, and that was correct. (snippet below)
C:
 TIM1->CCR1 = 500;
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

Now I am struggling to comprehend how you control the speed of the motor and how do you control when it is moving and when it is not. Could anyone give me some starting points please?
 

StefanZe

Joined Nov 6, 2019
191
Hi,
The speed of the motor is controlled with a controll loop. The feedback form the motor are the encoder outputs. With these outputs you can calculate how fast your motor is moving and adapt the pwm signal accordingly.
With the reset pin of the motor driver you can disable the motor and the DIR pin is used to reverse the direction.
Hope this helps.
 

Thread Starter

ponas.jonas

Joined Feb 28, 2023
36
Why would 1KHz be too low for a PWM controller?

Also how do I find out what the smallest duty cycle is needed to push the linear drive motor the smallest distance possible? (the encoder resolution is 5 μm ). Is it solely by trial and error?

I currently have managed to bet the drive to work with the aforementioned variables. The supply I have is 10V
 

StefanZe

Joined Nov 6, 2019
191
Your control loop should be able to settle the distance within the encoder resolution (5µm to 10µm).
But there is no duty cycle that will move your motor a given distance. The duty cycle will controll the force the motor is producing.
You need a control loop to move a given distance.
 

Thread Starter

ponas.jonas

Joined Feb 28, 2023
36
Thank you for your reply :)

In order to do that I would have to choose the optimal PWM frequency as well as duty cycle, correct? If so, what is the best way to go about so, would it be just to choose the highest clock speed, highest possible frequency the motor driver can withstand etc? Or would it not matter once I start designing my PID controller?
 

Thread Starter

ponas.jonas

Joined Feb 28, 2023
36
Is there a specific reason why such a PWM frequency would be selected? Is there any benefit for choosing a higher or lower frequency?
 

Thread Starter

ponas.jonas

Joined Feb 28, 2023
36
I have made it work with the PWM frequency of 5kHz a while ago and everything works correctly, but I still am slightly confused and cannot explain WHY I used this value. Is there a simple step process to go through to find the optimal PWM signal?

I have tried looking for scientific articles and such but nothing really gives me a straightforward answer...
 

StefanZe

Joined Nov 6, 2019
191
Hi,
some factors that play a role in selecting the PWM frequency:
.) the duty cycle can only be updated as fast as the frequency. So the control loop can only be as fast as your PWM frequency.
.) some frequencies will produce a buzzing sound from the motor (mechanical resonance)
.) Very fast frequencies will produce more heat in your drivers and more electrical noise.

So 5kHz is a good starting point to try things out. If problems occure with this frequency it can be changed to a higher or lower value.
 

Thread Starter

ponas.jonas

Joined Feb 28, 2023
36
Thank you for your reply :)

Could you please elaborate what you mean " the control loop can only be as fast as your PWM frequency" ? By control loop do you mean the PID controller I am using to move the motor?

If so, I know as an estimate that the calculations inside take ~3 micro seconds. I have the PID controller in a global interrupt timer (TIM5) which triggers every 7 micro seconds. In this case, it does not really have anything to do with the PWM signal, right? Because in this TIMER 5/PID function I change the TIMER 3 CCR1: "htim3.Instance->CCR1" to set the duty cycle of the PWM.

Lets say I try 5kHz and it works. How can I be sure that lets say 6.3kHz is not better?
 

StefanZe

Joined Nov 6, 2019
191
Hi,
If the PWM is 5kHz the signal needs 200µs for on period (if the duty cycle is 50% it is 100µs high and 100µs low). The duty cycle cannot be more than one value per period.

But does your feedback signal update with the same speed? Calculating the control loop without a new feedback signal will not give you a faster loop (unless you can estimate a new feedback value).

Test it with 6.3kHz and then you will know. But you have to define what is better for you.
 

MaxHeadRoom

Joined Jul 18, 2013
28,617
I have made it work with the PWM frequency of 5kHz a while ago and everything works correctly, but I still am slightly confused and cannot explain WHY I used this value. Is there a simple step process to go through to find the optimal PWM signal?

I have tried looking for scientific articles and such but nothing really gives me a straightforward answer...
Generally the aim is to use a PWM freq. as low as possible without any undesirable affects of any kind, As a rule the maximum you should need to go for motor control is ~20khz
 

Irving

Joined Jan 30, 2016
3,843
AFAIK the main reason for choosing a higher fundamental frequency is to avoid self resonance. A low frequency PWM at 50% duty will generate harmonics at fundamental and 3rd, 5th, etc, above. At other duty cycles it will be at fundamental, 2nd, 3rd, etc too. These harmonics produce annoying audible resonances, vibration in the load, and, worst case, can be destructive. They can also contribute to heating in the motor without assisting motion. By starting with a higher fundamental most, if not all, can be damped out.

Before choosing your PWM you need to define your motion profile, and that will be determined by several factors, including the mass you need to move, the speed you need to move it at, and the resolution required.
 

Irving

Joined Jan 30, 2016
3,843
Further to various comments above, as already said, PWM controls the current in the coil and therefore the force produced. Without any load that will result in rapid acceleration of the motor (recall that F = m . a, so a = F/m), and potentially a high terminal velocity into the end-stop (you have only 25mm to play with on that motor). A control loop is needed to constrain both acceleration and velocity in order to match your defined velocity profile which is itself informed by your positioning requirements. There are various options for the velocity profile, see the tech tips document here from the manufacturers of your motor. A simple and common one is the equal acceleration/deceleration trapezoidal profile (2nd one in document) that looks like this:

1683717494307.png

To get to a specific location from where you are currently requires an acceleration phase, a coasting phase and a deceleration phase. What that means in practice is determined by your load & system inertia, frictional losses in slides, etc. The PID control algorithm will have feedback loops from the encoder to assess & control acceleration/deceleration, velocity and ultimately position. The output from the algorithm is a Force/current value and a direction. Your input to the algorithm will be the desired position, either absolute or relative to current position, plus some preset parameters regarding the motor, and maximums for eg velocity, etc. The PID tuning will optimise for system inertia, frictional losses, etc. Normally you don't care or need to worry about the PWM, the control loop will manage that to maximise performance while managing to keep motor heating within parameters.

Rather than worrying about PWM frequency, you need to back up and consider the whole control loop, find a suitable PID control algorithm to implement (or better, an open-source package as debugging such software is very difficult). If you have access to MATLAB I'd recommend using their 'hardware-in-the-loop' simulation which will use your MCU & motor controller to tune an off-board PID algorithm after which you can then export the C or C++ code to your MCU to run stand-alone.
 
Top