iCub-main
Loading...
Searching...
No Matches
embObjSkin.cpp
Go to the documentation of this file.
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3/*
4 * Copyright (C) 2026 Mesh Facility, Istituto Italiano di Tecnologia
5 * Authors: Alberto Cardellino, Jacopo Losi
6 * CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
7 *
8 */
9
10// system includes
11#include <iostream>
12#include <string.h>
13
14// Ace & Yarp includes
15#include <yarp/os/Time.h>
16#include <yarp/os/NetType.h>
17#include <yarp/conf/environment.h>
18
19// iCub includes
20#include "embObjSkin.h"
21#include "EoProtocol.h"
22#include "EoProtocolMN.h"
23#include "EoProtocolSK.h"
24
25#include "EoSkin.h"
26#include "iCubCanProtocol.h"
27
28
29#include <ethResource.h>
31
32#include "EOnv.h"
33#include "EoCommon.h"
34
35using namespace std;
36using namespace iCub::skin::diagnostics;
37
38namespace {
39 YARP_LOG_COMPONENT(EMBOBJSKIN, "yarp.dev.embObjSkin")
40 // this is the default value for no load condition for skin taxels, meaning 0% pressure,
41 // and it is by definition equal to 240 in decimal.
42 // It is used as initialization value since it is passed by configuration file too on the noLoad parameter,
43 // but it is not necessarily the final no load value during runtime, since it can be changed by configuration file.
44 // So this constant is mainly the pre-config/default initialization value, not necessarily the final runtime no-load value.
45 constexpr double defaultSkinNoLoadValue{0xf0};
46
47 bool isCANaddressValid(int adr)
48 {
49 return ((adr>0) && (adr<15));
50 }
51
52 // adding these constants under the same enum since they are all related to skin hardware characteristics.
53 enum {
54 EMBSK_SIZE_INFO = 128,
56 MAX_NUM_OF_PATCHES_PER_ETH_BOARD = 2, // this is the maximum number of patches that can be associated to a single eth board. This is a limit that we can set since it is not possible to have more than 2 patches per board (since each patch corresponds to a can port and each board has 2 can ports)
57 SKIN_TAXELS_PER_TRIANGLE = 12, // this is the number of taxels per triangle, it is a fixed value since it depends on the hardware design of the skin, and it is not expected to change in the future.
58 TRIANGLES_PER_CAN_BOARD = 16 // this is the number of triangles per can board, it is a fixed value since it depends on the hardware design of the skin, and it is not expected to change in the future. Each can board has 16 triangles, each triangle has 12 taxels, so each can board has 192 taxels.
59 };
60}
61
62
63std::optional<size_t> SkinPatchInfo::checkCardAddrIsInList(int cardAddr)
64{
65 auto it = std::find(cardAddrList.begin(), cardAddrList.end(), cardAddr);
66 if (it == cardAddrList.end())
67 return std::nullopt;
68 return static_cast<size_t>(std::distance(cardAddrList.begin(), it));
69}
70
71EmbObjSkin::EmbObjSkin() : _isDiagnosticPresent(false)
72{
73 res = NULL;
74 ethManager = NULL;
75 opened = false;
76 sensorsNum = 0;
78 _skCfg.numOfPatches = 0;
79 _skCfg.totalCardsNum = 0;
80
81 std::string tmp = yarp::conf::environment::get_string("ETH_VERBOSEWHENOK");
82 if (tmp != "")
83 {
84 verbosewhenok = (bool)(yarp::conf::numeric::from_string(tmp, 0U));
85 }
86 else
87 {
88 verbosewhenok = false;
89 }
90
91 parser = NULL;
92
93 memset(&ethservice.configuration, 0, sizeof(ethservice.configuration));
94 ethservice.configuration.type = eomn_serv_NONE;
95}
96
97
99{
100 if(NULL != parser)
101 {
102 delete parser;
103 parser = NULL;
104 }
105}
106
107std::string EmbObjSkin::boardInfo() const
108{
109 if(nullptr == res)
110 return {"[res not yet available]"};
111 return std::string("BOARD ") + res->getProperties().boardnameString + " IP " + res->getProperties().ipv4addrString;
112}
113
114
115bool EmbObjSkin::initWithSpecialConfig(yarp::os::Searchable& config)
116{
117 eOprotID32_t protoid;
118 std::vector<SpecialSkinBoardCfgParam> boardCfgList(_skCfg.totalCardsNum);
119 unsigned int numofcfg;
120 unsigned int j = 0, p = 0;
121
122 if(!_newCfg)
123 {
124 return true; //if we use old style config then return
125 }
126
127 //-----------------------------------------------------------------------------------------------------
128 //------------ read special cfg board --------------------------------------------------------------
129
130 numofcfg = _skCfg.totalCardsNum;//set size of my vector boardCfgList;
131 //in output the function return number of special board cfg are in file xml
132 // TODO: add documentation regarding usage of special configuration in xml file
133 bool ret = _cfgReader.readSpecialBoardCfg(config, boardCfgList.data(), &numofcfg);
134
135 if(!ret)
136 return false;
137
138
139 for(j=0; j<numofcfg; j++) //for each special board config
140 {
141 //check if patch exist
142 for(p=0; p< _skCfg.patchInfoList.size(); p++)
143 {
144 if(_skCfg.patchInfoList[p].idPatch == boardCfgList[j].patch)
145 break;
146 }
147 if(p>=_skCfg.patchInfoList.size())
148 {
149 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": patch" << boardCfgList[j].patch << "not exists";
150 return false;
151 }
152 //now p is the index of patch.
153
154 eOcanport_t canport = _skCfg.patchInfoList[p].canport;
155
156 //check if card address are in patch
157 std::optional<size_t> boardIdx;
158 for(int a=boardCfgList[j].boardAddrStart; a<=boardCfgList[j].boardAddrEnd; a++)
159 {
160 boardIdx = _skCfg.patchInfoList[p].checkCardAddrIsInList(a);
161 if(!boardIdx)
162 {
163 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "card with address" << a << "is not present in patch" << _skCfg.patchInfoList[p].idPatch;
164 return(false);
165 }
166 }
167
168 // prepare data to send to ems
169 eOsk_cmd_boardsCfg_t bcfg;
170 // this message is for some addresses only
171 bcfg.candestination[0] = bcfg.candestination[1] = 0;
172 for(int adr=boardCfgList[j].boardAddrStart; adr<=boardCfgList[j].boardAddrEnd; adr++)
173 {
174 if(isCANaddressValid(adr))
175 {
176 eo_common_hlfword_bitset(&bcfg.candestination[canport], adr);
177 }
178 }
179
180 bcfg.cfg.skintype = boardCfgList[j].cfg.skinType;
181 bcfg.cfg.period = boardCfgList[j].cfg.period;
182 bcfg.cfg.noload = boardCfgList[j].cfg.noLoad;
183
184 // Init the data vector with special config values from "noLoad" param in config file.
185 // This is to have a correct initilization for the data sent through yarp port
186 for (size_t sensorId = 0; sensorId < TRIANGLES_PER_CAN_BOARD; sensorId++)
187 {
188 size_t index = TRIANGLES_PER_CAN_BOARD * SKIN_TAXELS_PER_TRIANGLE * (*boardIdx) + sensorId * SKIN_TAXELS_PER_TRIANGLE;
189 yCTrace(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": index is" << index << "value is:" << boardCfgList[j].cfg.noLoad;
190
191 // Message head
192 for(int k = 0; k < SKIN_TAXELS_PER_TRIANGLE; k++)
193 {
194 yCTrace(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": size is:" << skindata.size() << "index is" << (index+k) << "value is:" << boardCfgList[j].cfg.noLoad;
195 if((index+k) >= skindata.size())
196 {
197 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": index too big";
198 }
199 skindata[index + k] = boardCfgList[j].cfg.noLoad;
200 }
201 }
202
203 protoid = eoprot_ID_get(eoprot_endpoint_skin, eoprot_entity_sk_skin, _skCfg.patchInfoList[p].indexNv, eoprot_tag_sk_skin_cmmnds_boardscfg);
204
205 if(false == res->setRemoteValue(protoid, &bcfg))
206 {
207 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "Error in send special board config for mtb with addr from" << boardCfgList[j].boardAddrStart << "to addr" << boardCfgList[j].boardAddrEnd;
208 return false;
209 }
210
211 } //end for for each special board cfg
212
213 SystemClock::delaySystem(0.010); // 10 ms (m.a.a-delay: before it was 0.01)
214
215 //-----------------------------------------------------------------------------------------------------
216 //------------ read special cfg triangle --------------------------------------------------------------
217 std::vector<SpecialSkinTriangleCfgParam> triangleCfg(SPECIAL_TRIANGLE_CFG_MAX_NUM);
218 numofcfg = SPECIAL_TRIANGLE_CFG_MAX_NUM; //set size of my vector triangleCfg;
219 //in output the function return number of special triangle cfg are in file xml
220 ret = _cfgReader.readSpecialTriangleCfg(config, triangleCfg.data(), &numofcfg);
221 if(!ret)
222 return false;
223
224
225 for(j=0; j<numofcfg; j++)
226 {
227 for(p=0; p< _skCfg.patchInfoList.size(); p++)
228 {
229 if(_skCfg.patchInfoList[p].idPatch == triangleCfg[j].patch)
230 break;
231 }
232 if(p >= _skCfg.patchInfoList.size())
233 {
234 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ":patch" << triangleCfg[j].patch << "not exists";
235 return false;
236 }
237 //now p is index patch
238
239 eOcanport_t canport = _skCfg.patchInfoList[p].canport;
240
241 //check if bcfg.boardAddr is in my patches list
242 if(!_skCfg.patchInfoList[p].checkCardAddrIsInList(triangleCfg[j].boardAddr))
243 {
244 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "card with address" << triangleCfg[j].boardAddr << "is not present in patch" << _skCfg.patchInfoList[p].idPatch;
245 return(false);
246 }
247
248 //prepare data to send to ems
249 protoid = eoprot_ID_get(eoprot_endpoint_skin, eoprot_entity_sk_skin, _skCfg.patchInfoList[p].indexNv, eoprot_tag_sk_skin_cmmnds_trianglescfg);
250
251
252 eOsk_cmd_trianglesCfg_t tcfg = {0};
253
254 // this message is for one address only
255 tcfg.candestination[0] = tcfg.candestination[1] = 0;
256 int adr = triangleCfg[j].boardAddr;
257 if(isCANaddressValid(adr))
258 {
259 eo_common_hlfword_bitset(&tcfg.candestination[canport], adr);
260 }
261
262 tcfg.idstart = triangleCfg[j].triangleStart;
263 tcfg.idend = triangleCfg[j].triangleEnd;
264 tcfg.cfg.CDCoffset = triangleCfg[j].cfg.cdcOffset;
265 tcfg.cfg.enable = triangleCfg[j].cfg.enabled;
266 tcfg.cfg.shift = triangleCfg[j].cfg.shift;
267
268
269 SystemClock::delaySystem(0.010);
270
271 if(false == res->setRemoteValue(protoid, &tcfg))
272 {
273 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "Error in send special triangle config for board CAN" << canport+1 << ":" << triangleCfg[j].boardAddr;
274 return false;
275 }
276 }
277
278 SystemClock::delaySystem(0.010); // 10 ms (m.a.a-delay: before it was 0.01)
279
280 return true;
281}
282
283
284bool EmbObjSkin::fromConfig(yarp::os::Searchable& config)
285{
286 Bottle bPatches, bPatchList, xtmp;
287 //reset total num of cards
288 // A card is the number of each entry skinCanAddrsPatchX inside the patches group --> i.e. the number of CAN addresses found in config file. We should change the name of this variable with something more meaningful.
289 _skCfg.totalCardsNum = 0;
291
292
293 servConfigSkin_t skinconfig;
294 parser->parseService(config, skinconfig);
295
296 bPatches = config.findGroup("patches", "skin patches connected to this device");
297 if(bPatches.isNull())
298 {
299 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "patches group is missing";
300 return(false);
301 }
302
303 _skCfg.numOfPatches=0;
304 for (int i=1; i<=MAX_NUM_OF_PATCHES_PER_ETH_BOARD; i++)
305 {
306 std::string skinCanAddrsPatchx = "skinCanAddrsPatch";
307 skinCanAddrsPatchx.append(std::to_string(i));
308 if (bPatches.check(skinCanAddrsPatchx))
309 {
310 _skCfg.numOfPatches++; //we increase the number of patches per each instance of skinCanAddrsPatchX found in config file.
311 bPatchList.addInt32(i);
312 }
313 }
314
315 _skCfg.patchInfoList.clear();
316 _skCfg.patchInfoList.resize(_skCfg.numOfPatches);
317 for(int j=1; j<_skCfg.numOfPatches+1; j++)
318 {
319 int id = bPatchList.get(j-1).asInt32();
320 if((id!=1) && (id!=2))
321 {
322 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "expecting at most 2 patches";
323 return false;
324 }
325 _skCfg.patchInfoList[j-1].idPatch = id;
326 _skCfg.patchInfoList[j-1].indexNv = convertIdPatch2IndexNv(id);
327 _skCfg.patchInfoList[j-1].canport = (1 == id) ? eOcanport1 : eOcanport2;
328 }
329
330
331 for(int i=0; i<_skCfg.numOfPatches; i++)
332 {
333 char tmp[80];
334 int id = _skCfg.patchInfoList[i].idPatch;
335 snprintf(tmp, sizeof(tmp), "skinCanAddrsPatch%d", id);
336
337 xtmp = bPatches.findGroup(tmp);
338 if(xtmp.isNull())
339 {
340 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "does not find " << tmp << "in xml file";
341 return false;
342 }
343
344 _skCfg.patchInfoList[i].cardAddrList.resize(xtmp.size()-1);
345 _skCfg.patchInfoList[i].taxelsSize = _skCfg.patchInfoList[i].cardAddrList.size() * TRIANGLES_PER_CAN_BOARD * SKIN_TAXELS_PER_TRIANGLE;
346 _skCfg.patchInfoList[i].taxelsOffset = _cumulativeTaxels;
347 _cumulativeTaxels += _skCfg.patchInfoList[i].taxelsSize;
348 for(int j=1; j<xtmp.size(); j++)
349 {
350 int addr = xtmp.get(j).asInt32();
351 _skCfg.totalCardsNum++;
352 _skCfg.patchInfoList[i].cardAddrList[j-1] = addr;
353 }
354 }
355 // impose the number of sensors (triangles found in config file)
357 // sensorsNum is the total number of taxels/sensors on this ETH board.
358
359 // resize the skindata holder
360 mtx.lock();
361
362 this->skindata.resize(sensorsNum);
363 int ttt = this->skindata.size();
364 for (int i=0; i < ttt; i++)
365 {
366 this->skindata[i]=defaultSkinNoLoadValue; // skindata contains data for all the cards. 240 is the max value for a taxel, i.e. 0% pressure on it.
367 }
368
369 mtx.unlock();
370
371 // fill the ethservice ...
372
373 ethservice.configuration.type = eomn_serv_SK_skin;
374
375
376 ethservice.configuration.data.sk.skin.boardinfo.type = skinconfig.canboard.type;
377
378 ethservice.configuration.data.sk.skin.boardinfo.protocol.major = skinconfig.canboard.protocol.major;
379 ethservice.configuration.data.sk.skin.boardinfo.protocol.minor = skinconfig.canboard.protocol.minor;
380
381 ethservice.configuration.data.sk.skin.boardinfo.firmware.major = skinconfig.canboard.firmware.major;
382 ethservice.configuration.data.sk.skin.boardinfo.firmware.minor = skinconfig.canboard.firmware.minor;
383 ethservice.configuration.data.sk.skin.boardinfo.firmware.build = skinconfig.canboard.firmware.build;
384
385
386 ethservice.configuration.data.sk.skin.numofpatches = _skCfg.numOfPatches;
387 if(ethservice.configuration.data.sk.skin.numofpatches > eomn_serv_skin_maxpatches)
388 {
389 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": Cannot have so many skin patches. detected" << ethservice.configuration.data.sk.skin.numofpatches << "max is" << eomn_serv_skin_maxpatches;
390 return false;
391 }
392 // tagliato e cucito per le schede eb2 eb4 eb10 ed eb11. da migliorare sia i file xml che il parser.
393 for(int np=0; np<ethservice.configuration.data.sk.skin.numofpatches; np++)
394 {
395 // patch np-th, can1 ... for each address put a bit using eo_common_hlfword_bitset
396 ethservice.configuration.data.sk.skin.canmapskin[np][0] = 0;
397 //eo_common_hlfword_bitset(&ethservice.configuration.data.sk.skin.canmapskin[np][0], 3);
398
399 // patch np-th, can2 ... for each address put a bit using
400 ethservice.configuration.data.sk.skin.canmapskin[np][1] = 0;
401
402 eOcanport_t canport = _skCfg.patchInfoList[np].canport;
403
404 int max = _skCfg.patchInfoList[np].cardAddrList.size();
405 for(int n=0; n<max; n++)
406 {
407 int adr = _skCfg.patchInfoList[np].cardAddrList.at(n);
408 adr = adr;
409 if(isCANaddressValid(adr))
410 {
411 eo_common_hlfword_bitset(&ethservice.configuration.data.sk.skin.canmapskin[np][canport], adr);
412 }
413 }
414 }
415
416
417 if( _cfgReader.isDefaultBoardCfgPresent(config) && _cfgReader.isDefaultTriangleCfgPresent(config))
418 {
419 _newCfg = true;
420 }
421 else
422 {
423 _newCfg = false;
424 return true;
425 }
426
427 /*read skin board default configuration*/
428 _brdCfg.setDefaultValues();
429 if(!_cfgReader.readDefaultBoardCfg(config, &_brdCfg))
430 return false;
431
432 // Fill the data vector with default values from "noLoad" param in config file.
433 for (int board_idx = 0; board_idx < _skCfg.totalCardsNum; board_idx++)
434 {
435 for (int triangleId = 0; triangleId < TRIANGLES_PER_CAN_BOARD; triangleId++)
436 {
437 int index = TRIANGLES_PER_CAN_BOARD * SKIN_TAXELS_PER_TRIANGLE * board_idx + triangleId * SKIN_TAXELS_PER_TRIANGLE;
438
439 // Message head
440 for (size_t k = 0; k < SKIN_TAXELS_PER_TRIANGLE; k++)
441 {
442 if((index+k) >= skindata.size())
443 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": index too big";
444 skindata[index + k] = _brdCfg.noLoad;
445 }
446 }
447 }
448
449 /*read skin triangle default configuration*/
450 _triangCfg.setDefaultValues();
451 if(! _cfgReader.readDefaultTriangleCfg(config, &_triangCfg))
452 return false;
453
454 return true;
455}
456
457
458bool EmbObjSkin::open(yarp::os::Searchable& config)
459{
460 // - first thing to do is verify if the eth manager is available. then i parse info about the eth board.
461
463 if(NULL == ethManager)
464 {
465 yCFatal(EMBOBJSKIN) << __YFUNCTION__ << ": fails to instantiate ethManager";
466 return false;
467 }
468
469 if(false == ethManager->verifyEthBoardInfo(config, ipv4addr, boardIPstring, boardName))
470 {
471 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": object TheEthManager fails in parsing ETH properties from xml file";
472 return false;
473 }
474 // add specific info about this device ...
475
476 // - now all other things
477
478 if(NULL == parser)
479 {
480 parser = new ServiceParser;
481 }
482
483 // read config file
484 if(false == fromConfig(config))
485 {
486 yCError(EMBOBJSKIN) << __YFUNCTION__ << ": fails in fromConfig():" << boardInfo() << ": cannot proceed any further";
487 cleanup();
488 return false;
489 }
490
491
492
493 // -- instantiate EthResource etc.
494
495 res = ethManager->requestResource2(this, config);
496 if(NULL == res)
497 {
498 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": fails because could not instantiate the ethResource for BOARD w/ IP =" << boardIPstring << "... unable to continue";
499 return false;
500 }
501
502 // now we have an ip address, thus we can set the name in object SkinConfigReader
503 char name[80];
504 snprintf(name, sizeof(name), "embObjSkin on BOARD %s IP %s", res->getProperties().boardnameString.c_str(), res->getProperties().ipv4addrString.c_str());
505 _cfgReader.setName(name);
506
507
508 if(!res->verifyEPprotocol(eoprot_endpoint_skin))
509 {
510 cleanup();
511 return false;
512 }
513
514
515 const eOmn_serv_parameter_t* servparam = &ethservice;
516
517 if(false == res->serviceVerifyActivate(eomn_serv_category_skin, servparam, 5.0))
518 {
519 yCError(EMBOBJSKIN) << __YFUNCTION__ << ": error in ethResources::serviceVerifyActivate() for" << boardInfo();
520 cleanup();
521 return false;
522 }
523
524 if(!init())
525 return false;
526
527 /* Following delay is necessary in order to give enough time to skin boards to configure all its triangles */
528 SystemClock::delaySystem(0.500);
529
530 if(!initWithSpecialConfig(config))
531 {
532 cleanup();
533 return false;
534 }
535
537 {
538 cleanup();
539 return false;
540 }
541
542 if(!start())
543 {
544 cleanup();
545 return false;
546 }
547
548 if(false == res->serviceStart(eomn_serv_category_skin))
549 {
550 yCError(EMBOBJSKIN) << __YFUNCTION__ << ": fails to start skin service for" << boardInfo() << ": cannot continue";
551 cleanup();
552 return false;
553 }
554 else
555 {
556 if(verbosewhenok)
557 {
558 yCDebug(EMBOBJSKIN) << __YFUNCTION__ << ": correctly starts skin service of" << boardInfo();
559 }
560 }
561
562 return true;
563}
564
565
567{
568 if(ethManager == NULL) return;
569
570 int ret = ethManager->releaseResource2(res, this);
571 res = NULL;
572 if(ret == -1)
573 ethManager->killYourself();
574}
575
577{
578 cleanup();
579 return true;
580}
581
583{
584 yCDebug(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": n. of enabled skin patches" << _skCfg.numOfPatches;
585 return _skCfg.numOfPatches; //should return the number of patches defined as the parameter `skinCanAddrsPatch1` in the hw configuration file (thus for our boards 1 or 2)
586}
587
588MAS_status EmbObjSkin::getSkinPatchStatus(size_t sens_index) const
589{
590 if(sens_index >= _skCfg.numOfPatches)
591 {
592 return MAS_ERROR;
593 }
594
595 return MAS_OK;
596}
597
598bool EmbObjSkin::getSkinPatchName(size_t sens_index, std::string &name) const
599{
600 if(sens_index >= _skCfg.numOfPatches)
601 {
602 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": index out of range. Requested:" << sens_index << "max:" << _skCfg.numOfPatches;
603 return false;
604 }
605
606 name = "skin_patch_" + std::to_string(_skCfg.patchInfoList[sens_index].idPatch);
607 yCDebug(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << name;
608 return true;
609}
610
611bool EmbObjSkin::getSkinPatchMeasure(size_t sens_index, yarp::sig::Vector& out, double& timestamp) const
612{
613 if(sens_index >= _skCfg.numOfPatches)
614 {
615 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": index out of range. Requested:" << sens_index << "max:" << _skCfg.numOfPatches;
616 return false;
617 }
618
619 const auto& patch = _skCfg.patchInfoList[sens_index];
620 out.resize(patch.taxelsSize);
621 std::lock_guard<std::mutex> lck(mtx);
622 for (size_t i = 0; i < patch.taxelsSize; i++)
623 {
624 out[i] = skindata[patch.taxelsOffset + i];
625 }
626 timestamp = yarp::os::Time::now();
627
628 return true;
629}
630
631size_t EmbObjSkin::getSkinPatchSize(size_t sens_index) const
632{
633 if(sens_index >= _skCfg.numOfPatches)
634 {
635 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": index out of range. Requested:" << sens_index << "max:" << _skCfg.numOfPatches;
636 return 0;
637 }
638 return _skCfg.patchInfoList[sens_index].taxelsSize; //should return the number of taxels, that is the number of triangles (16) per card, times the number of taxels per triangle (12), times the number of cards per patch found in config file.
639}
640
642{
643 eOprotID32_t protoid;
644 uint8_t dat;
645 bool ret = true;
646 int i;
647
648 if(_newCfg)
649 {
650 dat = eosk_sigmode_signal;
651 }
652 else
653 {
654 dat = eosk_sigmode_signal_oldway;
655 }
656
657 for(i=0; i<_skCfg.numOfPatches;i++)
658 {
659 protoid = eoprot_ID_get(eoprot_endpoint_skin, eoprot_entity_sk_skin, _skCfg.patchInfoList[i].indexNv, eoprot_tag_sk_skin_config_sigmode);
660 ret = res->setRemoteValue(protoid, &dat);
661 if(!ret)
662 {
663 yCError(EMBOBJSKIN) << __YFUNCTION__ << ": unable to start skin for" << boardInfo() << "on port" << _skCfg.patchInfoList[i].idPatch;
664 return false;
665 }
666 }
667 return ret;
668}
669
671{
672 // - configure regular rops
673
674 vector<eOprotID32_t> id32v(0);
675 eOprotID32_t protoid = eo_prot_ID32dummy;
676
677 // choose the variables and put them inside vector
678 for(int i=0; i<_skCfg.numOfPatches; i++)
679 {
680 protoid = eoprot_ID_get(eoprot_endpoint_skin, eoprot_entity_sk_skin, _skCfg.patchInfoList[i].indexNv, eoprot_tag_sk_skin_status_arrayofcandata);
681 id32v.push_back(protoid);
682 }
683
684
685 if(false == res->serviceSetRegulars(eomn_serv_category_skin, id32v))
686 {
687 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": fails to add its variables to regulars: cannot proceed any further";
688 return false;
689 }
690 else
691 {
692 if(verbosewhenok)
693 {
694 yCDebug(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": added" << id32v.size() << "regular rops";
695 char nvinfo[128];
696 for (size_t r = 0; r<id32v.size(); r++)
697 {
698 uint32_t id32 = id32v.at(r);
699 eoprot_ID2information(id32, nvinfo, sizeof(nvinfo));
700 yCDebug(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": added regular rop for" << nvinfo;
701 }
702 }
703 }
704 SystemClock::delaySystem(0.005); // 5 ms (m.a.a-delay: before it was 0)
705
706 return true;
707}
708
710{
711 int j = 0;
712 eOprotID32_t protoid;
713
714 // if old configuration style returns
715 if(!_newCfg)
716 {
717 return true;
718 }
719
720 // send default board and triangle configuration (new configuration style)
721 eOsk_cmd_boardsCfg_t defBoardCfg = {0};
722 eOsk_cmd_trianglesCfg_t defTriangleCfg = {0};
723 size_t i, k;
724
725 defBoardCfg.cfg.skintype = _brdCfg.skinType;
726 defBoardCfg.cfg.period = _brdCfg.period;
727 defBoardCfg.cfg.noload = _brdCfg.noLoad;
728
729 defTriangleCfg.idstart = 0;
730 defTriangleCfg.idend = 15;
731 defTriangleCfg.cfg.enable = _triangCfg.enabled;
732 defTriangleCfg.cfg.shift = _triangCfg.shift;
733 defTriangleCfg.cfg.CDCoffset = _triangCfg.cdcOffset;
734
735 for(i=0; i<_skCfg.numOfPatches;i++)
736 {
737 protoid = eoprot_ID_get(eoprot_endpoint_skin, eoprot_entity_sk_skin, _skCfg.patchInfoList[i].indexNv, eoprot_tag_sk_skin_cmmnds_boardscfg);
738
739 // get min and max address
740 uint8_t minAddr = 16;
741 uint8_t maxAddr = 0;
742
743 for(k=0; k<_skCfg.patchInfoList[i].cardAddrList.size(); k++)
744 {
745 int adr = _skCfg.patchInfoList[i].cardAddrList[k];
746
747 if(isCANaddressValid(adr))
748 {
749 if(_skCfg.patchInfoList[i].cardAddrList[k] < minAddr)
750 minAddr = _skCfg.patchInfoList[i].cardAddrList[k];
751
752 if(_skCfg.patchInfoList[i].cardAddrList[k] > maxAddr)
753 maxAddr = _skCfg.patchInfoList[i].cardAddrList[k];
754 }
755 }
756 // we send the config to the whole patch, hence 0xffff
757 defBoardCfg.candestination[0] = defBoardCfg.candestination[1] = 0xffff;
758
759 if(false == res->setRemoteValue(protoid, &defBoardCfg))
760 {
761 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "Error in send default board config for patch #" << i;
762 return false;
763 }
764
765 }
766 SystemClock::delaySystem(0.010);
767
768 for(i=0; i<_skCfg.numOfPatches;i++)
769 {
770 protoid = eoprot_ID_get(eoprot_endpoint_skin, eoprot_entity_sk_skin, _skCfg.patchInfoList[i].indexNv, eoprot_tag_sk_skin_cmmnds_trianglescfg);
771
772
773 // we send the config to the whole patch, hence 0xffff
774 defTriangleCfg.candestination[0] = defTriangleCfg.candestination[1] = 0xffff;
775
776 if(false == res->setRemoteValue(protoid, &defTriangleCfg))
777 {
778 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "Error in send default triangle config for patch #" << i;
779 return false;
780 }
781
782 }
783
784 opened = true;
785
786 return true;
787}
788
790{
791 return opened;
792}
793
798
799#undef DEBUG_PRINT_RX_STATS
800#if defined(DEBUG_PRINT_RX_STATS)
801static uint32_t receivedpatches[2][16] = {0};
802static uint32_t counterpa = 0;
803#endif
804
805bool EmbObjSkin::update(eOprotID32_t id32, double timestamp, void *rxdata)
806{
807 uint8_t msgtype = 0;
808 uint8_t i, triangle = 0;
809 static int error = 0;
810 int p;
811 EOarray* arrayof = (EOarray*)rxdata;
812 uint8_t sizeofarray = eo_array_Size(arrayof);
813
814 eOprotIndex_t indexpatch = eoprot_ID2index(id32);
815
816 for(p=0; p<_skCfg.numOfPatches; p++)
817 {
818 if(_skCfg.patchInfoList[p].indexNv == indexpatch)
819 break;
820 }
821 if(p >= _skCfg.numOfPatches)
822 {
823 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": received data of patch with nvindex=" << indexpatch;
824 return false;
825 }
826
827 errors.resize(sizeofarray);
828
829 for(i=0; i<sizeofarray; i++)
830 {
831 eOsk_candata_t *candata = (eOsk_candata_t*) eo_array_At(arrayof, i);
832
833 if(NULL == candata)
834 {
835 break;
836 }
837
838 uint16_t canframeid11 = EOSK_CANDATA_INFO2IDCAN(candata->info);
839 uint8_t canframesize = EOSK_CANDATA_INFO2SIZE(candata->info);
840 uint8_t *canframedata = candata->data;
841
842 uint8_t cardAddr = 0;
843 uint8_t valid = 0;
844 uint8_t skinClass;
845
846 if(_newCfg)
847 skinClass = ICUBCANPROTO_CLASS_PERIODIC_SKIN;
848 else
849 skinClass = ICUBCANPROTO_CLASS_PERIODIC_ANALOGSENSOR;
850
851
852
853 valid = (((canframeid11 & 0x0f00) >> 8) == skinClass) ? 1 : 0;
854
855 if(valid)
856 {
857 cardAddr = (canframeid11 & 0x00f0) >> 4;
858 size_t localBoardIndex = 0;
859 bool foundCardAddr = false;
860 for (size_t cId_index = 0; cId_index< _skCfg.patchInfoList[p].cardAddrList.size(); cId_index++)
861 {
862 if(_skCfg.patchInfoList[p].cardAddrList[cId_index] == cardAddr)
863 {
864 localBoardIndex = cId_index;
865 foundCardAddr = true;
866 break;
867 }
868 }
869
870 if(!foundCardAddr)
871 {
872 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << ": Unknown cardId from skin";
873 return false;
874 }
875
876 triangle = (canframeid11 & 0x000f);
877 msgtype = (int) canframedata[0];
878
879 const size_t patchBase = _skCfg.patchInfoList[p].taxelsOffset;
880 const size_t boardBase = localBoardIndex * TRIANGLES_PER_CAN_BOARD * SKIN_TAXELS_PER_TRIANGLE;
881 const size_t index = patchBase + boardBase + triangle * SKIN_TAXELS_PER_TRIANGLE;
882
883 // marco.accame: added lock to avoid concurrent access to this->skindata. i lock at triangle resolution ...
884 mtx.lock();
885
886 if (msgtype == 0x40)
887 {
888#if defined(DEBUG_PRINT_RX_STATS)
889 receivedpatches[p][cardAddr]++;
890 counterpa ++;
891#endif
892 // Message head
893 for(int k = 0; k < 7; k++)
894 {
895 skindata[index + k] = canframedata[k + 1];
896 }
897 }
898 else if (msgtype == 0xC0)
899 {
900 // Message tail
901 for(int k = 0; k < 5; k++)
902 {
903 skindata[index + k + 7] = canframedata[k + 1];
904 }
905
906 // Skin diagnostics
907 if (_brdCfg.useDiagnostic) // if user requests to check the diagnostic
908 {
909 if (canframesize == 8)
910 {
911 // Skin diagnostics is active
912 _isDiagnosticPresent = true;
913
914 // Get error code head and tail
915 short head = canframedata[6];
916 short tail = canframedata[7];
917 int fullMsg = (head << 8) | (tail & 0xFF);
918
919 // Store error message
920 errors[i].net = indexpatch;
921 errors[i].board = cardAddr;
922 errors[i].sensor = triangle;
923 errors[i].error = fullMsg;
924
925 if (fullMsg != SkinErrorCode::StatusOK)
926 {
927 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() <<
928 "canDeviceNum: " << errors[i].net <<
929 "board: " << errors[i].board <<
930 "sensor: " << errors[i].sensor <<
931 "error: " << iCub::skin::diagnostics::printErrorCode(errors[i].error).c_str();
932 }
933 }
934 else
935 {
936 _isDiagnosticPresent = false;
937 }
938 }
939 }
940 mtx.unlock();
941 }
942 else if(canframeid11 == 0x100)
943 {
944 /* Can frame with id =0x100 contains Debug info. SO I skip it.*/
945 return true;
946 }
947 else
948 {
949 if(error == 0)
950 yCError(EMBOBJSKIN) << __YFUNCTION__ << ":" << boardInfo() << "Unknown Message received from skin (" << i << "/" << sizeofarray << "): frameID=" << canframeid11 << " len=" << canframesize << "canframe.data=" << canframedata[0] << " " << canframedata[1] << " " << canframedata[2] << " " << canframedata[3];
951 error++;
952 if (error == 10000)
953 error = 0;
954 }
955 }
956
957#if defined(DEBUG_PRINT_RX_STATS)
958 if(counterpa >= 10000)
959 {
960 counterpa = 0;
961 yDebug("pa = {{%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d} {%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d}}",
962 receivedpatches[0][0], receivedpatches[0][1], receivedpatches[0][2], receivedpatches[0][3], receivedpatches[0][4], receivedpatches[0][5], receivedpatches[0][6], receivedpatches[0][7],
963 receivedpatches[0][8], receivedpatches[0][9], receivedpatches[0][10], receivedpatches[0][11], receivedpatches[0][12], receivedpatches[0][13], receivedpatches[0][14], receivedpatches[0][15],
964 receivedpatches[1][0], receivedpatches[1][1], receivedpatches[1][2], receivedpatches[1][3], receivedpatches[1][4], receivedpatches[1][5], receivedpatches[1][6], receivedpatches[1][7],
965 receivedpatches[1][8], receivedpatches[1][9], receivedpatches[1][10], receivedpatches[1][11], receivedpatches[1][12], receivedpatches[1][13], receivedpatches[1][14], receivedpatches[1][15]
966 );
967
968 }
969#endif
970
971 return true;
972}
973/* *********************************************************************************************************************** */
974// eof
#define SPECIAL_TRIANGLE_CFG_MAX_NUM
bool fromConfig(yarp::os::Searchable &config)
eth::AbstractEthResource * res
Definition embObjSkin.h:85
SkinConfigReader _cfgReader
Definition embObjSkin.h:94
SkinConfig _skCfg
Definition embObjSkin.h:95
virtual bool getSkinPatchMeasure(size_t sens_index, yarp::sig::Vector &out, double &timestamp) const override
Get the last readings of the sensors related to the specified patch.
Vector skindata
Definition embObjSkin.h:90
string boardName
Definition embObjSkin.h:81
SkinBoardCfgParam _brdCfg
Definition embObjSkin.h:91
virtual bool initialised()
void cleanup(void)
bool _newCfg
Definition embObjSkin.h:93
string boardIPstring
Definition embObjSkin.h:80
eOprotIndex_t convertIdPatch2IndexNv(int idPatch)
Definition embObjSkin.h:103
bool initWithSpecialConfig(yarp::os::Searchable &config)
SkinTriangleCfgParam _triangCfg
Definition embObjSkin.h:92
virtual bool open(yarp::os::Searchable &config)
size_t sensorsNum
Definition embObjSkin.h:88
eOipv4addr_t ipv4addr
Definition embObjSkin.h:82
virtual bool close()
virtual eth::iethresType_t type()
virtual size_t getNrOfSkinPatches() const override
Get the number of skin patches exposed by THIS device.
virtual bool update(eOprotID32_t id32, double timestamp, void *rxdata)
virtual MAS_status getSkinPatchStatus(size_t sens_index) const override
Get the status of the specified patch.
bool configPeriodicMessage(void)
std::mutex mtx
Definition embObjSkin.h:87
virtual size_t getSkinPatchSize(size_t sens_index) const override
Get the size of the specified skin patch, i.e., the number of taxels (sensors) it contains.
virtual bool getSkinPatchName(size_t sens_index, std::string &name) const override
Get the name of the specified patch.
eth::TheEthManager * ethManager
Definition embObjSkin.h:84
size_t _cumulativeTaxels
Definition embObjSkin.h:89
std::optional< size_t > checkCardAddrIsInList(int cardAddr)
std::vector< int > cardAddrList
Definition embObjSkin.h:56
static TheEthManager * instance()
int n
bool error
iethresType_t
@ iethres_skin
std::string printErrorCode(int code)
out
Definition sine.m:8
eObrd_cantype_t type
eObrd_protocolversion_t protocol
eObrd_firmwareversion_t firmware
servCanBoard_t canboard