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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434 | /* File: clipx.cpp
* This file is part of the program ethercatcpp-clipx
* Program description : EtherCAT driver libraries for HMB ClipX
* Copyright (C) 2018-2022 - Robin Passama (CNRS/LIRMM) Arnaud Meline
* (CNRS/LIRMM). 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).
*/
#include <ethercatcpp/clipx.h>
#include <iostream>
#include <stdexcept>
namespace ethercatcpp {
namespace {
//----------------------------------------------------------------------------//
// B U F F E R S 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 {
float fieldbus_value_1; // 0x1601
float fieldbus_value_2; // 0x1602
uint16_t fieldbus_flag; // 0x1603
float limit_value_1; // 0x1621
float limit_value_2; // 0x1622
float limit_value_3; // 0x1623
float limit_value_4; // 0x1624
uint32_t control_word; // 0x1625
uint16_t parameter_set_number; // 0x1626
};
struct [[gnu::packed]] buffer_in_cyclic_status_t {
uint32_t system_status; // 0x1A00
float adc_value; // 0x1A01
float field_value; // 0x1A02
float gross_value; // 0x1A03
float net_value; // 0x1A04
float min_value; // 0x1A05
float max_value; // 0x1A06
float peak_to_peak_value; // 0x1A07
float captured_value_1; // 0x1A08
float captured_value_2; // 0x1A09
float calculated_value_1; // 0x1A0A
float calculated_value_2; // 0x1A0B
float calculated_value_3; // 0x1A0C
float calculated_value_4; // 0x1A0D
float calculated_value_5; // 0x1A0E
float calculated_value_6; // 0x1A0F
float neighbor_value_1; // 0x1A15
float neighbor_value_2; // 0x1A16
float neighbor_value_3; // 0x1A17
float neighbor_value_4; // 0x1A18
float neighbor_value_5; // 0x1A19
float neighbor_value_6; // 0x1A1A
float external_eth_value_1; // 0x1A1B
float external_eth_value_2; // 0x1A1C
float analog_output_value; // 0x1A1D
uint32_t io_status_low; // 0x1A1E
uint32_t io_stauts_high; // 0x1A1F
uint32_t measured_value_status; // 0x1A20
float limit_value_1; // 0x1A21
float limit_value_2; // 0x1A22
float limit_value_3; // 0x1A23
float limit_value_4; // 0x1A24
uint32_t control_word_validation; // 0x1A25
uint16_t parameter_set_number; // 0x1A26
};
} // namespace
ClipX::ClipX()
: SlaveDevice(), fieldbus_value_1_(0), fieldbus_value_2_(0),
fieldbus_flag_(0), limit_value_1_(0), limit_value_2_(0),
limit_value_3_(0), limit_value_4_(0), control_word_(0),
parameter_set_number_(0), calibrate_from_V_to_N_(1),
clipx_calibrated_(false) {
set_id("ClipX", 0x0000011d, 0x00000f01);
configure_dc_sync0(1000000U, 1000U);
configure_at_init([this]() {
// Link PDO
// Select command PDO mapping
start_command_pdo_mapping<uint8_t>();
add_command_pdo_mapping<uint8_t>(0x1601);
add_command_pdo_mapping<uint8_t>(0x1602);
add_command_pdo_mapping<uint8_t>(0x1603);
add_command_pdo_mapping<uint8_t>(0x1621);
add_command_pdo_mapping<uint8_t>(0x1622);
add_command_pdo_mapping<uint8_t>(0x1623);
add_command_pdo_mapping<uint8_t>(0x1624);
add_command_pdo_mapping<uint8_t>(0x1625);
add_command_pdo_mapping<uint8_t>(0x1626);
end_command_pdo_mapping<uint8_t>();
// Select status PDO mapping
start_status_pdo_mapping<uint8_t>();
add_status_pdo_mapping<uint8_t>(0x1A00);
add_status_pdo_mapping<uint8_t>(0x1A02);
add_status_pdo_mapping<uint8_t>(0x1A03);
add_status_pdo_mapping<uint8_t>(0x1A04);
add_status_pdo_mapping<uint8_t>(0x1A05);
add_status_pdo_mapping<uint8_t>(0x1A06);
add_status_pdo_mapping<uint8_t>(0x1A07);
add_status_pdo_mapping<uint8_t>(0x1A08);
add_status_pdo_mapping<uint8_t>(0x1A09);
add_status_pdo_mapping<uint8_t>(0x1A0A);
add_status_pdo_mapping<uint8_t>(0x1A0B);
add_status_pdo_mapping<uint8_t>(0x1A0C);
add_status_pdo_mapping<uint8_t>(0x1A0D);
add_status_pdo_mapping<uint8_t>(0x1A0E);
add_status_pdo_mapping<uint8_t>(0x1A0F);
add_status_pdo_mapping<uint8_t>(0x1A01);
add_status_pdo_mapping<uint8_t>(0x1A15);
add_status_pdo_mapping<uint8_t>(0x1A16);
add_status_pdo_mapping<uint8_t>(0x1A17);
add_status_pdo_mapping<uint8_t>(0x1A18);
add_status_pdo_mapping<uint8_t>(0x1A19);
add_status_pdo_mapping<uint8_t>(0x1A1A);
add_status_pdo_mapping<uint8_t>(0x1A1B);
add_status_pdo_mapping<uint8_t>(0x1A1C);
add_status_pdo_mapping<uint8_t>(0x1A1D);
add_status_pdo_mapping<uint8_t>(0x1A1E);
add_status_pdo_mapping<uint8_t>(0x1A1F);
add_status_pdo_mapping<uint8_t>(0x1A20);
add_status_pdo_mapping<uint8_t>(0x1A21);
add_status_pdo_mapping<uint8_t>(0x1A22);
add_status_pdo_mapping<uint8_t>(0x1A23);
add_status_pdo_mapping<uint8_t>(0x1A24);
add_status_pdo_mapping<uint8_t>(0x1A25);
add_status_pdo_mapping<uint8_t>(0x1A26);
end_status_pdo_mapping<uint8_t>();
}); // canopen_Configure_SDO End
// Mailboxes configuration
define_physical_buffer<mailbox_out_t>(ASYNCHROS_OUT, 0x1000, 0x00010036);
define_physical_buffer<mailbox_in_t>(ASYNCHROS_IN, 0x1080, 0x00010032);
define_physical_buffer<buffer_out_cyclic_command_t>(
SYNCHROS_OUT, 0x1100, 0x00010074); // size depand of configured PDO
define_physical_buffer<buffer_in_cyclic_status_t>(
SYNCHROS_IN, 0x1d00, 0x00010030); // size depand of configured PDO
//----------------------------------------------------------------------------//
// I N I T S S T E P S //
//----------------------------------------------------------------------------//
add_init_step([]() {}, [this]() { unpack_status_buffer(); });
//----------------------------------------------------------------------------//
// R U N S S T E P S //
//----------------------------------------------------------------------------//
add_run_step([this]() { update_command_buffer(); },
[this]() {
unpack_status_buffer();
update_control_word_ack();
}); // add_Run_Step end
} // constructor end
void ClipX::update_command_buffer() {
auto buff = output_buffer<buffer_out_cyclic_command_t>(0x1100);
buff->fieldbus_value_1 = fieldbus_value_1_;
buff->fieldbus_value_2 = fieldbus_value_2_;
buff->fieldbus_flag = fieldbus_flag_;
buff->limit_value_1 = limit_value_1_;
buff->limit_value_2 = limit_value_2_;
buff->limit_value_3 = limit_value_3_;
buff->limit_value_4 = limit_value_4_;
buff->control_word = control_word_;
buff->parameter_set_number = parameter_set_number_;
}
void ClipX::unpack_status_buffer() {
auto buff = input_buffer<buffer_in_cyclic_status_t>(0x1d00);
system_status_ = buff->system_status;
adc_value_ = buff->adc_value;
field_value_ = buff->field_value;
gross_value_ = buff->gross_value;
net_value_ = buff->net_value;
min_value_ = buff->min_value;
max_value_ = buff->max_value;
peak_to_peak_value_ = buff->peak_to_peak_value;
captured_value_1_ = buff->captured_value_1;
captured_value_2_ = buff->captured_value_2;
calculated_value_1_ = buff->calculated_value_1;
calculated_value_2_ = buff->calculated_value_2;
calculated_value_3_ = buff->calculated_value_3;
calculated_value_4_ = buff->calculated_value_4;
calculated_value_5_ = buff->calculated_value_5;
calculated_value_6_ = buff->calculated_value_6;
neighbor_value_1_ = buff->neighbor_value_1;
neighbor_value_2_ = buff->neighbor_value_2;
neighbor_value_3_ = buff->neighbor_value_3;
neighbor_value_4_ = buff->neighbor_value_4;
neighbor_value_5_ = buff->neighbor_value_5;
neighbor_value_6_ = buff->neighbor_value_6;
external_eth_value_1_ = buff->external_eth_value_1;
external_eth_value_2_ = buff->external_eth_value_2;
analog_output_value_ = buff->analog_output_value;
io_status_low_ = buff->io_status_low;
io_stauts_high_ = buff->io_stauts_high;
measured_value_status_ = buff->measured_value_status;
limit_value_1_read_ = buff->limit_value_1;
limit_value_2_read_ = buff->limit_value_2;
limit_value_3_read_ = buff->limit_value_3;
limit_value_4_read_ = buff->limit_value_4;
control_word_validation_ = buff->control_word_validation;
parameter_set_number_read_ = buff->parameter_set_number;
}
void ClipX::update_control_word_ack() {
control_word_ = control_word_ & ~control_word_validation_;
}
//****************************************************************************//
// E N D U S E R F U N C T I O N
//****************************************************************************//
void ClipX::calibrate_volt_To_newton(float value) {<--- Function 'calibrate_volt_To_newton' argument 1 names different: declaration 'coef' definition 'value'.
clipx_calibrated_ = true;
calibrate_from_V_to_N_ = value;
}
float ClipX::adc() { return (adc_value_ * calibrate_from_V_to_N_); }
float ClipX::field() { return (field_value_ * calibrate_from_V_to_N_); }
float ClipX::gross() { return (gross_value_ * calibrate_from_V_to_N_); }
float ClipX::net() { return (net_value_ * calibrate_from_V_to_N_); }
float ClipX::min() { return (min_value_ * calibrate_from_V_to_N_); }
float ClipX::max() { return (max_value_ * calibrate_from_V_to_N_); }
float ClipX::peak_to_peak() {
return (peak_to_peak_value_ * calibrate_from_V_to_N_);
}
float ClipX::captured_1() {
return (captured_value_1_ * calibrate_from_V_to_N_);
}
float ClipX::captured_2() {
return (captured_value_2_ * calibrate_from_V_to_N_);
}
float ClipX::calculated_1() { return (calculated_value_1_); }
float ClipX::calculated_2() { return (calculated_value_2_); }
float ClipX::calculated_3() { return (calculated_value_3_); }
float ClipX::calculated_4() { return (calculated_value_4_); }
float ClipX::calculated_5() { return (calculated_value_5_); }
float ClipX::calculated_6() { return (calculated_value_6_); }
float ClipX::neighbor_1() {
return (neighbor_value_1_ * calibrate_from_V_to_N_);
}
float ClipX::neighbor_2() {
return (neighbor_value_2_ * calibrate_from_V_to_N_);
}
float ClipX::neighbor_3() {
return (neighbor_value_3_ * calibrate_from_V_to_N_);
}
float ClipX::neighbor_4() {
return (neighbor_value_4_ * calibrate_from_V_to_N_);
}
float ClipX::neighbor_5() {
return (neighbor_value_5_ * calibrate_from_V_to_N_);
}
float ClipX::neighbor_6() {
return (neighbor_value_6_ * calibrate_from_V_to_N_);
}
float ClipX::external_eth_1() { return (external_eth_value_1_); }
float ClipX::external_eth_2() { return (external_eth_value_2_); }
float ClipX::analog_output() { return (analog_output_value_); }
void ClipX::set_limit_1(float value) {
limit_value_1_ = value / calibrate_from_V_to_N_;
}
float ClipX::limit_1() {
return (limit_value_1_read_ * calibrate_from_V_to_N_);
}
void ClipX::set_limit_2(float value) {
limit_value_2_ = value / calibrate_from_V_to_N_;
}
float ClipX::limit_2() {
return (limit_value_2_read_ * calibrate_from_V_to_N_);
}
void ClipX::set_limit_3(float value) {
limit_value_3_ = value / calibrate_from_V_to_N_;
}
float ClipX::limit_3() {
return (limit_value_3_read_ * calibrate_from_V_to_N_);
}
void ClipX::set_limit_4(float value) {
limit_value_4_ = value / calibrate_from_V_to_N_;
}
float ClipX::limit_4() {
return (limit_value_4_read_ * calibrate_from_V_to_N_);
}
void ClipX::change_parameters(uint16_t value) {
if (value < 1 or value > 10) {
throw std::runtime_error("ClipX::change_parameters: invalid value " +
std::to_string(value) +
", must be in range [1,10]");
} else {
parameter_set_number_ = value;
}
}
uint16_t ClipX::parameters() { return (parameter_set_number_read_); }
void ClipX::fieldbus_1(float value) { fieldbus_value_1_ = value; }
void ClipX::fieldbus_2(float value) { fieldbus_value_2_ = value; }
void ClipX::fieldbus_flag(fieldbus_flag_t flag_id, bool state) {
fieldbus_flag_ ^= (-state ^ fieldbus_flag_) & (1U << flag_id);
}
bool ClipX::check_io_status(io_status_t io_id) {
if (io_id < 32) {
// get the state of the element selected
return ((io_status_low_ >> io_id) & 1U);
} else {
// get the state of the element selected of the "high" element
return ((io_stauts_high_ >> (io_id - 32)) & 1U);
}
}
bool ClipX::system_status(system_status_t status_id) {
// get the state of the element selected
return ((system_status_ >> status_id) & 1U);
}
bool ClipX::check_measure_validity(mesured_value_t mesured_value_id) {
// 0 -> value valid || 1 -> invalid value
// get the state of the element selected
return ((measured_value_status_ >> mesured_value_id) & 1U);
}
void ClipX::set_control_word(control_word_t control_word_id, bool state) {
control_word_ ^= (-state ^ control_word_) & (1U << control_word_id);
}
void ClipX::print_data() {
std::string unit = clipx_calibrated_ ? " N" : " mV/V";
std::cout << "system_status = 0x" << std::hex << system_status_ << std::dec
<< "\n"
<< "calibrate_from_V_to_N_ = " << calibrate_from_V_to_N_ << "\n"
<< "clipx_calibrated_ = " << clipx_calibrated_ << "\n"
<< "adc_value = " << adc_value_ << unit << "\n"
<< "field_value = " << field_value_ << unit << "\n"
<< "gross_value = " << gross_value_ << unit << "\n"
<< "net_value = " << net_value_ << unit << "\n"
<< "min_value = " << min_value_ << unit << "\n"
<< "max_value = " << max_value_ << unit << "\n"
<< "peak_to_peak_value = " << peak_to_peak_value_ << unit << "\n"
<< "captured_value_1 = " << captured_value_1_ << unit << "\n"
<< "captured_value_2 = " << captured_value_2_ << unit << "\n"
<< "calculated_value_1 = " << calculated_value_1_ << "\n"
<< "calculated_value_2 = " << calculated_value_2_ << "\n"
<< "calculated_value_3 = " << calculated_value_3_ << "\n"
<< "calculated_value_4 = " << calculated_value_4_ << "\n"
<< "calculated_value_5 = " << calculated_value_5_ << "\n"
<< "calculated_value_6 = " << calculated_value_6_ << "\n"
<< "neighbor_value_1 = " << neighbor_value_1_ << unit << "\n"
<< "neighbor_value_2 = " << neighbor_value_2_ << unit << "\n"
<< "neighbor_value_3 = " << neighbor_value_3_ << unit << "\n"
<< "neighbor_value_4 = " << neighbor_value_4_ << unit << "\n"
<< "neighbor_value_5 = " << neighbor_value_5_ << unit << "\n"
<< "neighbor_value_6 = " << neighbor_value_6_ << unit << "\n"
<< "external_eth_value_1 = " << external_eth_value_1_ << "\n"
<< "external_eth_value_2 = " << external_eth_value_2_ << "\n"
<< "analog_output_value = " << analog_output_value_ << "\n"
<< "io_status_low = 0x" << std::hex << io_status_low_ << std::dec
<< "\n"
<< "io_stauts_high = 0x" << std::hex << io_stauts_high_ << std::dec
<< "\n"
<< "measured_value_status = 0x" << std::hex
<< measured_value_status_ << std::dec << "\n"
<< "limit_value_1_read = " << limit_value_1_read_ << unit << "\n"
<< "limit_value_2_read = " << limit_value_2_read_ << unit << "\n"
<< "limit_value_3_read = " << limit_value_3_read_ << unit << "\n"
<< "limit_value_4_read = " << limit_value_4_read_ << unit << "\n"
<< "control_word_validation = 0x" << std::hex
<< control_word_validation_ << std::dec << "\n"
<< "parameter_set_number_read = " << parameter_set_number_read_
<< std::endl;
}
} // namespace ethercatcpp
|