etherlab/plc_etherlab.c
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 28 Sep 2018 13:07:46 +0300
changeset 2350 9f7028ab8294
parent 2165 02a2b5dee5e3
child 2641 c9deff128c37
permissions -rw-r--r--
Use the list of supported by app translations instead of supported translations by wx

This will make possible to translate application into locales, that
are not explicitly supported by wx: for example, 'pl' or 'bn_BD', 'bn_IN'.
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
     1
/*
2165
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     2
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     3
Template C code used to produce target Ethercat C code
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     4
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     5
Copyright (C) 2011-2014: Laurent BESSARD, Edouard TISSERANT
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     6
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     7
Distributed under the terms of the GNU Lesser General Public License as
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     8
published by the Free Software Foundation; either version 2 of the License, or
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
     9
(at your option) any later version.
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
    10
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
    11
See COPYING file for copyrights details.
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
    12
02a2b5dee5e3 Merged GPL + LGPL v2 or later EtherCAT extension. Fixed headers and copyright notice.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2123
diff changeset
    13
*/
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    14
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    15
#include <rtdm/rtdm.h>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    16
#include <native/task.h>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    17
#include <native/timer.h>
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    18
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    19
#include "ecrt.h"
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    20
2106
4ec3b833f2d5 Now use beremiz.h
Edouard Tisserant
parents: 2081
diff changeset
    21
#include "beremiz.h"
4ec3b833f2d5 Now use beremiz.h
Edouard Tisserant
parents: 2081
diff changeset
    22
#include "iec_types_all.h"
2036
7c31fab22c74 Adding support for setting TargetPosition only if PositionSetPoint value from MCL is valid
laurent
parents: 2032
diff changeset
    23
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    24
// declaration of interface variables
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    25
%(located_variables_declaration)s
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    26
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    27
// process data
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2028
diff changeset
    28
uint8_t *domain1_pd = NULL;
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    29
%(used_pdo_entry_offset_variables_declaration)s
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    30
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    31
const static ec_pdo_entry_reg_t domain1_regs[] = {
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    32
%(used_pdo_entry_configuration)s
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    33
    {}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    34
};
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    35
/*****************************************************************************/
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    36
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    37
%(pdos_configuration_declaration)s
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    38
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    39
long long wait_period_ns = 100000LL;
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    40
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    41
// EtherCAT
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    42
static ec_master_t *master = NULL;
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    43
static ec_domain_t *domain1 = NULL;
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
    44
static int first_sent=0;
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    45
%(slaves_declaration)s
2107
6f9aa410a997 Added Logging
Edouard Tisserant
parents: 2106
diff changeset
    46
