salmonM

Greetings!

I have had such a good response here so i felt like reaching out again! My colleague and I are working with OPLC on a real-time target and are trying our best to model the latency end to end. We have a UniPi set up with analog input and output from/to our target.

When using OPLC locally on the UniPi and doing nothing in the PLC program but send AI -> AO we have a latency of about 120 +- 80ms. This is not terrible but we would like to understand the latency (and reduce it if possible).

Does anybody have any recommendations or tips? Any response is greatly appreciated!

Best regards!

Reply 0 0
thiagoralves
The problem is with the UniPi platform itself. Their hardware takes way too long to respond to an analog reading, to the point I had to create a separate thread just to read analog inputs otherwise it would interfere with the PLC scan cycle. You can check the behavior on this code snippet:
void *readAdcThread(void *args)
{
    while(1)
    {
        unsigned char config;
        unsigned char i2c_data[4];
        unsigned int value = 0;
        double conversion;
        bool conversionInProgress;
        
        config = 0x88; //read channel 0
        wiringPiI2CWrite(adc_fd, config);
        i2c_data[2] = 0;
        conversionInProgress = 1;
        while (conversionInProgress)
        {
            read(adc_fd, i2c_data, 3);
            conversionInProgress = (i2c_data[2] >> 7);
            if (conversionInProgress)
                sleep_ms(1);
        }
        value = (i2c_data [0] << 8) | i2c_data [1];
        conversion = value*2.28;
        if (conversion > 65535.0) {conversion=65535.0;}
        value = (unsigned int)conversion;
        pthread_mutex_lock(&adcBufferLock);
        adcBuffer[0] = value;
        pthread_mutex_unlock(&adcBufferLock);
        config = 0xA8; //read channel 1
        wiringPiI2CWrite(adc_fd, config);
        i2c_data[2] = 0;
        conversionInProgress = 1;
        while (conversionInProgress)
        {
            read(adc_fd, i2c_data, 3);
            conversionInProgress = (i2c_data[2] >> 7);
            if (conversionInProgress)
                sleep_ms(1);
        }
        value = (i2c_data [0] << 8) | i2c_data [1];
        conversion = value*2.28;
        if (conversion > 65535.0) {conversion=65535.0;}
        value = (unsigned int)conversion;
        pthread_mutex_lock(&adcBufferLock);
        adcBuffer[1] = value;
        pthread_mutex_unlock(&adcBufferLock);
    }
}

int mcp_adcSetup (int i2cAddress)
{
    if ((adc_fd = wiringPiI2CSetup(i2cAddress)) < 0)
    {
        printf("error on adc setup!\n");
        return adc_fd;
    }
    return 0 ;
}

int mcp_adcRead(int chan)
{
    int returnValue;
    if (chan < 2)
    {
        pthread_mutex_lock(&adcBufferLock);
        returnValue = adcBuffer[chan];
        pthread_mutex_unlock(&adcBufferLock);
        return returnValue;
    }
    return 0;
}


As you can see, main code calls mcp_adcRead(int chan) and that outputs whatever is on the adc buffer instantly (asynchronous response). Since the analog reading is asynchronous, specifically on the analog inputs there isn't much you can do to lower latency or guarantee some real time behavior. For digital IO however, you should get a much better response as the readings are synchronous with the PLC scan cycle. There are some tweaks you can do on the bootloader to significantly improve the real time response for OpenPLC. I discuss about it on this thread:  https://openplc.discussion.community/post/problem-with-simple-project-10310920?pid=1309893961&random=21082
Reply 0 0
Reply