20 #include <unordered_set>
25 dbg <<
"(name = \"" <<
t.name() <<
"\"";
26 if (!
t.params().empty()) {
27 dbg <<
", params = [";
31 if (!
t.devices().empty()) {
32 dbg <<
", devices = [";
138 for (
int i = 0; i < externalDevices.size(); i++) {
140 if (
name == externalDevice->
key) {
152 deviceFound.
poly =
nullptr;
153 deviceFound.
key =
"";
163 for (
int i = 0; i < externalDevices.size(); i++) {
165 if (
name == externalDevice->
key) {
166 deviceFound = *externalDevice;
175 std::unordered_set<std::string> externalDevicesNames;
177 for (
int i = 0; i < externalDevicesList.
size(); i++) {
179 externalDevicesNames.insert(externalDevice->
key);
183 if (externalDevicesNames.find(
device.
name()) != externalDevicesNames.end()) {
184 yError() <<
"Device name " <<
device.
name() <<
" is used for both an internal and external device.";
207 yWarning() <<
"There was some problem opening one or more devices. Please check the log and your configuration";
229 yWarning() <<
"There was some problem closing one or more devices. Please check the log and your configuration";
237 std::vector<unsigned int> levels;
244 if (action.phase() == phase) {
245 levels.push_back(action.level());
250 std::sort(levels.begin(), levels.end());
251 auto it = std::unique(levels.begin(), levels.end());
252 levels.resize(it - levels.begin());
260 std::vector<std::pair<yarp::robotinterface::Device, yarp::robotinterface::Action>> actions;
267 if (action.phase() == phase && action.level() == level) {
268 actions.emplace_back(
device, action);
291 if (!hasDeviceIncludingExternal(targetDeviceName)) {
292 yError() <<
"Target device" << targetDeviceName <<
"does not exist.";
324 std::string targetNetwork =
"...";
329 if (!hasDeviceIncludingExternal(targetDeviceName)) {
330 yError() <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
336 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
343 for (
int i = 0; i < externalDevices.size(); i++) {
345 drivers.
push(externalDevice->
poly,
"all");
352 for (
size_t i = 0; i < targetNetworks.
size(); ++i) {
353 std::string targetNetwork = targetNetworks.
get(i).
toString();
360 if (!hasDeviceIncludingExternal(targetDeviceName)) {
361 yError() <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
367 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
374 if (!drivers.
size()) {
408 if (!hasDeviceIncludingExternal(targetDeviceName)) {
409 yError() <<
"Target device" << targetDeviceName <<
"does not exist.";
449 if (&other !=
this) {
451 mPriv->build = other.mPriv->
build;
456 mPriv->devices.clear();
457 mPriv->devices = other.mPriv->
devices;
459 mPriv->params.clear();
460 mPriv->params = other.mPriv->
params;
483 return mPriv->portprefix;
488 for (
auto& device : devices()) {
492 device.params().push_back(
Param(
"verbose",
"1"));
499 for (
auto& device : devices()) {
503 device.params().push_back(
Param(
"allow-deprecated-devices",
"1"));
510 return mPriv->params;
515 return mPriv->devices;
520 return *mPriv->findDevice(name);
535 return mPriv->portprefix;
540 return mPriv->params;
545 return mPriv->devices;
550 return mPriv->hasDevice(name);
555 return *mPriv->findDevice(name);
560 yInfo() <<
"Interrupt received. Stopping all running threads.";
564 for (
auto& device : devices()) {
565 device.stopThreads();
571 bool nameConflict = mPriv->checkForNamingConflictsInExternalDevices(list);
576 mPriv->externalDevices = list;
584 mPriv->currentPhase = phase;
585 mPriv->currentLevel = 0;
589 if (!mPriv->openDevices()) {
590 yError() <<
"One or more devices failed opening... see previous log messages for more info";
591 if (!mPriv->closeDevices()) {
592 yError() <<
"One or more devices failed closing";
600 if (mPriv->getLevels(phase).size() != 0) {
613 for (
auto& device : devices()) {
614 device.joinThreads();
618 std::vector<unsigned int> levels = mPriv->getLevels(phase);
621 for (std::vector<unsigned int>::const_iterator lit = levels.begin(); lit != levels.end(); ++lit) {
623 const unsigned int level = *lit;
626 mPriv->currentLevel = level;
630 if (mPriv->currentPhase != phase) {
635 std::vector<std::pair<Device, Action>> actions = mPriv->getActions(phase, level);
637 for (
auto& ait : actions) {
639 Device& device = ait.first;
640 Action& action = ait.second;
644 if (mPriv->currentPhase != phase) {
649 switch (action.
type()) {
651 if (!mPriv->configure(device, action.
params())) {
652 yError() <<
"Cannot run configure action on device" << device.
name();
657 if (!mPriv->calibrate(device, action.
params())) {
658 yError() <<
"Cannot run calibrate action on device" << device.
name();
663 if (!mPriv->attach(device, action.
params())) {
664 yError() <<
"Cannot run attach action on device" << device.
name();
669 if (!mPriv->abort(device, action.
params())) {
670 yError() <<
"Cannot run abort action on device" << device.
name();
675 if (!mPriv->detach(device, action.
params())) {
676 yError() <<
"Cannot run detach action on device" << device.
name();
681 if (!mPriv->park(device, action.
params())) {
682 yError() <<
"Cannot run park action on device" << device.
name();
687 if (!mPriv->custom(device, action.
params())) {
688 yError() <<
"Cannot run custom action on device" << device.
name();
699 yInfo() <<
"All actions for action level" << level <<
"of" <<
ActionPhaseToString(phase) <<
"phase started. Waiting for unfinished actions.";
702 for (
auto& device : devices()) {
703 device.joinThreads();
711 yWarning() <<
"There was some problem running actions for" <<
ActionPhaseToString(phase) <<
"phase . Please check the log and your configuration";
715 if (!mPriv->closeDevices()) {
716 yError() <<
"One or more devices failed closing";
728 return mPriv->currentPhase;
733 return mPriv->currentLevel;
#define YARP_FIXME_NOTIMPLEMENTED(what)
yarp::os::LogStream operator<<(yarp::os::LogStream dbg, const yarp::robotinterface::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
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
Device * findDevice(const std::string &name)
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::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)
Robot & operator=(const Robot &other)
std::string findParam(const std::string &name) const
Device & device(const std::string &name)
yarp::robotinterface::Param Param
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)