#define SLOGF(level, format, args...)\
6f9aa410a997 Added Logging
Edouard Tisserant
parents: 2106
diff changeset
    47
{\
6f9aa410a997 Added Logging
Edouard Tisserant
parents: 2106
diff changeset
    48
    char sbuf[256];\
6f9aa410a997 Added Logging
Edouard Tisserant
parents: 2106
diff changeset
    49
    int slen = snprintf(sbuf , sizeof(sbuf) , format , ##args);\
6f9aa410a997 Added Logging
Edouard Tisserant
parents: 2106
diff changeset
    50
    LogMessage(level, sbuf, slen);\
6f9aa410a997 Added Logging
Edouard Tisserant
parents: 2106
diff changeset
    51
}
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    52
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    53
/* Beremiz plugin functions */
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    54
int __init_%(location)s(int argc,char **argv)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    55
{
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2028
diff changeset
    56
    uint32_t abort_code;
2039
3a218f6bd805 Adding support for uploading default value for unused entries in output pdos from slaves before starting master
laurent
parents: 2036
diff changeset
    57
    size_t result_size;
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2028
diff changeset
    58
    
2123
68beaf825a20 Added initialization of abort_code and result_size to prevent warning message if not used in following code
Laurent Bessard
parents: 2117
diff changeset
    59
    abort_code = 0;
68beaf825a20 Added initialization of abort_code and result_size to prevent warning message if not used in following code
Laurent Bessard
parents: 2117
diff changeset
    60
    result_size = 0;
68beaf825a20 Added initialization of abort_code and result_size to prevent warning message if not used in following code
Laurent Bessard
parents: 2117
diff changeset
    61
2117
accc4cbca8d3 Now failure in template based part of C code also release the master for later use...
Edouard Tisserant
parents: 2116
diff changeset
    62
    master = ecrt_request_master(%(master_number)d);
accc4cbca8d3 Now failure in template based part of C code also release the master for later use...
Edouard Tisserant
parents: 2116
diff changeset
    63
    if (!master) {
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    64
        SLOGF(LOG_CRITICAL, "EtherCAT master request failed!");
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    65
        return -1;
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    66
    }
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    67
2117
accc4cbca8d3 Now failure in template based part of C code also release the master for later use...
Edouard Tisserant
parents: 2116
diff changeset
    68
    if(!(domain1 = ecrt_master_create_domain(master))){
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    69
        SLOGF(LOG_CRITICAL, "EtherCAT Domain Creation failed!");
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    70
        goto ecat_failed;
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    71
    }
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    72
2079
49242019a9ca Fix C code Generator to use CoE section contained in the slave ESI file for defining Ethercat network configuration
Laurent Bessard
parents: 2076
diff changeset
    73
    // slaves PDO configuration
49242019a9ca Fix C code Generator to use CoE section contained in the slave ESI file for defining Ethercat network configuration
Laurent Bessard
parents: 2076
diff changeset
    74
%(slaves_configuration)s
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    75
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    76
    if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    77
        SLOGF(LOG_CRITICAL, "EtherCAT PDO registration failed!");
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    78
        goto ecat_failed;
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    79
    }
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    80
2117
accc4cbca8d3 Now failure in template based part of C code also release the master for later use...
Edouard Tisserant
parents: 2116
diff changeset
    81
    ecrt_master_set_send_interval(master, common_ticktime__);
2036
7c31fab22c74 Adding support for setting TargetPosition only if PositionSetPoint value from MCL is valid
laurent
parents: 2032
diff changeset
    82
2079
49242019a9ca Fix C code Generator to use CoE section contained in the slave ESI file for defining Ethercat network configuration
Laurent Bessard
parents: 2076
diff changeset
    83
    // slaves initialization
2032
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2028
diff changeset
    84
%(slaves_initialization)s
766078d83e22 Adding support for DS402 node subplugin (only simple state transition implemented)
laurent
parents: 2028
diff changeset
    85
2079
49242019a9ca Fix C code Generator to use CoE section contained in the slave ESI file for defining Ethercat network configuration
Laurent Bessard
parents: 2076
diff changeset
    86
    // extracting default value for not mapped entry in output PDOs
2039
3a218f6bd805 Adding support for uploading default value for unused entries in output pdos from slaves before starting master
laurent
parents: 2036
diff changeset
    87
%(slaves_output_pdos_default_values_extraction)s
3a218f6bd805 Adding support for uploading default value for unused entries in output pdos from slaves before starting master
laurent
parents: 2036
diff changeset
    88
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    89
    if (ecrt_master_activate(master)){
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    90
        SLOGF(LOG_CRITICAL, "EtherCAT Master activation failed");
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    91
        goto ecat_failed;
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    92
    }
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    93
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    94
    if (!(domain1_pd = ecrt_domain_data(domain1))) {
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    95
        SLOGF(LOG_CRITICAL, "Failed to map EtherCAT process data");
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    96
        goto ecat_failed;
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    97
    }
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
    98
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
    99
    SLOGF(LOG_INFO, "Master %(master_number)d activated.");
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   100
    
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   101
    first_sent = 0;
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   102
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   103
    return 0;
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
   104
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
   105
ecat_failed:
2117
accc4cbca8d3 Now failure in template based part of C code also release the master for later use...
Edouard Tisserant
parents: 2116
diff changeset
   106
    ecrt_release_master(master);
2116
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
   107
    return -1;
2b1980a038b1 Better logging, saffer failure at init (allows restart)
Edouard Tisserant
parents: 2108
diff changeset
   108
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   109
}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   110
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   111
void __cleanup_%(location)s(void)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   112
{
2117
accc4cbca8d3 Now failure in template based part of C code also release the master for later use...
Edouard Tisserant
parents: 2116
diff changeset
   113
    //release master
accc4cbca8d3 Now failure in template based part of C code also release the master for later use...
Edouard Tisserant
parents: 2116
diff changeset
   114
    ecrt_release_master(master);
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   115
    first_sent = 0;
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   116
}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   117
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   118
void __retrieve_%(location)s(void)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   119
{
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   120
    // receive ethercat
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   121
    if(first_sent){
2108
6bcfd50d5a47 Updated generated code to etherlab 1.5.2
Edouard Tisserant
parents: 2107
diff changeset
   122
        ecrt_master_receive(master);
6bcfd50d5a47 Updated generated code to etherlab 1.5.2
Edouard Tisserant
parents: 2107
diff changeset
   123
        ecrt_domain_process(domain1);
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   124
%(retrieve_variables)s
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   125
    }
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   126
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   127
}
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   128
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   129
static RTIME _last_occur=0;
2076
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   130
static RTIME _last_publish=0;
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   131
RTIME _current_lag=0;
2076
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   132
RTIME _max_jitter=0;
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   133
static inline RTIME max(RTIME a,RTIME b){return a>b?a:b;}
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   134
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   135
void __publish_%(location)s(void)
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   136
{
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   137
%(publish_variables)s
2108
6bcfd50d5a47 Updated generated code to etherlab 1.5.2
Edouard Tisserant
parents: 2107
diff changeset
   138
    ecrt_domain_queue(domain1);
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   139
    {
2076
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   140
        RTIME current_time = rt_timer_read();
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   141
        // Limit spining max 1/5 of common_ticktime
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   142
        RTIME maxdeadline = current_time + (common_ticktime__ / 5);
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   143
        RTIME deadline = _last_occur ? 
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   144
            _last_occur + common_ticktime__ : 
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   145
            current_time + _max_jitter; 
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   146
        if(deadline > maxdeadline) deadline = maxdeadline;
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   147
        _current_lag = deadline - current_time;
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   148
        if(_last_publish != 0){
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   149
            RTIME period = current_time - _last_publish;
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   150
            if(period > common_ticktime__ )
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   151
                _max_jitter = max(_max_jitter, period - common_ticktime__);
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   152
            else
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   153
                _max_jitter = max(_max_jitter, common_ticktime__ - period);
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   154
        }
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   155
        _last_publish = current_time;
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   156
        _last_occur = current_time;
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   157
        while(current_time < deadline) {
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   158
            _last_occur = current_time; //Drift backward by default
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   159
            current_time = rt_timer_read();
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   160
        }
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   161
        if( _max_jitter * 10 < common_ticktime__ && _current_lag < _max_jitter){
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   162
            //Consuming security margin ?
22e9993348f6 Better jitter compensation before sending Ethercat frame. Jitter can be compensed in both directions, within some limits
Edouard Tisserant
parents: 2075
diff changeset
   163
            _last_occur = current_time; //Drift forward
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   164
        }
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   165
    }
2108
6bcfd50d5a47 Updated generated code to etherlab 1.5.2
Edouard Tisserant
parents: 2107
diff changeset
   166
    ecrt_master_send(master);
2075
21eb91dc7071 Different etherlab sequencing within retrieve and publish. Now, timer is polled after PLC exec until frame have to be sent. This slightly reduce jitter, compared to previous sequencing
Edouard Tisserant
parents: 2039
diff changeset
   167
    first_sent = 1;
2022
c2295d311402 First working implementation of Beremiz plugin for etherlab
laurent
parents:
diff changeset
   168
}