GeertVc

I'm having a look into the file raspberrypi.cpp in the OpenPLC_v3 repository because I want to use both I2c as well as the PLC facilities.


Since OpenPLC also uses the I2c SDA and SCL line, they have to be excluded in the call to initializeHardware() so that they're not taken into account as PLC input bits.  The check is done in the first for loop through the function pinNotPresent() which is taking 3 parameters as input.  The first parameter is an array of integers, called ignored_bool_inputs, the second one is the size of the array and the third one is the pin number.
Therefor, the array ignored_bool_inputs[] located in custom_layer.h should be changed to { 8, 9} instead of the default { -1}.  Those two numbers are the wiringPi numbers for GPIO 2 and GPIO 3 (or physical header pins 3 and 5.  Correct me if I'm wrong.


However, when looking to the implementation of pinNotPresent() then the incoming array is iterated using a for loop.  But inside the for loop there's a check to see if the incoming pin number is part of the given integer array, being ignored_bool_inputs in this case.  And I think there's an issue here, since the caller in the method initializeHardware() uses simply the value of the counter as pin number, while - to me - that should be the pin number of the array inBufferPinMask located in raspberrypi.cpp.


So, instead of this:

//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
        }
    }
}


I think it should be

//set pins as input
for (int i = 0; i < MAX_INPUT; i++)
{
    if (pinNotPresent(ignored_bool_inputs, ARRAY_SIZE(ignored_bool_inputs), inBufferPinMask[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
        }
    }
}


That is, the 3rd parameter of the call to pinNotPresent() should be inBufferPinMask[i] instead of just i.


Is this correct?


Reply 0 0
GeertVc
I've tested the above on a RPi with the below ladder scheme:

ladder_testscheme.png

I adapted the file custom_layer.h and added {8, 9} to the igrnored_bool_inputs[] array. 

Without my changes, it doesn't make any difference. 
When I'm not pressing the contacts (NO contacts) representing I01 and I02, the LEDs are blinking at a certain rate defined by TON0/1 and TOF0/1.  Note that header pins 3 and 5 have internal pull-up resistors, so not pressing them means a logical 1 is seen at %IX0.0 and %IX0.1.  
When I press the contacts the LEDs are switched off since both inputs are grounded in that state.
That means the value of %IX0.0 as well as %IX0.1 is still taken into account while, if the SW would be correct, they should be excluded from the list of input bits.

With my changes, all is working fine.  That is, I01 as well as I02 have no input value since they're excluded from the list of input bits.  And as such, whether I press the contacts or not, the LEDs are not flashing.

I reverted back the changes in custom_layer.h only as a cross-check (so, again {-1} as only value in the array) and with my changes still in, all is again working fine.  Since the two input bits %IX0.0 and %IX0.1 are not anymore excluded, they're part of the game again. 
When not pressing the switches, the LEDs are blinking (as it should be), when pressing the switches the LEDs are off.

In fact, all the calls to pinNotPresent() in the file raspberrypi.cpp face the same issue.  I've corrected them locally already and I'm busy further testing this and will make a commit/pull request on the repository when we agree with the issue and I've been able to test it enough.

Again, the reason why I tested this and came to the conclusion that something was probably wrong in the code is because I want to reclaim header pins 3 and 5 to do I2c communication in combination with PLC stuff.

Best rgds,
--Geert
Reply 0 0
thiagoralves
Hi Geert! Thank you so much for your detailed report. OpenPLC custom layer was abandoned in favor of PSM. PSM is a much simpler way to write drivers for OpenPLC in plain python. You can search this forum to learn more about it. In any case, if you still want to use the custom layer, your changes are valid.
Reply 0 0
GeertVc
Oh... really...  I'm a bit surprised/confused now.  Does that mean none of the files in ./core/hardware_layers is actually not used/maintained anymore?  A bit strange since when I followed the whole build chain from  install.sh  all the way up to  compile_program.sh I saw there's still lots of references to hardware_layers and calls to g++ in the scripts.

At first, I totally didn't know what PSM stood for, first time I heard of it.  Now I know it's the abbrev for Python Sub Module. 
One question though: speed?  Is the C/CPP implementation in the hardware layers files not much more efficient/faster than the Python alternatives?  Just curious.

And what about the UniPI driver?  Is this also abandoned in favour of PSM?

For now, I think I will stick to the custom layer approach until I have some more grip on the PSM environment.

Best rgds,
--Geert
Reply 1 0
thiagoralves
The hardware layer files are still used. They are the native drivers on OpenPLC. The custom layer file was a way to expand the native drivers with custom C code, but since most OpenPLC users are not proficient in C, I decided to create PSM as a replacement for the custom driver. PSM is one of the hardware layer drivers, and basically creates a bridge between Python and OpenPLC core in C. Of course there is the performance penalty to pay if you use PSM. A native C driver would be much more efficient. But for most cases, PSM is still fast enough to give you good results, and dead easy to implement.
Reply 0 0
GeertVc
The hardware layer files are still used. They are the native drivers on OpenPLC.


That's very good news to hear.  It means my efforts to solve the issue I found recently won't be "a waste of time"... :-)  I was hesitating to push my changes and ask for a pull request but I will do so now that I know the hardware layers are alive and kicking.

Regarding the C language: yes, I know.  It's a very powerful language but can be oh so deadly if you don't really know what you're doing.  That's the downside of being very powerful.

And in the meantime I also looked into that PSM stuff.  It took me not so long to get a grip on it.  The issue I struggled most was controlling the outputs.  Took me some 1/2 hour before I got the picture.  I thought the QX stuff was the "real" output state but later on I found that it's the internal state of the ladder diagram (in case you're using the ladder diagram) and it reacts as such.  Based on the internal levels, only then you send out a TRUE or FALSE to the "real" GPIO pins (in case of a RPi).
Reply 0 0
Reply