Impl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::RobotStatePredictionClientExample
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2022
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
24 #include "Impl.h"
25 
29 
34 #include <RobotAPI/libraries/armem_robot_state/aron/Proprioception.aron.generated.h>
35 #include <RobotAPI/libraries/armem_robot_state/aron/Transform.aron.generated.h>
39 
40 namespace simox::alg
41 {
42  template <class... Args>
43  std::vector<Args...>
44  concatenate(const std::vector<Args...>& lhs, const std::vector<Args...>& rhs)
45  {
46  std::vector<Args...> conc = lhs;
47  std::copy(rhs.begin(), rhs.end(), std::back_inserter(conc));
48  return conc;
49  }
50 
51  template <class KeyT, class ValueT>
52  std::map<KeyT, ValueT>
53  map_from_key_value_pairs(const std::vector<KeyT>& lhs, const std::vector<ValueT>& rhs)
54  {
55  const size_t size = std::min(lhs.size(), rhs.size());
56 
57  std::map<KeyT, ValueT> map;
58  for (size_t i = 0; i < size; ++i)
59  {
60  map.emplace(lhs[i], rhs[i]);
61  }
62  return map;
63  }
64 
65  template <class KeyT, class ValueT>
66  std::vector<ValueT>
67  multi_at(const std::map<KeyT, ValueT>& map,
68  const std::vector<KeyT>& keys,
69  bool skipMissing = false)
70  {
71  std::vector<ValueT> values;
72  values.reserve(keys.size());
73 
74  for (const KeyT& key : keys)
75  {
76  if (skipMissing)
77  {
78  if (auto it = map.find(key); it != map.end())
79  {
80  values.push_back(it->second);
81  }
82  }
83  else
84  {
85  // Throw an exception if missing.
86  values.push_back(map.at(key));
87  }
88  }
89 
90  return values;
91  }
92 
93  template <class... Args>
94  std::vector<Args...>
95  slice(const std::vector<Args...>& vector,
96  size_t start = 0,
97  std::optional<size_t> end = std::nullopt)
98  {
99  std::vector<Args...> result;
100  auto beginIt = vector.begin() + start;
101  auto endIt = end ? vector.begin() + *end : vector.end();
102  std::copy(beginIt, endIt, std::back_inserter(result));
103  return result;
104  }
105 
106 } // namespace simox::alg
107 
109 {
110 
112  {
113  client.remote.robotReader.emplace();
114  }
115 
116  Impl::~Impl() = default;
117 
118  void
120  const std::string& prefix)
121  {
122  defs->optional(properties.robotName, prefix + "robotName", "Name of the robot.");
123 
124  defs->optional(properties.predictAheadSeconds,
125  prefix + "predictAheadSeconds",
126  "How far into the future to predict [s].");
127 
128  client.remote.robotReader->registerPropertyDefinitions(defs);
129  }
130 
131  void
133  {
134  try
135  {
136  client.remote.reader =
138  ARMARX_IMPORTANT << "got reader";
139  }
141  {
142  ARMARX_WARNING << e.what();
143  }
144 
145  client.remote.robotReader->connect(mns);
146 
147  this->remote.arviz = arviz;
148  }
149 
150  void
152  {
153  task = new armarx::SimpleRunningTask<>([this]() { this->run(); });
154  task->start();
155  }
156 
157  void
159  {
160  task->stop();
161  }
162 
163  void
165  {
167 
168  while (task and not task->isStopped())
169  {
170  metronome.waitForNextTick();
171 
172  runOnce();
173  }
174  }
175 
176  void
178  {
179  ARMARX_CHECK(client.remote.reader);
180 
181  const DateTime now = Clock::Now();
182  const DateTime predictedTime =
184 
185  if (not robotViz)
186  {
187  auto desc = client.remote.robotReader->queryDescription(properties.robotName, now);
188  if (desc.has_value())
189  {
190  PackagePath& pp = desc->xml;
192  .file(pp.serialize().package, pp.serialize().path)
193  .overrideColor(simox::Color::cyan(255, 64));
194  }
195  else
196  {
197  ARMARX_INFO << "Failed to get robot description.";
198  }
199  }
200  if (not robotViz)
201  {
202  return;
203  }
204 
205  // Which entities to predict?
206 
207  const std::vector<armem::MemoryID> locEntityIDs = this->localizationEntityIDs.has_value()
208  ? this->localizationEntityIDs.value()
210  const std::vector<armem::MemoryID> propEntityIDs =
211  this->propioceptionEntityIDs.has_value() ? this->propioceptionEntityIDs.value()
213 
214  // Predict.
215 
216  auto prediction = client.predictWholeBody(
217  locEntityIDs, propEntityIDs, predictedTime, properties.robotName, "Linear");
218 
219  // Gather results.
220 
221  if (prediction.globalPose.has_value())
222  {
223  // ARMARX_INFO << "Predicted global pose: \n" << globalPose->matrix();
224  robotViz->pose(prediction.globalPose->matrix());
225 
226  if (not localizationEntityIDs)
227  {
228  // Store entity IDs for successful lookup.
229  this->localizationEntityIDs = locEntityIDs;
230  }
231  }
232  if (prediction.jointPositions.has_value())
233  {
234  robotViz->joints(prediction.jointPositions.value());
235 
236  if (not propioceptionEntityIDs)
237  {
238  this->propioceptionEntityIDs = propEntityIDs;
239  }
240  }
241 
242  // Visualize.
243  {
244  viz::Layer layer = remote.arviz.layer(properties.robotName + " Prediction");
245  layer.add(robotViz.value());
246  remote.arviz.commit(layer);
247  }
248  }
249 
250 
251 } // namespace armarx::robot_state_prediction_client_example
simox::alg::slice
std::vector< Args... > slice(const std::vector< Args... > &vector, size_t start=0, std::optional< size_t > end=std::nullopt)
Definition: Impl.cpp:95
armarx::SimpleRunningTask
Usage:
Definition: TaskUtil.h:70
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::armem::robot_state::RobotStatePredictionClient::remote
Remote remote
Definition: RobotStatePredictionClient.h:103
armarx::robot_state_prediction_client_example::Impl::localizationEntityIDs
std::optional< std::vector< armem::MemoryID > > localizationEntityIDs
Definition: Impl.h:81
armarx::robot_state_prediction_client_example::Impl::start
void start()
Definition: Impl.cpp:151
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
query.h
simox::alg::multi_at
std::vector< ValueT > multi_at(const std::map< KeyT, ValueT > &map, const std::vector< KeyT > &keys, bool skipMissing=false)
Definition: Impl.cpp:67
simox::alg
Definition: Impl.cpp:40
armarx::robot_state_prediction_client_example::Impl::propioceptionEntityIDs
std::optional< std::vector< armem::MemoryID > > propioceptionEntityIDs
Definition: Impl.h:82
armarx::robot_state_prediction_client_example::Impl::run
void run()
Definition: Impl.cpp:164
armarx::robot_state_prediction_client_example::Impl::Impl
Impl()
Definition: Impl.cpp:111
armarx::armem::robot_state::constants::memoryName
const std::string memoryName
Definition: constants.h:26
armarx::robot_state_prediction_client_example::Impl::Remote::arviz
viz::Client arviz
Definition: Impl.h:71
constants.h
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::armem::robot_state::RobotStatePredictionClient::queryProprioceptionEntityIDs
std::vector< armem::MemoryID > queryProprioceptionEntityIDs()
Definition: RobotStatePredictionClient.cpp:58
armarx::robot_state_prediction_client_example::Impl::Properties::robotName
std::string robotName
Definition: Impl.h:63
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::robot_state_prediction_client_example::Impl::remote
Remote remote
Definition: Impl.h:74
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::PackagePath::serialize
data::PackagePath serialize() const
Definition: PackagePath.cpp:76
armarx::robot_state_prediction_client_example::Impl::client
armem::robot_state::RobotStatePredictionClient client
Definition: Impl.h:79
TransformHelper.h
armarx::robot_state_prediction_client_example::Impl::robotViz
std::optional< viz::Robot > robotViz
Definition: Impl.h:83
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
armarx::armem::human::Robot
@ Robot
Definition: util.h:14
error.h
armarx::robot_state_prediction_client_example::Impl::task
armarx::SimpleRunningTask ::pointer_type task
Definition: Impl.h:76
armarx::armem::client::MemoryNameSystem::useReader
Reader useReader(const MemoryID &memoryID)
Use a memory server and get a reader for it.
Definition: MemoryNameSystem.cpp:184
armarx::robot_state_prediction_client_example::Impl::defineProperties
void defineProperties(IceUtil::Handle< armarx::PropertyDefinitionContainer > &defs, const std::string &prefix)
Definition: Impl.cpp:119
armarx::core::time::Duration::SecondsDouble
static Duration SecondsDouble(double seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:90
Metronome.h
VirtualRobotReader.h
Impl.h
armarx::robot_state_prediction_client_example::Impl::runOnce
void runOnce()
Definition: Impl.cpp:177
memory_definitions.h
ExpressionException.h
armarx::core::time::Metronome::waitForNextTick
Duration waitForNextTick()
Wait and block until the target period is met.
Definition: Metronome.cpp:31
armarx::armem::robot_state::RobotStatePredictionClient::queryLocalizationEntityIDs
std::vector< armem::MemoryID > queryLocalizationEntityIDs()
Definition: RobotStatePredictionClient.cpp:51
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::robot_state_prediction_client_example
Definition: Component.cpp:29
armarx::armem::robot_state::RobotStatePredictionClient::predictWholeBody
WholeBodyPrediction predictWholeBody(const std::vector< armem::MemoryID > &localizationEntityIDs, const std::vector< armem::MemoryID > &proprioceptionEntityIDs, armem::Time predictedTime, const std::string &robotName, const std::string &engineID="Linear")
Definition: RobotStatePredictionClient.cpp:247
simox::alg::map_from_key_value_pairs
std::map< KeyT, ValueT > map_from_key_value_pairs(const std::vector< KeyT > &lhs, const std::vector< ValueT > &rhs)
Definition: Impl.cpp:53
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
PropertyDefinitionContainer.h
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::robot_state_prediction_client_example::Impl::~Impl
~Impl()
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:35
simox::alg::concatenate
std::vector< Args... > concatenate(const std::vector< Args... > &lhs, const std::vector< Args... > &rhs)
Definition: Impl.cpp:44
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
armarx::robot_state_prediction_client_example::Impl::Properties::predictAheadSeconds
float predictAheadSeconds
Definition: Impl.h:64
armarx::armem::error::CouldNotResolveMemoryServer
Indicates that a query to the Memory Name System failed.
Definition: mns.h:26
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
armarx::core::time::Frequency::Hertz
static Frequency Hertz(std::int64_t hertz)
Definition: Frequency.cpp:23
MemoryNameSystem.h
armarx::robot_state_prediction_client_example::Impl::properties
Properties properties
Definition: Impl.h:67
min
T min(T t1, T t2)
Definition: gdiam.h:42
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:73
armarx::viz::Client
Definition: Client.h:109
armarx::PackagePath
Definition: PackagePath.h:55
armarx::viz::Layer
Definition: Layer.h:12
armarx::robot_state_prediction_client_example::Impl::stop
void stop()
Definition: Impl.cpp:158
armarx::robot_state_prediction_client_example::Impl::Properties::updateFrequencyHz
float updateFrequencyHz
Definition: Impl.h:61
armarx::robot_state_prediction_client_example::Impl::connect
void connect(armem::client::MemoryNameSystem &mns, viz::Client arviz)
Definition: Impl.cpp:132