RobotUnitReader.cpp
Go to the documentation of this file.
1 #include "RobotUnitReader.h"
2 
3 #include <filesystem>
4 #include <istream>
5 
9 
12 
14 {
15 
17 
18  void
21  const std::string& robotTypeName)
22  {
23  {
26  << "No converter for robot type '" << robotTypeName << "' available. \n"
27  << "Known are: " << proprioceptionConverterRegistry.getKeys();
28 
29  config.loggingNames.push_back(properties.sensorPrefix);
30  receiver = robotUnitPlugin.startDataStreaming(config);
31  description = receiver->getDataDescription();
32  }
33 
34  {
37  << "No converter for robot type '" << robotTypeName << "' available. \n"
38  << "Known are: " << exteroceptionConverterRegistry.getKeys();
39  }
40 
41  {
42  // Thread-local copy of debug observer helper.
44  Logging::tag.tagName, debugObserverPlugin.getDebugObserver(), true);
45  }
46  }
47 
48  void
49  RobotUnitReader::run(float pollFrequency, Queue& dataBuffer)
50  {
51  Metronome metronome(Frequency::HertzDouble(pollFrequency));
52 
53  while (task and not task->isStopped())
54  {
55  auto start = std::chrono::high_resolution_clock::now();
56 
57  if (std::optional<RobotUnitData> commit = fetchAndConvertLatestRobotUnitData())
58  {
59  debugObserver->setDebugObserverDatafield(
60  "RobotUnitReader | t commitTimestamp [us]",
61  commit->timestamp.toMicroSecondsSinceEpoch());
62 
63  // will lock a mutex
64  dataBuffer.push(std::move(commit.value()));
65  }
66  auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
67  std::chrono::high_resolution_clock::now() - start);
68  if (debugObserver)
69  {
70  debugObserver->setDebugObserverDatafield("RobotUnitReader | t run [ms]",
71  duration.count() / 1000.f);
72  debugObserver->sendDebugObserverBatch();
73  }
74 
75  metronome.waitForNextTick();
76  }
77  }
78 
79  std::optional<RobotUnitData>
81  {
83 
84  RobotUnitData result;
85 
86  std::optional<RobotUnitDataStreaming::TimeStep> data;
87  {
88  auto start = std::chrono::high_resolution_clock::now();
89  data = fetchLatestData();
91 
92  auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
93  std::chrono::high_resolution_clock::now() - start);
94  if (debugObserver)
95  {
96  debugObserver->setDebugObserverDatafield("RobotUnitReader | t Fetch [ms]",
97  duration.count() / 1000.f);
98  }
99  }
100  if (not data.has_value())
101  {
102  return std::nullopt;
103  }
104 
105  ARMARX_DEBUG << "RobotUnitReader: Converting data current timestep to commit";
106  auto start = std::chrono::high_resolution_clock::now();
107 
108 
109  if (converterProprioception != nullptr)
110  {
112  }
113 
114  if (converterExteroception != nullptr)
115  {
117  }
118 
119  result.timestamp = Time(Duration::MicroSeconds(data->timestampUSec));
120 
121  auto stop = std::chrono::high_resolution_clock::now();
122  auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
123  ARMARX_DEBUG << "RobotUnitReader: The total time needed to convert the data to commit: "
124  << duration;
125 
126  if (debugObserver)
127  {
128  debugObserver->setDebugObserverDatafield("RobotUnitReader | t Read+Group [ms]",
129  duration.count() / 1000.f);
130  }
131 
132  return result;
133  }
134 
135  std::optional<RobotUnitDataStreaming::TimeStep>
136  RobotUnitReader::fetchLatestData()
137  {
138  std::deque<RobotUnitDataStreaming::TimeStep>& data = receiver->getDataBuffer();
139  if (debugObserver)
140  {
141  debugObserver->setDebugObserverDatafield("RobotUnitReader | Buffer Size", data.size());
142  if (data.size())
143  {
144  debugObserver->setDebugObserverDatafield("RobotUnitReader | RT Timestamp USec",
145  data.back().timestampUSec);
146  debugObserver->setDebugObserverDatafield(
147  "RobotUnitReader | RT Timestamp Since Last Iteration",
148  data.back().timesSinceLastIterationUSec);
149  debugObserver->setDebugObserverDatafield(
150  "RobotUnitReader | Timestamp Arrived in RSM",
151  armarx::DateTime::Now().toMicroSecondsSinceEpoch());
152  }
153  }
154  if (data.empty())
155  {
156  return std::nullopt;
157  }
158  else
159  {
160  RobotUnitDataStreaming::TimeStep currentTimestep = data.back();
161  data.clear();
162  return currentTimestep;
163  }
164  }
165 
166 
167 } // namespace armarx::armem::server::robot_state::proprioception
armarx::armem::server::robot_state::proprioception::RobotUnitReader::config
RobotUnitDataStreaming::Config config
Definition: RobotUnitReader.h:71
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem::server::robot_state::proprioception::RobotUnitReader::converterExteroception
exteroception::ConverterInterface * converterExteroception
Definition: RobotUnitReader.h:78
armarx::armem::server::robot_state::proprioception::RobotUnitReader::properties
Properties properties
Definition: RobotUnitReader.h:66
Frequency.h
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::armem::server::robot_state::proprioception::RobotUnitReader::converterProprioception
proprioception::ConverterInterface * converterProprioception
Definition: RobotUnitReader.h:77
armarx::armem::server::robot_state::proprioception
Definition: forward_declarations.h:78
Dict.h
armarx::armem::server::robot_state::proprioception::RobotUnitReader::Properties::sensorPrefix
std::string sensorPrefix
Definition: RobotUnitReader.h:64
armarx::armem::server::robot_state::proprioception::RobotUnitReader::debugObserver
std::optional< DebugObserverHelper > debugObserver
Definition: RobotUnitReader.h:68
RobotUnitDataStreamingReceiver.h
armarx::armem::server::robot_state::proprioception::RobotUnitReader::receiver
RobotUnitDataStreamingReceiverPtr receiver
Definition: RobotUnitReader.h:72
armarx::DebugObserverHelper::getDebugObserver
const DebugObserverInterfacePrx & getDebugObserver() const
Definition: DebugObserverHelper.cpp:149
armarx::armem::server::robot_state::exteroception::ConverterInterface::convert
virtual aron::data::DictPtr convert(const RobotUnitDataStreaming::TimeStep &data, const RobotUnitDataStreaming::DataStreamingDescription &description)=0
RobotUnitReader.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::server::robot_state::proprioception::RobotUnitData::timestamp
Time timestamp
Definition: RobotUnitData.h:15
armarx::core::time::Frequency::HertzDouble
static Frequency HertzDouble(double hertz)
Definition: Frequency.cpp:34
armarx::armem::server::robot_state::proprioception::RobotUnitReader::fetchAndConvertLatestRobotUnitData
std::optional< RobotUnitData > fetchAndConvertLatestRobotUnitData()
Definition: RobotUnitReader.cpp:80
DebugObserverComponentPlugin.h
armarx::armem::server::robot_state::proprioception::RobotUnitData
Definition: RobotUnitData.h:13
Metronome.h
armarx::armem::server::robot_state::proprioception::RobotUnitData::proprioception
aron::data::DictPtr proprioception
Definition: RobotUnitData.h:17
armarx::armem::server::robot_state::proprioception::RobotUnitReader::exteroceptionConverterRegistry
exteroception::ConverterRegistry exteroceptionConverterRegistry
Definition: RobotUnitReader.h:76
armarx::control::hardware_config::tagName
std::string tagName(ConfigTag tag)
Definition: Config.cpp:302
armarx::plugins::RobotUnitComponentPlugin
Definition: RobotUnitComponentPlugin.h:11
armarx::armem::server::robot_state::proprioception::RobotUnitReader::Queue
armarx::armem::server::robot_state::proprioception::Queue Queue
Definition: RobotUnitReader.h:42
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::plugins::RobotUnitComponentPlugin::startDataStreaming
RobotUnitDataStreamingReceiverPtr startDataStreaming(const RobotUnitDataStreaming::Config &cfg)
Definition: RobotUnitComponentPlugin.cpp:11
armarx::armem::server::robot_state::proprioception::RobotUnitData::timestampArrived
Time timestampArrived
Definition: RobotUnitData.h:16
armarx::core::time::Metronome::waitForNextTick
Duration waitForNextTick()
Wait and block until the target period is met.
Definition: Metronome.cpp:31
armarx::armem::server::robot_state::proprioception::RobotUnitReader::RobotUnitReader
RobotUnitReader()
armarx::armem::server::robot_state::proprioception::RobotUnitReader::run
void run(float pollFrequency, Queue &dataBuffer)
Reads data from handler and fills dataQueue.
Definition: RobotUnitReader.cpp:49
armarx::armem::server::robot_state::proprioception::RobotUnitReader::connect
void connect(armarx::plugins::RobotUnitComponentPlugin &robotUnitPlugin, armarx::plugins::DebugObserverComponentPlugin &debugObserverPlugin, const std::string &robotTypeName)
Definition: RobotUnitReader.cpp:19
armarx::armem::server::robot_state::proprioception::RobotUnitReader::task
armarx::SimpleRunningTask ::pointer_type task
Definition: RobotUnitReader.h:80
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:35
armarx::Logging::tag
LogTag tag
Definition: Logging.h:271
armarx::armem::server::robot_state::exteroception::ConverterRegistry::getKeys
std::vector< std::string > getKeys() const
Definition: ConverterRegistry.cpp:26
armarx::armem::server::robot_state::proprioception::ConverterRegistry::get
ConverterInterface * get(const std::string &key) const
Definition: ConverterRegistry.cpp:21
armarx::armem::server::robot_state::proprioception::RobotUnitReader::description
RobotUnitDataStreaming::DataStreamingDescription description
Definition: RobotUnitReader.h:73
armarx::armem::server::robot_state::proprioception::ConverterRegistry::getKeys
std::vector< std::string > getKeys() const
Definition: ConverterRegistry.cpp:28
armarx::armem::server::robot_state::proprioception::RobotUnitData::exteroception
aron::data::DictPtr exteroception
Definition: RobotUnitData.h:18
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
armarx::plugins::DebugObserverComponentPlugin
Brief description of class DebugObserverComponentPlugin.
Definition: DebugObserverComponentPlugin.h:48
armarx::armem::server::robot_state::proprioception::RobotUnitReader::proprioceptionConverterRegistry
ConverterRegistry proprioceptionConverterRegistry
Definition: RobotUnitReader.h:75
armarx::DebugObserverHelper
Brief description of class DebugObserverHelper.
Definition: DebugObserverHelper.h:50
armarx::armem::server::robot_state::proprioception::ConverterInterface::convert
virtual aron::data::DictPtr convert(const RobotUnitDataStreaming::TimeStep &data, const RobotUnitDataStreaming::DataStreamingDescription &description)=0
armarx::armem::server::robot_state::exteroception::ConverterRegistry::get
ConverterInterface * get(const std::string &key) const
Definition: ConverterRegistry.cpp:19