23 #include <unordered_set>
28 oss <<
"(name = \"" <<
t.name() <<
"\"";
29 if (!
t.params().empty()) {
30 oss <<
", params = [";
34 if (!
t.devices().empty()) {
35 oss <<
", devices = [";
138 if (hasDevice(
name)) {
141 for (
int i = 0; i < externalDevices.size(); i++) {
143 if (
name == externalDevice->
key) {
155 deviceFound.
poly =
nullptr;
156 deviceFound.
key =
"";
166 for (
int i = 0; i < externalDevices.size(); i++) {
168 if (
name == externalDevice->
key) {
169 deviceFound = *externalDevice;
178 std::unordered_set<std::string> externalDevicesNames;
180 for (
int i = 0; i < externalDevicesList.
size(); i++) {
182 externalDevicesNames.insert(externalDevice->
key);
186 if (externalDevicesNames.find(
device.
name()) != externalDevicesNames.end()) {
187 yError() <<
"Device name " <<
device.
name() <<
" is used for both an internal and external device.";
210 yWarning() <<
"There was some problem opening one or more devices. Please check the log and your configuration";
232 yWarning() <<
"There was some problem closing one or more devices. Please check the log and your configuration";
240 std::vector<unsigned int> levels;
247 if (action.phase() == phase) {
248 levels.push_back(action.level());
253 std::sort(levels.begin(), levels.end());
254 auto it = std::unique(levels.begin(), levels.end());
255 levels.resize(it - levels.begin());
263 std::vector<std::pair<yarp::robotinterface::experimental::Device, yarp::robotinterface::experimental::Action>> actions;
270 if (action.phase() == phase && action.level() == level) {
271 actions.emplace_back(
device, action);
294 if (!hasDeviceIncludingExternal(targetDeviceName)) {
295 yError() <<
"Target device" << targetDeviceName <<
"does not exist.";
330 if (!hasDeviceIncludingExternal(targetDeviceName)) {
331 yError() <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
337 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
344 for (
int i = 0; i < externalDevices.size(); i++) {
346 drivers.
push(externalDevice->
poly,
"all");
353 for (
size_t i = 0; i < targetNetworks.
size(); ++i) {
354 std::string targetNetwork = targetNetworks.
get(i).
toString();
361 if (!hasDeviceIncludingExternal(targetDeviceName)) {
362 yError() <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
368 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
375 if (!drivers.
size()) {
409 if (!hasDeviceIncludingExternal(targetDeviceName)) {
410 yError() <<
"Target device" << targetDeviceName <<
"does not exist.";
426 std::ostringstream oss;
458 if (&other !=
this) {
460 mPriv->build = other.mPriv->
build;
465 mPriv->devices.clear();
466 mPriv->devices = other.mPriv->
devices;
468 mPriv->params.clear();
469 mPriv->params = other.mPriv->
params;
492 return mPriv->portprefix;
497 for (
auto& device : devices()) {
501 device.params().push_back(
Param(
"verbose",
"1"));
508 for (
auto& device : devices()) {
512 device.params().push_back(
Param(
"allow-deprecated-devices",
"1"));
519 return mPriv->params;
524 return mPriv->devices;
529 return *mPriv->findDevice(name);
544 return mPriv->portprefix;
549 return mPriv->params;
554 return mPriv->devices;
559 return *mPriv->findDevice(name);
564 yInfo() <<
"Interrupt received. Stopping all running threads.";
568 for (
auto& device : devices()) {
569 device.stopThreads();
575 bool nameConflict = mPriv->checkForNamingConflictsInExternalDevices(list);
580 mPriv->externalDevices = list;
588 mPriv->currentPhase = phase;
589 mPriv->currentLevel = 0;
593 if (!mPriv->openDevices()) {
594 yError() <<
"One or more devices failed opening... see previous log messages for more info";
595 if (!mPriv->closeDevices()) {
596 yError() <<
"One or more devices failed closing";
604 if (mPriv->getLevels(phase).size() != 0) {
617 for (
auto& device : devices()) {
618 device.joinThreads();
622 std::vector<unsigned int> levels = mPriv->getLevels(phase);
625 for (std::vector<unsigned int>::const_iterator lit = levels.begin(); lit != levels.end(); ++lit) {
627 const unsigned int level = *lit;
630 mPriv->currentLevel = level;
634 if (mPriv->currentPhase != phase) {
639 std::vector<std::pair<Device, Action>> actions = mPriv->getActions(phase, level);
641 for (
auto& ait : actions) {
643 Device& device = ait.first;
644 Action& action = ait.second;
648 if (mPriv->currentPhase != phase) {
653 switch (action.
type()) {
655 if (!mPriv->configure(device, action.
params())) {
656 yError() <<
"Cannot run configure action on device" << device.
name();
661 if (!mPriv->calibrate(device, action.
params())) {
662 yError() <<
"Cannot run calibrate action on device" << device.
name();
667 if (!mPriv->attach(device, action.
params())) {
668 yError() <<
"Cannot run attach action on device" << device.
name();
673 if (!mPriv->abort(device, action.
params())) {
674 yError() <<
"Cannot run abort action on device" << device.
name();
679 if (!mPriv->detach(device, action.
params())) {
680 yError() <<
"Cannot run detach action on device" << device.
name();
685 if (!mPriv->park(device, action.
params())) {
686 yError() <<
"Cannot run park action on device" << device.
name();
691 if (!mPriv->custom(device, action.
params())) {
692 yError() <<
"Cannot run custom action on device" << device.
name();
703 yInfo() <<
"All actions for action level" << level <<
"of" <<
ActionPhaseToString(phase) <<
"phase started. Waiting for unfinished actions.";
706 for (
auto& device : devices()) {
707 device.joinThreads();
715 yWarning() <<
"There was some problem running actions for" <<
ActionPhaseToString(phase) <<
"phase . Please check the log and your configuration";
719 if (!mPriv->closeDevices()) {
720 yError() <<
"One or more devices failed closing";
732 return mPriv->currentPhase;
737 return mPriv->currentLevel;
#define YARP_FIXME_NOTIMPLEMENTED(what)
std::ostringstream & operator<<(std::ostringstream &oss, const yarp::robotinterface::experimental::Robot &t)
void push(PolyDriver *p, const char *k)
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.
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
yarp::dev::PolyDriver * driver() const
bool calibrate(const yarp::dev::PolyDriverDescriptor &target) const
bool park(const yarp::dev::PolyDriverDescriptor &target) const
bool custom(const Device &device, const ParamList ¶ms)
bool detach(const Device &device, const ParamList ¶ms)
bool attach(const Device &device, const ParamList ¶ms)
bool abort(const Device &device, const ParamList ¶ms)
yarp::robotinterface::experimental::ActionPhase currentPhase
bool hasDeviceIncludingExternal(const std::string &name) const
bool checkForNamingConflictsInExternalDevices(const yarp::dev::PolyDriverList &newExternalDevicesList)
std::vector< unsigned int > getLevels(ActionPhase phase) const
Device * findDevice(const std::string &name)
bool hasDevice(const std::string &name) const
std::vector< std::pair< Device, Action > > getActions(ActionPhase phase, unsigned int level) const
yarp::dev::PolyDriverDescriptor findDeviceIncludingExternal(const std::string &name)
bool park(const Device &device, const ParamList ¶ms)
bool calibrate(const Device &device, const ParamList ¶ms)
yarp::dev::PolyDriverList externalDevices
unsigned int currentLevel
bool configure(const Device &device, const ParamList ¶ms)
bool hasParam(const std::string &name) const
void setAllowDeprecatedDevices(bool allowDeprecatedDevices)
bool enterPhase(yarp::robotinterface::experimental::ActionPhase phase)
std::string findParam(const std::string &name) const
void setVerbose(bool verbose)
Device & device(const std::string &name)
Robot & operator=(const Robot &other)
yarp::robotinterface::experimental::ActionPhase currentPhase() const
std::string & portprefix()
bool setExternalDevices(const yarp::dev::PolyDriverList &list)
std::string findParam(const robotinterface::experimental::ParamList &list, const std::string &name)
bool hasParam(const robotinterface::experimental::ParamList &list, const std::string &name)
std::string ActionPhaseToString(robotinterface::experimental::ActionPhase actionphase)
std::vector< robotinterface::experimental::Device > DeviceList
std::string ActionTypeToString(robotinterface::experimental::ActionType actiontype)
std::vector< robotinterface::experimental::Param > ParamList