Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MotorEconders-consistency - retrieve the coupling matrix at runtime #60

Merged
merged 12 commits into from
Feb 21, 2022
96 changes: 81 additions & 15 deletions src/motorEncoders-consistency/motorEncodersConsistency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <fstream>
#include "motorEncodersConsistency.h"
#include <iostream>
#include <yarp/dev/IRemoteVariables.h>

using namespace std;

Expand Down Expand Up @@ -124,20 +125,24 @@ bool OpticalEncodersConsistency::setup(yarp::os::Property& property) {
{
matrix.resize(matrix_size,matrix_size);
matrix.eye();
Bottle* matrixBottle = property.find("matrix").asList();
if (matrixBottle!= NULL && matrixBottle->size() == (matrix_size*matrix_size) )
{
for (int i=0; i< (matrix_size*matrix_size); i++)
{
matrix.data()[i]=matrixBottle->get(i).asFloat64();
}
}
else
{
char buff [500];
sprintf (buff, "invalid number of elements of parameter matrix %d!=%d", matrixBottle->size() , (matrix_size*matrix_size));
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(buff);
}

// The couplig matrix is retrived run-time by the IRemoteVariable interface accessing the jinimatic_mj variable
//
// Bottle* matrixBottle = property.find("matrix").asList();

// if (matrixBottle!= NULL && matrixBottle->size() == (matrix_size*matrix_size) )
// {
// for (int i=0; i< (matrix_size*matrix_size); i++)
// {
// matrix.data()[i]=matrixBottle->get(i).asFloat64();
// }
// }
// else
// {
// char buff [500];
// sprintf (buff, "invalid number of elements of parameter matrix %d!=%d", matrixBottle->size() , (matrix_size*matrix_size));
// ROBOTTESTINGFRAMEWORK_ASSERT_ERROR(buff);
// }
}
else
{
Expand All @@ -161,6 +166,8 @@ bool OpticalEncodersConsistency::setup(yarp::os::Property& property) {
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(iimd),"Unable to open interaction mode interface");
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(imotenc),"Unable to open motor encoders interface");
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(imot),"Unable to open motor interface");
ROBOTTESTINGFRAMEWORK_ASSERT_ERROR_IF_FALSE(dd->view(ivar),"Unable to open motor interface");


if (!ienc->getAxes(&n_part_joints))
{
Expand Down Expand Up @@ -202,6 +209,7 @@ bool OpticalEncodersConsistency::setup(yarp::os::Property& property) {
gearbox[i]=t;
}


return true;
}

Expand Down Expand Up @@ -317,6 +325,59 @@ void OpticalEncodersConsistency::run()
int cycle=0;
double start_time = yarp::os::Time::now();

//****************************************************************************************
//Retrieving coupling matrix using IRemoteVariable
//****************************************************************************************

yarp::os::Bottle b;

ivar->getRemoteVariable("kinematic_mj", b);
pattacini marked this conversation as resolved.
Show resolved Hide resolved

auto bb = b.get(1);
pattacini marked this conversation as resolved.
Show resolved Hide resolved

int matrix_size = matrix.cols();

matrix.eye();

int njoints [4];

for(int i=0 ; i< b.size() ; i++)
{
Bottle bv;
bv.fromString(b.get(i).toString());
njoints[i] = sqrt(bv.size());

int ele = 0;
if(i==0) {
for (int r=0; r < njoints[i]; r++)
{
for (int c=0; c < njoints[i]; c++)
{
matrix(r,c) = bv.get(ele).asFloat64();
ele++;
}
}

}
else{
for (int r=0; r < njoints[i]; r++)
{
for (int c=0; c < njoints[i]; c++)
{
int jntprev = 0;
for (int j=0; j < i; j++) jntprev += njoints[j];
if(!jntprev > matrix_size) matrix(r+jntprev,c+jntprev) = bv.get(ele).asFloat64();
ele++;
}
}
}

}

// yDebug() << "MATRIX J2M : \n" << matrix.toString();

// **************************************************************************************

trasp_matrix = matrix.transposed();
inv_matrix = yarp::math::luinv(matrix);
inv_trasp_matrix = inv_matrix.transposed();
Expand All @@ -341,6 +402,8 @@ void OpticalEncodersConsistency::run()
yarp::sig::Vector tmp_vector;
tmp_vector.resize(n_part_joints);



