How to pre-allocate an array for 6 DOF PUMA robot?

I am trying to prepare a neural network for 6 DOF robot. I have written a following program, but since I am not able to pre-allocate the array size, this program is taking too many days to run. Even after that it is not giving me the results & gets hanged in between.
Below is the program I have written - *****************************************************************************
% Constants as defined by for PUMA robot
a2 = 431.8;
a3 = -20.32;
d2 = 149.09;
d4 = 433.07;
d6 = 56.25;
% Joint Range of an angle Theta for PUMA Robot links.
T1=-160*pi/180:10*pi/180:160*pi/180;%Convert to radians.
T2=-225*pi/180:10*pi/180:45*pi/180;%Convert to radians.
T3=-45*pi/180:10*pi/180:225*pi/180;%Convert to radians.
T4=-110*pi/180:10*pi/180:70*pi/180;%Convert to radians.
T5=-100*pi/180:10*pi/180:100*pi/180;%Convert to radians.
% Index used
i=1;
j=1;
k=1;
l=1;
m=1;
n=1;
% Calculating the length of Joint range.
L1=(length(T1));
L2=(length(T2));
L3=(length(T3));
L4=(length(T4));
L5=(length(T5));
% Loop to calculate the X,Y&Z coordinates.
for i=1:1:L1
for j=1:1:L2
for k=1:1:L3
for l=1:1:L4
for m=1:1:L5
% Equations used to calculate Coordinates for PUMA robot with T Matrix
% X = COS1[d6(COS23*COS4*SIN5+SIN23*COS5)+
% (SIN23*d4)+(a3*COS23)+(a2*COS2)]-
% [SIN1(d6*SIN4*SIN5+d2)] (based on this formula.)
X= cos(T1(i))*(d6*((cos(T2(j)+T3(k))*cos(T4(l))*sin(T5(m)))+
(sin(T2(j)+T3(k))*cos(T5(m))))+(sin(T2(j)+T3(k))*d4)+
(a3*cos(T2(j)+T3(k)))+(a2*cos(T2(j))))-
sin(T1(i))*(d6*sin(T4(l))*sin(T5(m))+d2);
% Y = SIN1[d6(COS23*COS4*SIN5+SIN23*COS5)+
% (SIN23*d4)+(a3*COS23)+(a2*COS2)]+
% [COS1(d6*SIN4*SIN5+d2)] (based on this formula.)
Y= sin(T1(i))*(d6*((cos(T2(j)+T3(k))*cos(T4(l))*sin(T5(m)))+
(sin(T2(j)+T3(k))*cos(T5(m))))+(sin(T2(j)+T3(k))*d4)+
(a3*cos(T2(j)+T3(k)))+(a2*cos(T2(j))))+
cos(T1(i))*(d6*sin(T4(l))*sin(T5(m))+d2);
% Z = [d6(COS23*COS5-SIN23*COS4*SIN5)]+
(COS23*d4)-(a3*SIN23)-(a2*SIN2) (based on this formula.)
Z= d6*((cos(T2(j)+T3(k))*cos(T5(m)))-
(sin(T2(j)+T3(k))*cos(T4(l))*sin(T5.(m))))+
(cos(T2(j)+T3(k))*d4)-(a3*sin(T2(j)+T3(k)))-(a2*sin(T2(j)));
% Storing values of X, Y & Z coordinates. Also respective angle of the same.
[aa(n)]=[X];
[bb(n)]=[Y];
[cc(n)]=[Z];
tt1(n)=T1(i);
tt2(n)=T2(j);
tt3(n)=T3(k);
tt4(n)=T4(l);
tt5(n)=T5(m);
n=n+1;
end
end
end
end
end
****************************************************************************
I have Matlab 7.0 version.

13 Comments

