Loading [MathJax]/extensions/tex2jax.js
iCub-main
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
FakeRawValuesPublisher.cpp
Go to the documentation of this file.
1// -*- Mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3/*
4 * Copyright (C) 2024 iCub Facility, Istituto Italiano di Tecnologia
5 * Authors: Jacopo Losi, Valentina Gaggero
6 * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
7 *
8 */
9
10// api
11
13
14// yarp includes
15#include <yarp/os/Log.h>
16#include <yarp/os/LogStream.h>
17#include <yarp/os/LogComponent.h>
18
19namespace
20{
21 YARP_LOG_COMPONENT(FAKERAWVALUESPUBLISHER, "iCub.debugLibrary.fakeRawvaluespublisher")
22}
23
24using namespace iCub::debugLibrary;
25
26// Static inline functions
27static inline bool NOT_YET_IMPLEMENTED(const char* txt)
28{
29 yCError(FAKERAWVALUESPUBLISHER) << txt << "has not yet been emplemented";
30 return true;
31}
32
33static inline bool DEPRECATED(const char* txt)
34{
35 yCError(FAKERAWVALUESPUBLISHER) << txt << "has been deprecated";
36 return true;
37}
38
39#undef DEBUG_RAW_VALUES_MACRO
40
42{
43 m_numberOfJomos = 4;
44 // set sawtooth amplitude to default value of 128. It will be overridden by the conf value
45 m_sawtoothThreshold = 128;
46 m_sawthootTestVal = 0;
47 m_rawValuesVectorTag = "rawJomoEncoderValues";
48 m_rawValuesAxesNames = {};
49 m_rawDataAuxVector = {};
50 m_rawValuesMetadataMap = {};
51}
52
53bool FakeRawValuesPublisher::getRawDataMap(std::map<std::string, std::vector<std::int32_t>> &map)
54{
55 for (auto it = m_rawValuesMetadataMap.begin(); it != m_rawValuesMetadataMap.end(); it++)
56 {
57 if(!getRawData_core(it->first, m_rawDataAuxVector))
58 {
59 yCError(FAKERAWVALUESPUBLISHER) << "getRawData_core() failed. Cannot retrieve all data from local memory";
60 return false;
61 }
62 map.insert({it->first, m_rawDataAuxVector});
63 }
64 return true;
65}
66
67bool FakeRawValuesPublisher::getRawData_core(std::string key, std::vector<std::int32_t> &data)
68{
69 //Here I need to be sure 100% the key exists!!!
70 // It must exists since the call is made while iterating over the map
71 data.clear();
72 m_sawthootTestVal = (m_sawthootTestVal < m_sawtoothThreshold) ? (++m_sawthootTestVal) : 0;
73 for (uint8_t i = 0; i < m_rawValuesMetadataMap[key].size; i++)
74 {
75 data.push_back(m_sawthootTestVal);
76 }
77 return true;
78}
79
80bool FakeRawValuesPublisher::getRawData(std::string key, std::vector<std::int32_t> &data)
81{
82 if (m_rawValuesMetadataMap.find(key) != m_rawValuesMetadataMap.end())
83 {
84 getRawData_core(key, data);
85 }
86 else
87 {
88 yCError(FAKERAWVALUESPUBLISHER) << "Request key:" << key << "is not available. Cannot retrieve get raw data.";
89 return false;
90 }
91
92 return true;
93}
94
95bool FakeRawValuesPublisher::getKeys(std::vector<std::string> &keys)
96{
97 keys.clear();
98 for (const auto &p : m_rawValuesMetadataMap)
99 {
100 keys.push_back(p.first);
101 }
102
103 return true;
104}
105
107{
108 return m_rawValuesMetadataMap.size();
109}
110
111
113{
114
115 #ifdef DEBUG_RAW_VALUES_MACRO
116 for (auto [k, v] : m_rawValuesMetadataMap)
117 {
118 yCDebug(FAKERAWVALUESPUBLISHER) << "size of elements at key:" << k << "is:" << v.rawValueNames.size();
119 for (size_t e = 0; e < v.rawValueNames.size(); e++)
120 {
121 yCDebug(FAKERAWVALUESPUBLISHER) << "For axis:" << v.axesNames[e] << "GOT rawValueNames:" << v.rawValueNames[e];
122 }
123
124 }
125 #endif
126
127 if (m_rawValuesMetadataMap.empty())
128 {
129 yCError(FAKERAWVALUESPUBLISHER) << "embObjMotionControl Map is empty. No reason to proceed...";
130 return false;
131 }
132
133 metamap.metadataMap = m_rawValuesMetadataMap;
134 return true;
135}
137{
138 if(m_rawValuesMetadataMap.find(key) != m_rawValuesMetadataMap.end())
139 {
140 meta = m_rawValuesMetadataMap[key];
141 }
142 else
143 {
144 yCError(FAKERAWVALUESPUBLISHER) << "Requested key" << key << "is not available in the map. Exiting";
145 return false;
146 }
147 return true;
148}
149
150bool FakeRawValuesPublisher::getAxesNames(std::string key, std::vector<std::string> &axesNames)
151{
152 axesNames.clear();
153 if (m_rawValuesMetadataMap.find(key) != m_rawValuesMetadataMap.end())
154 {
155 axesNames.assign(m_rawValuesMetadataMap[key].axesNames.begin(), m_rawValuesMetadataMap[key].axesNames.end());
156 }
157 else
158 {
159 yCError(FAKERAWVALUESPUBLISHER) << "Requested key" << key << "is not available in the map. Exiting";
160 return false;
161 }
162 return true;
163}
164
165bool FakeRawValuesPublisher::open(yarp::os::Searchable& config)
166{
167 if (!parseParams(config))
168 {
169 yCError(FAKERAWVALUESPUBLISHER) << "Failed to parse parameters from configuration";
170 return false;
171 }
172
173 m_numberOfJomos = m_njomos;
174 // Override sawtooth threshold value with the one passed by the configuration file
175 m_sawtoothThreshold = m_threshold;
176
177 // Instantiate map of raw values vectors
178 m_rawValuesMetadataMap.insert({m_rawValuesVectorTag, rawValuesKeyMetadata({}, {}, m_numberOfJomos)});
179 for (int i = 0; i < m_numberOfJomos; i++)
180 {
181 m_rawValuesMetadataMap[m_rawValuesVectorTag].axesNames.push_back("fake_jomo_"+std::to_string(i));
182 m_rawValuesMetadataMap[m_rawValuesVectorTag].rawValueNames.push_back("fake_jomo_rawvalue_"+std::to_string(i));
183 }
184
185 m_rawDataAuxVector.resize(m_numberOfJomos);
186 return true;
187}
188
189bool FakeRawValuesPublisher::close()
190{
191 yCInfo(FAKERAWVALUESPUBLISHER) << "Closing the device.";
192 return true;
193}
static bool NOT_YET_IMPLEMENTED(const char *txt)
static bool DEPRECATED(const char *txt)
@ data
bool parseParams(const yarp::os::Searchable &config) override
virtual bool getAxesNames(std::string key, std::vector< std::string > &axesNames) override
virtual bool getKeys(std::vector< std::string > &keys) override
virtual bool getRawData(std::string key, std::vector< std::int32_t > &data) override
virtual bool getRawDataMap(std::map< std::string, std::vector< std::int32_t > > &map) override
virtual bool getKeyMetadata(std::string key, rawValuesKeyMetadata &meta) override
virtual int getNumberOfKeys() override
virtual bool getMetadataMap(rawValuesKeyMetadataMap &metamap) override
std::map< std::string, rawValuesKeyMetadata > metadataMap