Vector Matrix computation on MATLAB

The Question and Answer and Support Forum for the 2017 Fall Virtual Workshop.
Locked
User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Vector Matrix computation on MATLAB

Post by Hide Kimpara » Tue Oct 24, 2017 4:16 pm

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

User avatar
Mazen Al Borno
Posts: 3
Joined: Mon Mar 14, 2016 6:20 pm

Re: Vector Matrix computation on MATLAB

Post by Mazen Al Borno » Tue Oct 24, 2017 4:33 pm

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?

User avatar
Thomas Uchida
Posts: 1792
Joined: Wed May 16, 2012 11:40 am

Re: Vector Matrix computation on MATLAB

Post by Thomas Uchida » Tue Oct 24, 2017 4:34 pm

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).

User avatar
Hide Kimpara
Posts: 135
Joined: Mon Sep 19, 2016 5:12 am

Re: Vector Matrix computation on MATLAB

Post by Hide Kimpara » Tue Oct 24, 2017 4:47 pm

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

Locked