populating matrices

This is a discussion on populating matrices within the C Programming forums, part of the General Programming Boards category; hi all im am having trouble defining my matrices i have defined them as follows Code: float Tx[4][4], Ty[4][4], Tz ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    populating matrices

    hi all

    im am having trouble defining my matrices i have defined them as follows

    Code:
    float Tx[4][4], Ty[4][4], Tz [4][4];
    float Rx[4][4], Ry[4][4], Rz [4][4];
    
    float d, theta;
    then i have populated d and theta with variable data and try and do the following to populate matricies

    Code:
    Tx[][]= { {1, 0, 0,d}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} } ;
    Rx[][]= { {1, 0, 0, 0}, {0, cos(theta), -sin(thera), 0}, {0, sin(theta), cos(theta), 0}, {0, 0, 0, 1} };
    and i keep getting lots of syntax errors. is this posible or do i have to it element wise?

    many thanks in advance.

    jamie

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    here is the full code it might make what i said above make a little more sence.

    Code:
    int i;
    float d, theta;
    
    /*----- translational motion matices----*/
    float Tx[4][4], Ty[4][4], Tz[4][4];
    
    /*----- Rotational motion matices----*/
    float Rx[4][4], Ry[4][4], Rz[4][4];
    
    float result[4][4];
    
    	for(i=1;i<index+1;i++){
    
    		struct Joint *create=&Joints[i]; /*local pointer to make addressing quicker*/
    
    		theta=(create->position)*PI/180;
    		
    
    		struct Section *temp=&Sections[i];
    		d=temp->length;
    		
    
    
    		/*-------------------joint actuation------*/
    		if(create->type=='r'){
    			if(create->axis=='x'){
    				Rx = {  {1, 0, 0, 0}, {0, cos(theta), -sin(theta), 0}, {0, sin(theta), cos(theta), 0}, {0, 0, 0, 1}  };
    				mult_matrices(Rx, result, result);
    			}
    			if(create->axis=='y'){
    				Ry[][4] = {  {cos(theta), 0, sin(theta), 0}, {0, 1, 0, 0}, {-sin(theta), 0, cos(theta), 0}, {0, 0, 0, 1} };
    				mult_matrices(Ry, result, result);
    			}
    			if(create->axis=='z'){
    				Rz[][4] = {  {cos(theta), -sin(theta), 0, 0}, {sin(theta), cos(theta), 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} };
    				printf("......  %f  ....\n",Rz[1][1]);
    				 mult_matrices(Rz, result, result);
    			}
    		}
    
    		if(create->type=='t'){
    			if(create->axis=='x'){
    				Tx[4][4] = {  {1, 0, 0, d}, {0, 1, 0, 0}, {0, 0, 1,0}, {0, 0, 0, 1}  };
    				mult_matrices(Tx, result, result);	 
    			}
    			if(create->axis=='y'){
    				Ty[4][4] = {  {1, 0, 0, 0}, {0, 1, 0, d }, {0, 0, 1,0}, {0, 0, 0, 1}  };
    				mult_matrices(Ty, result, result);
    			}
    			if(create->axis=='z'){
    				Tz[4][4] = {  {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, d}, {0, 0, 0, 1}  };
    				mult_matrices(Tz, result, result);
    			}
    		}
    		
    		/*----------------section translation-------*/
    			 mult_matrices(Tz, result, result);
    
    		/*----------Angular offset to next joint-------*/ 
    	
    			 if (i<3){
    				 struct Joint *create2=&Joints[i+1];
    				
    				 if(create2->axis='x'){
    						
    		
    			
    				 }
    			 }
    
    
    
    
    	}
    /*multiplication of all the matricies*/
    
    
    }
    it is part of a much bigger robotics simulation. the aim being to try and transfer between one cordinate frame and another, i think the problem is just the way i am trying to populate the matrices that is the problem. i have been trying to read about the subject but everyone seems to populate them when they define them which i can not do as d and theta change in real time.

    thanks again

    jamie

  3. #3
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Try doing it this way
    Code:
       double d = 0.5;
       double theta = 45.0;
    
       double Tx[4][4] = { 
          1.0, 0.0, 0,d, 
          0.0, 1.0, 0.0, 0.0, 
          0.0, 0.0, 1.0, 0.0, 
          0.0, 0.0, 0.0, 1.0
       };
    
       double Rx[4][4]= { 
          1.0, 0.0, 0.0, 0.0, 
          0.0, cos(theta), -sin(theta), 0.0, 
          0.0, sin(theta), cos(theta), 0.0, 
          0.0, 0.0, 0.0, 1.0 
       };
    This should get rid of your compile errors and any warnings as well.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    yeah thats how i was doing it befor, but theta and d change as the program runs as they are fetched from another structure and using this method the values in the matrix dont get updated.

    thankyou for your time

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    ok thanks Kcpilot i have solved it now i think heres how if anyone is intersted


    Code:
    void Robot::Posiition(int index){
    
    /*--------------------------------------forward kinematics---------------------------------------------------------*/
    
    int i;
    float d=9, theta=1;
    
    /*----- translational motion matices----*/
    double Tx[4][4] =	{	{1, 0, 0, d},
    			{0, 1, 0, 0},
    			{0, 0, 1, 0},
    			{0, 0, 0, 1}  
    		};
    double Ty[4][4] =	{	{1, 0, 0, 0},
    			{0, 1, 0, d},
    			{0, 0, 1, 0},
    			{0, 0, 0, 1}  
    		};
    double Tz[4][4] ={	{1, 0, 0, 0}, 
    		{0, 1, 0, 0}, 
    		{0, 0, 1, d}, 
    		{0, 0, 0, 1}  
    	            };
    
    /*----- Rotational motion matices----*/
    double Rx[4][4] =	{   {1, 0,			0,			 0},
    		     {0, cos(theta),                    -sin(theta),                               0},
    		     {0, sin(theta),                      cos(theta),	                 0},
    		     {0, 0,			0,			 1} 
    		};
    double Ry[4][4]=	{  {cos(theta), 0, sin(theta), 0},
    		    {0,              1,   0,              0},
    		    {-sin(theta),0, cos(theta), 0}, 
    		    {0,              0,	0,           1} 
    		};
     double Rz[4][4] =	{  {cos(theta), -sin(theta), 0, 0},
    		    {sin(theta), cos(theta),  0, 0},
    		    {0,	      0,	          1,  0}, 
    		    {0,	      0,	          0,  1} 
    	                 };
    
     double result[4][4] = {	{1, 0, 0, 0},	//Initalised to identity maxtix equivalant to 1
    			{0, 1, 0, 0},
    			{0, 0, 1, 0},
    			{0, 0, 0, 1},	 
    		};
    
    	for(i=1;i<index+1;i++){
    		struct Joint *create=&Joints[i]; /*local pointer to make addressing quicker*/
    
    		theta=(create->position)*PI/180;
    
    		struct Section *temp=&Sections[i];
    		d=temp->length;
    		
    
    
    		/*-----------------------------------Joint Actuation-----------------------------------*/
    
    		/*---Rotational Joints---*/
    		if(create->type=='r'){
    			if(create->axis=='x'){
    				Rx[1][1] = cos(theta);
    				Rx[1][2] = -sin(theta);
    				Rx[2][1] = sin(theta);
    				Rx[2][2] = cos(theta);
    
    				mult_matrices(Rx, result, result);
    			
    			}
    			if(create->axis=='y'){
    
    				Ry[0][0] = cos(theta);
    				Ry[0][2] = sin(theta);
    				Ry[2][0] = -sin(theta);
    				Ry[2][2] = cos(theta);
    
    				mult_matrices(Ry, result, result);
    			
    			}
    			if(create->axis=='z'){
    				
    				Rz[0][0] = cos(theta);
    				Rz[0][1] = -sin(theta);
    				Rz[1][0] = sin(theta);
    				Rz[1][1] = cos(theta);
    				mult_matrices(Rz, result, result);
    			 
    			}
    		/*---section Length translation to next joint-------*/
    
    			Tz[2][3] = d;
    			mult_matrices(Tz, result, result);
    		}
    
    		/*---Translational Joints---*/
    		if(create->type=='t'){
    			if(create->axis=='x'){
    
    				Tx[0][3]=d;
    				mult_matrices(Tx, result, result);
    			
    			}
    			if(create->axis=='y'){
    			
    				Ty[1][3]=d;
    				mult_matrices(Ty, result, result);
    			
    			}
    			if(create->axis=='z'){
    				
    				Tz[2][3] = d;
    				mult_matrices(Tz, result, result);
    			
    			}
    		}
    		
    		
    
    		/*----------Angular offset to next joint-------*/ 
    	
    		if (i<3){
    			struct Joint *create2=&Joints[i+1];
    				
    			if(create2->axis='x'){
    					
    			
    			}
    		}
    
    	}
    
    }
    appoliges for the indenting it all got changed when i copyed it in i have tried to tidy it up as best as possible

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 06:20 PM
  2. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 09:51 PM
  3. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 09:21 AM
  4. Multiplying matrices
    By Star Lancer in forum C++ Programming
    Replies: 7
    Last Post: 05-22-2003, 07:07 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 09:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21