Please format your code legibly (See {} Code toolbar icon) to have your question re-opened.
I think you missed the point. Your code is not formatted using the '{} Code' toolbar icon, but whatever.... People can read it now.
Hi Matt, Thanks for your input. Actually not very familiar with Matlab.
Matt J
Matt J on 7 Jul 2013
Edited: Matt J on 7 Jul 2013
It's not an issue of MATLAB knowledge. The formatting I'm talking about is a feature of this website. When you post a question or comment, can you see that there is a toolbar icon labelled '{} Code' above the box where you write text?
Or OP can use the sequence
' MATLAB code'
at the beginning of the code (no line break following and don't use the quotes; they're just there to highlight the two leading spaces required. Also note that the formatting applies to following paragraph, blank line terminates it.
I wish TMW would switch the default format to that for code instead of linewrap; it is, after all, supposed to be a coding forum... :(
_written a following program, but since I am not able to pre-allocate the array size, _
And why not, pray tell?
I recast your basic equations a little for readability...
% Equations to calculate Coordinates for PUMA robot with T Matrix
% X = COS1*[d6(COS23*COS4*SIN5+SIN23*COS5) +
% SIN23*d4 + a3*COS23 + a2*COS2] -
% SIN1*(d4*SIN4*SIN5+d2)
...
MATLAB code
X= cos(T1)* ...
(d6*((cos(T2+T3)*cos(T4)*sin(T5)+ (sin(T2)+T3)*cos(T5))) + ...
(sin(T2+T3)*d4) + (a3*cos(T2+T3)) + (a2*cos(T2))) - ...
sin(T1)*((d6*sin(T4)*sin(T5))+d2);
NB that in the above expression for X (in which I removed the subscripts simply to make it easier to see the variables, that the last term (last line in expression) is SIN1*(d4... in ref equation but you've written sin(T1)*((d6... in the code.
This appears a typo??? Need to check your equations very carefully it appears.
I didn't go further 'til that was resolved re: how to compute but as a bare minimum you certainly can preallocate--the length of each vector is simply prod(L1,L2,L3,...,L6)
hi dpn,
Thanks for your input, yes it was a typo error. Both for X & Y, I made that error, but I have corrected it now.
About preallocation I didnot get you, do you mean to say I have to my lenths a2,a3,d2,d4 & d6?
Jan
Jan on 10 Jul 2013
Edited: Jan on 10 Jul 2013
@Prashant: I cannot understand why you hesitate to format tzhe code of your question properly. It is such easy and fast and would improve the readability of the code substantially. With a bad formatted code the question looks like you do not care about the readers and are not really eager to get an answer.
I do not understand "I have to my lenths a2,a3,d2,d4 & d6".
@ Jan Simon - Sorry for the inconvenience, now I am able to use the CODE button properly. So it should be readable now.
Oh I missed a word in the line, I mean to say do I need to preallocat lengths? Here in my programs the variables which I used e.g. a2,a3,d2,d4 & d6 are the lengths of my matrix.
@Prashant: Thank you for formatting.
Excepting for leaving in an inordinate number of blank lines...
I thought I made the following comment before but I don't see it...
% Equations used to calculate Coordinates for PUMA robot with T Matrix
% X = COS1[d6(COS23*COS4*SIN5+SIN23*COS5)+
% (SIN23*d4)+(a3*COS23)+(a2*COS2)]-
% [SIN1(d4*SIN4*SIN5+d2)] (based on this formula.)
X= cos(T1(i))*(d6*((cos(T2(j)+T3(k))*cos(T4(l))*sin(T5(m)))+
(sin(T2(j)+T3(k))*cos(T5(m))))+(sin(T2(j)+T3(k))*d4)+
(a3*cos(T2(j)+T3(k)))+(a2*cos(T2(j))))-
sin(T1(i))*(d6*sin(T4(l))*sin(T5(m))+d2);
In comparing the definition equation to your code, the term SIN1[d4...
on the third line seems to be sin(T1*d6... in the code. But if d6 is really d4 then it would seem the COS1[d6 in the first line should be implemented as cos(T1*d4... so I'm left to conclude either the comment is wrong or the code doesn't match the comment.
Looks to me like you first need to really double-check the equations.
@dpb - Yes that was a typo error in definition equation. I have corrected now. Thanks for your comments.

Sign in to comment.

 Accepted Answer

A demonstration of how to avoid repeated calculations:
for i=1:1:L1
c1 = cos(T1(i));
c5 = sin(T1(i));
for j=1:1:L2
c7 = a2*cos(T2(j));
for k=1:1:L3
c2 = cos(T2(j)+T3(k));
c4 = sin(T2(j)+T3(k));
for l=1:1:L4
c3 = cos(T4(l)) * c2;
c6 = c5*(d6*sin(T4(l));
for m=1:1:L5
X = c1 * (d6 * (c3 * sin(T5(m)) + (c4 * cos(T5(m)))) + ...
(c4 * d4) + (a3 * c2) + c7) - c6 * sin(T5(m)) + d2);
etc.

2 Comments

@Jan - Thanks for your inputs, I will try this one.
@Jan - Hey Thanks buddy for your input, I am able to successfully run above 5 "for loops" within around 3 to 4 mins. :)
But now I am facing one more problem of Out of Memory. getting an error as below -
??? Error using ==> horzcat
Out of memory. Type HELP MEMORY for your options.
I have a matlab installed with 32 bit option. My system is 64 bit with 3 gb Ram. So could you please suggest me what I can do here?

Sign in to comment.

More Answers (1)

The missing readability of the code discourages me to read it. But I can see two problems which reduces the speed at first glance:
  1. Avoid repeated calculations, e.g. cos(T2(j)+T3(k)) is calculated several times. Using a temporary variable instead is much better.
  2. Pre-allocate the outputs. There is no efficient way if you omit this.

Categories

Find more on Commercial & Off-Highway Vehicles in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!