Skip to content

Commit 066b81a

Browse files
Add real32 (Beckhoff float) and real64 (Beckhoff double) on jazzy (#197)
* Added real32 (Beckhoff float) https://infosys.beckhoff.com/english.php?content=../content/1033/ioanalogmanual/12855866763.html&id= * Changed EC_READ_REAL and EC_WRITE_REAL to bitcasting. EC_READ_REAL and EC_WRITE_REAL aren't always available. * Working SDO + altered casting of floats + added doubles * Removed unused code part SDO * Readd float+double in new pdo_channel_manager * Fix missing , * Fix -Wstrict-aliasing --------- Co-authored-by: Jens Vanhooydonck <[email protected]>
1 parent 2e4c6ff commit 066b81a

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

ethercat_interface/include/ethercat_interface/ec_sdo_manager.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class SdoConfigEntry
4343
EC_WRITE_U16(buffer, static_cast<uint16_t>(data));
4444
} else if (data_type == "int16") {
4545
EC_WRITE_S16(buffer, static_cast<int16_t>(data));
46-
} else if (data_type == "uint32") {
46+
} else if (data_type == "uint32" || data_type == "real32" || data_type == "float") {
4747
EC_WRITE_U32(buffer, static_cast<uint32_t>(data));
4848
} else if (data_type == "int32") {
4949
EC_WRITE_S32(buffer, static_cast<int32_t>(data));
50-
} else if (data_type == "uint64") {
50+
} else if (data_type == "uint64" || data_type == "real64" || data_type == "double") {
5151
EC_WRITE_U64(buffer, static_cast<uint64_t>(data));
5252
} else if (data_type == "int64") {
5353
EC_WRITE_S64(buffer, static_cast<int64_t>(data));
@@ -79,7 +79,15 @@ class SdoConfigEntry
7979
}
8080
// value
8181
if (sdo_config["value"]) {
82-
data = sdo_config["value"].as<int>();
82+
if (data_type == "float" || data_type == "real32") {
83+
float floatvalue = sdo_config["value"].as<float>();
84+
data = *reinterpret_cast<int *>(&floatvalue);
85+
} else if (data_type == "double" || data_type == "real64") {
86+
double doublevalue = sdo_config["value"].as<double>();
87+
data = *reinterpret_cast<int *>(&doublevalue);
88+
} else {
89+
data = sdo_config["value"].as<int>();
90+
}
8391
} else {
8492
std::cerr << "sdo " << index << ": missing sdo value" << std::endl;
8593
return false;
@@ -105,9 +113,9 @@ class SdoConfigEntry
105113
return 1;
106114
} else if (type == "int16" || type == "uint16") {
107115
return 2;
108-
} else if (type == "int32" || type == "uint32") {
116+
} else if (type == "int32" || type == "uint32" || type == "float" || type == "real32") {
109117
return 4;
110-
} else if (type == "int64" || type == "uint64") {
118+
} else if (type == "int64" || type == "uint64" || type == "double" || type == "real64") {
111119
return 8;
112120
}
113121
return 0;

ethercat_interface/src/ec_pdo_channel_manager.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const std::vector<std::string> ec_pdo_channel_data_types = {
3030
"int8", "uint8",
3131
"int16", "uint16",
3232
"int32", "uint32",
33-
"int64", "uint64"
33+
"int64", "uint64",
34+
"float", "real32",
35+
"double", "real64"
3436
};
3537

3638
const std::vector<uint8_t> ec_pdo_channel_data_bits = {
@@ -40,6 +42,8 @@ const std::vector<uint8_t> ec_pdo_channel_data_bits = {
4042
8, 8,
4143
16, 16,
4244
32, 32,
45+
64, 64,
46+
32, 32,
4347
64, 64
4448
};
4549

@@ -181,6 +185,22 @@ double int64_read(uint8_t * domain_address, uint8_t /*data_mask*/)
181185
return static_cast<double>(EC_READ_S64(domain_address));
182186
}
183187

188+
double real32_read(uint8_t * domain_address, uint8_t /*data_mask*/)
189+
{
190+
uint32_t raw = EC_READ_U32(domain_address);
191+
float value;
192+
std::memcpy(&value, &raw, sizeof(value));
193+
return value;
194+
}
195+
196+
double real64_read(uint8_t * domain_address, uint8_t /*data_mask*/)
197+
{
198+
uint64_t raw = EC_READ_U64(domain_address);
199+
double value;
200+
std::memcpy(&value, &raw, sizeof(value));
201+
return value;
202+
}
203+
184204
double bool_read(uint8_t * domain_address, uint8_t data_mask)
185205
{
186206
return (EC_READ_U8(domain_address) & data_mask) ? 1. : 0.;
@@ -199,6 +219,8 @@ const SingleReadFunctionType ec_pdo_single_read_functions[] = {
199219
int16_read, uint16_read,
200220
int32_read, uint32_read,
201221
int64_read, uint64_read,
222+
real32_read, real32_read,
223+
real64_read, real64_read,
202224
octet_read
203225
};
204226

@@ -246,6 +268,21 @@ void int64_write(uint8_t * domain_address, double value, uint8_t /*data_mask*/)
246268
EC_WRITE_S64(domain_address, static_cast<int64_t>(value));
247269
}
248270

271+
void real32_write(uint8_t * domain_address, double value, uint8_t /*data_mask*/)
272+
{
273+
float f = static_cast<float>(value);
274+
uint32_t raw;
275+
std::memcpy(&raw, &f, sizeof(raw));
276+
EC_WRITE_U32(domain_address, static_cast<uint32_t>(raw));
277+
}
278+
279+
void real64_write(uint8_t * domain_address, double value, uint8_t /*data_mask*/)
280+
{
281+
uint64_t raw;
282+
std::memcpy(&raw, &value, sizeof(raw));
283+
EC_WRITE_U64(domain_address, static_cast<uint64_t>(raw));
284+
}
285+
249286
/** @brief Helper function that counts the number of bits in an octet */
250287
inline
251288
uint8_t count_bits(uint8_t octet)
@@ -299,6 +336,8 @@ const SingleWriteFunctionType ec_pdo_single_write_functions[] = {
299336
int16_write, uint16_write,
300337
int32_write, uint32_write,
301338
int64_write, uint64_write,
339+
real32_write, real32_write,
340+
real64_write, real64_write,
302341
octet_override
303342
};
304343

0 commit comments

Comments
 (0)