Omicron
If your USB-Cable is broken, or even when the OpenPLC-runtime is aborted you may acounter problems with outputs remain its current state. For this case I've extended the Arduino-Code with a modbus-timeout of 1 second and set the outputs to defined default-values (safestate).

Here is the code that I've changed:

In OpenPLC_Uno.ino line 58-60 (note that my amount of outputs differs from the original code)
//(de)fault-values
bool default_DOUT[] = {1, 1, 1, 1, 1, 1, 1, 1};
int default_AOUT[] = {0, 0};

and the loop at the bottom:
void loop()
{
//Run the main modbus task
int result = 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)
{
if(result == 0)
digitalWrite(pinMask_DOUT[i], modbus.Coil(i));
else
digitalWrite(pinMask_DOUT[i], default_DOUT[i]);
}
for (int i = 0; i < NUM_HOLDING_REGISTERS; ++i)
{
if(result == 0)
analogWrite(pinMask_AOUT[i], (modbus.Hreg(i) / 256));
else
analogWrite(pinMask_AOUT[i], default_AOUT[i]);
}
}


in ModbusSerial.h line 28:
unsigned long _timestamp;
and line 38:
int task();

in ModbusSerial.cpp function task (from line 239):
int ModbusSerial::task() {
_len = 0;

while ((*_port).available() > _len) {
_len = (*_port).available();
delayMicroseconds(_t15);
}

if (_len == 0)
{
unsigned long now = millis();
if (now > _timestamp && now - _timestamp > 1000)
return -1;
else
return 0;
}

byte i;
_frame = (byte*) malloc(_len);
for (i=0 ; i < _len ; i++) _frame[i] = (*_port).read();

#ifdef DEBUG_MODE
(*DebugPort).println(F("GOT Serial CMD"));
for (int i=0 ; i < _len ; i++) {
(*DebugPort).print(_frame[i], DEC);
(*DebugPort).print(":");
//(*DebugPort).println(sendbuffer[i], HEX);
}
(*DebugPort).println();
(*DebugPort).println(F("-----------------"));
#endif

if (this->receive(_frame)) {
if (_reply == MB_REPLY_NORMAL)
this->sendPDU(_frame);
else
if (_reply == MB_REPLY_ECHO)
this->send(_frame);
}

free(_frame);
_len = 0;
_timestamp = millis();
return 0;
}

Maybe you find this useful somehow 🙂
greets,
Omicron
Reply 1 0
Omicron
Btw this is my useCase
IMG-20210510-WA0018.jpeg 
Reply 1 0
NWT.Stuff
Which device are you using to run OpenPLC on ?
Reply 0 0
Omicron
It is an OrangePi PC2. Am running FUXA for the HMI on it too. It might be a bit weak, maybe I will change it to a Raspi 4b soon.
Reply 0 0
NWT.Stuff

And what is the application ? I can see a contractor. A power supply for the controls ? What are the orange bits ? Terminals ?

 

Reply 0 0
Omicron
I could have written a bit more 😉

Yes, the orange bits are terminals (WAGO)

I've soldered a Arduino Nano (Uno) directly onto the relais-board, to make it a Modbus-Slave. It has analog inputs (green terminals) for a pressure sensor. One of the relais is switching the big coil, which is for a waterpump. The application is a overhead sprinkling system for a riding hall. There are 5 Pipes where I'll put 5 valves. At the end of each Pipe there is a drainingvalve, so after filling the pipe it closes and the presure will rise. Now it should sprikle a selectable time. After that time the next valve will open, and after the drop of the pressure the current valve will close.

bb3629a7-0ea4-4f1d-9568-dcceb39258f0.jpg 

faa02717-94b7-461f-8ec1-bbc3466e1b37.jpg 
Reply 0 0
NWT.Stuff

Nice application. So Regarding your pump.  Is the digital output from the arduino or the Pi. ? Also are you using NC or NO contact to run the pump ?

From my own projects I have some experience to share about Fail Safe. I will need to write it up first as it is quite a complex thing to explain. Will post it soon 🙂

Reply 0 0
Omicron
The Arduino is switching the Relaisboard, but it needs high-signal to turn the relais off. As these relais are switching on startup, I have used one relais to be off to enable the power for the others, so it doesn't matter when all are on or off. The next I can switch the big coil on which is normally open.
Reply 0 0
NWT.Stuff
I have made this document which explains that for certain applications a High Level Trigger relay output will help achieve FAILSAFE on the Hardware Side.

pdf LowLevelTrigger.pdf      
Reply 0 0
Omicron
My approach is just for modbus-timeout, so if the communication is lost you are still able to turn off the pump. 
Reply 0 0
NWT.Stuff
Ah OK. I think I understand better now. You are handling to STOP Pump when you no longer have Communications Arduino <> Open PLC via modbus. Correct ?

Out of interest what happens if the power of the Arduino is lost ?  I have never user Arduino slave with Open PLC so I have no experience in this area. ?
Reply 0 0
Omicron
Correct.

If the power is lost the outputs will fall too
Reply 0 0
NWT.Stuff
Omicron wrote:
Correct.

If the power is lost the outputs will fall too


OK. Interesting I always use Digital Outputs on RPI GPIO so this explains the need for a different approach 🙂
Reply 0 0
Reply