while (1)
{
double curr_time = yarp::os::Time::now();
Expand Down Expand Up @@ -375,13 +438,16 @@ void OpticalEncodersConsistency::run()
{
off_enc_jnt = enc_jnt;
off_enc_mot2jnt = enc_mot2jnt;

}

enc_jnt2mot = matrix * enc_jnt;
enc_mot2jnt = inv_matrix * (enc_mot - off_enc_mot);
vel_jnt2mot = matrix * vel_jnt;
//acc_jnt2mot = matrix * acc_jnt;
for (unsigned int i = 0; i < jointsList.size(); i++) enc_jnt2mot[i] = enc_jnt2mot[i] * gearbox[i];


for (unsigned int i = 0; i < jointsList.size(); i++) enc_jnt2mot[i] = enc_jnt2mot[i] * gearbox[i];;
for (unsigned int i = 0; i < jointsList.size(); i++) vel_jnt2mot[i] = vel_jnt2mot[i] * gearbox[i];
//for (unsigned int i = 0; i < jointsList.size(); i++) acc_jnt2mot[i] = acc_jnt2mot[i] * gearbox[i];
for (unsigned int i = 0; i < jointsList.size(); i++) enc_mot2jnt[i] = enc_mot2jnt[i] / gearbox[i];
Expand Down
6 changes: 4 additions & 2 deletions src/motorEncoders-consistency/motorEncodersConsistency.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ class OpticalEncodersConsistency : public yarp::robottestingframework::TestCase

int n_part_joints;
int cycles;

yarp::dev::PolyDriver *dd;
yarp::dev::IPositionControl *ipos;
yarp::dev::IControlMode *icmd;
yarp::dev::IPositionControl *ipos;
yarp::dev::IControlMode *icmd;
yarp::dev::IInteractionMode *iimd;
yarp::dev::IEncoders *ienc;
yarp::dev::IMotorEncoders *imotenc;
yarp::dev::IMotor *imot;
yarp::dev::IRemoteVariables *ivar;

yarp::sig::Vector zero_vector;
yarp::sig::Vector enc_jnt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ robot ${robotname}
part left_leg
joints (0 1 2 3 4 5)
home (0 0 0 0 0 0)
speed (20 20 20 20)
speed (40 40 40 40)
max (15 15 15 -15 15 -15)
min (0 0 0 0 0 0)
cycles 10
Expand Down
15 changes: 2 additions & 13 deletions suites/encoders-icub.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@
<description> Testing encoders</description>
<environment>--robotname icub</environment>

<test type="dll" param="--from optical_encoders_drift_left_arm.ini"> OpticalEncodersDrift </test>
<test type="dll" param="--from optical_encoders_drift_right_arm.ini"> OpticalEncodersDrift </test>
<test type="dll" param="--from optical_encoders_drift_left_leg.ini"> OpticalEncodersDrift </test>
<test type="dll" param="--from optical_encoders_drift_right_leg.ini"> OpticalEncodersDrift </test>
<test type="dll" param="--from optical_encoders_drift_torso.ini"> OpticalEncodersDrift </test>
pattacini marked this conversation as resolved.
Show resolved Hide resolved

<test type="dll" param="--from motorEncoderConsistency_left_arm.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from motorEncoderConsistency_right_arm.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from motorEncoderConsistency_left_leg.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from motorEncoderConsistency_right_leg.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from motorEncoderConsistency_torso.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from motorEncodersConsistency_face.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from motorEncodersConsistency_head.ini"> MotorEncodersConsistency </test>

<test type="dll" param="--from contexts/icub/motorEncoderConsistency_left_leg.ini"> MotorEncodersConsistency </test>
</suite>
7 changes: 4 additions & 3 deletions suites/encoders-icubSim.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
<environment>--robotname icubSim</environment>
<fixture param="--fixture icubsim-fixture.xml"> yarpmanager </fixture>

<test type="dll" param="--from optical_encoders_consistency_left_arm.ini"> MotorEncodersConsistency </test>

<test type="dll" param="--from contexts/icubSim/optical_encoders_consistency_left_arm.ini"> MotorEncodersConsistency </test>
<!-- <test type="dll" param="--from optical_encoders_consistency_left_arm.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from optical_encoders_consistency_right_arm.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from optical_encoders_consistency_left_leg.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from optical_encoders_consistency_right_leg.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from optical_encoders_consistency_torso.ini"> MotorEncodersConsistency </test>
<test type="dll" param="--from optical_encoders_consistency_torso.ini"> MotorEncodersConsistency </test> -->
</suite>