1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*      File: EL3164.cpp
*       This file is part of the program ethercatcpp-core
*       Program description : EtherCAT driver libraries for UNIX
*       Copyright (C) 2017-2024 -  Robin Passama (LIRMM / CNRS) Arnaud Meline (LIRMM / CNRS) Benjamin Navarro (LIRMM / CNRS). All Right reserved.
*
*       This software is free software: you can redistribute it and/or modify
*       it under the terms of the CeCILL-C license as published by
*       the CEA CNRS INRIA, either version 1
*       of the License, or (at your option) any later version.
*       This software 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
*       CeCILL-C License for more details.
*
*       You should have received a copy of the CeCILL-C License
*       along with this software. If not, it can be found on the official website
*       of the CeCILL licenses family (http://www.cecill.info/index.en.html).
*/
/**
 * @file beckhoff_terminals/EL3164.cpp
 * @author Robin Passama
 * @brief source file for EL3164 class
 *
 */
#include <ethercatcpp/beckhoff_terminals/EL3164.h>

#include <pid/log/ethercatcpp-core_ethercatcpp-core.h>

namespace ethercatcpp {

namespace {

//----------------------------------------------------------------------------//
//                M A I L B O X    D E F I N I T I O N S //
//----------------------------------------------------------------------------//

struct [[gnu::packed]] mailbox_out_t {
    int8_t mailbox[128];<--- struct member 'mailbox_out_t::mailbox' is never used.
};
struct [[gnu::packed]] mailbox_in_t {
    int8_t mailbox[128];<--- struct member 'mailbox_in_t::mailbox' is never used.
};

//----------------------------------------------------------------------------//
//                 C Y C L I C    B U F F E R //
//----------------------------------------------------------------------------//

struct [[gnu::packed]] buffer_out_cyclic_command_t {
    // No output data
};

struct [[gnu::packed]] input_data_channel_t {
    uint16_t status_word;<--- struct member 'input_data_channel_t::status_word' is never used.
    int16_t data_value;<--- struct member 'input_data_channel_t::data_value' is never used.
};

struct [[gnu::packed]] buffer_in_cyclic_status_t {
    input_data_channel_t channel_1; // 0x6000
    input_data_channel_t channel_2; // 0x6010
    input_data_channel_t channel_3; // 0x6020
    input_data_channel_t channel_4; // 0x6030
};

} // namespace

EL3164::EL3164()
    : SlaveDevice(), last_updated_{-1}, updated_{false, false, false, false} {
    set_id("EL3164", 0x00000002, 0x0c5c3052);

    // canopen_Configure_SDO( [this](){
    //
    // } ); // canopen_Configure_SDO End

    // Mailboxes configuration
    define_physical_buffer<mailbox_out_t>(ASYNCHROS_OUT, 0x1000, 0x00010026);
    define_physical_buffer<mailbox_in_t>(ASYNCHROS_IN, 0x1080, 0x00010022);
    // In/out buffer config
    define_physical_buffer<buffer_out_cyclic_command_t>(
        SYNCHROS_OUT, 0x1100, 0x00000004); // size depand of configured PDO
    define_physical_buffer<buffer_in_cyclic_status_t>(
        SYNCHROS_IN, 0x1180, 0x00010020); // size depand of configured PDO

    //----------------------------------------------------------------------------//
    //                     R U N S     S T E P S //
    //----------------------------------------------------------------------------//

    add_run_step([]() {},
                 [this]() { unpack_status_buffer(); }); // add_Run_Step end

} // constructor end

EL3164::~EL3164() = default;

void EL3164::unpack_status_buffer() {
    auto buff = input_buffer<buffer_in_cyclic_status_t>(0x1180);
    channel_1_.status_word = buff->channel_1.status_word;
    channel_1_.data_value = buff->channel_1.data_value;
    channel_2_.status_word = buff->channel_2.status_word;
    channel_2_.data_value = buff->channel_2.data_value;
    channel_3_.status_word = buff->channel_3.status_word;
    channel_3_.data_value = buff->channel_3.data_value;
    channel_4_.status_word = buff->channel_4.status_word;
    channel_4_.data_value = buff->channel_4.data_value;
    check_updated(channel_1);
    check_updated(channel_2);
    check_updated(channel_3);
    check_updated(channel_4);
}

int EL3164::channel_value(channel_id_t channel) const {
    if (not error(channel) and
        valid(channel)) { // check if data have no error and is valid
        switch (channel) {
        case channel_1:
            return channel_1_.data_value;
        case channel_2:
            return channel_2_.data_value;
        case channel_3:
            return channel_3_.data_value;
        case channel_4:
            return channel_4_.data_value;
        default:
            return 0;
        }
    } else {
        pid_log << pid::warning << "error on channel " << channel + 1
                << pid::endl
                << pid::flush;
        return 0;
    }
}

bool EL3164::under_range(channel_id_t channel) const {
    switch (channel) {
    case channel_1:
        return (channel_1_.status_word >> 0) & 1U;
    case channel_2:
        return (channel_2_.status_word >> 0) & 1U;
    case channel_3:
        return (channel_3_.status_word >> 0) & 1U;
    case channel_4:
        return (channel_4_.status_word >> 0) & 1U;
    default:
        return false;
    }
}

bool EL3164::over_range(channel_id_t channel) const {
    switch (channel) {
    case channel_1:
        return (channel_1_.status_word >> 1) & 1U;
    case channel_2:
        return (channel_2_.status_word >> 1) & 1U;
    case channel_3:
        return (channel_3_.status_word >> 1) & 1U;
    case channel_4:
        return (channel_4_.status_word >> 1) & 1U;
    default:
        return false;
    }
}

// int EL3164::check_Limit_1_Monitoring(channel_id_t channel){
//   switch (channel) {
//     case channel_1:
//       return((int)(channel_1_.status_word >> 2) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     case channel_2:
//       return((int)(channel_2_.status_word >> 2) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     case channel_3:
//       return((int)(channel_3_.status_word >> 2) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     case channel_4:
//       return((int)(channel_4_.status_word >> 2) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     default:
//     return(0);
//   }
// }
//
// int EL3164::check_Limit_2_Monitoring(channel_id_t channel){
//   switch (channel) {
//     case channel_1:
//       return((int)(channel_1_.status_word >> 4) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     case channel_2:
//       return((int)(channel_2_.status_word >> 4) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     case channel_3:
//       return((int)(channel_3_.status_word >> 4) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     case channel_4:
//       return((int)(channel_4_.status_word >> 4) & 3U); //Mask is 3U to keep
//       the 2 last bits
//     break;
//     default:
//     return(0);
//   }
// }

bool EL3164::error(channel_id_t channel) const {
    switch (channel) {
    case channel_1:
        return (channel_1_.status_word >> 6) & 1U;
    case channel_2:
        return (channel_2_.status_word >> 6) & 1U;
    case channel_3:
        return (channel_3_.status_word >> 6) & 1U;
    case channel_4:
        return (channel_4_.status_word >> 6) & 1U;
    default:
        return false;
    }
}

bool EL3164::sync_error(channel_id_t channel) const {
    switch (channel) {
    case channel_1:
        return (channel_1_.status_word >> 13) & 1U;
    case channel_2:
        return (channel_2_.status_word >> 13) & 1U;
    case channel_3:
        return (channel_3_.status_word >> 13) & 1U;
    case channel_4:
        return (channel_4_.status_word >> 13) & 1U;
    default:
        return false;
    }
}

bool EL3164::valid(channel_id_t channel) const {
    switch (channel) {
    case channel_1:
        return not((channel_1_.status_word >> 14) & 1U);
    case channel_2:
        return not((channel_2_.status_word >> 14) & 1U);
    case channel_3:
        return not((channel_3_.status_word >> 14) & 1U);
    case channel_4:
        return not((channel_4_.status_word >> 14) & 1U);
    default:
        return false;
    }
}

void EL3164::check_updated(channel_id_t channel) {
    int val{0};
    switch (channel) {
    case channel_1:
        val = (channel_1_.status_word >> 15) & 1;
        break;
    case channel_2:
        val = (channel_2_.status_word >> 15) & 1;
        break;
    case channel_3:
        val = (channel_3_.status_word >> 15) & 1;
        break;
    case channel_4:
        val = (channel_4_.status_word >> 15) & 1;
        break;
    default:
        return;
    }
    if (val != last_updated_) {
        last_updated_ = static_cast<int8_t>(val);
        updated_[channel] = true;
    }
    last_updated_ = static_cast<int8_t>(val);
    updated_[channel] = false;
}

bool EL3164::updated(channel_id_t channel) const {
    return updated_[channel];
}

void EL3164::print_all_channels() const {

    std::vector<channel_id_t> all_channel{channel_1, channel_2, channel_3,
                                          channel_4};

    for (auto& channel : all_channel) {
        pid_log << pid::info << "Channel " << channel + 1 << " datas :" << "\n";
        pid_log << pid::info << "Data_Value = " << channel_value(channel)
                << "\n";
        pid_log << pid::info << "Underrange = " << under_range(channel) << "\n";
        pid_log << pid::info << "Overrange = " << over_range(channel) << "\n";
        // cout << "Limit_1_Monitoring = " <<  check_Limit_1_Monitoring(channel)
        // << "\n"; cout << "Limit_2_Monitoring = " <<
        // check_Limit_2_Monitoring(channel) << "\n";
        pid_log << pid::info << "Error = " << error(channel) << "\n";
        pid_log << pid::info << "Sync_Error = " << sync_error(channel) << "\n";
        pid_log << pid::info << "value valid = " << valid(channel) << "\n";
        pid_log << pid::info << "Value updated = " << updated(channel) << "\n";
        pid_log << pid::info << "\n";
    }
    pid_log << pid::flush;
}

} // namespace ethercatcpp