314{
315 char buff [500];
316 setMode(VOCAB_CM_POSITION);
317 goHome();
318
319 bool go_to_max=false;
320 for (unsigned int i=0; i<jointsList.size(); i++)
321 {
322 ipos->positionMove((int)jointsList[i], min[i]);
323 }
324
325 int cycle=0;
326 double start_time = yarp::os::Time::now();
327
328
329
330
331
332 yarp::os::Bottle b;
333
334 ivar->getRemoteVariable("kinematic_mj", b);
335
336 int matrix_size = matrix.cols();
337
338 matrix.eye();
339
340 int njoints [4];
341
342 for(int i=0 ; i< b.size() ; i++)
343 {
344 Bottle bv;
345 bv.fromString(b.get(i).toString());
346 njoints[i] = sqrt(bv.size());
347
348 int ele = 0;
349 if(i==0) {
350 for (int r=0; r < njoints[i]; r++)
351 {
352 for (int c=0; c < njoints[i]; c++)
353 {
354 matrix(r,c) = bv.get(ele).asFloat64();
355 ele++;
356 }
357 }
358
359 }
360 else{
361 for (int r=0; r < njoints[i]; r++)
362 {
363 for (int c=0; c < njoints[i]; c++)
364 {
365 int jntprev = 0;
366 for (int j=0; j < i; j++) jntprev += njoints[j];
367 if(!jntprev > matrix_size) matrix(r+jntprev,c+jntprev) = bv.get(ele).asFloat64();
368 ele++;
369 }
370 }
371 }
372
373 }
374
375
376
377
378
379 trasp_matrix = matrix.transposed();
380 inv_matrix = yarp::math::luinv(matrix);
381 inv_trasp_matrix = inv_matrix.transposed();
382
383 sprintf(buff,"Matrix:\n %s \n", matrix.toString().c_str());
384 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
385 sprintf(buff,"Inv matrix:\n %s \n", inv_matrix.toString().c_str());
386 ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
387
388 Bottle dataToPlot_test1;
389 Bottle dataToPlot_test2;
390 Bottle dataToPlot_test3;
391 Bottle dataToPlot_test4;
392 Bottle dataToPlot_test1rev;
393
394 bool test_data_is_valid = false;
395 bool first_time = true;
396 yarp::sig::Vector off_enc_mot; off_enc_mot.resize(jointsList.size());
397 yarp::sig::Vector off_enc_jnt; off_enc_jnt.resize(jointsList.size());
398 yarp::sig::Vector off_enc_jnt2mot; off_enc_jnt2mot.resize(jointsList.size());
399 yarp::sig::Vector off_enc_mot2jnt; off_enc_mot2jnt.resize(jointsList.size());
400 yarp::sig::Vector tmp_vector;
401 tmp_vector.resize(n_part_joints);
402
403
404
405 while (1)
406 {
407 double curr_time = yarp::os::Time::now();
408 double elapsed = curr_time - start_time;
409
410 bool ret = true;
411 ret = ienc->getEncoders(tmp_vector.data());
412 for (unsigned int i = 0; i < jointsList.size(); i++)
413 enc_jnt[i] = tmp_vector[jointsList[i]];
414
415
416 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(ret, "ienc->getEncoders returned false");
417 ret = imotenc->getMotorEncoders(tmp_vector.data()); for (unsigned int i = 0; i < jointsList.size(); i++) enc_mot[i] = tmp_vector[jointsList(i)];
418 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(ret, "imotenc->getMotorEncoder returned false");
419 ret = ienc->getEncoderSpeeds(tmp_vector.data()); for (unsigned int i = 0; i < jointsList.size(); i++) vel_jnt[i] = tmp_vector[jointsList(i)];
420 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(ret, "ienc->getEncoderSpeeds returned false");
421 ret = imotenc->getMotorEncoderSpeeds(tmp_vector.data()); for (unsigned int i = 0; i < jointsList.size(); i++) vel_mot[i] = tmp_vector[jointsList(i)];
422 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(ret, "imotenc->getMotorEncoderSpeeds returned false");
423 ret = ienc->getEncoderAccelerations(tmp_vector.data()); for (unsigned int i = 0; i < jointsList.size(); i++) acc_jnt[i] = tmp_vector[jointsList(i)];
424 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(ret, "ienc->getEncoderAccelerations returned false");
425 ret = imotenc->getMotorEncoderAccelerations(tmp_vector.data()); for (unsigned int i = 0; i < jointsList.size(); i++) acc_mot[i] = tmp_vector[jointsList(i)];
426 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(ret, "imotenc->getMotorEncoderAccelerations returned false");
427
428
429
430
431
432
433
434
435 if (first_time)
436 {
437 off_enc_jnt = enc_jnt;
438 off_enc_mot2jnt = enc_mot2jnt;
439
440 }
441
442 enc_jnt2mot = matrix * enc_jnt;
443 enc_mot2jnt = inv_matrix * (enc_mot - off_enc_mot);
444 vel_jnt2mot = matrix * vel_jnt;
445
446
447
448 for (unsigned int i = 0; i < jointsList.size(); i++) enc_jnt2mot[i] = enc_jnt2mot[i] * gearbox[i];;
449 for (unsigned int i = 0; i < jointsList.size(); i++) vel_jnt2mot[i] = vel_jnt2mot[i] * gearbox[i];
450
451 for (unsigned int i = 0; i < jointsList.size(); i++) enc_mot2jnt[i] = enc_mot2jnt[i] / gearbox[i];
452
453 bool reached = false;
454 int in_position = 0;
455 for (unsigned int i = 0; i < jointsList.size(); i++)
456 {
457 double curr_val = 0;
458 if (go_to_max == false) curr_val = min[i];
459 else curr_val = max[i];
460 if (fabs(enc_jnt[i] - curr_val) < tolerance) in_position++;
461 }
462 if (in_position == jointsList.size()) reached = true;
463
464 if (elapsed >= 20.0)
465 {
466 ROBOTTESTINGFRAMEWORK_ASSERT_ERROR("Timeout while moving joint");
467 }
468
469 if (reached)
470 {
471 sprintf(buff, "Test cycle %d/%d", cycle, cycles); ROBOTTESTINGFRAMEWORK_TEST_REPORT(buff);
472 if (go_to_max == false)
473 {
474 for (unsigned int i = 0; i < jointsList.size(); i++)
475 ipos->positionMove(jointsList[i], max[i]);
476 go_to_max = true;
477 cycle++;
478 start_time = yarp::os::Time::now();
479 }
480 else
481 {
482 for (unsigned int i = 0; i < jointsList.size(); i++)
483 ipos->positionMove(jointsList[i], min[i]);
484 go_to_max = false;
485 cycle++;
486 start_time = yarp::os::Time::now();
487 }
488 }
489
490
491 diff_enc_jnt = (enc_jnt - prev_enc_jnt) / 0.010;
492 diff_enc_mot = (enc_mot - prev_enc_mot) / 0.010;
493 diff_enc_jnt2mot = (enc_jnt2mot - prev_enc_jnt2mot) / 0.010;
494 diff_vel_jnt = (vel_jnt - prev_vel_jnt) / 0.010;
495 diff_vel_mot = (vel_mot - prev_vel_mot) / 0.010;
496 diff_vel_jnt2mot = (vel_jnt2mot - prev_vel_jnt2mot) / 0.010;
497 diff_acc_jnt = (acc_jnt - prev_acc_jnt) / 0.010;
498 diff_acc_mot = (acc_mot - prev_acc_mot) / 0.010;
499
500 prev_enc_jnt = enc_jnt;
501 prev_enc_mot = enc_mot;
502 prev_enc_jnt2mot = enc_jnt2mot;
503 prev_vel_jnt = vel_jnt;
504 prev_vel_mot = vel_mot;
505 prev_vel_jnt2mot = vel_jnt2mot;
506
507
508
509
510 if (first_time)
511 {
512 off_enc_mot = enc_mot;
513 off_enc_jnt2mot = enc_jnt2mot;
514 }
515
516 {
517
518
519 Bottle& row_test1 = dataToPlot_test1.addList();
520 Bottle& v1_test1 = row_test1.addList();
521 Bottle& v2_test1 = row_test1.addList();
522 yarp::sig::Vector v1 = enc_mot - off_enc_mot;
523 yarp::sig::Vector v2 = enc_jnt2mot - off_enc_jnt2mot;
524 v1_test1.read(v1);
525 v2_test1.read(v2);
526 }
527
528 {
529
530 Bottle& row_test2 = dataToPlot_test2.addList();
531 Bottle& v1_test2 = row_test2.addList();
532 Bottle& v2_test2 = row_test2.addList();
533 v1_test2.read(vel_mot);
534 v2_test2.read(vel_jnt2mot);
535 }
536
537 {
538
539 if (first_time == false)
540 {
541 Bottle& row_test3 = dataToPlot_test3.addList();
542 Bottle& v1_test3 = row_test3.addList();
543 Bottle& v2_test3 = row_test3.addList();
544 v1_test3.read(vel_jnt);
545 v2_test3.read(diff_enc_jnt);
546 }
547 }
548
549 {
550
551 if (first_time == false)
552 {
553 Bottle& row_test4 = dataToPlot_test4.addList();
554 Bottle& v1_test4 = row_test4.addList();
555 Bottle& v2_test4 = row_test4.addList();
556 v1_test4.read(vel_mot);
557 v2_test4.read(diff_enc_mot);
558 }
559 }
560
561 {
562
563 Bottle& row_test1 = dataToPlot_test1rev.addList();
564 Bottle& v1_test1 = row_test1.addList();
565 Bottle& v2_test1 = row_test1.addList();
566 yarp::sig::Vector v1 = enc_jnt;
567 yarp::sig::Vector v2 = enc_mot2jnt + off_enc_jnt;
568 v1_test1.read(v1);
569 v2_test1.read(v2);
570 }
571
572
573 first_time = false;
574
575
576 if (cycle>=cycles) break;
577 }
578
579 goHome();
580
581 yarp::os::ResourceFinder rf;
582 rf.setDefaultContext("scripts");
583
584 string partfilename = partName+".txt";
585 string testfilename = "encConsis_";
586 string filename1 = testfilename + "jointPos_MotorPos_" + partfilename;
587 saveToFile(filename1,dataToPlot_test1);
588 string filename2 = testfilename + "jointVel_motorVel_" + partfilename;
589 saveToFile(filename2,dataToPlot_test2);
590 string filename3 = testfilename + "joint_derivedVel_vel_" + partfilename;
591 saveToFile(filename3,dataToPlot_test3);
592 string filename4 = testfilename + "motor_derivedVel_vel_" + partfilename;
593 saveToFile(filename4,dataToPlot_test4);
594
595 string filename1rev = testfilename + "jointPos_MotorPos_reversed_" + partfilename;
596 saveToFile(filename1rev,dataToPlot_test1rev);
597
598
599 std::string octaveFile = rf.findFile("encoderConsistencyPlotAll.m");
600 if(octaveFile.size() == 0)
601 {
602 yError()<<"Cannot find file encoderConsistencyPlotAll.m";
603 return;
604 }
605
606
607 std::string octaveCommand= "octave --path "+ getPath(octaveFile);
608 stringstream ss;
609 ss << jointsList.size();
610 string str = ss.str();
611 octaveCommand+= " -q --eval \"encoderConsistencyPlotAll('" +partName +"'," + str +")\" --persist";
612
613 if(plot_enabled)
614 {
615 int ret = system (octaveCommand.c_str());
616 }
617 else
618 {
619 yInfo() << "Test has collected all data. You need to plot data to check is test is passed";
620 yInfo() << "Please run following command to plot data.";
621 yInfo() << octaveCommand;
622 yInfo() << "To exit from Octave application please type 'exit' command.";
623 }
624
625}