Page 1 of 1

Vector Matrix computation on MATLAB

Posted: Tue Oct 24, 2017 4:16 pm
by hkimpara
Hello,

We are trying to transfer controller from C++ to MATLAB.
Then we realized subtract or multiplying of vector or matrix were not working on MATLAB.

For example, we'd like to calculate controlLaw based on feedback gains of kp and kv. The issue was vector matrix computation for controlLaw.
On the MATLAB, we cannot achieve this equation: controlLaw = -1. * KP*(q - q_des) - KV*(dq - dq_des);

Do you think there are any other better ways to do that on MATLAB? Or is it benefit of using C++ programming?

original C++ programming

Code: Select all

 //definition variables
		double q1, q2, dq1, dq2, q1_des, q2_des, dq1_des, dq2_des;

		Vec2 controlLaw;
		Vec2 q(q1, q2);
		Vec2 dq(dq1, dq2);
		Vec2 q_des(q1_des, q2_des);
		Vec2 dq_des(dq1_des, dq2_des);

		Mat22 KP, KV;

 // inputs variables ...  (SKIP HERE)
 
 // Vector Matrix computation  (THIS IS OUR POINT)
		controlLaw = -KP*(q - q_des) - KV*(dq - dq_des);
MATLAB code:

Code: Select all

     q1 = joint_1.getValue(s);    dq1 = joint_1.getSpeedValue(s);      q2 = joint_2.getValue(s);    dq2 = joint_2.getSpeedValue(s);
     q1_des =  80. * pi() / 180.;    dq1_des =  0.0 * pi() / 180.;     q2_des = -80. * pi() / 180.;    dq2_des =  0.0 * pi() / 180.;
    
    controlLaw = Vector( 2, 0.0 );
    q = Vector( 2, 0.0 );
    dq = Vector( 2, 0.0 );
    q_des = Vector( 2, 0.0 );
    dq_des = Vector( 2, 0.0 );
    
    KP = Matrix( 2, 2, 0.0 ); KV = Matrix( 2, 2, 0.0 );
    KP.set( 0, 0, kp ); KP.set( 1, 1, kp );
    KV.set( 0, 0, kv ); KV.set( 1, 1, kv );
    
    controlLaw = -1. * KP*(q - q_des) - KV*(dq - dq_des);
    

Thank you,
Hide

Re: Vector Matrix computation on MATLAB

Posted: Tue Oct 24, 2017 4:33 pm
by maz
Hi!

Sorry, why would this equation not work in matlab?:
-1 * KP * (q - q_des) - KV * (dq - dq_des)?

What's the dimensions of q, q_des, dq, dq_des, KP and KV?

Re: Vector Matrix computation on MATLAB

Posted: Tue Oct 24, 2017 4:34 pm
by tkuchida
I don't think you can perform arithmetic like that in MATLAB with SimTK data types because OpenSim doesn't overload MATLAB operators to work on SimTK data types like Vector and Matrix. I suggest reading the values from the SimTK objects into MATLAB vectors and matrices, performing calculations using standard MATLAB syntax, then stuffing the results back into a SimTK type for passing to OpenSim. James has written utility functions that might help (see https://github.com/opensim-org/opensim- ... /Utilities).

Re: Vector Matrix computation on MATLAB

Posted: Tue Oct 24, 2017 4:47 pm
by hkimpara
Hi Tom,

You are right. This way was what we discussed here.
The suggested method was only good for Vec3. So we will expand it to other size vector or matrix.

I think this question reaches conclusion. It's good to be locked.


Hi Mazen,

The variables of q, q_des, dq, dq_des, KP and KV were defined with SimBody vector or matrix. Therefore, it's different from MATLAB's regular vector or matrix. I think we may need to transfer element by element.


Thank you,
Hide