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 = [";
137 for (
int i = 0; i < externalDevices.size(); i++) {
139 if (
name == externalDevice->
key) {
151 deviceFound.
poly =
nullptr;
152 deviceFound.
key =
"";
162 for (
int i = 0; i < externalDevices.size(); i++) {
164 if (
name == externalDevice->
key) {
165 deviceFound = *externalDevice;
174 std::unordered_set<std::string> externalDevicesNames;
176 for (
int i = 0; i < externalDevicesList.
size(); i++) {
178 externalDevicesNames.insert(externalDevice->
key);
182 if (externalDevicesNames.find(
device.
name()) != externalDevicesNames.end()) {
183 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";
238 yWarning() <<
"There was some problem closing one or more devices. Please check the log and your configuration";
246 std::vector<unsigned int> levels;
253 if (action.phase() == phase) {
254 levels.push_back(action.level());
259 std::sort(levels.begin(), levels.end());
260 auto it = std::unique(levels.begin(), levels.end());
261 levels.resize(it - levels.begin());
269 std::vector<std::pair<yarp::robotinterface::Device, yarp::robotinterface::Action>> actions;
276 if (action.phase() == phase && action.level() == level) {
277 actions.emplace_back(
device, action);
300 if (!hasDeviceIncludingExternal(targetDeviceName)) {
301 yError() <<
"Target device" << targetDeviceName <<
"does not exist.";
338 std::string targetNetwork =
"...";
343 if (!hasDeviceIncludingExternal(targetDeviceName)) {
344 yError() <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
352 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
361 for (
int i = 0; i < externalDevices.size(); i++) {
363 drivers.
push(externalDevice->
poly,
"all");
371 for (
size_t i = 0; i < targetNetworks.
size(); ++i) {
372 std::string targetNetwork = targetNetworks.
get(i).
toString();
379 if (!hasDeviceIncludingExternal(targetDeviceName)) {
380 yError() <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
388 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
400 if (!drivers.
size()) {
438 if (!hasDeviceIncludingExternal(targetDeviceName)) {
439 yError() <<
"Target device" << targetDeviceName <<
"does not exist.";
486 if (&other !=
this) {
488 mPriv->build = other.mPriv->
build;
492 mPriv->dryrun = other.mPriv->
dryrun;
495 mPriv->devices.clear();
496 mPriv->devices = other.mPriv->
devices;
498 mPriv->params.clear();
499 mPriv->params = other.mPriv->
params;
522 return mPriv->portprefix;
527 for (
auto& device : devices()) {
531 device.params().push_back(
Param(
"verbose",
"1"));
538 for (
auto& device : devices()) {
542 device.params().push_back(
Param(
"allow-deprecated-devices",
"1"));
549 mPriv->dryrun = dryrun;
554 mPriv->reverseShutdownActionOrder = reverseShutdownActionOrder;
559 return mPriv->params;
564 return mPriv->devices;
569 return *mPriv->findDevice(name);
584 return mPriv->portprefix;
589 return mPriv->params;
594 return mPriv->devices;
599 return mPriv->hasDevice(name);
604 return *mPriv->findDevice(name);
609 yInfo() <<
"Interrupt received. Stopping all running threads.";
613 for (
auto& device : devices()) {
620 bool nameConflict = mPriv->checkForNamingConflictsInExternalDevices(list);
625 mPriv->externalDevices = list;
633 mPriv->currentPhase = phase;
634 mPriv->currentLevel = 0;
638 if (!mPriv->openDevices()) {
639 yError() <<
"One or more devices failed opening... see previous log messages for more info";
640 if (!mPriv->closeDevices()) {
641 yError() <<
"One or more devices failed closing";
649 if (mPriv->getLevels(phase).size() != 0) {
662 for (
auto& device : devices()) {
663 device.joinThreads();
667 std::vector<unsigned int> levels = mPriv->getLevels(phase);
672 std::reverse(levels.begin(), levels.end());
676 for (
unsigned int level : levels) {
679 mPriv->currentLevel = level;
683 if (mPriv->currentPhase != phase) {
688 std::vector<std::pair<Device, Action>> actions = mPriv->getActions(phase, level);
690 for (
auto& ait : actions) {
692 Device& device = ait.first;
693 Action& action = ait.second;
697 if (mPriv->currentPhase != phase) {
704 switch (action.
type()) {
706 if (!mPriv->configure(device, action.
params())) {
707 yError() <<
"Cannot run configure action on device" << device.
name();
712 if (!mPriv->calibrate(device, action.
params())) {
713 yError() <<
"Cannot run calibrate action on device" << device.
name();
718 if (!mPriv->attach(device, action.
params())) {
719 yError() <<
"Cannot run attach action on device" << device.
name();
724 if (!mPriv->abort(device, action.
params())) {
725 yError() <<
"Cannot run abort action on device" << device.
name();
730 if (!mPriv->detach(device, action.
params())) {
731 yError() <<
"Cannot run detach action on device" << device.
name();
736 if (!mPriv->park(device, action.
params())) {
737 yError() <<
"Cannot run park action on device" << device.
name();
742 if (!mPriv->custom(device, action.
params())) {
743 yError() <<
"Cannot run custom action on device" << device.
name();
754 yInfo() <<
"All actions for action level" << level <<
"of" <<
ActionPhaseToString(phase) <<
"phase started. Waiting for unfinished actions.";
757 for (
auto& device : devices()) {
758 device.joinThreads();
766 yWarning() <<
"There was some problem running actions for" <<
ActionPhaseToString(phase) <<
"phase . Please check the log and your configuration";
770 if (!mPriv->closeDevices()) {
771 yError() <<
"One or more devices failed closing";
783 return mPriv->currentPhase;
788 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 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::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)
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)