22#include <unordered_set>
32 dbg <<
"(name = \"" << t.
name() <<
"\"";
34 dbg <<
", params = [";
39 dbg <<
", devices = [";
148 for (
int i = 0; i < externalDevices.size(); i++) {
150 if (
name == externalDevice->
key) {
162 deviceFound.
poly =
nullptr;
163 deviceFound.
key =
"";
173 for (
int i = 0; i < externalDevices.size(); i++) {
175 if (
name == externalDevice->
key) {
176 deviceFound = *externalDevice;
185 std::unordered_set<std::string> externalDevicesNames;
187 for (
int i = 0; i < externalDevicesList.
size(); i++) {
189 externalDevicesNames.insert(externalDevice->
key);
193 if (externalDevicesNames.find(
device.
name()) != externalDevicesNames.end()) {
194 yCError(YRI_ROBOT) <<
"Device name " <<
device.
name() <<
" is used for both an internal and external device.";
206 pcfg.
put(
"device",
"robotDescriptionStorage");
207 pcfg.
put(
"name",
"yriDescriptionStorage");
208 m_ddstorage.
open(pcfg);
209 m_ddstorage.view(m_istorage);
227 std::vector<std::string> stp;
233 pddrv->
view(dparams);
241 yCWarning(YRI_ROBOT) <<
"Device" <<
device.
name() <<
"does not derive from IDeviceDriverParams.";
244 if (!pddrv || scfg.empty())
246 yCWarning(YRI_ROBOT) <<
"Unable to get device" <<
device.
name() <<
"configuration. yarprobotinterface will continue, but some features for inspecting the device parameters will be disabled.";
247 yCDebug(YRI_ROBOT) <<
"It is recommended that devices used by yarprobinterface implement the `yarp::dev::IDeviceDriverParams` interface.";
248 yCDebug(YRI_ROBOT) <<
"See yarprobotinterface documentation page.";
257 yCError(YRI_ROBOT) <<
"Unable to register device" <<
device.
name() <<
"in robotDescriptionStorage";
266 std::vector<yarp::dev::DeviceDescription> ll;
267 m_istorage->getAllDevices(ll);
268 std::ostringstream oss;
269 for (
auto& it_device : ll)
271 oss <<
"- name:" << it_device.device_name <<
", type:" << it_device.device_type <<
"\n";
273 yCInfo(YRI_ROBOT) <<
"List of opened devices:\n"
274 << oss.str() <<
"End of list";
280 yCWarning(YRI_ROBOT) <<
"There was some problem opening one or more devices. Please check the log and your configuration";
306 yCInfo(YRI_ROBOT) <<
"All devices closed.";
308 yCWarning(YRI_ROBOT) <<
"There was some problem closing one or more devices. Please check the log and your configuration";
311 if (m_ddstorage.isValid())
321 std::vector<unsigned int> levels;
328 if (action.phase() == phase) {
329 levels.push_back(action.level());
334 std::sort(levels.begin(), levels.end());
335 auto it = std::unique(levels.begin(), levels.end());
336 levels.resize(it - levels.begin());
344 std::vector<std::pair<yarp::robotinterface::Device, yarp::robotinterface::Action>> actions;
351 if (action.phase() == phase && action.level() == level) {
352 actions.emplace_back(
device, action);
375 if (!hasDeviceIncludingExternal(targetDeviceName)) {
376 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"does not exist.";
392 if (
device.
type() ==
"robotDescription_nws_yarp")
396 drv_list.
push(&m_ddstorage, yriDescriptionStorageName.c_str());
400 yCError(YRI_ROBOT) <<
"cannot attach robotDescription_nws_yarp to yriDescriptionStorage";
426 std::string targetNetwork =
"...";
431 if (!hasDeviceIncludingExternal(targetDeviceName)) {
432 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
440 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
449 for (
int i = 0; i < externalDevices.size(); i++) {
451 drivers.
push(externalDevice->
poly,
"all");
459 for (
size_t i = 0; i < targetNetworks.
size(); ++i) {
460 std::string targetNetwork = targetNetworks.
get(i).
toString();
467 if (!hasDeviceIncludingExternal(targetDeviceName)) {
468 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
476 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
488 if (!drivers.
size()) {
525 if (!hasDeviceIncludingExternal(targetDeviceName)) {
526 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"does not exist.";
573 if (&other !=
this) {
575 mPriv->build = other.mPriv->
build;
579 mPriv->dryrun = other.mPriv->
dryrun;
582 mPriv->devices.clear();
583 mPriv->devices = other.mPriv->
devices;
585 mPriv->params.clear();
586 mPriv->params = other.mPriv->
params;
609 return mPriv->portprefix;
614 for (
auto& device : devices()) {
618 device.params().push_back(
Param(
"verbose",
"1"));
625 for (
auto& device : devices()) {
629 device.params().push_back(
Param(
"allow-deprecated-devices",
"1"));
636 mPriv->dryrun = dryrun;
641 mPriv->reverseShutdownActionOrder = reverseShutdownActionOrder;
646 return mPriv->params;
651 return mPriv->devices;
656 return *mPriv->findDevice(name);
671 return mPriv->portprefix;
676 return mPriv->params;
681 return mPriv->devices;
686 return mPriv->hasDevice(name);
691 return *mPriv->findDevice(name);
696 yCInfo(YRI_ROBOT) <<
"Interrupt received. Stopping all running threads.";
700 for (
auto& device : devices()) {
707 bool nameConflict = mPriv->checkForNamingConflictsInExternalDevices(list);
712 mPriv->externalDevices = list;
720 mPriv->currentPhase = phase;
721 mPriv->currentLevel = 0;
725 if (!mPriv->openDevices()) {
726 yCError(YRI_ROBOT) <<
"One or more devices failed opening... see previous log messages for more info";
727 if (!mPriv->closeDevices()) {
728 yCError(YRI_ROBOT) <<
"One or more devices failed closing";
736 if (mPriv->getLevels(phase).size() != 0) {
749 for (
auto& device : devices()) {
750 device.joinThreads();
754 std::vector<unsigned int> levels = mPriv->getLevels(phase);
759 std::reverse(levels.begin(), levels.end());
763 for (
unsigned int level : levels) {
766 mPriv->currentLevel = level;
770 if (mPriv->currentPhase != phase) {
775 std::vector<std::pair<Device, Action>> actions = mPriv->getActions(phase, level);
777 for (
auto& ait : actions) {
779 Device& device = ait.first;
780 Action& action = ait.second;
784 if (mPriv->currentPhase != phase) {
791 switch (action.
type()) {
793 if (!mPriv->configure(device, action.
params())) {
794 yCError(YRI_ROBOT) <<
"Cannot run configure action on device" << device.
name();
799 if (!mPriv->calibrate(device, action.
params())) {
800 yCError(YRI_ROBOT) <<
"Cannot run calibrate action on device" << device.
name();
805 if (!mPriv->attach(device, action.
params())) {
806 yCError(YRI_ROBOT) <<
"Cannot run attach action on device" << device.
name();
811 if (!mPriv->abort(device, action.
params())) {
812 yCError(YRI_ROBOT) <<
"Cannot run abort action on device" << device.
name();
817 if (!mPriv->detach(device, action.
params())) {
818 yCError(YRI_ROBOT) <<
"Cannot run detach action on device" << device.
name();
823 if (!mPriv->park(device, action.
params())) {
824 yCError(YRI_ROBOT) <<
"Cannot run park action on device" << device.
name();
829 if (!mPriv->custom(device, action.
params())) {
830 yCError(YRI_ROBOT) <<
"Cannot run custom action on device" << device.
name();
841 yCInfo(YRI_ROBOT) <<
"All actions for action level" << level <<
"of" <<
ActionPhaseToString(phase) <<
"phase started. Waiting for unfinished actions.";
844 for (
auto& device : devices()) {
845 device.joinThreads();
853 yCWarning(YRI_ROBOT) <<
"There was some problem running actions for" <<
ActionPhaseToString(phase) <<
"phase . Please check the log and your configuration";
857 if (!mPriv->closeDevices()) {
858 yCError(YRI_ROBOT) <<
"One or more devices failed closing";
870 return mPriv->currentPhase;
875 return mPriv->currentLevel;
#define YARP_FIXME_NOTIMPLEMENTED(what)
yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::Robot &t)
std::string device_configuration
configuration parameters of the device
std::string device_type
type of the device
std::string device_name
name of the device
bool view(T *&x)
Get an interface to the device driver.
An interface for the management of the parameters of a DeviceDriver.
virtual std::vector< std::string > getListOfParams() const =0
Return a list of all params used by the device.
virtual std::string getConfiguration() const =0
Return the configuration of the device.
This interface allows users to retrieve a list which contains the names and the types of the currentl...
void push(PolyDriver *p, const char *k)
A container for a device driver.
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
A class for storing options and configuration information.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
A single value (typically within a Bottle).
virtual Bottle * asList() const
Get list value.
std::string toString() const override
Return a standard text representation of the content of the object.
void fromString(const char *str)
Set value to correspond to a textual representation.
bool attach(const yarp::dev::PolyDriverList &drivers) const
bool park(const yarp::dev::PolyDriverDescriptor &target) const
yarp::dev::PolyDriver * driver() const
bool calibrate(const yarp::dev::PolyDriverDescriptor &target) const
unsigned int currentLevel
bool detach(const Device &device, const ParamList ¶ms)
std::vector< unsigned int > getLevels(ActionPhase phase) const
yarp::dev::IRobotDescription * m_istorage
Device * findDevice(const std::string &name)
std::string yriDescriptionStorageName
bool reverseShutdownActionOrder
bool park(const Device &device, const ParamList ¶ms)
bool custom(const Device &device, const ParamList ¶ms)
bool hasDevice(const std::string &name) const
bool calibrate(const Device &device, const ParamList ¶ms)
bool hasDeviceIncludingExternal(const std::string &name) const
bool attach(const Device &device, const ParamList ¶ms)
bool checkForNamingConflictsInExternalDevices(const yarp::dev::PolyDriverList &newExternalDevicesList)
yarp::dev::PolyDriver m_ddstorage
yarp::robotinterface::ActionPhase currentPhase
yarp::dev::PolyDriverDescriptor findDeviceIncludingExternal(const std::string &name)
bool configure(const Device &device, const ParamList ¶ms)
std::vector< std::pair< Device, Action > > getActions(ActionPhase phase, unsigned int level) const
yarp::dev::PolyDriverList externalDevices
bool abort(const Device &device, const ParamList ¶ms)
bool hasParam(const std::string &name) const
void setAllowDeprecatedDevices(bool allowDeprecatedDevices)
bool hasDevice(const std::string &name) const
yarp::robotinterface::ActionPhase currentPhase() const
std::string & portprefix()
void setVerbose(bool verbose)
bool enterPhase(yarp::robotinterface::ActionPhase phase)
bool setExternalDevices(const yarp::dev::PolyDriverList &list)
void setReverseShutdownActionOrder(bool reverseShutdownActionOrder)
Robot & operator=(const Robot &other)
std::string findParam(const std::string &name) const
Device & device(const std::string &name)
void setDryRun(bool dryrun)
#define yCInfo(component,...)
#define yCError(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
std::string ActionTypeToString(robotinterface::ActionType actiontype)
bool hasParam(const robotinterface::ParamList &list, const std::string &name)
std::vector< robotinterface::Param > ParamList
std::string findParam(const robotinterface::ParamList &list, const std::string &name)
std::vector< robotinterface::Device > DeviceList
std::string ActionPhaseToString(robotinterface::ActionPhase actionphase)