How to pre-allocate an array for 6 DOF PUMA robot?
Show older comments
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
Matt J
on 5 Jul 2013
Please format your code legibly (See {} Code toolbar icon) to have your question re-opened.
Matt J
on 6 Jul 2013
I think you missed the point. Your code is not formatted using the '{} Code' toolbar icon, but whatever.... People can read it now.
Prashant Chopade
on 7 Jul 2013
dpb
on 7 Jul 2013
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... :(
dpb
on 7 Jul 2013
_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)
Prashant Chopade
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".
Prashant Chopade
on 10 Jul 2013
Jan
on 10 Jul 2013
@Prashant: Thank you for formatting.
dpb
on 11 Jul 2013
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.
Prashant Chopade
on 16 Jul 2013
Accepted Answer
More Answers (1)
Jan
on 10 Jul 2013
0 votes
The missing readability of the code discourages me to read it. But I can see two problems which reduces the speed at first glance:
- Avoid repeated calculations, e.g. cos(T2(j)+T3(k)) is calculated several times. Using a temporary variable instead is much better.
- 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
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!