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 yCError(YRI_ROBOT) <<
"Device" <<
device.
name() <<
"does not derive from IDeviceDriverParams.";
244 if (!pddrv || scfg.empty())
246 yCError(YRI_ROBOT) <<
"Unable to get device" <<
device.
name() <<
"configuration";
255 yCError(YRI_ROBOT) <<
"Unable to register device" <<
device.
name() <<
"in robotDescriptionStorage";
264 std::vector<yarp::dev::DeviceDescription> ll;
265 m_istorage->getAllDevices(ll);
266 std::ostringstream oss;
267 for (
auto& it_device : ll)
269 oss <<
"- name:" << it_device.device_name <<
", type:" << it_device.device_type <<
"\n";
271 yCInfo(YRI_ROBOT) <<
"List of opened devices:\n"
272 << oss.str() <<
"End of list";
278 yCWarning(YRI_ROBOT) <<
"There was some problem opening one or more devices. Please check the log and your configuration";
304 yCInfo(YRI_ROBOT) <<
"All devices closed.";
306 yCWarning(YRI_ROBOT) <<
"There was some problem closing one or more devices. Please check the log and your configuration";
309 if (m_ddstorage.isValid())
319 std::vector<unsigned int> levels;
326 if (action.phase() == phase) {
327 levels.push_back(action.level());
332 std::sort(levels.begin(), levels.end());
333 auto it = std::unique(levels.begin(), levels.end());
334 levels.resize(it - levels.begin());
342 std::vector<std::pair<yarp::robotinterface::Device, yarp::robotinterface::Action>> actions;
349 if (action.phase() == phase && action.level() == level) {
350 actions.emplace_back(
device, action);
373 if (!hasDeviceIncludingExternal(targetDeviceName)) {
374 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"does not exist.";
390 if (
device.
type() ==
"robotDescription_nws_yarp")
394 drv_list.
push(&m_ddstorage, yriDescriptionStorageName.c_str());
398 yCError(YRI_ROBOT) <<
"cannot attach robotDescription_nws_yarp to yriDescriptionStorage";
424 std::string targetNetwork =
"...";
429 if (!hasDeviceIncludingExternal(targetDeviceName)) {
430 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
438 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
447 for (
int i = 0; i < externalDevices.size(); i++) {
449 drivers.
push(externalDevice->
poly,
"all");
457 for (
size_t i = 0; i < targetNetworks.
size(); ++i) {
458 std::string targetNetwork = targetNetworks.
get(i).
toString();
465 if (!hasDeviceIncludingExternal(targetDeviceName)) {
466 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"(network =" << targetNetwork <<
") does not exist.";
474 drivers.
push(targetDevice.
poly, targetNetwork.c_str());
486 if (!drivers.
size()) {
523 if (!hasDeviceIncludingExternal(targetDeviceName)) {
524 yCError(YRI_ROBOT) <<
"Target device" << targetDeviceName <<
"does not exist.";
571 if (&other !=
this) {
573 mPriv->build = other.mPriv->
build;
577 mPriv->dryrun = other.mPriv->
dryrun;
580 mPriv->devices.clear();
581 mPriv->devices = other.mPriv->
devices;
583 mPriv->params.clear();
584 mPriv->params = other.mPriv->
params;
607 return mPriv->portprefix;
612 for (
auto& device : devices()) {
616 device.params().push_back(
Param(
"verbose",
"1"));
623 for (
auto& device : devices()) {
627 device.params().push_back(
Param(
"allow-deprecated-devices",
"1"));
634 mPriv->dryrun = dryrun;
639 mPriv->reverseShutdownActionOrder = reverseShutdownActionOrder;
644 return mPriv->params;
649 return mPriv->devices;
654 return *mPriv->findDevice(name);
669 return mPriv->portprefix;
674 return mPriv->params;
679 return mPriv->devices;
684 return mPriv->hasDevice(name);
689 return *mPriv->findDevice(name);
694 yCInfo(YRI_ROBOT) <<
"Interrupt received. Stopping all running threads.";
698 for (
auto& device : devices()) {
705 bool nameConflict = mPriv->checkForNamingConflictsInExternalDevices(list);
710 mPriv->externalDevices = list;
718 mPriv->currentPhase = phase;
719 mPriv->currentLevel = 0;
723 if (!mPriv->openDevices()) {
724 yCError(YRI_ROBOT) <<
"One or more devices failed opening... see previous log messages for more info";
725 if (!mPriv->closeDevices()) {
726 yCError(YRI_ROBOT) <<
"One or more devices failed closing";
734 if (mPriv->getLevels(phase).size() != 0) {
747 for (
auto& device : devices()) {
748 device.joinThreads();
752 std::vector<unsigned int> levels = mPriv->getLevels(phase);
757 std::reverse(levels.begin(), levels.end());
761 for (
unsigned int level : levels) {
764 mPriv->currentLevel = level;
768 if (mPriv->currentPhase != phase) {
773 std::vector<std::pair<Device, Action>> actions = mPriv->getActions(phase, level);
775 for (
auto& ait : actions) {
777 Device& device = ait.first;
778 Action& action = ait.second;
782 if (mPriv->currentPhase != phase) {
789 switch (action.
type()) {
791 if (!mPriv->configure(device, action.
params())) {
792 yCError(YRI_ROBOT) <<
"Cannot run configure action on device" << device.
name();
797 if (!mPriv->calibrate(device, action.
params())) {
798 yCError(YRI_ROBOT) <<
"Cannot run calibrate action on device" << device.
name();
803 if (!mPriv->attach(device, action.
params())) {
804 yCError(YRI_ROBOT) <<
"Cannot run attach action on device" << device.
name();
809 if (!mPriv->abort(device, action.
params())) {
810 yCError(YRI_ROBOT) <<
"Cannot run abort action on device" << device.
name();
815 if (!mPriv->detach(device, action.
params())) {
816 yCError(YRI_ROBOT) <<
"Cannot run detach action on device" << device.
name();
821 if (!mPriv->park(device, action.
params())) {
822 yCError(YRI_ROBOT) <<
"Cannot run park action on device" << device.
name();
827 if (!mPriv->custom(device, action.
params())) {
828 yCError(YRI_ROBOT) <<
"Cannot run custom action on device" << device.
name();
839 yCInfo(YRI_ROBOT) <<
"All actions for action level" << level <<
"of" <<
ActionPhaseToString(phase) <<
"phase started. Waiting for unfinished actions.";
842 for (
auto& device : devices()) {
843 device.joinThreads();
851 yCWarning(YRI_ROBOT) <<
"There was some problem running actions for" <<
ActionPhaseToString(phase) <<
"phase . Please check the log and your configuration";
855 if (!mPriv->closeDevices()) {
856 yCError(YRI_ROBOT) <<
"One or more devices failed closing";
868 return mPriv->currentPhase;
873 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 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)