Armar6Converter.cpp
Go to the documentation of this file.
1 #include "Armar6Converter.h"
2 #include <cstddef>
3 
4 #include <SimoxUtility/algorithm/get_map_keys_values.h>
5 #include <SimoxUtility/algorithm/advanced.h>
6 
7 #include <RobotAPI/interface/units/RobotUnit/RobotUnitInterface.h>
10 
12 
13 #include "ConverterTools.h"
14 
15 
17 {
18 
20  tools(std::make_unique<ConverterTools>())
21  {
22  }
23 
24 
26  {
27  }
28 
29 
32  const RobotUnitDataStreaming::TimeStep& data,
33  const RobotUnitDataStreaming::DataStreamingDescription& description)
34  {
35  arondto::Proprioception dto;
36  dto.iterationID = data.iterationId;
37 
38  for (const auto& [dataEntryName, dataEntry] : description.entries)
39  {
40  process(dto, dataEntryName, {data, dataEntry});
41  }
42  return dto.toAron();
43  }
44 
45 
47  arondto::Proprioception& dto,
48  const std::string& entryName,
49  const ConverterValue& value)
50  {
51  const std::vector<std::string> split = simox::alg::split(entryName, ".", false, false);
53  const std::set<size_t> acceptedSizes{3, 4, 5};
54  ARMARX_CHECK_GREATER(acceptedSizes.count(split.size()), 0)
55  << "Data entry name could not be parsed (exected 3 or 4 or 5 components between '.'): "
56  << "\n- split: '" << split << "'";
57 
58  const std::string& category = split.at(0);
59  const std::string& name = split.at(1);
60  const std::string& field = split.at(2);
61  ARMARX_CHECK_EQUAL(category, "sens") << category << " | " << entryName;
62 
63  if (name == "Platform")
64  {
65  // Platform
66  processPlatformEntry(dto.platform, field, value);
67  }
68  else if (simox::alg::starts_with(name, "FT"))
69  {
70  // Force Torque
71  processForceTorqueEntry(dto.forceTorque, split, value);
72  }
73  else
74  {
75  // Joint
76  bool processed = processJointEntry(dto.joints, split, value);
77  if (not processed)
78  {
79  // Fallback: Put in extra.
80  const std::vector<std::string> comps{simox::alg::advanced(split.begin(), 1), split.end()};
81  const std::string key = simox::alg::join(comps, ".");
82 
83  switch (value.entry.type)
84  {
85  case RobotUnitDataStreaming::NodeTypeFloat:
86  dto.extraFloats[key] = getValueAs<float>(value);
87  break;
88  case RobotUnitDataStreaming::NodeTypeLong:
89  dto.extraLongs[key] = getValueAs<long>(value);
90  break;
91  case RobotUnitDataStreaming::NodeTypeBool:
92  dto.extraBools[key] = getValueAs<bool>(value);
93  break;
94  case RobotUnitDataStreaming::NodeTypeInt:
95  dto.extraInts[key] = getValueAs<int>(value);
96  break;
97  case RobotUnitDataStreaming::NodeTypeShort:
98  dto.extraShorts[key] = getValueAs<Ice::Short>(value);
99  break;
100  case RobotUnitDataStreaming::NodeTypeByte:
101  dto.extraBytes[key] = getValueAs<Ice::Byte>(value);
102  break;
103  default:
104  ARMARX_DEBUG << "Cannot handle extra field '" << key << "' of type "
105  << RobotUnitDataStreaming::DataEntryNames.to_name(value.entry.type);
106  break;
107  }
108  }
109  }
110  }
111 
112 
113 
114  void Armar6Converter::processPlatformEntry(
115  prop::arondto::Platform& dto,
116  const std::string& fieldName,
117  const ConverterValue& value)
118  {
119  if (findByPrefix(fieldName, tools->platformIgnored))
120  {
121  return;
122  }
123  else if (auto getter = findByPrefix(fieldName, tools->platformPoseGetters))
124  {
125  if (Eigen::Vector3f* dst = getter(dto))
126  {
127  if (auto setter = findBySuffix(fieldName, tools->vector3fSetters))
128  {
129  setter(*dst, getValueAs<float>(value));
130  }
131  }
132  }
133  else
134  {
135  // No setter for this field. Put in extra.
136  dto.extra[fieldName] = getValueAs<float>(value);
137  }
138  }
139 
140 
141  void Armar6Converter::processForceTorqueEntry(
142  std::map<std::string, prop::arondto::ForceTorque>& fts,
143  const std::vector<std::string>& split,
144  const ConverterValue& value)
145  {
146  const std::string& name = split.at(1);
147  std::vector<std::string> splitName = simox::alg::split(name, " ", false, false);
148  ARMARX_CHECK_EQUAL(splitName.size(), 2);
149  ARMARX_CHECK_EQUAL(splitName.at(0), "FT");
150 
151  auto it = tools->sidePrefixMap.find(splitName.at(1));
152  ARMARX_CHECK(it != tools->sidePrefixMap.end()) << splitName.at(1);
153 
154  const std::string& side = it->second;
155  processForceTorqueEntry(fts[side], split, value);
156  }
157 
158 
159  void Armar6Converter::processForceTorqueEntry(
160  prop::arondto::ForceTorque& dto,
161  const std::vector<std::string>& split,
162  const ConverterValue& value)
163  {
164  const std::string& fieldName = split.at(2);
165  if (auto getter = findByPrefix(fieldName, tools->ftGetters))
166  {
167  if (Eigen::Vector3f* dst = getter(dto))
168  {
169  if (auto setter = findBySuffix(fieldName, tools->vector3fSetters))
170  {
171  setter(*dst, getValueAs<float>(value));
172  }
173  }
174  }
175  else
176  {
177  // No setter for this field. Put in extra.
178  std::string key = split.size() == 4
179  ? (fieldName + "." + split.at(3))
180  : fieldName;
181 
182  switch (value.entry.type)
183  {
184  case RobotUnitDataStreaming::NodeTypeFloat:
185  dto.extra[key] = getValueAs<float>(value);
186  break;
187  case RobotUnitDataStreaming::NodeTypeInt:
188  dto.extra[key] = getValueAs<int>(value);
189  break;
190  case RobotUnitDataStreaming::NodeTypeLong:
191  dto.extra[key] = getValueAs<long>(value);
192  break;
193  default:
194  ARMARX_DEBUG << "Cannot handle extra field '" << key << "' of type "
195  << RobotUnitDataStreaming::DataEntryNames.to_name(value.entry.type);
196  break;
197  }
198  }
199  }
200 
201 
202  bool Armar6Converter::processJointEntry(
204  const std::vector<std::string>& split,
205  const ConverterValue& value)
206  {
207  const std::string& jointName = split.at(1);
208  const std::string& fieldName = split.at(2);
209  if (false)
210  {
211  // Only in simulation.
212  if (auto getter = findByPrefix(fieldName, tools->jointGetters))
213  {
214  if (std::map<std::string, float>* map = getter(dto))
215  {
216  (*map)[jointName] = getValueAs<float>(value);
217  }
218  }
219  }
220 
221  const std::string tempSuffix = "Temperature";
222  if (simox::alg::ends_with(split.at(2), tempSuffix))
223  {
224  // Handle "dieTemperature" etc
225  const std::string name = split.at(2).substr(0, split.at(2).size() - tempSuffix.size());
226  dto.temperature[split.at(1)][name] = getValueAs<float>(value);
227  return true;
228  }
229  else if (auto it = tools->jointSetters.find(fieldName); it != tools->jointSetters.end())
230  {
231  const ConverterTools::JointSetter& setter = it->second;
232  setter(dto, split, value);
233  return true;
234  }
235  else
236  {
237  // ARMARX_DEBUG << "Ignoring unhandled field: '" << simox::alg::join(split, ".") << "'";
238  return false;
239  }
240  }
241 
242 }
armarx::armem::server::robot_state::proprioception::findBySuffix
ValueT findBySuffix(const std::string &key, const std::map< std::string, ValueT > &map)
Definition: ConverterTools.h:62
armarx::armem::server::robot_state::proprioception::ConverterTools
Definition: ConverterTools.h:76
Armar6Converter.h
armarx::human::pose::model::k4a_bt_body_32::Joints
Joints
Joints with index as defined in the body model.
Definition: k4a_bt_body_32.h:39
armarx::armem::server::robot_state::proprioception::Armar6Converter::Armar6Converter
Armar6Converter()
Definition: Armar6Converter.cpp:19
ARMARX_CHECK_GREATER
#define ARMARX_CHECK_GREATER(lhs, rhs)
This macro evaluates whether lhs is greater (>) than rhs and if it turns out to be false it will thro...
Definition: ExpressionException.h:116
armarx::armem::server::robot_state::proprioception
Definition: forward_declarations.h:78
ConverterTools.h
armarx::armem::server::robot_state::proprioception::findByPrefix
std::optional< std::string > findByPrefix(const std::string &key, const std::set< std::string > &prefixes)
Search.
Definition: ConverterTools.cpp:14
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:43
Dict.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
RobotUnitDataStreamingReceiver.h
armarx::armem::server::robot_state::proprioception::ConverterValue
Definition: ConverterTools.h:20
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:123
aron_conversions.h
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::armem::server::robot_state::proprioception::Armar6Converter::convert
aron::data::DictPtr convert(const RobotUnitDataStreaming::TimeStep &data, const RobotUnitDataStreaming::DataStreamingDescription &description) override
Definition: Armar6Converter.cpp:31
armarx::armem::server::robot_state::proprioception::Armar6Converter::~Armar6Converter
virtual ~Armar6Converter() override
Definition: Armar6Converter.cpp:25
armarx::armem::server::robot_state::proprioception::ConverterTools::JointSetter
std::function< void(prop::arondto::Joints &dto, const std::vector< std::string > &split, const ConverterValue &value)> JointSetter
Definition: ConverterTools.h:90
std
Definition: Application.h:66
armarx::RobotUnitDataStreaming::DataEntryNames
const simox::meta::EnumNames< DataEntryType > DataEntryNames
Definition: aron_conversions.cpp:58
armarx::ends_with
bool ends_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:50
armarx::armem::server::robot_state::proprioception::Armar6Converter::process
void process(arondto::Proprioception &dto, const std::string &entryName, const ConverterValue &value)
Definition: Armar6Converter.cpp:46
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36