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
Reply 0 0
xale12
tttt.png 
Reply 0 0
thiagoralves
xale12 wrote:
ggg.jpg 

When I said to post your code here I actually meant for you to copy and paste the entire file here so it is readable. Your low-res picture only shows half of your code, and more surprisingly, doesn't show where the problem is, so it is of no help.
Reply 0 0
xale12
//-----------------------------------------------------------------------------
// Copyright 2015 Thiago Alves
//
// Based on the LDmicro software by Jonathan Westhues
// This file is part of the OpenPLC Software Stack.
//
// OpenPLC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenPLC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with OpenPLC. If not, see < http://www.gnu.org/licenses/>.
//------
//
// This file is the hardware layer for the OpenPLC. If you change the platform
// where it is running, you may only need to change this file. All the I/O
// related stuff is here. Basically it provides functions to read and write
// to the OpenPLC internal buffers in order to update I/O state.
// Thiago Alves, Dec 2015
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <pthread.h>

#include "ladder.h"
#include "custom_layer.h"

#if !defined(ARRAY_SIZE)
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
#endif

#define MAX_INPUT 14
#define MAX_OUTPUT 11
#define MAX_ANALOG_OUT 1

/********************I/O PINS CONFIGURATION*********************
* A good source for RaspberryPi I/O pins information is:
* http://pinout.xyz
*
* The buffers below works as an internal mask, so that the
* OpenPLC can access each pin sequentially
****************************************************************/
//inBufferPinMask: pin mask for each input, which
//means what pin is mapped to that OpenPLC input
int inBufferPinMask[MAX_INPUT] = { 8, 9, 7, 0, 2, 3, 12, 13, 14, 21, 22, 23, 24, 25 };

//outBufferPinMask: pin mask for each output, which
//means what pin is mapped to that OpenPLC output
int outBufferPinMask[MAX_OUTPUT] = { 15, 16, 4, 5, 6, 10, 11, 26, 27, 28, 29 };

//analogOutBufferPinMask: pin mask for the analog PWM
//output of the RaspberryPi
int analogOutBufferPinMask[MAX_ANALOG_OUT] = { 1 };

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is initializing.
// Hardware initialization procedures should be here.
//-----------------------------------------------------------------------------

void initializeHardware()
{
wiringPiSetup();
//piHiPri(99);

//set pins as input
for (int i = 0; i < MAX_INPUT; i++)
{
if (pinNotPresent(ignored_bool_inputs, ARRAY_SIZE(ignored_bool_inputs), i))
{
pinMode(inBufferPinMask[i], INPUT);
if (i != 0 && i != 1) //pull down can't be enabled on the first two pins
{
pullUpDnControl(inBufferPinMask[i], PUD_DOWN); //pull down enabled
}
}
}

//set pins as output
for (int i = 0; i < MAX_OUTPUT; i++)
{
if (pinNotPresent(ignored_bool_outputs, ARRAY_SIZE(ignored_bool_outputs), i))
pinMode(outBufferPinMask[i], OUTPUT);
}

//set PWM pins as output
for (int i = 0; i < MAX_ANALOG_OUT; i++)
{
if (pinNotPresent(ignored_int_outputs, ARRAY_SIZE(ignored_int_outputs), i))
pinMode(analogOutBufferPinMask[i], PWM_OUTPUT);
}
//-----------------------------------------------------------------------------
pwmSetMode(PWM_MODE_MS);
pwmSetClock(375);
}

//-----------------------------------------------------------------------------
// This function is called by the main OpenPLC routine when it is finalizing.
// Resource clearing procedures should be here.
//-----------------------------------------------------------------------------
void finalizeHardware()
{
}

//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual state of the input pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateBuffersIn()
{
pthread_mutex_lock(&bufferLock); //lock mutex

//INPUT
for (int i = 0; i < MAX_INPUT; i++)
{
if (pinNotPresent(ignored_bool_inputs, ARRAY_SIZE(ignored_bool_inputs), i))
if (bool_input[i/8][i%8] != NULL) *bool_input[i/8][i%8] = digitalRead(inBufferPinMask[i]);
}

pthread_mutex_unlock(&bufferLock); //unlock mutex
}

