21 constexpr
int MAX_ENTRIES = 255;
41 yCError(VIRTUALANALOGSERVER) <<
"Check configuration file top<base.";
50 mTorques.resize(mMap1-mMap0+1);
61 yCError(VIRTUALANALOGSERVER) <<
"Wrong device" << key.c_str();
68 yCError(VIRTUALANALOGSERVER) <<
"'configure' should be called before you can attach any device";
74 yCError(VIRTUALANALOGSERVER) <<
"Invalid device (null pointer)";
80 if (mpDevice->isValid())
82 mpDevice->
view(mpSensor);
86 yCError(VIRTUALANALOGSERVER) <<
"Invalid device " << key <<
" (isValid() returned false)";
114 mIsVerbose = (config.
check(
"verbose",
"if present, give detailed output"));
116 if (mIsVerbose)
yCDebug(VIRTUALANALOGSERVER) <<
"Running with verbose output\n";
121 yCDebug(VIRTUALANALOGSERVER) <<
"Using VirtualAnalogServer\n";
123 if (!config.
check(
"networks",
"list of networks merged by this wrapper"))
125 yCError(VIRTUALANALOGSERVER) <<
"Missing networks parameters";
130 mNSubdevs=networks->
size();
131 mSubdevices.resize(mNSubdevs);
133 mChan2Board.resize(MAX_ENTRIES);
134 mChan2BAddr.resize(MAX_ENTRIES);
135 for (
int i = 0; i < MAX_ENTRIES; i++)
143 for (
size_t k=0; k<networks->
size(); ++k)
146 int map0, map1, map2, map3;
148 if (parameters.size() == 2)
155 tmpBot.
fromString(parameters.get(1).asString());
156 if (tmpBot.
size() != 4)
158 yCError(VIRTUALANALOGSERVER) <<
"Error: check network parameters in part description"
159 <<
"--> I was expecting" << networks->
get(k).
asString() <<
"followed by four integers between parentheses"
160 <<
"Got: " << parameters.toString();
168 map1 = bot->get(1).asInt32();
169 map2 = bot->get(2).asInt32();
170 map3 = bot->get(3).asInt32();
172 else if (parameters.size() == 5)
174 yCError(VIRTUALANALOGSERVER) <<
"Parameter networks use deprecated syntax";
175 map0 = parameters.get(1).asInt32();
176 map1 = parameters.get(2).asInt32();
177 map2 = parameters.get(3).asInt32();
178 map3 = parameters.get(4).asInt32();
182 yCError(VIRTUALANALOGSERVER) <<
"Error: check network parameters in part description"
183 <<
"--> I was expecting" << networks->
get(k).
asString() <<
"followed by four integers between parentheses"
184 <<
"Got: " << parameters.toString();
188 if (map0 >= MAX_ENTRIES || map1 >= MAX_ENTRIES || map2>= MAX_ENTRIES || map3>= MAX_ENTRIES ||
189 map0 <0 || map1 <0 || map2<0 || map3<0)
191 yCError(VIRTUALANALOGSERVER) <<
"Invalid map entries in networks section, failed initial check";
195 for (
int j=map0; j<=map1; ++j)
198 mChan2BAddr[j]=j-map0+map2;
201 if (!mSubdevices[k].configure(map2,map3,networks->
get(k).
asString()))
203 yCError(VIRTUALANALOGSERVER) <<
"Configure of subdevice ret false";
211 if(!config.
check(
"robotName") )
213 yCError(VIRTUALANALOGSERVER) <<
"Missing robotName, check your configuration file!";
217 if (config.
check(
"deviceId"))
219 yCError(VIRTUALANALOGSERVER) <<
"The parameter 'deviceId' has been deprecated, please use parameter 'name' instead. \n"
220 <<
"e.g. In the VFT wrapper configuration files of your robot, replace '<param name=""deviceId""> left_arm </param>' \n"
221 <<
"with '/icub/joint_vsens/left_arm:i' ";
225 std::string port_name = config.
check(
"name",
Value(
"controlboard"),
"Virtual analog wrapper port name, e.g. /icub/joint_vsens/left_arm:i").asString();
226 std::string robot_name = config.
find(
"robotName").
asString();
228 if (!mPortInputTorques.open(port_name))
230 yCError(VIRTUALANALOGSERVER) <<
"Can't open port " << port_name.c_str();
239 mPortInputTorques.interrupt();
240 mPortInputTorques.close();
249 for (
int p=0; p<polylist.
size(); ++p)
251 std::string key=polylist[p]->key;
254 for (
auto& mSubdevice : mSubdevices)
256 if (mSubdevice.getKey() == key)
258 if (!mSubdevice.attach(polylist[p]->poly,key))
268 for (
auto& mSubdevice : mSubdevices)
270 if (!mSubdevice.isAttached())
288 for(
int k=0; k<mNSubdevs; ++k)
290 mSubdevices[k].detach();
302 if (first_check)
return true;
304 for (
int i=0; i<elems; i++)
306 if (mChan2Board[i]==-1 || mChan2BAddr[i]==-1)
308 yCError(VIRTUALANALOGSERVER) <<
"Invalid map entries in networks section, failed runtime check"
309 <<
" i: " << i <<
"mChan2Board[i] is " << mChan2Board[i] <<
" chan2add is " << mChan2BAddr[i];
314 yCTrace(VIRTUALANALOGSERVER) <<
"perform_first_check() successfully completed";
322 bool sendLastValueBeforeTimeout =
false;
323 while (!Thread::isStopping())
325 pTorques=mPortInputTorques.
read(
false);
330 sendLastValueBeforeTimeout =
false;
337 if (perform_first_check(6)==
false)
break;
338 mSubdevices[mChan2Board[0]].setTorque(mChan2BAddr[0],pTorques->
get(1).
asFloat64());
339 mSubdevices[mChan2Board[1]].setTorque(mChan2BAddr[1],pTorques->
get(2).
asFloat64());
340 mSubdevices[mChan2Board[2]].setTorque(mChan2BAddr[2],pTorques->
get(3).
asFloat64());
341 mSubdevices[mChan2Board[3]].setTorque(mChan2BAddr[3],pTorques->
get(4).
asFloat64());
342 mSubdevices[mChan2Board[4]].setTorque(mChan2BAddr[4],pTorques->
get(5).
asFloat64());
343 mSubdevices[mChan2Board[5]].setTorque(mChan2BAddr[5],0.0);
347 if (perform_first_check(6)==
false)
break;
348 mSubdevices[mChan2Board[0]].setTorque(mChan2BAddr[0],pTorques->
get(1).
asFloat64());
349 mSubdevices[mChan2Board[1]].setTorque(mChan2BAddr[1],pTorques->
get(2).
asFloat64());
350 mSubdevices[mChan2Board[2]].setTorque(mChan2BAddr[2],pTorques->
get(3).
asFloat64());
351 mSubdevices[mChan2Board[3]].setTorque(mChan2BAddr[3],pTorques->
get(4).
asFloat64());
352 mSubdevices[mChan2Board[4]].setTorque(mChan2BAddr[4],pTorques->
get(5).
asFloat64());
353 mSubdevices[mChan2Board[5]].setTorque(mChan2BAddr[5],pTorques->
get(6).
asFloat64());
357 if (perform_first_check(6)==
false)
break;
358 mSubdevices[mChan2Board[0]].setTorque(mChan2BAddr[0],pTorques->
get(6).
asFloat64());
359 mSubdevices[mChan2Board[1]].setTorque(mChan2BAddr[1],pTorques->
get(7).
asFloat64());
360 mSubdevices[mChan2Board[2]].setTorque(mChan2BAddr[2],0.0);
361 mSubdevices[mChan2Board[3]].setTorque(mChan2BAddr[3],0.0);
362 mSubdevices[mChan2Board[4]].setTorque(mChan2BAddr[4],0.0);
363 mSubdevices[mChan2Board[5]].setTorque(mChan2BAddr[5],0.0);
367 if (perform_first_check(3)==
false)
break;
368 mSubdevices[mChan2Board[0]].setTorque(mChan2BAddr[0],pTorques->
get(1).
asFloat64());
369 mSubdevices[mChan2Board[1]].setTorque(mChan2BAddr[1],pTorques->
get(2).
asFloat64());
370 mSubdevices[mChan2Board[2]].setTorque(mChan2BAddr[2],pTorques->
get(3).
asFloat64());
377 yCError(VIRTUALANALOGSERVER) <<
"Got unexpected " << pTorques->
get(0).
asInt32() <<
" message on virtualAnalogServer.";
380 for (
int d=0; d<mNSubdevs; ++d)
382 mSubdevices[d].flushTorques();
395 if ((lastRecv+0.080 < timeNow) && (!sendLastValueBeforeTimeout))
400 for (
int d=0; d<mNSubdevs; ++d)
402 mSubdevices[d].resetTorque();
403 mSubdevices[d].flushTorques();
408 yCError(VIRTUALANALOGSERVER) <<
"Timeout!! No new value received for more than " << timeNow - lastRecv <<
" secs.";
409 sendLastValueBeforeTimeout =
true;
bool configure(int map0, int map1, const std::string &key)
bool attach(yarp::dev::PolyDriver *driver, const std::string &key)
bool close() override
Close the DeviceDriver.
bool attachAll(const yarp::dev::PolyDriverList &p) override
Attach to a list of objects.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool detachAll() override
Detach the object (you must have first called attach).
void run() override
Main body of the new thread.
bool perform_first_check(int elems)
bool view(T *&x)
Get an interface to the device driver.
A container for a device driver.
A simple collection of objects that can be described and transmitted in a portable way.
void fromString(const std::string &text)
Initializes bottle from a string.
size_type size() const
Gets the number of elements in the bottle.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
A single value (typically within a Bottle).
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual Bottle * asList() const
Get list value.
virtual std::string asString() const
Get string value.
#define yCError(component,...)
#define yCTrace(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
void delay(double seconds)
Wait for a certain number of seconds.
An interface to the operating system, including Port based communication.