3 #ifndef __DEVICE_MAPPER_HPP__
4 #define __DEVICE_MAPPER_HPP__
12 #include <unordered_map>
15 #include <yarp/dev/PolyDriver.h>
17 #include "FutureTask.hpp"
18 #include "RawDevice.hpp"
84 {
return taskFactory->createTask(); }
90 using dev_group_t = std::tuple<const RawDevice *, std::vector<int>,
int>;
96 const std::vector<std::unique_ptr<const RawDevice>> &
getDevices()
const
131 {
return totalAxes; }
134 template<
typename T,
typename... T_ref>
138 template<
typename T,
typename... T_ref>
142 T * p = device->getHandle<T>();
143 return p ? std::invoke(fn, p, offset, ref...) :
false;
147 template<
typename T,
typename... T_refs>
151 template<
typename T,
typename... T_refs>
159 T * p = device->template getHandle<T>();
160 ok |= p && (task->add(p, fn, refs + offset...),
true);
164 return ok && task->dispatch();
168 template<
typename T,
typename... T_refs>
172 template<
typename T,
typename... T_refs>
179 for (
const auto & [device, localIndices, globalIndex] : devices)
181 T * p = device->template getHandle<T>();
182 ok &= p && (task->add(p, fn, localIndices.size(), localIndices.data(), refs + globalIndex...),
true);
186 return ok && task->dispatch();
194 if (
auto it = connectedSensors.find(
typeid(T)); it != connectedSensors.cend())
211 if (
auto it = sensorOffsets.find(
typeid(T)); it != sensorOffsets.cend())
213 const auto & [deviceIndex, offset, count] = it->second[globalIndex];
214 return {devices[deviceIndex].get(), globalIndex - offset};
220 template<
typename T,
typename T_out>
221 using sensor_status_fn = T_out (T::*)(std::size_t)
const;
227 template<
typename T,
typename T_out>
230 auto [device, offset] = getSensorDevice<T>(index);
231 T * p = device->template getHandle<T>();
232 return p ? std::invoke(fn, p, offset) :
static_cast<T_out
>(DeviceMapper::getSensorFailureStatus());
236 using sensor_size_fn = std::size_t (T::*)(std::size_t)
const;
245 auto [device, offset] = getSensorDevice<T>(index);
246 T * p = device->template getHandle<T>();
247 return p ? std::invoke(fn, p, offset) : 0;
250 template<
typename T,
typename... T_out_params>
251 using sensor_output_fn = bool (T::*)(std::size_t, T_out_params &...)
const;
257 template<
typename T,
typename... T_out_params>
258 bool getSensorOutput(sensor_output_fn<T, T_out_params...> fn, std::size_t index, T_out_params &... params)
const
260 auto [device, offset] = getSensorDevice<T>(index);
261 T * p = device->template getHandle<T>();
262 return p ? std::invoke(fn, p, offset, params...) :
false;
266 static const int getSensorFailureStatus();
268 using dev_index_offset_t = std::tuple<int, int, int>;
270 std::vector<std::unique_ptr<const RawDevice>> devices;
271 std::vector<dev_index_offset_t> motorOffsets;
272 std::unordered_map<std::type_index, std::vector<dev_index_offset_t>> sensorOffsets;
273 std::unordered_map<std::type_index, int> connectedSensors;
277 std::unique_ptr<FutureTaskFactory> taskFactory;
Exposes raw subdevice interface handles on a per-axis manner.
Definition: DeviceMapper.hpp:65
const std::vector< std::unique_ptr< const RawDevice > > & getDevices() const
Retrieve all registered raw devices, regardless of type.
Definition: DeviceMapper.hpp:96
std::vector< dev_index_t > getMotorDevicesWithOffsets() const
Retrieve all registered motor subdevices and their associated offsets.
Definition: DeviceMapper.cpp:166
std::tuple< const RawDevice *, std::vector< int >, int > dev_group_t
Tuple of a raw device pointer, its local indices and the global index.
Definition: DeviceMapper.hpp:90
std::tuple< const RawDevice *, int > dev_index_t
Tuple of a raw device pointer and either an offset or a local index.
Definition: DeviceMapper.hpp:87
bool(T::*)(T_refs *...) motor_all_joints_fn
Alias for a full-joint command. See class description.
Definition: DeviceMapper.hpp:148
int getConnectedSensors() const
Retrieve the number of connected sensors of the specified type across all subdevices.
Definition: DeviceMapper.hpp:191
std::size_t getSensorArraySize(sensor_size_fn< T > fn, std::size_t index) const
Retrieve the size of the sensor array at the specified global index.
Definition: DeviceMapper.hpp:243
DeviceMapper()
Constructor.
Definition: DeviceMapper.cpp:110
void enableParallelization(unsigned int concurrentTasks)
Whether to enable parallel mappings and on how many concurrent threads.
Definition: DeviceMapper.cpp:119
bool(T::*)(int, T_ref...) motor_single_joint_fn
Alias for a single-joint command. See class description.
Definition: DeviceMapper.hpp:135
dev_index_t getSensorDevice(int globalIndex) const
Retrieve a sensor device handle and its local index given a global index.
Definition: DeviceMapper.hpp:209
void clear()
Delete all internal handles.
Definition: DeviceMapper.cpp:205
bool registerDevice(yarp::dev::PolyDriver *driver)
Extract interface handles and perform sanity checks.
Definition: DeviceMapper.cpp:124
std::unique_ptr< FutureTask > createTask() const
Create an instance of a deferred task.
Definition: DeviceMapper.hpp:83
~DeviceMapper()
Destructor.
Definition: DeviceMapper.cpp:114
T_out getSensorStatus(sensor_status_fn< T, T_out > fn, std::size_t index) const
Retrieve the status of the sensor device at the specified global index.
Definition: DeviceMapper.hpp:228
std::vector< dev_group_t > getMotorDevicesWithIndices(int globalAxesCount, const int *globalAxes) const
Retrieve motor subdevices that map to the specified global axes.
Definition: DeviceMapper.cpp:180
int getControlledAxes() const
Retrieve the number of controlled axes across all subdevices.
Definition: DeviceMapper.hpp:130
bool(T::*)(int, const int *, T_refs *...) motor_multi_joints_fn
Alias for a joint-group command. See class description.
Definition: DeviceMapper.hpp:169
bool mapSingleJoint(motor_single_joint_fn< T, T_ref... > fn, int j, T_ref... ref)
Single-joint command mapping. See class description.
Definition: DeviceMapper.hpp:139
bool mapJointGroup(motor_multi_joints_fn< T, T_refs... > fn, int n_joint, const int *joints, T_refs *... refs)
Joint-group command mapping. See class description.
Definition: DeviceMapper.hpp:173
bool mapAllJoints(motor_all_joints_fn< T, T_refs... > fn, T_refs *... refs)
Full-joint command mapping. See class description.
Definition: DeviceMapper.hpp:152
dev_index_t getMotorDevice(int globalAxis) const
Retrieve a motor device handle and its local index given a global index.
Definition: DeviceMapper.cpp:160
bool getSensorOutput(sensor_output_fn< T, T_out_params... > fn, std::size_t index, T_out_params &... params) const
Retrieve information from the sensor device at the specified global index.
Definition: DeviceMapper.hpp:258
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6
const RawDevice invalidDevice(nullptr)
Singleton instance for an invalid (empty) raw device.