How to control a servo motor with PWM?

Started by bora, August 03, 2023, 02:06:57 PM

Previous topic - Next topic

bora

Hi i have a AM3352 SOM and a servo motor.
I want to control my servo motor with PWM. How can i do this and write C code?
I did some research but I couldn't find how to do this in AM3352 SOM.

LubOlimex

Technical support and documentation manager at Olimex


kyrk.5

What kind of Servo motor do you have? A real servo or an RC Servo?

LubOlimex

You need to first enable the PWM driver then.
Technical support and documentation manager at Olimex

bora

Quote from: LubOlimex on August 21, 2023, 02:37:26 PMYou need to first enable the PWM driver then.
There is no pwm driver on my linux. How can i enable?

bora

Quote from: kyrk.5 on August 17, 2023, 05:46:35 PMWhat kind of Servo motor do you have? A real servo or an RC Servo?
I have Tower Pro MG995 servo motor. I don't have an idea about its.

LubOlimex

Quote from: bora on August 23, 2023, 11:05:41 AM
Quote from: LubOlimex on August 21, 2023, 02:37:26 PMYou need to first enable the PWM driver then.
There is no pwm driver on my linux. How can i enable?

It would require re-building the image with PWM drivers enabled, also you should make sure that the kernel sources have the options to add such support in first place (older kernels might lack PWM entries). PWM driver enabling is detailed in the link I shared above, https://software-dl.ti.com/processor-sdk-linux/esd/docs/07_03_00_005/linux/Foundational_Components/Kernel/Kernel_Drivers/PWM.html
Technical support and documentation manager at Olimex

kyrk.5

I am not quite sure if a PWM Driver is exactly what you need.

As far as I know, the signal is high for around 0.8ms(motor turns to one end)-2.2ms(turns to the other end). The rest of the time it is low. And the period of the signal is 20ms.
1ms is the middle position.

It is obvious, that the high time is relative small compared to the periodic of the signal. And also the range where the signal have meaning, is quite small compared to the period time.

So the problem is, that you need to have a PWM signal with a duty cycle from 4% to 11%. Smaller or bigger values are just moving the motor to the respective end position, so they do not contribute. This means you can only use the 7% range from the whole 100%. Assuming now for example the PWM have 10bit resoultion, this means 1024 different steps or % that you can set. This mean the minimum change would be: 0,09765625%
For 7% it means you have around ca. 71 steps.
An RC Motor can turn around 180 Degree. Dividing it to 71 steps, it would be 2,5 Degree per step.

So it is crucial to know, how many bits the PWM does even have in the HW. Does it have 10 bits? Then you can calculate the degree of steps that you can make. And consider if that one is enough or not.