//-----------------------------------------------------------------------------
// This function is called by the OpenPLC in a loop. Here the internal buffers
// must be updated to reflect the actual state of the output pins. The mutex buffer_lock
// must be used to protect access to the buffers on a threaded environment.
//-----------------------------------------------------------------------------
void updateBuffersOut()
{
pthread_mutex_lock(&bufferLock); //lock mutex

//OUTPUT
for (int i = 0; i < MAX_OUTPUT; i++)
{
if (pinNotPresent(ignored_bool_outputs, ARRAY_SIZE(ignored_bool_outputs), i))
if (bool_output[i/8][i%8] != NULL) digitalWrite(outBufferPinMask[i], *bool_output[i/8][i%8]);
}

//ANALOG OUT (PWM)
for (int i = 0; i < MAX_ANALOG_OUT; i++)
{
if (pinNotPresent(ignored_int_outputs, ARRAY_SIZE(ignored_int_outputs), i))
if (int_output[i] != NULL) pwmWrite(analogOutBufferPinMask[i], (*int_output[i] / 64));
}

pthread_mutex_unlock(&bufferLock); //unlock mutex
}
Reply 0 0
xale12
ptimizing ST program...
Generating C files...
POUS.c
POUS.h
LOCATED_VARIABLES.h
VARIABLES.csv
Config0.c
Config0.h
Res0.c
Moving Files...
Compiling for Raspberry Pi
Generating object files...
Generating glueVars...
varName: __QX0_0 varType: BOOL
Compiling main program...
/usr/bin/ld: hardware_layer.o🙁.data+0x0): multiple definition of `ignored_bool_inputs'; /tmp/ccnrbhFz.o🙁.data+0x0): first defined here
/usr/bin/ld: hardware_layer.o🙁.data+0x4): multiple definition of `ignored_bool_outputs'; /tmp/ccnrbhFz.o🙁.data+0x4): first defined here
/usr/bin/ld: hardware_layer.o🙁.data+0x8): multiple definition of `ignored_int_inputs'; /tmp/ccnrbhFz.o🙁.data+0x8): first defined here
/usr/bin/ld: hardware_layer.o🙁.data+0xc): multiple definition of `ignored_int_outputs'; /tmp/ccnrbhFz.o🙁.data+0xc): first defined here
/usr/bin/ld: hardware_layer.o: in function `initCustomLayer()':
hardware_layer.cpp🙁.text+0x0): multiple definition of `initCustomLayer()'; /tmp/ccnrbhFz.o:hardware_layer.cpp🙁.text+0x0): first defined here
/usr/bin/ld: hardware_layer.o: in function `updateCustomIn()':
hardware_layer.cpp🙁.text+0x18): multiple definition of `updateCustomIn()'; /tmp/ccnrbhFz.o:hardware_layer.cpp🙁.text+0x18): first defined here
/usr/bin/ld: hardware_layer.o: in function `updateCustomOut()':
hardware_layer.cpp🙁.text+0x30): multiple definition of `updateCustomOut()'; /tmp/ccnrbhFz.o:hardware_layer.cpp🙁.text+0x30): first defined here
/usr/bin/ld: hardware_layer.o🙁.data+0x10): multiple definition of `inBufferPinMask'; /tmp/ccnrbhFz.o🙁.data+0x10): first defined here
/usr/bin/ld: hardware_layer.o🙁.data+0x48): multiple definition of `outBufferPinMask'; /tmp/ccnrbhFz.o🙁.data+0x48): first defined here
/usr/bin/ld: hardware_layer.o🙁.data+0x74): multiple definition of `analogOutBufferPinMask'; /tmp/ccnrbhFz.o🙁.data+0x74): first defined here
/usr/bin/ld: hardware_layer.o: in function `initializeHardware()':
hardware_layer.cpp🙁.text+0x48): multiple definition of `initializeHardware()'; /tmp/ccnrbhFz.o:hardware_layer.cpp🙁.text+0x48): first defined here
/usr/bin/ld: hardware_layer.o: in function `finalizeHardware()':
hardware_layer.cpp🙁.text+0x1c8): multiple definition of `finalizeHardware()'; /tmp/ccnrbhFz.o:hardware_layer.cpp🙁.text+0x1c8): first defined here
/usr/bin/ld: hardware_layer.o: in function `updateBuffersIn()':
hardware_layer.cpp🙁.text+0x1e0): multiple definition of `updateBuffersIn()'; /tmp/ccnrbhFz.o:hardware_layer.cpp🙁.text+0x1e0): first defined here
/usr/bin/ld: hardware_layer.o: in function `updateBuffersOut()':
hardware_layer.cpp🙁.text+0x300): multiple definition of `updateBuffersOut()'; /tmp/ccnrbhFz.o:hardware_layer.cpp🙁.text+0x300): first defined here
collect2: error: ld returned 1 exit status
Error compiling C files
Compilation finished with errors!
Reply 0 0
thiagoralves
Your Raspberry Pi driver seems correct. Have you messed around with the custom hardware layer? It seems to me that the issue is in there. If so, just reset it to the defaults and then you should be good.

Also, if you have saved the file (/OpenPLC_v3/webserver/core/hardware_layers/raspberry_pi.cpp) but have not reapplied the driver on the Hardware tab, then your changes were not taken into effect.
Reply 0 0
xale12
hello  Thiago, I have read again about PWM. I think that If I set the frequency of PWM pin by 50 HZ(cycle =20ms), when I set the value of PWM pin = 65535/5=13107. I have Ton=20/5 = 4ms ,Toff= 16s.Is that correct? By the way, if I use Arduino how can I have a PWM signal.Thank for your help .DDD
Reply 0 0
thiagoralves
Yes, your calculations seem correct. On the Arduino side, the PWM is handled by the analogWrite() function call of the Arduino environment. Check their documentation for more details.
Reply 0 0
xale12
No.  I mean that I use Arduino like a slave for open PLC ,Do it have any different example frequency of Pi is 19200. Do I have to change the frequency of arduino to caculate 
Reply 0 0
thiagoralves
On the Arduino side, the PWM is handled by the analogWrite() function call of the Arduino environment. Check their documentation for more details.

What that means is that OpenPLC uses Arduino's analogWrite() function to control the PWM on the Arduino board. If you want to change the frequency for that call you will have to check their documentation and modify OpenPLC firmware that goes on the Arduino.
Reply 1 0
xale12
Thank you verymuch . I understand it now ...
Reply 0 0
xale12
arduino use AVR code to control frequenc of its Pin .I find the code :
 TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);
 TCCR2B = _BV(WGM22) | _BV(CS22)| _BV(CS21)| _BV(CS20);           //
 
Is this correct because I can not control the servo 🙁(
Reply 0 0
xale12
after a night without sleep, I finally finish my work. Now I can control a servo with OpenPLC and Node-red. I have to change your source code on Arduino . Here is my code :
void setup()
{
int i;
TCCR1A = 0;
TCCR1B = 0;
// RESET 2 registers (of arduino microcontroller)
DDRB |= (1 << PB1);
//  PB1 is OUTPUT ( pin 9)

TCCR1A |= (1 << WGM11);
TCCR1B |= (1 << WGM12) | (1 << WGM13);
// choose Fast PWM, mode TOP_value free ICR1
TCCR1A |= (1 << COM1A1);
// ( none-inverting)

TCCR1B |= (1 << CS11);
// P_clock=16mhz/8=2mhz
// each P_clock equal 1/2mhz= 0.5 us
OCR1A = modbus.Hreg(i);// this value control pulse width of PWM and servo angle , 1060=0 degree,4820 =180 degree .
ICR1 = 40000;
//PWM cycle = 40000x0.5(us)=20 ms=>Tpwm=50hz .
and I change your void loop :
void loop()
{
//Run the main modbus task
modbus.task();


//Update modbus registers
for (int i = 0; i < NUM_DISCRETE_INPUT; ++i)
{
modbus.Ists(i, digitalRead(pinMask_DIN[i]));
}
for (int i = 0; i < NUM_INPUT_REGISTERS; ++i)
{
modbus.Ireg(i, (analogRead(pinMask_AIN[i]) * 64));
}
for (int i = 0; i < NUM_COILS; ++i)
{
digitalWrite(pinMask_DOUT[i], modbus.Coil(i));
}
for (int i = 0; i < NUM_HOLDING_REGISTERS; ++i)
{
analogWrite(pinMask_AOUT[i], modbus.Hreg(i));
}
}
Reply 0 0
Marcel
Could you help me..
Reply 0 0
Marcel
always error happens
Reply 0 0
Marcel
when i add ; into programm..again error
Reply 0 0
Reply