12#include <yarp/os/Time.h>
13#include <yarp/os/Log.h>
17#include <iCubCanProtocol.h>
21using namespace yarp::os;
28 yarp::os::Time::delay(
time/1000);
43 if (hexStg[
n] > 0x29 && hexStg[
n] < 0x40 )
44 digit[
n] = hexStg[
n] & 0x0f;
45 else if (hexStg[
n] >=
'a' && hexStg[
n] <=
'f')
46 digit[
n] = (hexStg[
n] & 0x0f) + 9;
47 else if (hexStg[
n] >=
'A' && hexStg[
n] <=
'F')
48 digit[
n] = (hexStg[
n] & 0x0f) + 9;
59 intValue = intValue | (digit[
n] << (m << 2));
68int cDownloader::build_id(
int source,
int dest)
70 return (ICUBCANPROTO_CLASS_BOOTLOADER << 8) + ( source << 4) + dest;
73int cDownloader::get_src_from_id (
int id)
76 return ((
id >> 4) & 0x0F);
79int cDownloader::get_dst_from_id (
int id)
105 _externalLoggerFptr = logger;
106 _externalLoggerCaller = caller;
109void cDownloader::Log(
const std::string &msg)
111 yDebug() <<
"$" << msg;
113 if(NULL != _externalLoggerFptr)
115 _externalLoggerFptr(_externalLoggerCaller,
"$ " + msg);
124 if (m_idriver !=NULL)
126#if defined(DOWNLOADER_USE_IDRIVER2)
130 m_idriver->destroyBuffer(txBuffer);
131 m_idriver->destroyBuffer(rxBuffer);
149 if (m_idriver !=NULL)
158 if (config.find(
"device").asString()==
"ETH")
160#if defined(DOWNLOADER_USE_IDRIVER2)
166 if((1 != tmp) && (2 != tmp))
173#if defined(DOWNLOADER_USE_IDRIVER2)
178 tmp = config.check(
"canDeviceNum")?config.find(
"canDeviceNum").asInt32():99;
181 if (0 != (ret = m_idriver->
init(config, _verbose)))
195#if defined(DOWNLOADER_USE_IDRIVER2)
197 txBuffer[0].setCanBus(tmp);
201 rxBuffer[i].setCanBus(tmp);
204 txBuffer=m_idriver->createBuffer(1);
217 if (m_idriver == NULL)
219 if(_verbose) yError (
"Driver not ready\n");
224 txBuffer[0].setId((2 << 8) + target_id);
225 txBuffer[0].setLen(1);
226 txBuffer[0].getData()[0]= 0x09;
227 set_bus(txBuffer[0], bus);
232 if(_verbose) yError (
"Unable to send message\n");
239 for (
int i=0; i<read_messages; i++)
241 if (rxBuffer[i].getData()[0]==0x09 &&
242 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
244 if(rxBuffer[i].getData()[1]!=0)
246 if(_verbose) yInfo (
"Data has been saved in EEprom correctly\n");
251 if(_verbose) yError (
"Error in data saving in EEprom \n");
256 if(_verbose) yError (
"Save_to_eeprom didn't receive answer...maybe strain firmware is obsolete \n");
351 if (m_idriver == NULL)
353 if(_verbose) yError (
"Driver not ready\n");
358 txBuffer[0].setId((2 << 8) + target_id);
359 txBuffer[0].setLen(3);
360 txBuffer[0].getData()[0]= 0x0C;
361 txBuffer[0].getData()[1]= channel;
362 txBuffer[0].getData()[2]= type;
363 set_bus(txBuffer[0], bus);
368 if(_verbose) yError (
"Unable to send message\n");
376 for (
int i=0; i<read_messages; i++)
378 if (rxBuffer[i].getData()[0]==0x0C &&
379 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
381 int ret_channel = rxBuffer[i].getData()[1];
382 if (ret_channel == channel)
384 adc = rxBuffer[i].getData()[3]<<8 | rxBuffer[i].getData()[4];
389 if(_verbose) yError (
"strain_get_adc : invalid response\n");
402 if (m_idriver == NULL)
404 if(_verbose) yError (
"Driver not ready\n");
409 txBuffer[0].setId((2 << 8) + target_id);
410 txBuffer[0].setLen(2);
411 txBuffer[0].getData()[0]= 0x0B;
412 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
415 set_bus(txBuffer[0], bus);
421 for (
int i=0; i<read_messages; i++)
423 if (rxBuffer[i].getData()[0]==0x0B &&
424 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
426 int ret_channel = rxBuffer[i].getData()[1] & 0x0f;
427 if (channel==ret_channel)
429 offset = rxBuffer[i].getData()[2]<<8 | rxBuffer[i].getData()[3];
434 if(_verbose) yError (
"strain_get_offset : invalid response\n");
447 if (m_idriver == NULL)
449 if(_verbose) yError (
"Driver not ready\n");
454 txBuffer[0].setId((2 << 8) + target_id);
455 txBuffer[0].setLen(2);
456 txBuffer[0].getData()[0]= 0x14;
457 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
458 set_bus(txBuffer[0], bus);
462 for (
int i=0; i<read_messages; i++)
464 if (rxBuffer[i].getData()[0]==0x14 &&
465 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
467 bias = rxBuffer[i].getData()[2]<<8 | rxBuffer[i].getData()[3];
479 if (m_idriver == NULL)
481 if(_verbose) yError (
"Driver not ready\n");
486 txBuffer[0].setId((2 << 8) + target_id);
487 txBuffer[0].setLen(2);
488 txBuffer[0].getData()[0]= 0x13;
489 txBuffer[0].getData()[1]= 1;
490 set_bus(txBuffer[0], bus);
500 if (m_idriver == NULL)
502 if(_verbose) yError (
"Driver not ready\n");
507 txBuffer[0].setId((2 << 8) + target_id);
508 txBuffer[0].setLen(5);
509 txBuffer[0].getData()[0]= 0x13;
510 txBuffer[0].getData()[1]= 2;
511 txBuffer[0].getData()[2]= ((regset << 4) & 0xf0) | (channel & 0x0f);
512 txBuffer[0].getData()[3]= bias >> 8;
513 txBuffer[0].getData()[4]= bias & 0xFF;
515 set_bus(txBuffer[0], bus);
527 if (m_idriver == NULL)
529 if(_verbose) yError (
"Driver not ready\n");
534 txBuffer[0].setId((2 << 8) + target_id);
535 txBuffer[0].setLen(2);
536 txBuffer[0].getData()[0]= 0x13;
537 txBuffer[0].getData()[1]= 0;
538 set_bus(txBuffer[0], bus);
548 if (m_idriver == NULL)
550 if(_verbose) yError (
"Driver not ready\n");
555 txBuffer[0].setId((2 << 8) + target_id);
556 txBuffer[0].setLen(2);
557 txBuffer[0].getData()[0]= 0x16;
558 txBuffer[0].getData()[1]= channel;
559 set_bus(txBuffer[0], bus);
563 for (
int i=0; i<read_messages; i++)
565 if (rxBuffer[i].getData()[0]==0x16 &&
566 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
568 bias = rxBuffer[i].getData()[2]<<8 | rxBuffer[i].getData()[3];
578 if (m_idriver == NULL)
580 if(_verbose) yError (
"Driver not ready\n");
585 txBuffer[0].setId((2 << 8) + target_id);
586 txBuffer[0].setLen(2);
587 txBuffer[0].getData()[0]= 0x15;
588 txBuffer[0].getData()[1]= 1;
589 set_bus(txBuffer[0], bus);
600 if (m_idriver == NULL)
602 if(_verbose) yError (
"Driver not ready\n");
607 txBuffer[0].setId((2 << 8) + target_id);
608 txBuffer[0].setLen(5);
609 txBuffer[0].getData()[0]= 0x15;
610 txBuffer[0].getData()[1]= 2;
611 txBuffer[0].getData()[3]= channel;
612 txBuffer[0].getData()[4]= bias >> 8;
613 txBuffer[0].getData()[5]= bias & 0xFF;
615 set_bus(txBuffer[0], bus);
625 if (m_idriver == NULL)
627 if(_verbose) yError (
"Driver not ready\n");
632 txBuffer[0].setId((2 << 8) + target_id);
633 txBuffer[0].setLen(2);
634 txBuffer[0].getData()[0]= 0x15;
635 txBuffer[0].getData()[1]= 0;
636 set_bus(txBuffer[0], bus);
647 if (m_idriver == NULL)
649 if(_verbose) yError (
"Driver not ready\n");
654 txBuffer[0].setId((2 << 8) + target_id);
655 txBuffer[0].setLen(8);
656 txBuffer[0].getData()[0]= 0x19;
657 txBuffer[0].getData()[1]= serial_number[0];
658 txBuffer[0].getData()[2]= serial_number[1];
659 txBuffer[0].getData()[3]= serial_number[2];
660 txBuffer[0].getData()[4]= serial_number[3];
661 txBuffer[0].getData()[5]= serial_number[4];
662 txBuffer[0].getData()[6]= serial_number[5];
663 txBuffer[0].getData()[7]= serial_number[6];
664 set_bus(txBuffer[0], bus);
675 if (m_idriver == NULL)
677 if(_verbose) yError (
"Driver not ready\n");
682 txBuffer[0].setId((2 << 8) + target_id);
683 txBuffer[0].setLen(1);
684 txBuffer[0].getData()[0]= 0x1A;
687 set_bus(txBuffer[0], bus);
693 for (
int i=0; i<read_messages; i++)
695 if (rxBuffer[i].getData()[0]==0x1A &&
696 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
698 serial_number[0] = rxBuffer[i].getData()[1];
699 serial_number[1] = rxBuffer[i].getData()[2];
700 serial_number[2] = rxBuffer[i].getData()[3];
701 serial_number[3] = rxBuffer[i].getData()[4];
702 serial_number[4] = rxBuffer[i].getData()[5];
703 serial_number[5] = rxBuffer[i].getData()[6];
704 serial_number[6] = rxBuffer[i].getData()[7];
705 serial_number[7] = 0;
716 if (m_idriver == NULL)
718 if(_verbose) yError (
"Driver not ready\n");
723 txBuffer[0].setId((2 << 8) + target_id);
724 txBuffer[0].setLen(1);
725 txBuffer[0].getData()[0]= 0x1B;
728 set_bus(txBuffer[0], bus);
734 for (
int i=0; i<read_messages; i++)
736 if (rxBuffer[i].getData()[0]==0x1B &&
737 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
739 *status = rxBuffer[i].getData()[1]!=0;
749 if (m_idriver == NULL)
751 if(_verbose) yError (
"Driver not ready\n");
756 txBuffer[0].setId((2 << 8) + target_id);
757 txBuffer[0].setLen(1);
758 txBuffer[0].getData()[0]= 0x12;
761 set_bus(txBuffer[0], bus);
767 for (
int i=0; i<read_messages; i++)
769 if (rxBuffer[i].getData()[0]==0x12 &&
770 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
772 gain = rxBuffer[i].getData()[1];
811 if (m_idriver == NULL)
813 if(_verbose) yError (
"Driver not ready\n");
823 txBuffer[0].setId((2 << 8) + target_id);
824 txBuffer[0].setLen(2);
825 txBuffer[0].getData()[0]= 0x3D;
826 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (regsetmode & 0x0f);
828 set_bus(txBuffer[0], bus);
846 if (m_idriver == NULL)
848 if(_verbose) yError (
"Driver not ready\n");
853 txBuffer[0].setId((2 << 8) + target_id);
854 txBuffer[0].setLen(2);
855 txBuffer[0].getData()[0]= 0x3E;
856 txBuffer[0].getData()[1]= (regsetmode & 0x0f);
859 set_bus(txBuffer[0], bus);
865 for (
int i=0; i<read_messages; i++)
867 if (rxBuffer[i].getData()[0]== 0x3E &&
868 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
870 int rregset = (rxBuffer[i].getData()[1] >> 4) & 0x0F;
871 int rregsetmode = (rxBuffer[i].getData()[1]) & 0x0F;
872 if (rregsetmode == regsetmode)
879 if(_verbose) yError (
"strain_get_regulationset : invalid response\n");
894 if (m_idriver == NULL)
896 if(_verbose) yError (
"Driver not ready\n");
901 txBuffer[0].setId((2 << 8) + target_id);
902 txBuffer[0].setLen(2);
903 txBuffer[0].getData()[0]= 0x11;
904 txBuffer[0].getData()[1]= gain;
906 set_bus(txBuffer[0], bus);
916 if (m_idriver == NULL)
918 if(_verbose) yError (
"Driver not ready\n");
924 txBuffer[0].setId((2 << 8) + target_id);
925 txBuffer[0].setLen(8);
926 txBuffer[0].getData()[0]= 0x2B;
927 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
928 txBuffer[0].getData()[2]= ampregs.
data[0];
929 txBuffer[0].getData()[3]= ampregs.
data[1];
930 txBuffer[0].getData()[4]= ampregs.
data[2];
931 txBuffer[0].getData()[5]= ampregs.
data[3];
932 txBuffer[0].getData()[6]= ampregs.
data[4];
933 txBuffer[0].getData()[7]= ampregs.
data[5];
934 set_bus(txBuffer[0], bus);
940 yarp::os::Time::delay(0.010);
950 if (m_idriver == NULL)
952 if(_verbose) yError (
"Driver not ready\n");
958 txBuffer[0].setId((2 << 8) + target_id);
959 txBuffer[0].setLen(2);
960 txBuffer[0].getData()[0]= 0x2A;
961 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);;
962 set_bus(txBuffer[0], bus);
969 yarp::os::Time::delay(0.010);
972 for(
int i=0; i<rm; i++)
974 if (rxBuffer[i].getData()[0]==0x2A)
976 ampregs.
data[0] = rxBuffer[i].getData()[2];
977 ampregs.
data[1] = rxBuffer[i].getData()[3];
978 ampregs.
data[2] = rxBuffer[i].getData()[4];
979 ampregs.
data[3] = rxBuffer[i].getData()[5];
980 ampregs.
data[4] = rxBuffer[i].getData()[6];
981 ampregs.
data[5] = rxBuffer[i].getData()[7];
995 static const float mapofgains[ampl_gain_numberOf] =
997 256 , 128 , 96 , 64 , 48, 36, 24, 20, 16, 10, 8, 6, 4
1000 unsigned int index =
static_cast<unsigned int>(c);
1001 if(index >= ampl_gain_numberOf)
1003 if(_verbose) yError (
"strain_amplifier_discretegain2float(): cannot convert to a valid value\n");
1006 return mapofgains[index];
1012 if (m_idriver == NULL)
1014 if(_verbose) yError (
"Driver not ready\n");
1020 #define _NUMofREGS 6
1038 static const uint8_t _cfg1map[ampl_gain_numberOf][
_NUMofREGS] =
1041 {0x00, 0x80, 0x66, 0x06, 0xcb, 0x80},
1042 {0x00, 0x80, 0x56, 0x0c, 0xcb, 0x80},
1043 {0x00, 0x40, 0x56, 0x10, 0x0f, 0x81},
1044 {0x00, 0x80, 0x46, 0x17, 0x6d, 0x7f},
1045 {0x00, 0x40, 0x46, 0x1f, 0xb1, 0x7f},
1046 {0x00, 0x10, 0x46, 0x2a, 0x80, 0x80},
1047 {0x00, 0x40, 0x42, 0x3e, 0x62, 0x7f},
1048 {0x00, 0x20, 0x42, 0x4b, 0x15, 0x80},
1049 {0x00, 0x00, 0x42, 0x5e, 0x72, 0x80},
1050 {0x00, 0xC0, 0x02, 0x64, 0xf6, 0x6e},
1051 {0x00, 0x80, 0x02, 0x64, 0x29, 0x62},
1052 {0x00, 0x40, 0x02, 0x64, 0xd4, 0x4c},
1053 {0x00, 0x40, 0x00, 0x64, 0x29, 0x22}
1057 unsigned int index =
static_cast<unsigned int>(ampcfg);
1059 if(index >= ampl_gain_numberOf)
1061 if(_verbose) yError (
"strain_set_amplifier_discretegain(): cannot convert to a valid index\n");
1066 txBuffer[0].setId((2 << 8) + target_id);
1067 txBuffer[0].setLen(8);
1068 txBuffer[0].getData()[0]= 0x2B;
1069 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
1070 txBuffer[0].getData()[2]= _cfg1map[index][0];
1071 txBuffer[0].getData()[3]= _cfg1map[index][1];
1072 txBuffer[0].getData()[4]= _cfg1map[index][2];
1073 txBuffer[0].getData()[5]= _cfg1map[index][3];
1074 txBuffer[0].getData()[6]= _cfg1map[index][4];
1075 txBuffer[0].getData()[7]= _cfg1map[index][5];
1076 set_bus(txBuffer[0], bus);
1077 if(_verbose) yDebug(
"strain_set_amplifier_discretegain() is sending: [%x, %x, %x, %x, %x, %x, %x, %x]", txBuffer[0].getData()[0], txBuffer[0].getData()[1], txBuffer[0].getData()[2], txBuffer[0].getData()[3], txBuffer[0].getData()[4], txBuffer[0].getData()[5], txBuffer[0].getData()[6], txBuffer[0].getData()[7]);
1082 yarp::os::Time::delay(0.010);
1091 if (m_idriver == NULL)
1093 if(_verbose) yError (
"Driver not ready\n");
1097 txBuffer[0].setId((2 << 8) + target_id);
1098 txBuffer[0].setLen(2);
1099 txBuffer[0].getData()[0]= 0x20;
1100 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
1101 set_bus(txBuffer[0], bus);
1108 for(
int i=0; i<rm; i++)
1110 if (rxBuffer[i].getData()[0]==0x20)
1114 uint16_t g16 =
static_cast<uint16_t
>(rxBuffer[i].getData()[2]) |
static_cast<uint16_t
>(rxBuffer[i].getData()[3]) << 8;
1115 float fg =
static_cast<float>(g16) / 100;
1117 uint16_t o16 =
static_cast<uint16_t
>(rxBuffer[i].getData()[4]) |
static_cast<uint16_t
>(rxBuffer[i].getData()[5]) << 8;
1130 if (m_idriver == NULL)
1132 if(_verbose) yError (
"Driver not ready\n");
1137 txBuffer[0].setId((2 << 8) + target_id);
1138 txBuffer[0].setLen(7);
1139 txBuffer[0].getData()[0]= 0x21;
1140 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
1141 txBuffer[0].getData()[2]= 0;
1142 uint16_t gg =
static_cast<uint16_t
>(gain*100.0f);
1143 txBuffer[0].getData()[3] = (gg & 0x00ff);
1144 txBuffer[0].getData()[4] = (gg & 0xff00) >> 8;
1145 txBuffer[0].getData()[5] = (
offset & 0x00ff);
1146 txBuffer[0].getData()[6] = (
offset & 0xff00) >> 8;
1148 set_bus(txBuffer[0], bus);
1164 if(
false == convert(wp, dp))
1166 if(_verbose) yError (
"cannot load this pair gain-offset into the strain2\n");
1187 if (m_idriver == NULL)
1189 if(_verbose) yError (
"Driver not ready\n");
1194 txBuffer[0].setId((2 << 8) + target_id);
1195 txBuffer[0].setLen(2);
1196 txBuffer[0].getData()[0]= 0x18;
1197 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
1199 set_bus(txBuffer[0], bus);
1205 for (
int i=0; i<read_messages; i++)
1207 std::uint8_t data1 = rxBuffer[0].getData()[1];
1208 std::uint8_t rs = (data1 >> 4) & 0x0f;
1209 std::uint8_t cc = (data1 ) & 0x0f;
1211 if (rxBuffer[i].getData()[0]==0x18 &&
1212 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
1214 full_scale = rxBuffer[i].getData()[2]<<8 | rxBuffer[i].getData()[3];
1224 if (m_idriver == NULL)
1226 if(_verbose) yError (
"Driver not ready\n");
1231 txBuffer[0].setId((2 << 8) + target_id);
1232 txBuffer[0].setLen(4);
1233 txBuffer[0].getData()[0]= 0x17;
1234 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
1235 txBuffer[0].getData()[2]= full_scale >> 8;
1236 txBuffer[0].getData()[3]= full_scale & 0xFF;
1238 set_bus(txBuffer[0], bus);
1248 if (m_idriver == NULL)
1250 if(_verbose) yError (
"Driver not ready\n");
1255 txBuffer[0].setId((2 << 8) + target_id);
1256 txBuffer[0].setLen(3);
1257 txBuffer[0].getData()[0]= 0x0A;
1258 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (r & 0x0f);
1259 txBuffer[0].getData()[2]= c;
1262 set_bus(txBuffer[0], bus);
1268 for (
int i=0; i<read_messages; i++)
1270 if (rxBuffer[i].getData()[0]==0x0A &&
1271 rxBuffer[i].getId()==(2 << 8) + (target_id<<4))
1273 int ret_r = (rxBuffer[i].getData()[1]) & 0x0f;
1274 int ret_c = rxBuffer[i].getData()[2];
1275 if ((r==ret_r) && (c==ret_c))
1277 elem = rxBuffer[i].getData()[3]<<8 | rxBuffer[i].getData()[4];
1282 if(_verbose) yError (
"strain_get_matrix_rc : invalid response\n");
1294 if (m_idriver == NULL)
1296 if(_verbose) yError (
"Driver not ready\n");
1301 txBuffer[0].setId((2 << 8) + target_id);
1302 txBuffer[0].setLen(5);
1303 txBuffer[0].getData()[0]= 0x03;
1304 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (r & 0x0f);
1305 txBuffer[0].getData()[2]= c;
1306 txBuffer[0].getData()[3]= elem >> 8;
1307 txBuffer[0].getData()[4]= elem & 0xFF;
1308 set_bus(txBuffer[0], bus);
1319 if (m_idriver == NULL)
1321 if(_verbose) yError (
" Driver not ready\n");
1326 txBuffer[0].setId((2 << 8) + target_id);
1327 txBuffer[0].setLen(4);
1328 txBuffer[0].getData()[0]= 0x04;
1329 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);;
1330 txBuffer[0].getData()[2]=
offset >> 8;
1331 txBuffer[0].getData()[3]=
offset & 0xFF;
1332 set_bus(txBuffer[0], bus);
1354 if (m_idriver == NULL)
1356 if(_verbose) yError (
"Driver not ready\n");
1371 txBuffer[0].setId((2 << 8) + target_id);
1372 txBuffer[0].setLen(2);
1373 txBuffer[0].getData()[0]= 0x08;
1374 txBuffer[0].getData()[1]= txratemilli;
1375 set_bus(txBuffer[0], bus);
1380 if(_verbose) yError (
"Unable to send txrate set message\n");
1385 txBuffer[0].setId((2 << 8) + target_id);
1386 txBuffer[0].setLen(2);
1387 txBuffer[0].getData()[0]= 0x07;
1388 txBuffer[0].getData()[1]= (
false == calibmode) ? (0x03) : (0x00);
1389 set_bus(txBuffer[0], bus);
1394 if(_verbose) yError (
"Unable to send start tx message\n");
1398 strain_is_acquiring_in_calibratedmode = calibmode;
1407 if (m_idriver == NULL)
1409 if(_verbose) yError (
"Driver not ready\n");
1419 txBuffer[0].setId((2 << 8) + target_id);
1420 txBuffer[0].setLen(2);
1421 txBuffer[0].getData()[0]= 0x07;
1422 txBuffer[0].getData()[1]= 0x02;
1423 set_bus(txBuffer[0], bus);
1428 if(_verbose) yError (
"Unable to send stop tx message\n");
1435 const double TOUT = 0.100;
1436 const size_t maxframes = rxBuffer.size() - 1;
1437 for(
size_t n=0;
n<10;
n++)
1439 int read_messages = m_idriver->
receive_message(rxBuffer, maxframes, TOUT);
1441 if(0 == read_messages)
1462 unsigned int errorcount = 0;
1469 yDebug() <<
"cDownloader::strain_acquire_get() is using polling";
1472 for (
unsigned int s = 0; s < howmany; s++)
1477 for (
int c = 0; c < 6; c++)
1480 unsigned int adc = 0;
1485 yDebug() <<
"error in acquisition of an adc channel " << c <<
"incrementing error counter to" << errorcount;
1486 if (errorcount >= maxerrors)
1488 yError() <<
"reached" << maxerrors <<
"reception errors in adc acquisition: must quit";
1494 values.push_back(sv);
1496 if (NULL != updateProgressBar)
1498 float perc = (0 != howmany) ? (
static_cast<float>(s + 1) /
static_cast<float>(howmany)) : (100.0);
1499 updateProgressBar(arg, perc);
1506 const double TOUT = 3.0;
1514 for(
unsigned int s=0; s<howmany; s++)
1517 if(NULL != updateProgressBar)
1519 float perc = (0 != howmany) ? (
static_cast<float>(s+1)/
static_cast<float>(howmany)) : (100.0);
1520 updateProgressBar(arg, perc);
1529 if(2 == read_messages)
1531 for(
unsigned int i=0; i<2; i++)
1533 uint32_t
id = rxBuffer[i].getId();
1534 uint8_t type =
id & 0xf;
1536 if((0xA != type) && (0xB != type))
1538 yError() <<
"cDownloader::strain_acquire_get() has detected strange can frames of type = " << type <<
".... operation aborted";
1539 char rxframe[128] = {0};
1540 snprintf(rxframe,
sizeof(rxframe),
"l = %d, id = 0x%x, d = 0x[%x %x %x %x %x %x %x %x]", rxBuffer[i].getLen(), rxBuffer[i].getId(),
1541 rxBuffer[i].getData()[0], rxBuffer[i].getData()[1], rxBuffer[i].getData()[2], rxBuffer[i].getData()[3],
1542 rxBuffer[i].getData()[4], rxBuffer[i].getData()[5], rxBuffer[i].getData()[6], rxBuffer[i].getData()[7]);
1544 yError() <<
"frame -> " << rxframe;
1550 uint16_t
x =
static_cast<uint16_t
>(rxBuffer[i].getData()[0]) | (
static_cast<uint16_t
>(rxBuffer[i].getData()[1]) << 8);
1551 uint16_t
y =
static_cast<uint16_t
>(rxBuffer[i].getData()[2]) | (
static_cast<uint16_t
>(rxBuffer[i].getData()[3]) << 8);
1552 uint16_t
z =
static_cast<uint16_t
>(rxBuffer[i].getData()[4]) | (
static_cast<uint16_t
>(rxBuffer[i].getData()[5]) << 8);
1554 sv.
saturated = (6 == rxBuffer[i].getLen()) ? (
true) : (
false);
1557 sv.
calibrated = strain_is_acquiring_in_calibratedmode;
1568 void *tmp = &rxBuffer[i].getData()[6];
1569 icubCanProto_strain_forceSaturationInfo_t *sinfo =
reinterpret_cast<icubCanProto_strain_forceSaturationInfo_t*
>(tmp);
1570 if(1 == sinfo->thereIsSaturationInAtLeastOneChannel)
1572 sv.
saturationinfo[0] =
static_cast<icubCanProto_strain_saturationInfo_t
>(sinfo->saturationInChannel_0);
1573 sv.
saturationinfo[1] =
static_cast<icubCanProto_strain_saturationInfo_t
>(sinfo->saturationInChannel_1);
1574 sv.
saturationinfo[2] =
static_cast<icubCanProto_strain_saturationInfo_t
>(sinfo->saturationInChannel_2);
1578 else if(0xB == type)
1587 void *tmp = &rxBuffer[i].getData()[6];
1588 icubCanProto_strain_torqueSaturationInfo_t *sinfo =
reinterpret_cast<icubCanProto_strain_torqueSaturationInfo_t*
>(tmp);
1589 if(1 == sinfo->thereIsSaturationInAtLeastOneChannel)
1591 sv.
saturationinfo[3] =
static_cast<icubCanProto_strain_saturationInfo_t
>(sinfo->saturationInChannel_3);
1592 sv.
saturationinfo[4] =
static_cast<icubCanProto_strain_saturationInfo_t
>(sinfo->saturationInChannel_4);
1593 sv.
saturationinfo[5] =
static_cast<icubCanProto_strain_saturationInfo_t
>(sinfo->saturationInChannel_5);
1606 yDebug() <<
"cDownloader::strain_acquire_get(): did not receive two consecutive 0xA and 0xB. please check!";
1610 values.push_back(sv);
1616 yDebug() <<
"in streaming mode did not received two messages but " << read_messages <<
"incrementing error counter to" << errorcount;
1617 if(errorcount >= maxerrors)
1619 yError() <<
"reached" << maxerrors <<
"reception errors: must quit";
1636 if (m_idriver == NULL)
1638 if(_verbose) yError (
"Driver not ready\n");
1643 txBuffer[0].setId((2 << 8) + target_id);
1644 txBuffer[0].setLen(2);
1645 txBuffer[0].getData()[0]= 0x07;
1646 txBuffer[0].getData()[1]= 0x01;
1647 set_bus(txBuffer[0], bus);
1652 if(_verbose) yError (
"Unable to send message\n");
1662 if (m_idriver == NULL)
1664 if(_verbose) yError (
"Driver not ready\n");
1669 txBuffer[0].setId((2 << 8) + target_id);
1670 txBuffer[0].setLen(2);
1671 txBuffer[0].getData()[0]= 0x07;
1672 txBuffer[0].getData()[1]= 0x02;
1673 set_bus(txBuffer[0], bus);
1678 if(_verbose) yError (
"Unable to send message\n");
1699int cDownloader::strain_calibrate_offset2 (
int bus,
int target_id, icubCanProto_boardType_t boardtype,
const std::vector<strain2_ampl_discretegain_t> &gains,
const std::vector<int16_t> &targets,
string *errorstring)
1702 if(icubCanProto_boardType__strain == boardtype)
1704 int16_t i16 = (targets.empty()) ? (0) : (targets[0]);
1706 return strain_calibrate_offset2_strain1(bus, target_id, i16, errorstring);
1708 else if(icubCanProto_boardType__strain2 == boardtype || icubCanProto_boardType__strain2c == boardtype)
1710 return strain_calibrate_offset2_strain2(bus, target_id, gains, targets, errorstring);
1724 if (m_idriver == NULL)
1726 if(_verbose) yError (
"Driver not ready\n");
1730 int daclimit = 0x3ff;
1734#define STRAIN2_USE_NEW_MODE
1736#if !defined(STRAIN2_USE_NEW_MODE)
1737 if(icubCanProto_boardType__strain2 == boardtype || icubCanProto_boardType__strain2c == boardtype)
1744 if(icubCanProto_boardType__strain2 == boardtype || icubCanProto_boardType__strain2c == boardtype)
1746 yDebug() <<
"strain2-amplifier-tuning: see the various STEP-x";
1748 const uint8_t everychannel = 0x0f;
1750 uint8_t samples2average = 8;
1752#define TESTMODE_STRAIN2
1753#undef TESTMODE_STRAIN2_SAMEGAIN
1757#if defined(TESTMODE_STRAIN2)
1759 const unsigned int NUMofCHANNELS = 6;
1762#if defined(TESTMODE_STRAIN2_SAMEGAIN)
1764 const int i2u_all = index24;
1766 yDebug() <<
"imposing gain =" << gainvalues[i2u_all] <<
"in every channel";
1768 txBuffer[0].setId((2 << 8) + target_id);
1769 txBuffer[0].setLen(8);
1770 txBuffer[0].getData()[0]= 0x2B;
1771 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (everychannel & 0x0f);
1772 txBuffer[0].getData()[2]= cfg1map[i2u_all][0];
1773 txBuffer[0].getData()[3]= cfg1map[i2u_all][1];
1774 txBuffer[0].getData()[4]= cfg1map[i2u_all][2];
1775 txBuffer[0].getData()[5]= cfg1map[i2u_all][3];
1776 txBuffer[0].getData()[6]= cfg1map[i2u_all][4];
1777 txBuffer[0].getData()[7]= cfg1map[i2u_all][5];
1778 set_bus(txBuffer[0], bus);
1779 yDebug(
"sending: [%x, %x, %x, %x, %x, %x, %x, %x]", txBuffer[0].getData()[0], txBuffer[0].getData()[1], txBuffer[0].getData()[2], txBuffer[0].getData()[3], txBuffer[0].getData()[4], txBuffer[0].getData()[5], txBuffer[0].getData()[6], txBuffer[0].getData()[7]);
1784 yarp::os::Time::delay(1.0);
1789#if defined(DEBUGLEVEL_MAXIMUM)
1791 yDebug() <<
"reading (gain, offset) of front end amplifiers";
1793 for(
int c=0; c<NUMofCHANNELS; c++)
1796 uint16_t ooffset = 0;
1798 yDebug(
"channel %d: gain = %f, offset = %d", c, gaain, ooffset);
1807 yDebug() <<
"i get the amplifier reg config";
1809 txBuffer[0].setId((2 << 8) + target_id);
1810 txBuffer[0].setLen(2);
1811 txBuffer[0].getData()[0]= 0x2A;
1812 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (everychannel & 0x0f);
1813 set_bus(txBuffer[0], bus);
1816 yDebug() <<
"i print the amplifier reg config";
1818 for(
int nr=0; nr<NUMofCHANNELS; nr++)
1821 for(
int i=0; i<rm; i++)
1823 if (rxBuffer[i].getData()[0]==0x2A)
1826 uint8_t from = rxBuffer[i].getData()[1];
1827 yDebug(
"from %d: [%x, %x, %x, %x, %x, %x]", from, rxBuffer[i].getData()[2], rxBuffer[i].getData()[3], rxBuffer[i].getData()[4], rxBuffer[i].getData()[5], rxBuffer[i].getData()[6], rxBuffer[i].getData()[7]);
1833 yarp::os::Time::delay(2.0);
1848 yDebug() <<
"strain2-amplifier-tuning: STEP-1. imposing gains which are different of each channel";
1850 for(
int channel=0; channel<NUMofCHANNELS; channel++)
1857 yarp::os::Time::delay(1.0);
1866 yDebug() <<
"strain2-amplifier-tuning: STEP-2. reading (gain, offset) of front end amplifiers";
1868 for(
int c=0; c<NUMofCHANNELS; c++)
1871 uint16_t ooffset = 0;
1873 yDebug(
"strain2-amplifier-tuning: STEP-2. channel %d: gain = %f, offset = %d", c, gaain, ooffset);
1876 yarp::os::Time::delay(2.0);
1879#if defined(DEBUGLEVEL_MAXIMUM)
1881 yDebug() <<
"i get the amplifier reg config";
1883 txBuffer[0].setId((2 << 8) + target_id);
1884 txBuffer[0].setLen(2);
1885 txBuffer[0].getData()[0]= 0x2A;
1886 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (everychannel & 0x0f);
1887 set_bus(txBuffer[0], bus);
1890 yDebug() <<
"i print the amplifier reg config";
1892 for(
int nr=0; nr<NUMofCHANNELS; nr++)
1895 for(
int i=0; i<rm; i++)
1897 if (rxBuffer[i].getData()[0]==0x2A)
1900 uint8_t from = rxBuffer[i].getData()[1];
1901 yDebug(
"from %d: [%x, %x, %x, %x, %x, %x]", from, rxBuffer[i].getData()[2], rxBuffer[i].getData()[3], rxBuffer[i].getData()[4], rxBuffer[i].getData()[5], rxBuffer[i].getData()[6], rxBuffer[i].getData()[7]);
1907 yarp::os::Time::delay(2.0);
1918 yDebug() <<
"strain2-amplifier-tuning: STEP-3. regularisation of ADC to " << middle_val;
1919 yDebug() <<
"strain2-amplifier-tuning: STEP-3. other params: mae tolerence is" <<
tolerance <<
"and samples2average =" << samples2average;
1923 txBuffer[0].setId((2 << 8) + target_id);
1924 txBuffer[0].setLen(8);
1925 txBuffer[0].getData()[0]= 0x22;
1926 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (everychannel & 0x0f);
1927 txBuffer[0].getData()[2]= 0;
1928 txBuffer[0].getData()[3]= middle_val & 0x00ff;
1929 txBuffer[0].getData()[4]= (middle_val >> 8) & 0x00ff;
1930 txBuffer[0].getData()[5]=
tolerance & 0x00ff;
1931 txBuffer[0].getData()[6]= (
tolerance >> 8) & 0x00ff;
1932 txBuffer[0].getData()[7]= samples2average;
1933 set_bus(txBuffer[0], bus);
1934 yDebug(
"strain2-amplifier-tuning: STEP-3. sent message = [%x, %x, %x, %x, %x, %x, %x, %x]", txBuffer[0].getData()[0], txBuffer[0].getData()[1], txBuffer[0].getData()[2], txBuffer[0].getData()[3], txBuffer[0].getData()[4], txBuffer[0].getData()[5], txBuffer[0].getData()[6], txBuffer[0].getData()[7]);
1939 if(_verbose) yError (
"Unable to send message\n");
1946 yDebug() <<
"strain2-amplifier-tuning: STEP-3. results ...";
1949 for(
int i=0; i<read_messages; i++)
1951 if (rxBuffer[i].getData()[0]==0x22)
1953 yDebug(
"strain2-amplifier-tuning: STEP-3. received message = [%x, %x, %x, %x, %x, %x, %x, %x]", rxBuffer[0].getData()[0], rxBuffer[0].getData()[1], rxBuffer[0].getData()[2], rxBuffer[0].getData()[3], rxBuffer[0].getData()[4], rxBuffer[0].getData()[5], rxBuffer[0].getData()[6], rxBuffer[0].getData()[7]);
1956 uint8_t noisychannelmask = rxBuffer[i].getData()[2];
1957 uint8_t algorithmOKmask = rxBuffer[i].getData()[3];
1958 uint8_t finalmeasureOKmask = rxBuffer[i].getData()[4];
1959 uint16_t mae = (
static_cast<uint32_t
>(rxBuffer[i].getData()[6])) |
1960 (
static_cast<uint32_t
>(rxBuffer[i].getData()[7]) << 8);
1962 if((0x3f == algorithmOKmask) && (0x3f == finalmeasureOKmask))
1964 yDebug() <<
"strain2-amplifier-tuning: STEP-3. OK. regularisation to value" << middle_val <<
"is done and MAE = " << mae;
1965 if(0 != noisychannelmask)
1967 yDebug() <<
"however we found some noisy channels";
1968 yDebug(
"noisychannelmask = 0x%x, algorithmOKmask = 0x%x, finalmeasureOKmask = 0x%x, mae = %d", noisychannelmask, algorithmOKmask, finalmeasureOKmask, mae);
1975 if(0x3f != algorithmOKmask)
1977 yDebug() <<
"strain2-amplifier-tuning: STEP-3. KO. regularisation to value" << middle_val <<
"has sadly failed because algorithm found required values out of range of registers CFG0.OS or ZDAC.";
1981 yDebug() <<
"strain2-amplifier-tuning: STEP-3. KO. regularisation to value" << middle_val <<
"has failed because MAE error is high on some channels.";
1984 yDebug(
"noisychannelmask = 0x%x, algorithmOKmask = 0x%x, finalmeasureOKmask = 0x%x, mae = %d", noisychannelmask, algorithmOKmask, finalmeasureOKmask, mae);
1985 for(uint8_t i=0; i<NUMofCHANNELS; i++)
1987 if((algorithmOKmask & (0x01<<i)) == 0)
1989 yDebug() <<
"calibration fails in channel" << i;
1991 if((finalmeasureOKmask & (0x01<<i)) == 0)
1993 yDebug() <<
"mae is high in channel" << i;
2011 unsigned int measure = 0;
2012 unsigned int dac = 0;
2016 for (channel=0; channel<6; channel++)
2020 txBuffer[0].setId((2 << 8) + target_id);
2021 txBuffer[0].setLen(3);
2022 txBuffer[0].getData()[0]= 0x0C;
2023 txBuffer[0].getData()[1]= channel;
2024 txBuffer[0].getData()[2]= 0;
2025 set_bus(txBuffer[0], bus);
2030 if(_verbose) yError (
"Unable to send message\n");
2035 for (i=0; i<read_messages; i++)
2037 if (rxBuffer[i].getData()[0]==0x0C)
2039 measure = rxBuffer[i].getData()[3]<<8 | rxBuffer[i].getData()[4];
2045 txBuffer[0].setId((2 << 8) + target_id);
2046 txBuffer[0].setLen(2);
2047 txBuffer[0].getData()[0]= 0x0B;
2048 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
2049 set_bus(txBuffer[0], bus);
2053 for (i=0; i<read_messages; i++)
2055 if (rxBuffer[i].getData()[0]==0x0B)
2057 dac = rxBuffer[i].getData()[2]<<8 | rxBuffer[i].getData()[3];
2062 error = long(measure) - long(middle_val);
2067 if (
error>0) dac -= dacstep;
2068 else dac += dacstep;
2070 if (dac>daclimit) dac = daclimit;
2076 txBuffer[0].setId((2 << 8) + target_id);
2077 txBuffer[0].setLen(4);
2078 txBuffer[0].getData()[0]= 0x04;
2079 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
2080 txBuffer[0].getData()[2]= dac >> 8;
2081 txBuffer[0].getData()[3]= dac & 0xFF;
2082 set_bus(txBuffer[0], bus);
2089 txBuffer[0].setId((2 << 8) + target_id);
2090 txBuffer[0].setLen(3);
2091 txBuffer[0].getData()[0]= 0x0C;
2092 txBuffer[0].getData()[1]= channel;
2093 txBuffer[0].getData()[2]= 0;
2094 set_bus(txBuffer[0], bus);
2099 if(_verbose) yError (
"Unable to send message\n");
2104 for (i=0; i<read_messages; i++)
2106 if (rxBuffer[i].getData()[0]==0x0C)
2108 measure = rxBuffer[i].getData()[3]<<8 | rxBuffer[i].getData()[4];
2113 error = long(measure) - long(middle_val);
2127 if (serial_no == NULL)
return -1;
2129 memset (serial_no,0,8);
2132 if (m_idriver == NULL)
2134 if(_verbose) yError (
"Driver not ready\n");
2141 (
board_list[i].type==icubCanProto_boardType__strain) || (
board_list[i].type==icubCanProto_boardType__strain2))
2152#define EOCANPROT_D_CREATE_CANID(clss, orig, dest) ( (((clss)&0xF) << 8) | (((orig)&0xF) << 4) | ((dest)&0xF) )
2164 if(NULL == m_idriver)
2166 if(_verbose) yError (
"cDownloader::get_firmware_version(): driver not ready\n");
2170 int read_messages = 0;
2173 info->type = boardtype;
2174 info->firmware.major = info->firmware.minor = info->firmware.build = 0;
2175 info->protocol.major = info->protocol.minor = 0;
2177 txBuffer[0].setLen(3);
2178 txBuffer[0].getData()[0] = 0;
2179 txBuffer[0].getData()[1] = 0;
2180 txBuffer[0].getData()[2] = 0;
2186 bool boardisMC =
false;
2189 case eobrd_cantype_dsp:
2190 case eobrd_cantype_mc4:
2191 case eobrd_cantype_2dc:
2192 case eobrd_cantype_bll:
2193 case eobrd_cantype_foc:
2197 txBuffer[0].getData()[0] = ICUBCANPROTO_POL_MC_CMD__GET_FIRMWARE_VERSION;
2200 case eobrd_cantype_mtb:
2201 case eobrd_cantype_strain:
2202 case eobrd_cantype_mais:
2203 case eobrd_cantype_6sg:
2204 case eobrd_cantype_mtb4:
2205 case eobrd_cantype_strain2:
2206 case eobrd_cantype_rfe:
2207 case eobrd_cantype_sg3:
2208 case eobrd_cantype_psc:
2209 case eobrd_cantype_mtb4w:
2210 case eobrd_cantype_pmc:
2211 case eobrd_cantype_amcbldc:
2212 case eobrd_cantype_mtb4c:
2213 case eobrd_cantype_strain2c:
2217 txBuffer[0].getData()[0] = ICUBCANPROTO_POL_AS_CMD__GET_FW_VERSION;
2222 if(_verbose) yError (
"cDownloader::get_firmware_version(): this board %d is not supported. returning all zeros\n", boardtype);
2228 set_bus(txBuffer[0], bus);
2232 if(_verbose) yError (
"Unable to send message\n");
2238 if(0 == read_messages)
2245 for (
int i=0; i<read_messages; i++)
2249 if (rxBuffer[i].getData()[0]==ICUBCANPROTO_BL_GET_ADDITIONAL_INFO)
2251 fprintf(stderr,
"%.4x ", rxBuffer[i].getId());
2252 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[0]);
2253 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[1]);
2254 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[2]);
2255 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[3]);
2256 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[4]);
2257 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[5]);
2258 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[6]);
2259 fprintf(stderr,
"%.2x\n", rxBuffer[i].getData()[7]);
2263 if ((txBuffer[0].getData()[0] == rxBuffer[i].getData()[0]) && (8 == rxBuffer[i].getLen()))
2265 info->type = rxBuffer[i].getData()[1];
2266 info->firmware.major = rxBuffer[i].getData()[2];
2267 info->firmware.minor = rxBuffer[i].getData()[3];
2268 info->firmware.build = rxBuffer[i].getData()[4];
2269 info->protocol.major = rxBuffer[i].getData()[5];
2270 info->protocol.minor = rxBuffer[i].getData()[6];
2274 yWarning() <<
"unknown message";
2285 if (board_info == NULL)
return -1;
2287 memset (board_info,0x3f,32);
2290 if (m_idriver == NULL)
2292 if(_verbose) yError (
"Driver not ready\n");
2297 int read_messages = 0;
2300 txBuffer[0].setId(build_id(
ID_MASTER, target_id));
2301 txBuffer[0].setLen(1);
2302 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_GET_ADDITIONAL_INFO;
2303 set_bus(txBuffer[0], bus);
2309 if(_verbose) yError (
"Unable to send message\n");
2324 for (j=0; j<31; j++) board_info[j]=0;
2327 for (i=0; i<read_messages; i++)
2331 if (rxBuffer[i].getData()[0]==ICUBCANPROTO_BL_GET_ADDITIONAL_INFO)
2333 fprintf(stderr,
"%.4x ", rxBuffer[i].getId());
2334 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[0]);
2335 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[1]);
2336 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[2]);
2337 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[3]);
2338 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[4]);
2339 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[5]);
2340 fprintf(stderr,
"%.2x ", rxBuffer[i].getData()[6]);
2341 fprintf(stderr,
"%.2x\n", rxBuffer[i].getData()[7]);
2345 if (rxBuffer[i].getData()[0]==ICUBCANPROTO_BL_GET_ADDITIONAL_INFO && rxBuffer[i].getLen()==6)
2347 int part = rxBuffer[i].getData()[1];
2348 for (j = 0; j< 4; j++)
2351 board_info[tmp]=rxBuffer[i].getData()[j+2];
2368 if (m_idriver == NULL)
2370 if(_verbose) yError (
"Driver not ready\n");
2379 for (counter = 0 ; counter < 8; counter++)
2384 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_SET_ADDITIONAL_INFO;
2385 txBuffer[0].getData()[1]= counter;
2386 txBuffer[0].setId(build_id(
ID_MASTER, target_id));
2387 txBuffer[0].setLen(6);
2390 txBuffer[0].getData()[2+j] = board_info[j+counter*4];
2392 set_bus(txBuffer[0], bus);
2400 if(_verbose) yError (
"Unable to send message\n");
2419 if (m_idriver == NULL)
2421 if(_verbose) yError (
"Driver not ready\n");
2427 case icubCanProto_boardType__strain:
2428 case icubCanProto_boardType__skin:
2429 case icubCanProto_boardType__mais:
2430 case icubCanProto_boardType__6sg:
2431 case icubCanProto_boardType__mtb4:
2432 case icubCanProto_boardType__strain2:
2433 case icubCanProto_boardType__rfe:
2434 case icubCanProto_boardType__sg3:
2435 case icubCanProto_boardType__psc:
2436 case icubCanProto_boardType__mtb4w:
2437 case icubCanProto_boardType__mtb4c:
2438 case eobrd_cantype_pmc:
2439 case eobrd_cantype_amcbldc:
2440 case eobrd_cantype_strain2c:
2442 txBuffer[0].setId((0x02 << 8) + (
ID_MASTER << 4) + target_id);
2443 txBuffer[0].setLen(2);
2444 txBuffer[0].getData()[0]= ICUBCANPROTO_POL_MC_CMD__SET_BOARD_ID;
2445 txBuffer[0].getData()[1]= new_id;
2448 case icubCanProto_boardType__dsp:
2449 case icubCanProto_boardType__pic:
2450 case icubCanProto_boardType__2dc:
2451 case icubCanProto_boardType__4dc:
2452 case icubCanProto_boardType__bll:
2453 case icubCanProto_boardType__2foc:
2454 case icubCanProto_boardType__jog:
2455 txBuffer[0].setId((
ID_MASTER << 4) + target_id);
2456 txBuffer[0].setLen(2);
2457 txBuffer[0].getData()[0]= ICUBCANPROTO_POL_MC_CMD__SET_BOARD_ID;
2458 txBuffer[0].getData()[1]= new_id;
2462 if(_verbose) yError (
"Unknown board type for change of CAN address\n");
2467 set_bus(txBuffer[0], bus);
2473 if(_verbose) yError (
"Unable to send message\n");
2490 if (m_idriver == NULL)
2492 if(_verbose) yError (
"Driver not ready\n");
2500#if defined(DOWNLOADER_ETH_SUPPORTS_MULTIBUS)
2507 if(_verbose) yDebug(
"working on every CAN bus");
2512 txBuffer[0].setLen(1);
2513 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_BROADCAST;
2522 txBuffer[0].setLen(1);
2523 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_BROADCAST;
2531 if(_verbose) yDebug(
"Discovery on every CAN bus is not allowed: reverting to bus CAN1");
2539 txBuffer[0].setLen(1);
2540 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_BROADCAST;
2548 if(_verbose) yError (
"Unable to send message\n");
2557 int read_messages=0;
2562 if (read_messages==0)
2564 if(_verbose) yError (
"No answers\n");
2571 for (i=0; i<read_messages; i++)
2576 fprintf(stderr,
"CAN%d:%.2x, l=%d", get_bus(rxBuffer[i]), rxBuffer[i].getId(), rxBuffer[i].getLen());
2577 fprintf(stderr,
"d[0] %.2x ", rxBuffer[i].getData()[0]);
2578 fprintf(stderr,
"d[1] %.2x ", rxBuffer[i].getData()[1]);
2579 fprintf(stderr,
"d[2] %.2x ", rxBuffer[i].getData()[2]);
2580 fprintf(stderr,
"d[3] %.2x ", rxBuffer[i].getData()[3]);
2581 fprintf(stderr,
"d[4] %.2x ", rxBuffer[i].getData()[4]);
2582 fprintf(stderr,
"d[5] %.2x ", rxBuffer[i].getData()[5]);
2583 fprintf(stderr,
"d[6] %.2x ", rxBuffer[i].getData()[6]);
2584 fprintf(stderr,
"d[7] %.2x\n", rxBuffer[i].getData()[7]);
2587 if ((rxBuffer[i].getData()[0]==ICUBCANPROTO_BL_BROADCAST) &&
2588 ((rxBuffer[i].getLen()==4)||(rxBuffer[i].getLen()==5)))
2600 if(_verbose) yError (
"No Boards found\n");
2619 for (i=0; i<read_messages; i++)
2621 if ((rxBuffer[i].getData()[0]==ICUBCANPROTO_BL_BROADCAST) &&
2622 ((rxBuffer[i].getLen()==4)||(rxBuffer[i].getLen()==5)))
2624#if defined(DOWNLOADER_USE_IDRIVER2)
2639 if (rxBuffer[i].getLen()==4)
2654 char board_info [32];
2666 char serial_no [32];
2680 if(
board_list[i].type==icubCanProto_boardType__strain)
2684 else if(
board_list[i].type==icubCanProto_boardType__strain2 ||
board_list[i].type==icubCanProto_boardType__strain2c)
2693#define TEST_GET_FW_VERSION
2695#if defined(TEST_GET_FW_VERSION)
2697 if(_verbose) yDebug (
"about to ask fw version \n");
2704 eObrd_info_t info = {0};
2705 memset(&info, 0,
sizeof(info));
2706 bool noreply =
true;
2710 fprintf(stderr,
"board %d: ret = %d, reply = %d, type = %d, f=(%d, %d, %d), pr=(%d, %d)\n", i, rr, !noreply,
2712 info.firmware.major, info.firmware.minor, info.firmware.build,
2713 info.protocol.major, info.protocol.minor);
2723 if(_verbose) yDebug(
" BUS:id type version");
2749 if (m_idriver == NULL)
2751 if(_verbose) yError (
"START_CMD: Driver not ready\n");
2757 case icubCanProto_boardType__dsp:
2758 case icubCanProto_boardType__pic:
2759 case icubCanProto_boardType__2dc:
2760 case icubCanProto_boardType__4dc:
2761 case icubCanProto_boardType__bll:
2764 txBuffer[0].setId(build_id(
ID_MASTER, board_pid));
2765 txBuffer[0].setLen(1);
2766 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_BOARD;
2769 set_bus(txBuffer[0], bus);
2774 case icubCanProto_boardType__skin:
2775 case icubCanProto_boardType__strain:
2776 case icubCanProto_boardType__mais:
2777 case icubCanProto_boardType__2foc:
2778 case icubCanProto_boardType__6sg:
2779 case icubCanProto_boardType__jog:
2780 case icubCanProto_boardType__mtb4:
2781 case icubCanProto_boardType__strain2:
2782 case icubCanProto_boardType__rfe:
2783 case icubCanProto_boardType__sg3:
2784 case icubCanProto_boardType__psc:
2785 case icubCanProto_boardType__mtb4w:
2786 case icubCanProto_boardType__mtb4c:
2787 case eobrd_cantype_pmc:
2788 case eobrd_cantype_amcbldc:
2789 case eobrd_cantype_strain2c:
2790 case icubCanProto_boardType__unknown:
2793 txBuffer[0].setId(build_id(
ID_MASTER,board_pid));
2794 txBuffer[0].setLen(2);
2795 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_BOARD;
2796 txBuffer[0].getData()[1]= (int) board_eeprom;
2799 set_bus(txBuffer[0], bus);
2805 set_bus(txBuffer[0], bus);
2811 if(_verbose) yError (
"START_CMD: Unable to send message\n");
2822 for (
int i=0; i<read_messages; i++)
2824 if (rxBuffer[i].getData()[0]==ICUBCANPROTO_BL_BOARD &&
2825 (((rxBuffer[i].getId() >> 4) & 0x0F) == board_pid) &&
2826 (((rxBuffer[i].getId() >> 8) & 0x07) == ICUBCANPROTO_CLASS_BOOTLOADER))
2836 if(_verbose) yError (
"START_CMD: No ACK received from board %d\n", board_pid);
2846 if (m_idriver == NULL)
2848 if(_verbose) yError (
"STOP_CMD: Driver not ready\n");
2853 txBuffer[0].setId(build_id(
ID_MASTER, board_pid));
2854 txBuffer[0].setLen(1);
2855 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_END;
2856 set_bus(txBuffer[0], bus);
2862 if(_verbose) yError (
"STOP_CMD: Unable to send message\n");
2873 for (
int i=0; i<read_messages; i++)
2875 if (rxBuffer[i].getData()[0]==ICUBCANPROTO_BL_END &&
2876 (((rxBuffer[i].getId() >> 4) & 0x0F) == board_pid || board_pid == 15 ) &&
2877 (((rxBuffer[i].getId() >> 8) & 0x07) == ICUBCANPROTO_CLASS_BOOTLOADER))
2886 if(_verbose) yError (
"TOP_CMD: No ACK received from board %d\n", board_pid);
2894 char hexconv_buffer[5];
2895 memset (hexconv_buffer,
'\0',
sizeof(hexconv_buffer) );
2896 strncpy(hexconv_buffer,line,len);
2897 return axtoi (hexconv_buffer);
2901int cDownloader::verify_ack(
int command,
int read_messages)
2925 for (k=0; k<read_messages; k++)
2927 if ((rxBuffer[k].getData()[0]==command) &&
2928 (rxBuffer[k].getLen() == 2) &&
2929 (rxBuffer[k].getData()[1]==1))
2931 if(
board_list[i].pid == get_src_from_id(rxBuffer[k].getId()))
2933 #if defined(DOWNLOADER_USE_IDRIVER2)
2934 if(
board_list[i].bus == rxBuffer[k].getCanBus())
2966int cDownloader::download_motorola_line(
char* line,
int len,
int bus,
int board_pid)
2977 char sprsRecordType=0;
2978 unsigned long int sprsChecksum=0;
2979 int sprsMemoryType=1;
2980 long unsigned int sprsAddress;
2984 int read_messages=0;
2986 for (i=2; i<len; i=i+2)
2989 sprsChecksum+= value;
2994 if ((sprsChecksum & 0xFF) == 0xFF)
3000 if(_verbose) yError (
"Failed Checksum\n");
3005 if (!(line[0] ==
'S'))
3007 if(_verbose) yError (
"start tag character not found\n");
3013 sprsRecordType=char(*(line+i));
3020 switch (sprsRecordType)
3032 if (sprsAddress==0x0020)
3041 txBuffer[0].setId(build_id(
ID_MASTER,board_pid));
3042 txBuffer[0].setLen(5);
3043 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_ADDRESS;
3044 txBuffer[0].getData()[1]= sprsLength;
3045 txBuffer[0].getData()[2]= (
unsigned char) ((sprsAddress) & 0x00FF);
3046 txBuffer[0].getData()[3]= (
unsigned char) ((sprsAddress>>8) & 0x00FF);
3047 txBuffer[0].getData()[4]= sprsMemoryType;
3050 set_bus(txBuffer[0], bus);
3056 if(_verbose) yError (
"Unable to send message\n");
3065 if ((sprsLength%6) == 0)
3072 tmp=sprsLength / 6 + 1;
3073 rest=sprsLength % 6;
3076 for (j=1; j<=
tmp; j++)
3078 txBuffer[0].getData()[0]=ICUBCANPROTO_BL_DATA;
3079 if (j<tmp) txBuffer[0].setLen(7);
3080 else txBuffer[0].setLen(rest+1);
3082 for (k=1; k<=6; k++)
3084 txBuffer[0].getData()[k] =
getvalue(line+i+((k-1)*2+((j-1)*12)),2);
3088 set_bus(txBuffer[0], bus);
3094 if(_verbose) yError (
"Unable to send message\n");
3107 passed=Time::now()-now;
3118 txBuffer[0].setId(build_id(
ID_MASTER,board_pid));
3119 txBuffer[0].setLen(5);
3120 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_START;
3121 txBuffer[0].getData()[4]=
getvalue(line+i,2); i+=2;
3122 txBuffer[0].getData()[3]=
getvalue(line+i,2); i+=2;
3123 txBuffer[0].getData()[2]=
getvalue(line+i,2); i+=2;
3124 txBuffer[0].getData()[1]=
getvalue(line+i,2);
3127 set_bus(txBuffer[0], bus);
3133 if(_verbose) yError (
"Unable to send message\n");
3142 verify_ack(ICUBCANPROTO_BL_START, read_messages);
3149 if(_verbose) yError (
"wrong format tag character %c (hex:%X)\n", sprsRecordType, sprsRecordType);
3155 if(_verbose) yError (
"Can't reach here!\n");
3166int cDownloader::download_hexintel_line(
char* line,
int len,
int bus,
int board_pid,
bool eeprom,
int board_type)
3168 char sprsRecordType=0;
3169 unsigned int sprsState;
3170 unsigned long int sprsChecksum=0;
3171 int sprsMemoryType=0;
3172 long unsigned int sprsAddress=0;
3174 unsigned int sprsData[50];
3177 int read_messages=0;
3179 for (i=1; i<len; i=i+2)
3182 sprsChecksum+= value;
3185 sprsChecksum = (sprsChecksum & 0xFF);
3186 if (sprsChecksum == 0x00)
3192 if(_verbose) yError (
"Failed Checksum\n");
3204 if (!(line[0] ==
':'))
3206 if(_verbose) yError(
"start tag character not found in hex file\n");
3217 sprsRecordType=char(*(line+i+1));
3238 switch (sprsRecordType)
3242 for (k=0;k<sprsLength;k++)
3264 if((icubCanProto_boardType__mtb4 == board_type) || (icubCanProto_boardType__strain2 == board_type) ||
3265 (icubCanProto_boardType__rfe == board_type) || (icubCanProto_boardType__sg3 == board_type) ||
3266 (icubCanProto_boardType__psc == board_type) || (icubCanProto_boardType__mtb4w == board_type) ||
3267 (icubCanProto_boardType__pmc == board_type)
3268 || (icubCanProto_boardType__amcbldc == board_type)
3269 || (icubCanProto_boardType__mtb4c == board_type)
3270 || (icubCanProto_boardType__strain2c == board_type)
3279 snprintf(msg,
sizeof(msg),
"0x%04X",
sprsPage);
3280 yError() <<
"Upload of FW to board" << eoboards_type2string2((eObrd_type_t)board_type, eobool_true) <<
"is aborted because it was detected a wrong page number =" << msg <<
"in the .hex file";
3281 yError() <<
"You must have loaded the .hex file of another board. Perform a new discovery, check the file name and retry.";
3295 if (sprsChecksum==0)
3297 switch (sprsRecordType)
3304 txBuffer[0].setId(build_id(
ID_MASTER,board_pid));
3305 txBuffer[0].setLen(7);
3306 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_ADDRESS;
3307 txBuffer[0].getData()[1]= sprsLength;
3308 txBuffer[0].getData()[2]= (
unsigned char) ((sprsAddress) & 0x00FF);
3309 txBuffer[0].getData()[3]= (
unsigned char) ((sprsAddress>>8) & 0x00FF);
3310 txBuffer[0].getData()[4]= sprsMemoryType;
3311 txBuffer[0].getData()[5]= (
unsigned char) ((
sprsPage) & 0x00FF);
3312 txBuffer[0].getData()[6]= (
unsigned char) ((
sprsPage >>8) & 0x00FF);
3315 set_bus(txBuffer[0], bus);
3322 if(_verbose) yError (
"Unable to send message\n");
3330 if ((sprsLength%6) == 0)
3337 tmp=sprsLength / 6 + 1;
3338 rest=sprsLength % 6;
3341 for (j=1; j<=
tmp; j++)
3343 txBuffer[0].getData()[0]=ICUBCANPROTO_BL_DATA;
3344 if (j<tmp) txBuffer[0].setLen(7);
3345 else txBuffer[0].setLen(rest+1);
3347 for (k=1; k<=6; k++)
3349 txBuffer[0].getData()[k] = sprsData[(k-1)+((j-1)*6)];
3353 set_bus(txBuffer[0], bus);
3359 if(_verbose) yError (
"Unable to send message\n");
3367 ret=verify_ack(ICUBCANPROTO_BL_DATA, read_messages);
3377 txBuffer[0].setId(build_id(
ID_MASTER,board_pid));
3378 txBuffer[0].setLen(5);
3379 txBuffer[0].getData()[0]= ICUBCANPROTO_BL_START;
3380 txBuffer[0].getData()[1]= 0;
3381 txBuffer[0].getData()[2]= 0;
3382 txBuffer[0].getData()[3]= 0;
3383 txBuffer[0].getData()[4]= 0;
3386 set_bus(txBuffer[0], bus);
3391 if(_verbose) yError (
"Unable to send message\n");
3398 ret=verify_ack(ICUBCANPROTO_BL_START, read_messages);
3413 if(_verbose) yError (
"Checksum Error\n");
3420 if(_verbose) yError (
"Can't reach here!\n");
3433 if(_verbose) yError (
"Error opening file!\n");
3451 if(_verbose) yError (
"Error opening file!\n");
3468 if(_verbose) yError (
"File not open!\n");
3488 if (strlen(buffer)!=0)
3490 switch (download_type)
3492 case icubCanProto_boardType__dsp:
3493 case icubCanProto_boardType__2dc:
3494 case icubCanProto_boardType__4dc:
3495 case icubCanProto_boardType__bll:
3496 ret = download_motorola_line(buffer, strlen(buffer), bus, board_pid);
3498 case icubCanProto_boardType__pic:
3499 case icubCanProto_boardType__skin:
3500 case icubCanProto_boardType__strain:
3501 case icubCanProto_boardType__mais:
3502 case icubCanProto_boardType__2foc:
3503 case icubCanProto_boardType__jog:
3504 case icubCanProto_boardType__6sg:
3505 case icubCanProto_boardType__mtb4:
3506 case icubCanProto_boardType__strain2:
3507 case icubCanProto_boardType__rfe:
3508 case icubCanProto_boardType__sg3:
3509 case icubCanProto_boardType__psc:
3510 case icubCanProto_boardType__mtb4w:
3511 case icubCanProto_boardType__mtb4c:
3512 case icubCanProto_boardType__pmc:
3513 case icubCanProto_boardType__amcbldc:
3514 case icubCanProto_boardType__strain2c:
3515 ret = download_hexintel_line(buffer, strlen(buffer), bus, board_pid, board_eeprom, download_type);
3518 case icubCanProto_boardType__unknown:
3525 if(_verbose) yError(
"fatal error during download: abort\n");
3545void cDownloader::clean_rx(
void)
3550#if defined(DOWNLOADER_USE_IDRIVER2)
3552void cDownloader::set_bus(
CanPacket &pkt,
int bus)
3564void cDownloader::set_bus(yarp::dev::CanMessage &msg,
int bus)
3569int cDownloader::get_bus(yarp::dev::CanMessage &msg)
3578int cDownloader::strain_calibrate_offset2_strain1 (
int bus,
int target_id, int16_t t,
string *errorstring)
3581 return strain_calibrate_offset2_strain1safer(bus, target_id, t, 2,
false, errorstring);
3584 if (m_idriver == NULL)
3586 if(_verbose) yError (
"Driver not ready\n");
3591 unsigned int middle_val = 32768 + t;
3594 const int regset = 0;
3596 int daclimit = 0x3ff;
3604 unsigned int measure = 0;
3605 unsigned int dac = 0;
3609 for (channel=0; channel<6; channel++)
3613 txBuffer[0].setId((2 << 8) + target_id);
3614 txBuffer[0].setLen(3);
3615 txBuffer[0].getData()[0]= 0x0C;
3616 txBuffer[0].getData()[1]= channel;
3617 txBuffer[0].getData()[2]= 0;
3618 set_bus(txBuffer[0], bus);
3623 if(_verbose) yError (
"Unable to send message\n");
3628 for (i=0; i<read_messages; i++)
3630 if (rxBuffer[i].getData()[0]==0x0C)
3632 measure = rxBuffer[i].getData()[3]<<8 | rxBuffer[i].getData()[4];
3639 txBuffer[0].setId((2 << 8) + target_id);
3640 txBuffer[0].setLen(2);
3641 txBuffer[0].getData()[0]= 0x0B;
3642 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
3643 txBuffer[0].getData()[1]= (channel & 0x0f);
3644 set_bus(txBuffer[0], bus);
3648 for (i=0; i<read_messages; i++)
3650 if (rxBuffer[i].getData()[0]==0x0B)
3652 dac = rxBuffer[i].getData()[2]<<8 | rxBuffer[i].getData()[3];
3657 error = long(measure) - long(middle_val);
3662 if (
error>0) dac -= dacstep;
3663 else dac += dacstep;
3665 if (dac>daclimit) dac = daclimit;
3671 txBuffer[0].setId((2 << 8) + target_id);
3672 txBuffer[0].setLen(4);
3673 txBuffer[0].getData()[0]= 0x04;
3674 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel & 0x0f);
3675 txBuffer[0].getData()[2]= dac >> 8;
3676 txBuffer[0].getData()[3]= dac & 0xFF;
3677 set_bus(txBuffer[0], bus);
3684 txBuffer[0].setId((2 << 8) + target_id);
3685 txBuffer[0].setLen(3);
3686 txBuffer[0].getData()[0]= 0x0C;
3687 txBuffer[0].getData()[1]= channel;
3688 txBuffer[0].getData()[2]= 0;
3689 set_bus(txBuffer[0], bus);
3694 if(_verbose) yError (
"Unable to send message\n");
3699 for (i=0; i<read_messages; i++)
3701 if (rxBuffer[i].getData()[0]==0x0C)
3703 measure = rxBuffer[i].getData()[3]<<8 | rxBuffer[i].getData()[4];
3708 error = long(measure) - long(middle_val);
3719int cDownloader::strain_calibrate_offset2_strain2(
int bus,
int target_id,
const std::vector<strain2_ampl_discretegain_t> &gains,
const std::vector<int16_t> &targets,
string *errorstring)
3723 const unsigned int NUMofCHANNELS = 6;
3725 std::ostringstream ss;
3728 if (m_idriver == NULL)
3730 if(_verbose) yError (
"Driver not ready\n");
3731 Log(std::string(
"strain_calibrate_offset2_strain2(): failure. driver no ready"));
3738 ss <<
"performing offset autotuning for strain2";
3742 for(
int channel=0; channel<NUMofCHANNELS; channel++)
3753 Log(
"strain_calibrate_offset2_strain2(): failure of strain_set_amplifier_discretegain()");
3758 yarp::os::Time::delay(1.0);
3761 uint16_t ooffset = 0;
3764 ss <<
" and read (g, o) = (" << std::to_string(
static_cast<int>(gaain)) <<
", " << std::to_string(ooffset) <<
")";
3770 yarp::os::Time::delay(2.0);
3776 int16_t singletargetVALUE = targets[0];
3777 bool singletargetTHEREIS =
true;
3778 for(
int channel=1; channel<NUMofCHANNELS; channel++)
3780 if(singletargetVALUE != targets[channel])
3782 singletargetTHEREIS =
false;
3793 uint8_t samples2average = 8;
3798 const uint8_t everychannel = 0x0f;
3800 uint8_t channel2autocalib = everychannel;
3801 unsigned int middle_val = 32768;
3802 uint8_t okmask = 0x3f;
3804 if(
true == singletargetTHEREIS)
3807 channel2autocalib = everychannel;
3808 middle_val = 32768 + singletargetVALUE;
3814 ss <<
"STEP-2. there is a single ADC target: performing parallel regularization";
3819 ss <<
" params: target = " << std::to_string(singletargetVALUE) <<
" tolerance = " << std::to_string(
tolerance) <<
" samples2average = " << std::to_string(samples2average);
3822 txBuffer[0].setId((2 << 8) + target_id);
3823 txBuffer[0].setLen(8);
3824 txBuffer[0].getData()[0]= 0x22;
3825 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel2autocalib & 0x0f);
3826 txBuffer[0].getData()[2]= 0;
3827 txBuffer[0].getData()[3]= middle_val & 0x00ff;
3828 txBuffer[0].getData()[4]= (middle_val >> 8) & 0x00ff;
3829 txBuffer[0].getData()[5]=
tolerance & 0x00ff;
3830 txBuffer[0].getData()[6]= (
tolerance >> 8) & 0x00ff;
3831 txBuffer[0].getData()[7]= samples2average;
3832 set_bus(txBuffer[0], bus);
3838 if(_verbose) yError (
"Unable to send message\n");
3847 ss <<
"STEP-3. results ...";
3851 for(
int i=0; i<read_messages; i++)
3853 if (rxBuffer[i].getData()[0]==0x22)
3858 uint8_t noisychannelmask = rxBuffer[i].getData()[2];
3859 uint8_t algorithmOKmask = rxBuffer[i].getData()[3];
3860 uint8_t finalmeasureOKmask = rxBuffer[i].getData()[4];
3861 uint16_t mae = (
static_cast<uint32_t
>(rxBuffer[i].getData()[6])) |
3862 (
static_cast<uint32_t
>(rxBuffer[i].getData()[7]) << 8);
3864 if((okmask == algorithmOKmask) && (okmask == finalmeasureOKmask))
3868 ss <<
" OK w/ MAE = " << std::to_string(mae);
3871 if(0 != noisychannelmask)
3875 ss <<
" BUT noisy acquisition of samples: ";
3876 if((0x40 & noisychannelmask) == 0x40)
3878 ss <<
" in computing average ADC before algorithm ";
3880 if((0x80 & noisychannelmask) == 0x80)
3882 ss <<
"after algorithm in computing MAE";
3888 char tmp[256] = {0};
3889 snprintf(tmp,
sizeof(tmp),
"noisychannelmask = 0x%x, algorithmOKmask = 0x%x, finalmeasureOKmask = 0x%x, mae = %d", noisychannelmask, algorithmOKmask, finalmeasureOKmask, mae);
3890 ss <<
"COMPLETE RES: " <<
tmp;
3900 char tmp[256] = {0};
3901 snprintf(tmp,
sizeof(tmp),
"noisychannelmask = 0x%x, algorithmOKmask = 0x%x, finalmeasureOKmask = 0x%x, mae = %d", noisychannelmask, algorithmOKmask, finalmeasureOKmask, mae);
3905 if(0 != noisychannelmask)
3909 ss <<
" WITH noisy acquisition of samples: ";
3910 if((0x40 & noisychannelmask) == 0x40)
3912 ss <<
" in computing average ADC before algorithm ";
3914 if((0x80 & noisychannelmask) == 0x80)
3916 ss <<
"after algorithm in computing MAE";
3921 for(uint8_t channel=0; channel<NUMofCHANNELS; channel++)
3925 bool problems =
false;
3926 ss <<
"- on ch " << std::to_string(channel) <<
":";
3927 if((algorithmOKmask & (0x01<<channel)) == 0)
3930 ss <<
" [algorithm fails]";
3932 if((finalmeasureOKmask & (0x01<<channel)) == 0)
3935 ss <<
" [mae is high (does ADC work?)]";
3937 if(((noisychannelmask) & (0x01<<channel)) == (0x01<<channel))
3940 ss <<
" [noisy acquition]";
3944 ss <<
" [no detected problem]";
3958 ss <<
"STEP-2. there are multiple ADC targets: performing regularization channel by channel";
3963 ss <<
" common params: tolerance = " << std::to_string(
tolerance) <<
" samples2average = " << std::to_string(samples2average);
3967 for(
int channel=0; channel<NUMofCHANNELS; channel++)
3969 channel2autocalib = channel;
3970 middle_val = 32768 + targets[channel];
3971 okmask = 0x01 << channel;
3975 ss <<
"- on ch " << std::to_string(channel) <<
": ADC target = " << std::to_string(targets[channel]);
3982 txBuffer[0].setId((2 << 8) + target_id);
3983 txBuffer[0].setLen(8);
3984 txBuffer[0].getData()[0]= 0x22;
3985 txBuffer[0].getData()[1]= ((regset << 4) & 0xf0) | (channel2autocalib & 0x0f);
3986 txBuffer[0].getData()[2]= 0;
3987 txBuffer[0].getData()[3]= middle_val & 0x00ff;
3988 txBuffer[0].getData()[4]= (middle_val >> 8) & 0x00ff;
3989 txBuffer[0].getData()[5]=
tolerance & 0x00ff;
3990 txBuffer[0].getData()[6]= (
tolerance >> 8) & 0x00ff;
3991 txBuffer[0].getData()[7]= samples2average;
3992 set_bus(txBuffer[0], bus);
3997 if(_verbose) yError (
"Unable to send message\n");
4006 for(
int i=0; i<read_messages; i++)
4008 if (rxBuffer[i].getData()[0]==0x22)
4013 uint8_t noisychannelmask = rxBuffer[i].getData()[2];
4014 uint8_t algorithmOKmask = rxBuffer[i].getData()[3];
4015 uint8_t finalmeasureOKmask = rxBuffer[i].getData()[4];
4016 uint16_t mae = (
static_cast<uint32_t
>(rxBuffer[i].getData()[6])) |
4017 (
static_cast<uint32_t
>(rxBuffer[i].getData()[7]) << 8);
4019 if((okmask == algorithmOKmask) && (okmask == finalmeasureOKmask))
4023 ss <<
" OK w/ MAE = " << std::to_string(mae);
4026 if(0 != (noisychannelmask & okmask))
4030 ss <<
" BUT noisy acquisition of samples: ";
4033 if((0x40 & noisychannelmask) == 0x40)
4037 ss <<
" - in computing average ADC before algorithm ";
4040 if((0x80 & noisychannelmask) == 0x80)
4044 ss <<
" - after algorithm in computing MAE";
4050 char tmp[256] = {0};
4051 snprintf(tmp,
sizeof(tmp),
"noisychannelmask = 0x%x, algorithmOKmask = 0x%x, finalmeasureOKmask = 0x%x, mae = %d", noisychannelmask, algorithmOKmask, finalmeasureOKmask, mae);
4052 ss <<
"COMPLETE RES: " <<
tmp;
4062 ss <<
" KO /w MAE = " << std::to_string(mae) <<
" because: ";
4067 char tmp[256] = {0};
4068 snprintf(tmp,
sizeof(tmp),
" KO details: noisychannelmask = 0x%x, algorithmOKmask = 0x%x, finalmeasureOKmask = 0x%x, mae = %d", noisychannelmask, algorithmOKmask, finalmeasureOKmask, mae);
4072 bool problems =
false;
4073 if((algorithmOKmask & okmask) == 0)
4078 ss <<
" - algorithm fails";
4081 if((finalmeasureOKmask & okmask) == 0)
4086 ss <<
" - mae is high (does ADC work?)";
4093 ss <<
" .. strange: no detected problem";
4107int cDownloader::readADC(
int bus,
int target_id,
int channel,
int nmeasures)
4111 txBuffer[0].setId((2 << 8) + target_id);
4112 txBuffer[0].setLen(3);
4113 txBuffer[0].getData()[0]= 0x0C;
4114 txBuffer[0].getData()[1]= channel;
4115 txBuffer[0].getData()[2]= type;
4119 for(
int n=0;
n<nmeasures;
n++)
4122 set_bus(txBuffer[0], bus);
4127 yError (
"Unable to send message\n");
4132 for (
int i=0; i<read_messages; i++)
4134 if (rxBuffer[i].getData()[0]==0x0C)
4136 tmp = (rxBuffer[i].getData()[3]<<8 | rxBuffer[i].getData()[4]);
4141 printf(
"cDownloader::strain_calibrate_offset2_strain1(): fails in reading reply for a measure\n");
4148 measure /= nmeasures;
4153int cDownloader::strain_calibrate_offset2_strain1safer (
int bus,
int target_id, int16_t t, uint8_t nmeasures,
bool fullsearch,
string *errorstring)
4156 if (m_idriver == NULL)
4158 if(_verbose) yError (
"Driver not ready\n");
4162 std::ostringstream ss;
4165 unsigned int middle_val = 32768 + t;
4174 ss <<
"performing offset autotuning for strain1 in full search mode";
4179 ss <<
"params: " <<
"adc target =" << std::to_string(t) <<
" using" << std::to_string(nmeasures) <<
"adc acquisions for better averaging";
4182 for(
int channel=0; channel<6; channel++)
4185 long minABSerror = 128*1024;
4186 unsigned int minDAC = 0;
4189 for(
unsigned int testdac=0; testdac<1024; testdac++)
4196 unsigned int tmp = 0;
4200 yError() <<
"failed to impose DAC = " << testdac <<
"read:" <<
tmp;
4204 int measure = readADC(bus, target_id, channel, nmeasures);
4206 long error = long(measure) - long(middle_val);
4208 if(fabs(
error) < fabs(minABSerror))
4210 minABSerror = fabs(
error);
4221 unsigned int tmp = 0;
4225 yError() <<
"failed to impose DAC = " << minDAC <<
"read:" <<
tmp;
4230 ss <<
"RESULT of FULL SEARCH w/ nsamples average =" << std::to_string(nmeasures) <<
"-> channel =" << std::to_string(channel) <<
"minerror = " << std::to_string(minABSerror) <<
"mindac =" << std::to_string(minDAC);
4240 const int daclimit = 0x3ff;
4241 const int dacstep = 1;
4243 const int maxiterations = 1024;
4247 ss <<
"performing gradient descend in dac space to find best value to match adc ="<< std::to_string(t) <<
" using" << std::to_string(nmeasures) <<
"adc acquisions for better averaging";
4252 ss <<
"exit conditions: max number of iterations =" << std::to_string(maxiterations) <<
"error tolerance =" << std::to_string(
tolerance);
4255 for(
int channel=0; channel<6; channel++)
4258 long minABSerror = 128*1024;
4259 unsigned int minDAC = 0;
4260 unsigned int dac = 0;
4263 int measure = readADC(bus, target_id, channel, nmeasures);
4268 long error = long(measure) - long(middle_val);
4271 minABSerror = fabs(
error);
4275 while ((abs(
error)>
tolerance) && (cycle<daclimit) && (cycle<maxiterations))
4277 if (
error>0) dac -= dacstep;
4278 else dac += dacstep;
4280 if (dac>daclimit) dac = daclimit;
4290 unsigned int tmp = 0;
4294 yError() <<
"failed to impose DAC = " << dac <<
"read:" <<
tmp;
4298 measure = readADC(bus, target_id, channel, nmeasures);
4300 error = long(measure) - long(middle_val);
4303 if(fabs(
error) < fabs(minABSerror))
4305 minABSerror = fabs(
error);
4312 ss <<
"RESULT of gradient descend w/ nsamples average =" << std::to_string(nmeasures) <<
" -> channel =" << std::to_string(channel) <<
" num iters =" << std::to_string(cycle) <<
" err = " << std::to_string(
error) <<
" applied dac =" << std::to_string(dac) <<
" minerror = " << std::to_string(minABSerror) <<
" mindac =" << std::to_string(minDAC);
constexpr double tolerance
void setCanBus(unsigned int bus)
int open_file(std::string file)
int strain_get_serial_number(int bus, int target_id, char *serial_number, string *errorstring=NULL)
int strain_set_matrix_gain(int bus, int target_id, unsigned int gain, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_acquire_start(int bus, int target_id, uint8_t txratemilli=20, bool calibmode=true, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, string *errorstring=NULL)
int strain_get_offset(int bus, int target_id, char channel, unsigned int &offset, int regset=strain_regset_inuse, string *errorstring=NULL)
int change_card_address(int bus, int target_id, int new_id, int board_type)
int strain_get_full_scale(int bus, int target_id, unsigned char channel, unsigned int &full_scale, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_get_calib_bias(int bus, int target_id, char channel, signed int &bias, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_reset_curr_bias(int bus, int target_id, string *errorstring=NULL)
int get_board_info(int bus, int target_id, char *board_info)
int strain_set_offset(int bus, int target_id, char channel, unsigned int offset, int regset=strain_regset_inuse, string *errorstring=NULL)
int startscheda(int bus, int board_pid, bool board_eeprom, int download_type)
int strain_set_serial_number(int bus, int target_id, const char *serial_number, string *errorstring=NULL)
int strain_acquire_stop(int bus, int target_id, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, string *errorstring=NULL)
int strain_set_amplifier_discretegain(int bus, int target_id, unsigned char channel, strain2_ampl_discretegain_t ampset, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_get_regulationset(int bus, int target_id, int ®set, const int regsetmode=strain_regsetmode_temporary, string *errorstring=NULL)
int stopscheda(int bus, int board_pid)
int strain_save_to_eeprom(int bus, int target_id, string *errorstring=NULL)
void set_verbose(bool verbose)
int strain_acquire_get(int bus, int target_id, vector< strain_value_t > &values, const unsigned int howmany=10, void(*updateProgressBar)(void *, float)=NULL, void *arg=NULL, strain_acquisition_mode_t acqmode=strain_acquisition_mode_streaming, const unsigned int maxerrors=1, string *errorstring=NULL)
int change_board_info(int bus, int target_id, char *board_info)
int strain_get_adc(int bus, int target_id, char channel, unsigned int &adc, int type, string *errorstring=NULL)
int strain_stop_sampling(int bus, int target_id, string *errorstring=NULL)
cDownloader(bool verbose=true)
strain_acquisition_mode_t
@ strain_acquisition_mode_polling
int initdriver(yarp::os::Searchable &config, bool verbose=true)
void set_canbus_id(int id)
int strain_get_eeprom_saved(int bus, int target_id, bool *status, string *errorstring=NULL)
int get_firmware_version(int bus, int target_id, eObrd_cantype_t boardtype, eObrd_info_t *info, bool &noreply)
int get_serial_no(int bus, int target_id, char *board_info)
int strain_reset_calib_bias(int bus, int target_id, string *errorstring=NULL)
int strain_get_matrix_gain(int bus, int target_id, unsigned int &gain, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_get_matrix_rc(int bus, int target_id, char r, char c, unsigned int &elem, int regset=strain_regset_inuse, string *errorstring=NULL)
void set_external_logger(void *caller=NULL, void(*logger)(void *, const std::string &)=NULL)
int strain_set_full_scale(int bus, int target_id, unsigned char channel, unsigned int full_scale, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_set_regulationset(int bus, int target_id, int regset=strain_regset_one, int regsetmode=strain_regsetmode_temporary, string *errorstring=NULL)
@ strain_regsetmode_permanent
@ strain_regsetmode_temporary
int strain_set_calib_bias(int bus, int target_id, string *errorstring=NULL)
int strain_get_amplifier_regs(int bus, int target_id, unsigned char channel, strain2_ampl_regs_t &regs, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_set_curr_bias(int bus, int target_id, string *errorstring=NULL)
int strain_get_curr_bias(int bus, int target_id, char channel, signed int &bias, string *errorstring=NULL)
int strain_start_sampling(int bus, int target_id, string *errorstring=NULL)
int download_file(int bus, int board_pid, int download_type, bool eeprom)
int strain_get_amplifier_gain_offset(int bus, int target_id, unsigned char channel, float &gain, uint16_t &offset, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_set_matrix_rc(int bus, int target_id, char r, char c, unsigned int elem, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_set_amplifier_regs(int bus, int target_id, unsigned char channel, const strain2_ampl_regs_t &regs, int regset=strain_regset_inuse, string *errorstring=NULL)
int strain_set_amplifier_gain_offset(int bus, int target_id, unsigned char channel, float gain, uint16_t offset, int regset=strain_regset_inuse, string *errorstring=NULL)
float strain_amplifier_discretegain2float(strain2_ampl_discretegain_t c)
int strain_calibrate_offset2(int bus, int target_id, icubCanProto_boardType_t boardtype, const std::vector< strain2_ampl_discretegain_t > &gains, const std::vector< int16_t > &targets, string *errorstring=NULL)
int strain_calibrate_offset(int bus, int target_id, icubCanProto_boardType_t boardtype, unsigned int middle_val, string *errorstring=NULL)
virtual int send_message(vector< CanPacket > &canpackets, int n)=0
virtual iDriver2Type type()=0
virtual int init(yarp::os::Searchable &config, bool verbose=true)=0
virtual int receive_message(vector< CanPacket > &canpackets, int howMany=MAX_READ_MSG, double TIMEOUT=1)=0
virtual bool fill(void *data, size_t &size)
bool import(const Registers ®s, WideParams *wideparams=nullptr)
void drv_sleep(double time)
#define EOCANPROT_D_CREATE_CANID(clss, orig, dest)
int getvalue(char *line, int len)
#define SPRS_STATE_ADDRESS
#define BOARD_WAITING_ACK
void drv_sleep(double time)
#define SPRS_STATE_CHECKSUM
#define SPRS_STATE_LENGTH
#define BOARD_DOWNLOADING
strain2_ampl_discretegain_t
icubCanProto_strain_saturationInfo_t saturationinfo[6]
bool applicationisrunning
void load(Gain _g, Offset _o)