YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
RemoteControlBoardRemapper.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
7
8#include <yarp/os/Log.h>
10#include <yarp/os/LogStream.h>
11
12#include <algorithm>
13#include <iostream>
14#include <map>
15#include <cassert>
16
17using namespace yarp::os;
18using namespace yarp::dev;
19using namespace yarp::sig;
20
21namespace {
22YARP_LOG_COMPONENT(REMOTECONTROLBOARDREMAPPER, "yarp.device.remotecontrolboardremapper")
23}
24
25
26void RemoteControlBoardRemapper::closeAllRemoteControlBoards()
27{
28 for(size_t i = 0; i<m_remoteControlBoardDevices.size(); i++)
29 {
30 if (m_remoteControlBoardDevices[i])
31 {
32 m_remoteControlBoardDevices[i]->poly->close();
33 delete m_remoteControlBoardDevices[i]->poly;
34 m_remoteControlBoardDevices[i]->key = "";
35 m_remoteControlBoardDevices[i]->poly = nullptr;
36 }
37 }
38
39 m_remoteControlBoardDevices.clear();
40}
41
42
44{
46
47 closeAllRemoteControlBoards();
48
49 return ret;
50}
51
52bool RemoteControlBoardRemapper::openAllRemoteControlBoards(const Property& remoteControlBoardsOptions)
53{
54 for(size_t ctrlBrd=0; ctrlBrd < m_remoteControlBoards.size(); ctrlBrd++ )
55 {
56 std::string remote = m_remoteControlBoards[ctrlBrd];
57 // Note: as local parameter we use localPortPrefix+remoteOfTheReportControlBoard
58 std::string local = m_localPortPrefix+remote;
59
61 options.put("device", "controlBoard_nwc_yarp");
62 options.put("local", local);
63 options.put("remote", remote);
64
65 //the pointer will be inserted in a PolyDriverList, that will take care of closing the PolyDriver when it is destroyed
67 bool ok = tmpDriver->open(options);
68
69 if( !ok || !(tmpDriver->isValid()) )
70 {
71 yCError(REMOTECONTROLBOARDREMAPPER) << "Opening controlBoard_nwc_yarp with remote \"" << remote << "\", opening the device failed.";
72 closeAllRemoteControlBoards();
73 return false;
74 }
75
76 // We use the remote name of the controlBoard_nwc_yarp as the key for it, in absence of anything better
77 m_remoteControlBoardDevices.push(tmpDriver,remote.c_str());
78 }
79
80 return true;
81}
82
84{
85 // From RemoteControlBoardRemapper_ParamsParser
86 bool b = parseParams(config);
87 if (!b) return false;
88
89 // Load the REMOTE_CONTROLBOARD_OPTIONS, containing any additional option to pass to the remote control boards
90 Property prop;
91 prop.fromString(config.toString());
93 Bottle & optionsGroupBot = prop.findGroup("REMOTE_CONTROLBOARD_OPTIONS");
94 if( !(optionsGroupBot.isNull()) )
95 {
96 remoteControlBoardsOptions.fromString(optionsGroupBot.toString());
97 }
98
99 // Open all the remote control boards
100 b = openAllRemoteControlBoards(remoteControlBoardsOptions);
101 if (!b) return false;
102
103 // Device opened, now we open the ControlBoardRemapper and then we call attachAll
104 bool ok = true;
105/* ok = ControlBoardRemappingEngine::open(prop);
106 if( !ok )
107 {
108 yCError(REMOTECONTROLBOARDREMAPPER) << "Opening the controlboardremapper device, opening the device failed.";
109 ControlBoardRemappingEngine::detachAllDevices();
110 closeAllRemoteControlBoards();
111 return false;
112 }
113*/
116
117 // If open went ok, we now call attachAll
118 ok = ControlBoardRemappingEngine::attachAllDevices(m_remoteControlBoardDevices);
119
120 if( !ok )
121 {
122 yCError(REMOTECONTROLBOARDREMAPPER) << "Calling attachAll in the controlboardremapper device, opening the device failed.";
124 closeAllRemoteControlBoards();
125 return false;
126 }
127
128 // All went ok, return true
129 // TODO: close devices that are not actually used by the remapper
130 return true;
131}
void setAxesNames(const std::vector< std::string > &axesNames)
bool attachAllDevices(const yarp::dev::PolyDriverList &l)
void setNrOfControlledAxes(const size_t nrOfControlledAxes)
Set the number of controlled axes, resizing appropriately all the necessary buffers.
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool open(yarp::os::Searchable &prop) override
Open the device driver.
bool close() override
Close the device driver by deallocating all resources and closing ports.
void push(PolyDriver *p, const char *k)
A container for a device driver.
Definition PolyDriver.h:23
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
A mini-server for performing network communication in the background.
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.
Definition Property.h:32
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:980
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.