iCub-main
Loading...
Searching...
No Matches
RawValuesPublisherRemapper.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2025 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8
9#include <yarp/os/LogComponent.h>
10#include <yarp/os/LogStream.h>
11
12namespace {
13YARP_LOG_COMPONENT(RAWVALUESPUBLISHERREMAPPER, "yarp.device.rawvaluespublisherremapper")
14}
15
16// Private methods
17
18bool RawValuesPublisherRemapper::open(yarp::os::Searchable& config)
19{
20 yarp::os::Property prop;
21 prop.fromString(config.toString());
22
23 m_verbose = (prop.check("verbose","if present, give detailed output"));
24 if (m_verbose)
25 {
26 yCInfo(RAWVALUESPUBLISHERREMAPPER, "Running with verbose output\n");
27 }
28
29 if(!parseParams(prop))
30 {
31 yCError(RAWVALUESPUBLISHERREMAPPER) << "Error parsing configuration parameters";
32 return false;
33 }
34
35 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "RawValuesPublisherRemapper device started";
36 for (const auto& name : m_axesNames)
37 {
38 //TODO: debug print to be removed once the remapper will be fully implemented
39 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Axes Name: " << name;
40 }
41 return true;
42}
43
45{
46 return detachAll();
47}
48
49bool RawValuesPublisherRemapper::attachAll(const yarp::dev::PolyDriverList& drivers)
50{
51 if (drivers.size() < 1)
52 {
53 yCError(RAWVALUESPUBLISHERREMAPPER) << "attachAll: cannot attach to less than one device";
54 return false;
55 }
56 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Attaching to " << drivers.size() << " devices";
57 m_remappedControlBoards.resize(drivers.size());
58 for (size_t i = 0; i < drivers.size(); i++)
59 {
60 yarp::dev::PolyDriver* poly = drivers[i]->poly;
61 if (!poly)
62 {
63 yCError(RAWVALUESPUBLISHERREMAPPER) << "NullPointerException when getting the polyDriver at attachAll.";
64 detachAll();
65 return false;
66 }
67
68 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Attaching to device " << drivers[i]->key.c_str();
69
70 // View all the interfaces
71 if (!poly->view(m_remappedControlBoards[i]))
72 {
73 yCError(RAWVALUESPUBLISHERREMAPPER) << "Failure in viewing raw values publisher interface for device " << drivers[i]->key.c_str();
74 detachAll();
75 return false;
76 }
77 else
78 {
79 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Successfully viewed raw values publisher interface";
80 std::vector<std::string> keys;
81 if (m_remappedControlBoards[i]->getKeys(keys))
82 {
83 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Device " << drivers[i]->key.c_str() << " has " << keys.size() << " keys";
84 }
85 else
86 {
87 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Failed to get keys from device " << drivers[i]->key.c_str();
88 }
89 // Debug print keys
90 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Keys from device " << drivers[i]->key.c_str() << ":";
91 for (const auto& key : keys)
92 {
93 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Key: " << key;
94 }
95 }
96 }
97
98 return true;
99}
100
102{
103 m_remappedControlBoards.resize(0);
104 return true;
105}
106
107bool RawValuesPublisherRemapper::getRawDataMap(std::map<std::string, std::vector<std::int32_t>>& map)
108{
109 map.clear();
110 bool allOk = true;
111 for (size_t i = 0; i < m_remappedControlBoards.size(); i++)
112 {
113 if (!m_remappedControlBoards[i])
114 {
115 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Null pointer in m_remappedControlBoards at index " << i;
116 allOk = false;
117 continue;
118 }
119 std::map<std::string, std::vector<std::int32_t>> temp_map;
120 if (!m_remappedControlBoards[i]->getRawDataMap(temp_map))
121 {
122 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Failed to get raw data map from control board " << i;
123 allOk = false;
124 continue;
125 }
126 // If key already present in the map, append the values
127 // to avoid overwriting data from previous control boards
128 // This is useful when multiple control boards publish
129 // data under the same key
130 for (const auto& [k, v] : temp_map)
131 {
132 if (map.find(k) != map.end())
133 {
134 // Key already exists, append values
135 if(m_verbose)
136 {
137 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Key " << k << " already exists in the combined map. Appending values.";
138 }
139 map[k].insert(map[k].end(), v.begin(), v.end());
140 }
141 else
142 {
143 // Key does not exist, create new entry
144 map[k] = v;
145 }
146 }
147 }
148 return allOk;
149}
150
151bool RawValuesPublisherRemapper::getRawData(std::string key, std::vector<std::int32_t>& data)
152{
153 bool found = false;
154 data.clear();
155 for (size_t i = 0; i < m_remappedControlBoards.size(); i++)
156 {
157 if (!m_remappedControlBoards[i])
158 {
159 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Null pointer in m_remappedControlBoards at index " << i;
160 continue;
161 }
162 std::vector<std::int32_t> temp_data;
163 if (m_remappedControlBoards[i]->getRawData(key, temp_data))
164 {
165 data.insert(data.end(), temp_data.begin(), temp_data.end());
166 found = true;
167 }
168 }
169 if (!found) {
170 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Key not found: " << key;
171 }
172 return found;
173}
174
175bool RawValuesPublisherRemapper::getKeys(std::vector<std::string>& keys)
176{
177 keys.clear();
178 bool allOk = true;
179 std::set<std::string> unique_keys;
180 for (size_t i = 0; i < m_remappedControlBoards.size(); i++)
181 {
182 if (!m_remappedControlBoards[i]) {
183 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Null pointer in m_remappedControlBoards at index " << i;
184 allOk = false;
185 continue;
186 }
187 std::vector<std::string> temp_keys;
188 if (!m_remappedControlBoards[i]->getKeys(temp_keys))
189 {
190 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Failed to get keys from control board " << i;
191 allOk = false;
192 continue;
193 }
194 unique_keys.insert(temp_keys.begin(), temp_keys.end());
195 }
196 keys.assign(unique_keys.begin(), unique_keys.end());
197 return allOk;
198}
199
201{
202 std::vector<std::string> keys;
203 if (getKeys(keys)) {
204 return static_cast<int>(keys.size());
205 }
206 return -1;
207}
208
210{
211 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Getting metadata map from all attached control boards:" << m_remappedControlBoards.size();
212 bool allOk = true;
213 for (size_t i = 0; i < m_remappedControlBoards.size(); i++)
214 {
215 iCub::rawValuesKeyMetadataMap temp_metamap = {};
216 if (!m_remappedControlBoards[i])
217 {
218 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Null pointer in m_remappedControlBoards at index " << i;
219 allOk = false;
220 continue;
221 }
222 if (!m_remappedControlBoards[i]->getMetadataMap(temp_metamap))
223 {
224 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Failed to get metadata map from control board " << i;
225 allOk = false;
226 continue;
227 }
228 else
229 {
230 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Successfully got metadata map from control board " << i << "with elements:";
231 if(m_verbose)
232 {
233 for (const auto& [k, v] : temp_metamap.metadataMap)
234 {
235 yarp::os::Bottle axesNamesBottle, rawValueNamesBottle;
236 for(const auto& axisName : v.axesNames) { axesNamesBottle.addString(axisName); }
237 for(const auto& rawValueName : v.rawValueNames) { rawValueNamesBottle.addString(rawValueName); }
238 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "\tKey: " << k << " AxesNames: " << axesNamesBottle.toString() << " RawValueNames: " << rawValueNamesBottle.toString();
239 }
240 }
241 }
242 for (const auto& [k, v] : temp_metamap.metadataMap)
243 {
244 if (metamap.metadataMap.find(k) != metamap.metadataMap.end())
245 {
246 if(m_verbose)
247 {
248 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Key " << k << " already exists in the combined metadata map. Adding only values.";
249 }
250 // Append axesNames and rawValueNames to the existing entry
251 auto& existingMeta = metamap.metadataMap[k];
252 existingMeta.axesNames.insert(existingMeta.axesNames.end(), v.axesNames.begin(), v.axesNames.end());
253 existingMeta.rawValueNames.insert(existingMeta.rawValueNames.end(), v.rawValueNames.begin(), v.rawValueNames.end());
254 // Update size
255 existingMeta.size = static_cast<int>(existingMeta.rawValueNames.size());
256 }
257 else
258 {
259 yCDebug(RAWVALUESPUBLISHERREMAPPER) << "Inserting new key " << k << " in the combined metadata map.";
260 // Copying entire metadata entry
261 metamap.metadataMap[k] = v;
262 }
263 }
264
265 }
266 return allOk;
267}
268
270{
271 bool found = false;
272 meta = {};
273 for (size_t i = 0; i < m_remappedControlBoards.size(); i++)
274 {
275 if (!m_remappedControlBoards[i])
276 {
277 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Null pointer in m_remappedControlBoards at index " << i;
278 continue;
279 }
281 if (m_remappedControlBoards[i]->getKeyMetadata(key, temp_meta))
282 {
283 if (!found)
284 {
285 meta = temp_meta;
286 }
287 else
288 {
289 meta.axesNames.insert(meta.axesNames.end(), temp_meta.axesNames.begin(), temp_meta.axesNames.end());
290 meta.rawValueNames.insert(meta.rawValueNames.end(), temp_meta.rawValueNames.begin(), temp_meta.rawValueNames.end());
291 meta.size = static_cast<int>(meta.rawValueNames.size());
292 }
293 found = true;
294 }
295 }
296 if (!found) {
297 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Metadata not found for key: " << key;
298 }
299 return found;
300}
301
302bool RawValuesPublisherRemapper::getAxesNames(std::string key, std::vector<std::string>& axesNames)
303{
304 bool found = false;
305 axesNames.clear();
306 for (size_t i = 0; i < m_remappedControlBoards.size(); i++)
307 {
308 if (!m_remappedControlBoards[i]) {
309 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Null pointer in m_remappedControlBoards at index " << i;
310 continue;
311 }
312 std::vector<std::string> temp_axesNames;
313 if (m_remappedControlBoards[i]->getAxesNames(key, temp_axesNames))
314 {
315 axesNames.insert(axesNames.end(), temp_axesNames.begin(), temp_axesNames.end());
316 found = true;
317 }
318 }
319 if (!found)
320 {
321 yCWarning(RAWVALUESPUBLISHERREMAPPER) << "Axes names not found for key: " << key;
322 }
323 return found;
324}
@ data
bool parseParams(const yarp::os::Searchable &config) override
bool getAxesNames(std::string key, std::vector< std::string > &axesNames) override
bool attachAll(const yarp::dev::PolyDriverList &p) override
MultipeWrapper methods.
bool getRawDataMap(std::map< std::string, std::vector< std::int32_t > > &map) override
bool open(yarp::os::Searchable &config) override
bool getKeys(std::vector< std::string > &keys) override
bool getMetadataMap(iCub::rawValuesKeyMetadataMap &metamap) override
bool getRawData(std::string key, std::vector< std::int32_t > &data) override
bool getKeyMetadata(std::string key, iCub::rawValuesKeyMetadata &meta) override
std::map< std::string, rawValuesKeyMetadata > metadataMap
std::vector< std::string > axesNames
std::vector< std::string > rawValueNames