iCub-main
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 
12 #include "FakeRawValuesPublisher.h"
13 
14 // yarp includes
15 #include <yarp/os/Log.h>
16 #include <yarp/os/LogStream.h>
17 #include <yarp/os/LogComponent.h>
18 
19 namespace
20 {
21  YARP_LOG_COMPONENT(FAKERAWVALUESPUBLISHER, "iCub.debugLibrary.fakeRawvaluespublisher")
22 }
23 
24 using namespace iCub::debugLibrary;
25 
26 // Static inline functions
27 static inline bool NOT_YET_IMPLEMENTED(const char* txt)
28 {
29  yCError(FAKERAWVALUESPUBLISHER) << txt << "has not yet been emplemented";
30  return true;
31 }
32 
33 static 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_rawDataAuxVector = {};
49  m_rawValuesMetadataMap = {};
50 }
51 
52 bool FakeRawValuesPublisher::getRawDataMap(std::map<std::string, std::vector<std::int32_t>> &map)
53 {
54  for (auto it = m_rawValuesMetadataMap.begin(); it != m_rawValuesMetadataMap.end(); it++)
55  {
56  if(!getRawData_core(it->first, m_rawDataAuxVector))
57  {
58  yCError(FAKERAWVALUESPUBLISHER) << "getRawData_core() failed. Cannot retrieve all data from local memory";
59  return false;
60  }
61  map.insert({it->first, m_rawDataAuxVector});
62  }
63  return true;
64 }
65 
66 bool FakeRawValuesPublisher::getRawData_core(std::string key, std::vector<std::int32_t> &data)
67 {
68  //Here I need to be sure 100% the key exists!!!
69  // It must exists since the call is made while iterating over the map
70  data.clear();
71  m_sawthootTestVal = (m_sawthootTestVal < m_sawtoothThreshold) ? (++m_sawthootTestVal) : 0;
72  for (uint8_t i = 0; i < m_rawValuesMetadataMap[key].size; i++)
73  {
74  data.push_back(m_sawthootTestVal);
75  }
76  return true;
77 }
78 
79 bool FakeRawValuesPublisher::getRawData(std::string key, std::vector<std::int32_t> &data)
80 {
81  if (m_rawValuesMetadataMap.find(key) != m_rawValuesMetadataMap.end())
82  {
83  getRawData_core(key, data);
84  }
85  else
86  {
87  yCError(FAKERAWVALUESPUBLISHER) << "Request key:" << key << "is not available. Cannot retrieve get raw data.";
88  return false;
89  }
90 
91  return true;
92 }
93 
94 bool FakeRawValuesPublisher::getKeys(std::vector<std::string> &keys)
95 {
96  keys.clear();
97  for (const auto &p : m_rawValuesMetadataMap)
98  {
99  keys.push_back(p.first);
100  }
101 
102  return true;
103 }
104 
106 {
107  return m_rawValuesMetadataMap.size();
108 }
109 
110 
112 {
113 
114  #ifdef DEBUG_RAW_VALUES_MACRO
115  for (auto [k, v] : m_rawValuesMetadataMap)
116  {
117  yCDebug(FAKERAWVALUESPUBLISHER) << "size of elements name at key:" << k << "is:" << v.rawValueNames.size();
118  for (size_t e = 0; e < v.rawValueNames.size(); e++)
119  {
120  yCDebug(FAKERAWVALUESPUBLISHER) << "GOT to rawValueNames:" << v.rawValueNames[e];
121  }
122 
123  }
124  #endif
125 
126  if (m_rawValuesMetadataMap.empty())
127  {
128  yCError(FAKERAWVALUESPUBLISHER) << "embObjMotionControl Map is empty. No reason to proceed...";
129  return false;
130  }
131 
132  metamap.metadataMap = m_rawValuesMetadataMap;
133  return true;
134 }
136 {
137  if(m_rawValuesMetadataMap.find(key) != m_rawValuesMetadataMap.end())
138  {
139  meta = m_rawValuesMetadataMap[key];
140  }
141  else
142  {
143  yCError(FAKERAWVALUESPUBLISHER) << "Requested key" << key << "is not available in the map. Exiting";
144  return false;
145  }
146  return true;
147 }
148 
149 bool FakeRawValuesPublisher::open(yarp::os::Searchable& config)
150 {
151  if (!parseParams(config))
152  {
153  yCError(FAKERAWVALUESPUBLISHER) << "Failed to parse parameters from configuration";
154  return false;
155  }
156 
157  m_numberOfJomos = m_njomos;
158  // Override sawtooth threshold value with the one passed by the configuration file
159  m_sawtoothThreshold = m_threshold;
160 
161  // Instantiate map of raw values vectors
162  m_rawValuesMetadataMap.insert({m_rawValuesVectorTag, rawValuesKeyMetadata({}, m_numberOfJomos)});
163  for (int i = 0; i < m_numberOfJomos; i++)
164  {
165  m_rawValuesMetadataMap[m_rawValuesVectorTag].rawValueNames.push_back("fake_jomo_"+std::to_string(i));
166  }
167 
168  m_rawDataAuxVector.resize(m_numberOfJomos);
169  return true;
170 }
171 
172 bool FakeRawValuesPublisher::close()
173 {
174  yCInfo(FAKERAWVALUESPUBLISHER) << "Closing the device.";
175  return true;
176 }
static bool NOT_YET_IMPLEMENTED(const char *txt)
static bool DEPRECATED(const char *txt)
@ data
virtual bool getKeys(std::vector< std::string > &keys) override
virtual bool getRawData(std::string key, std::vector< std::int32_t > &data) override
virtual bool getKeyMetadata(std::string key, rawValuesKeyMetadata &meta) override
virtual bool getRawDataMap(std::map< std::string, std::vector< std::int32_t >> &map) override
virtual int getNumberOfKeys() override
virtual bool getMetadataMap(rawValuesKeyMetadataMap &metamap) override
std::map< std::string, rawValuesKeyMetadata > metadataMap
yarp::sig::Vector & map(yarp::sig::Vector &v, double(op)(double))
Performs a unary operator inplace on each element of a vector.
Definition: Math.cpp:305