matejpavlovic
Hello,

I'm trying to use a servo motor controlled by a PWM signal which requires a PWM signal frequency of 50Hz. Is there a way to set the frequency of the PWM signal to which the analog output %QW0.0 is translated?
I'm running OpenPLC on a Raspberry Pi 3.

Thank you for any suggestions!

Best,
Matej
Quote 0 0
thiagoralves
The OpenPLC uses the PWM pin in balanced mode, which means that the frequency varies according with the duty cycle. This is the default mode for the broadcom chip. If you want to set a fixed frequency, you will have to modify the RaspberryPi driver for OpenPLC. The driver is the file /core/hardware_layer.cpp. On the initializeHardware() function, you will have to set the PWM to the traditional mark and space (fixed frequency) with
pwmSetMode(PWM_MODE_MS);

The Raspberry Pi PWM clock has a base frequency of 19.2 MHz. This frequency, divided by the argument to pwmSetClock(), is the frequency at which the PWM counter is incremented. When the counter reaches a value equal to the specified range, it resets to zero. While the counter is less than the specified duty cycle, the output is high, otherwise the output is low.

It means that, if you want to set the PWM to have a specific frequency, you will have to change the pwmRange() and pwmClock() according to the following relationship:
pwmFrequency in Hz = 19200000 Hz / pwmClock / pwmRange

OpenPLC uses pwmRange = 1024, so I suggest you don't change that. According to this equation, your pwmClock should be 375:
19200000 Hz / 375 / 1024 = 50 Hz

Therefore, still on the initialization function, call:
pwmSetClock(375);

to have the PWM frequency set to 50 Hz fixed.

You can combine these functions in different ways to have different frequencies of PWM. Keep in mind that each time you change the hardware_layer.cpp file, you will have to upload your PLC program again for the changes to take effect.
Quote 1 0
matejpavlovic
Thank you very much for the extremely rapid, exhaustive and helpful response!
It worked flawlessly.
Quote 0 0
vidya.mazhur
Thanks a lot. This was what I was looking for.

Regards,
Vidya
Quote 0 0
xale12
IMG_0732.jpg  IMG_0733.png 
Quote 0 0
xale12
I try to do like what you say but absolutely I am absolutely  wrong by some way .
Quote 0 0
matejpavlovic
You need to put the code inside the initializeHardware() function:

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------
void initializeHardware()
{
    pwmSetMode(PWM_MODE_MS)
    pwmSetClock(375)
}

Quote 1 0
xale12
 Thank alot!! 
Quote 0 0
xale12
Is there any different between pi and arduino . I use  a arduino uno for slave .
Quote 0 0
xale12
I think that the PWM action control by the digital pin-like QX0.0 why in this case he uses the QW0 (What is an analog output ) . I know It call a PWM pin but I am so confused. Can anyone answer me !!
Quote 0 0
xale12
[QUOTE username=matejpavlovic userid=4844501 postid=1311275884]You need to put the code inside the initializeHardware() function:

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------
void initializeHardware()
{
    pwmSetMode(PWM_MODE_MS)
    pwmSetClock(375)
}
you deleted all the previous code of this fuction ??
Quote 0 0
xale12
ggggg.png 
Quote 0 0
thiagoralves
@xale12 Go read about what a PWM is and then you won’t be confused anymore. In very simple terms, PWM allows a digital pin to “emulate” analog voltage by rapidly switching its output. That’s why OpenPLC treats that pin as an analog pin.

About your error, it would be easier if you posted your whole hardware layer here instead of just snippets, then we can check where the error is
Quote 0 0
xale12
ggg.jpg 
Quote 0 0
xale12
I insert the code you said at the end of initializehardware fuction . When I compile in open PLC It show the error hhh.png 
Quote 0 0