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)
147 if (parameters.
size()!=5)
149 yCError(VIRTUALANALOGSERVER) <<
"Check network parameters in part description."
150 <<
"I was expecting" << networks->
get(k).
asString().c_str() <<
"followed by four integers";
158 if (map0 >= MAX_ENTRIES || map1 >= MAX_ENTRIES || map2>= MAX_ENTRIES || map3>= MAX_ENTRIES ||
159 map0 <0 || map1 <0 || map2<0 || map3<0)
161 yCError(VIRTUALANALOGSERVER) <<
"Invalid map entries in networks section, failed initial check";
165 for (
int j=map0; j<=map1; ++j)
168 mChan2BAddr[j]=j-map0+map2;
171 if (!mSubdevices[k].configure(map2,map3,networks->
get(k).
asString()))
173 yCError(VIRTUALANALOGSERVER) <<
"Configure of subdevice ret false";
181 if(!config.
check(
"robotName") )
183 yCError(VIRTUALANALOGSERVER) <<
"Missing robotName, check your configuration file!";
187 if (config.
check(
"deviceId"))
189 yCError(VIRTUALANALOGSERVER) <<
"The parameter 'deviceId' has been deprecated, please use parameter 'name' instead. \n"
190 <<
"e.g. In the VFT wrapper configuration files of your robot, replace '<param name=""deviceId""> left_arm </param>' \n"
191 <<
"with '/icub/joint_vsens/left_arm:i' ";
195 std::string port_name = config.
check(
"name",
Value(
"controlboard"),
"Virtual analog wrapper port name, e.g. /icub/joint_vsens/left_arm:i").asString();
196 std::string robot_name = config.
find(
"robotName").
asString();
198 if (!mPortInputTorques.open(port_name))
200 yCError(VIRTUALANALOGSERVER) <<
"Can't open port " << port_name.c_str();
209 mPortInputTorques.interrupt();
210 mPortInputTorques.close();
219 for (
int p=0; p<polylist.
size(); ++p)
221 std::string key=polylist[p]->key;
224 for (
auto& mSubdevice : mSubdevices)
226 if (mSubdevice.getKey() == key)
228 if (!mSubdevice.attach(polylist[p]->poly,key))
238 for (
auto& mSubdevice : mSubdevices)
240 if (!mSubdevice.isAttached())
258 for(
int k=0; k<mNSubdevs; ++k)
260 mSubdevices[k].detach();
272 if (first_check)
return true;
274 for (
int i=0; i<elems; i++)
276 if (mChan2Board[i]==-1 || mChan2BAddr[i]==-1)
278 yCError(VIRTUALANALOGSERVER) <<
"Invalid map entries in networks section, failed runtime check"
279 <<
" i: " << i <<
"mChan2Board[i] is " << mChan2Board[i] <<
" chan2add is " << mChan2BAddr[i];
284 yCTrace(VIRTUALANALOGSERVER) <<
"perform_first_check() successfully completed";
292 bool sendLastValueBeforeTimeout =
false;
293 while (!Thread::isStopping())
295 pTorques=mPortInputTorques.
read(
false);
300 sendLastValueBeforeTimeout =
false;
307 if (perform_first_check(6)==
false)
break;
308 mSubdevices[mChan2Board[0]].setTorque(mChan2BAddr[0],pTorques->
get(1).
asFloat64());
309 mSubdevices[mChan2Board[1]].setTorque(mChan2BAddr[1],pTorques->
get(2).
asFloat64());
310 mSubdevices[mChan2Board[2]].setTorque(mChan2BAddr[2],pTorques->
get(3).
asFloat64());
311 mSubdevices[mChan2Board[3]].setTorque(mChan2BAddr[3],pTorques->
get(4).
asFloat64());
312 mSubdevices[mChan2Board[4]].setTorque(mChan2BAddr[4],pTorques->
get(5).
asFloat64());
313 mSubdevices[mChan2Board[5]].setTorque(mChan2BAddr[5],0.0);
317 if (perform_first_check(6)==
false)
break;
318 mSubdevices[mChan2Board[0]].setTorque(mChan2BAddr[0],pTorques->
get(1).
asFloat64());
319 mSubdevices[mChan2Board[1]].setTorque(mChan2BAddr[1],pTorques->
get(2).
asFloat64());
320 mSubdevices[mChan2Board[2]].setTorque(mChan2BAddr[2],pTorques->
get(3).
asFloat64());
321 mSubdevices[mChan2Board[3]].setTorque(mChan2BAddr[3],pTorques->
get(4).
asFloat64());
322 mSubdevices[mChan2Board[4]].setTorque(mChan2BAddr[4],pTorques->
get(5).
asFloat64());
323 mSubdevices[mChan2Board[5]].setTorque(mChan2BAddr[5],pTorques->
get(6).
asFloat64());
327 if (perform_first_check(6)==
false)
break;
328 mSubdevices[mChan2Board[0]].setTorque(mChan2BAddr[0],pTorques->
get(6).
asFloat64());
329 mSubdevices[mChan2Board[1]].setTorque(mChan2BAddr[1],pTorques->
get(7).
asFloat64());
330 mSubdevices[mChan2Board[2]].setTorque(mChan2BAddr[2],0.0);
331 mSubdevices[mChan2Board[3]].setTorque(mChan2BAddr[3],0.0);
332 mSubdevices[mChan2Board[4]].setTorque(mChan2BAddr[4],0.0);
333 mSubdevices[mChan2Board[5]].setTorque(mChan2BAddr[5],0.0);
337 if (perform_first_check(3)==
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());
347 yCError(VIRTUALANALOGSERVER) <<
"Got unexpected " << pTorques->
get(0).
asInt32() <<
" message on virtualAnalogServer.";
350 for (
int d=0; d<mNSubdevs; ++d)
352 mSubdevices[d].flushTorques();
365 if ((lastRecv+0.080 < timeNow) && (!sendLastValueBeforeTimeout))
370 for (
int d=0; d<mNSubdevs; ++d)
372 mSubdevices[d].resetTorque();
373 mSubdevices[d].flushTorques();
378 yCError(VIRTUALANALOGSERVER) <<
"Timeout!! No new value received for more than " << timeNow - lastRecv <<
" secs.";
379 sendLastValueBeforeTimeout =
true;