Matlab inverse kinematics for 6 DOF arm robot - matlab

i want to specify limit between two angels for every joint how i can do that
i tried to make conition but i don't know how to do it
L(1)= Link('d', 0 , 'a', 39.08, 'alpha', pi/2);
L(2)= Link('d', 53.72, 'a', 259.6, 'alpha', 0);
L(3)= Link('d', 0 , 'a', 0, 'alpha', -pi/2);
L(4)= Link('d', 138.3 , 'a', 0, 'alpha', pi/2);
L(5)= Link('d', 0 , 'a', 52.6, 'alpha', -pi/2);
L(6)= Link('d', 0 , 'a', 97.9, 'alpha', 0);
Arm= SerialLink(L, 'name', 'Inmoov');
T=SE3(200,300,100)*SE3.Rx(90);
q = Arm.ikine(T, [0 0 0 0 0], 'mask', [1 1 1 1 1 1]);
Arm.teach(q);

Related

How to print labels and column names for Confusion Matrix?

I get the confusion matrix but since my actual data set has lot of classification categories, it's difficult to understand.
Example -
>>> from sklearn.metrics import confusion_matrix
>>> y_test
['a', 'a', 'b', 'c', 'd', 'd', 'e', 'a', 'c']
>>> y_pred
['b', 'a', 'b', 'c', 'a', 'd', 'e', 'a', 'c']
>>>
>>>
>>> confusion_matrix(y_test, y_pred)
array([[2, 1, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 2, 0, 0],
[1, 0, 0, 1, 0],
[0, 0, 0, 0, 1]], dtype=int64)
But how to print the labels/column names for better understanding?
I even tried this -
>>> pd.factorize(y_test)
(array([0, 0, 1, 2, 3, 3, 4, 0, 2], dtype=int64), array(['a', 'b', 'c', 'd', 'e'], dtype=object))
>>> pd.factorize(y_pred)
(array([0, 1, 0, 2, 1, 3, 4, 1, 2], dtype=int64), array(['b', 'a', 'c', 'd', 'e'], dtype=object))
Any help please?
Try something like this:
from sklearn.metrics import confusion_matrix
import pandas as pd
import numpy as np
y_test = ['a', 'a', 'b', 'c', 'd', 'd', 'e', 'a', 'c']
y_pred = ['b', 'a', 'b', 'c', 'a', 'd', 'e', 'a', 'c']
labels = np.unique(y_test)
a = confusion_matrix(y_test, y_pred, labels=labels)
pd.DataFrame(a, index=labels, columns=labels)
Output:
a b c d e
a 2 1 0 0 0
b 0 1 0 0 0
c 0 0 2 0 0
d 1 0 0 1 0
e 0 0 0 0 1

How do I extract specific rows from a PDL matrix?

Suppose I have:
$a = [
[1, 0, 1]
[0, 1, 0]
[0, 1, 1]
]
and I want to extract all rows where $row[2] == 1. My resulting piddle would look like:
$b = [
[1, 0, 1]
[0, 1, 1]
]
Is this possible with PDL?
You need to use which to generate a list of indexes of your matrix which have a value of 1 in the third column
which($aa->index(2) == 1)
and pass this to dice_axis, which will select the rows with the given indexes. Axis 0 is the columns and axis 1 is the rows, so the code looks like this
use strict;
use warnings 'all';
use PDL;
my $aa = pdl <<__END_PDL__;
[
[1, 0, 1]
[0, 1, 0]
[0, 1, 1]
]
__END_PDL__
my $result = $aa->dice_axis(1, which($aa->index(2) == 1));
print $result;
output
[
[1 0 1]
[0 1 1]
]
I'm new to PDL, but it seems like you can use which result as a mask.
You need to transpose original variable first, then transpose it back after using slice.
pdl> $a = pdl [[1, 0, 1], [0, 1, 0], [0, 1, 1]]
pdl> p which($a(2) == 1)
[0 2]
pdl> p $a->transpose
[
[1 0 0]
[0 1 1]
[1 0 1]
]
pdl> p $a->transpose->slice(which($a(2) == 1))->transpose
[
[1 0 1]
[0 1 1]
]

The same function behaves differently, why?

I have this function below which works perfecttly as I need:
function [pointsQRS, pointsP, pointsT] = VCG (pointsQRS,pointsP,pointsT)
global ax1 ax2 h
figure('Name','Vektorkardiogram','NumberTitle','off','Color',[0.8 0.8 0.8])
%% first axes
ax1=subplot(1,2,1);
set(ax1,'Position',[0.1,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([0 10])
plotCurve
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% second axes
ax2=subplot(1,2,2);
set(ax2,'Position',[0.6,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([10 10])
plotCurve
function plotCurve
for i=2:size(pointsQRS,1)
if mod(i,2)==0
QRS=plot3(pointsQRS([i-1:i],1),pointsQRS([i-
1:i],2),pointsQRS([i-1:i],3),'-g','LineWidth',1);
else
plot3(pointsQRS([i-1:i],1),pointsQRS([i-
1:i],2),pointsQRS([i-1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'-r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
for i=2:size(pointsP,1)
if mod(i,2)==0
P=plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-
1:i],3),'-b','LineWidth',1);
else
plot3(pointsP([i-1:i],1),pointsP([i-1:i],2),pointsP([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
xlabel('Vx');ylabel('Vy');zlabel('Vz');
end
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% Slider Rotace
S = uicontrol('Style','slider',...
'Position',[10 10 300 20],...
'Max',180,...
'Min',-180,...
'Value',0,...
'SliderStep',[1/360 1/360]);
LS=addlistener(S,'ContinuousValueChange',#slider_callback);
set(S,'UserData',LS)
end
function slider_callback(hObject,eventData)
global ax1 ax2
val = get(hObject,'value');
view(ax1,[val,10])
view(ax2,[val+10,10])
end
But when I changed the code, simplified, because in other function I need only pointsT it gives me the error as in the picture.
The simplified code is:
function pointsT = VCG_T (pointsT)
global ax1_T ax2_T h_T
figure('Name','Vektorkardiogram','NumberTitle','off','Color',[0.8 0.8 0.8])
%% first axes
ax1_T=subplot(1,2,1);
set(ax1_T,'Position',[0.1,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([0 10])
plotCurve
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% second axes
ax2_T=subplot(1,2,2);
set(ax2_T,'Position',[0.6,0.2,0.3,0.7])
title('Vektorkardiogram')
hold on
grid on
axis vis3d
view([10 10])
plotCurve
for i=2:size(pointsT,1)
if mod(i,2)==0
T=plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'-r','LineWidth',1);
else
plot3(pointsT([i-1:i],1),pointsT([i-1:i],2),pointsT([i-
1:i],3),'Color',[0 0 0],'LineWidth',1);
end
end
mArrow3([1.5 2 -1],[-0.5, 2, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, -0.5, -1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
mArrow3([1.5 2 -1],[1.5, 2, 1], 'stemWidth',
0.005,'color','red','facealpha',0.3);
text(-0.5, 2, -1, 'Vx','FontSize',12);
text(1.5, -0.5, -1, 'Vy','FontSize',12);
text(1.5, 2, 1, 'Vz','FontSize',12);
%% Slider Rotace
S = uicontrol('Style','slider',...
'Position',[10 10 300 20],...
'Max',180,...
'Min',-180,...
'Value',0,...
'SliderStep',[1/360 1/360]);
LS=addlistener(S,'ContinuousValueChange',#slider_callback);
set(S,'UserData',LS)
end
function slider_callback(hObject,eventData)
global ax1_T ax2_T
val = get(hObject,'value');
view(ax1_T,[val,10])
view(ax2_T,[val+10,10])
end
The picture of the error:
I have literally no idea why the simplified code doesn't work, probably I'm overlooking some thing.
Could you please give me a hint?
New problem below:
You have removed the line function plotCurve. This is an important one since it defines a local function that the main function VCG calls. In your reduced example this function does not exist anymore since you removed its header. That's why you see this error.
Just put it back (before the first loop), then it should work.

Multiple Array pattern matching

Let's say i have an array:
A = [0, -2, 0, 0, -3, 0, -1, 0];
And I want to see if it can fit any of the patterns below:
B = [1, 1, 1 , 0, 0 , 0 , 0, 0,];
C= [1, 1, 0 , 1, 0 , 0 , 0, 0,]
D= [0, 1, 0 , 1, 0 , 1 , 0, 0];
Where 1 means that the number is unique and 0 means that the number remains the same until another 1 is met but is different from the number before.. Here are some examples:
[-3, -2, -1, 0, 0, 0, 0, 0]; --- A matches B.
[-3, -2, -1, -1, 0, 0, 0, 0]; -- This matches C
[-3, -3, 3, 3, -2, -2, 0, 0]; -- This matches D
Is there any Matlab function for this or must I think up my own way? Any advice, I am very new to Matlab.
There is something wrong about your rule, you seem to treat 0 as a special case (i.e. it is not a number or something).
you could just do something like this:
A=[-3, -2, -1, -1, 0, 0, 0, 0];
[ia ib] = unique(A);
R = zeros(1,8);
R(ib) = 1
>> R =
1 1 0 1 0 0 0 1
and match this, this is assuming that you treat 0 as a number just as you state in the rule.
If you wanted 0 to be special case, you need to :
A=[-3, -2, -1, -1, 0, 0, 0, 0];
[ia ib] = unique(A);
ib(ia==0)=[];
R = zeros(1,8);
R(ib) = 1
>> R =
1 1 0 1 0 0 0 0
and simply match this vector to your B,C,D etc. The second method matches your desired answer, but does not match the rule you state. The first method matches the rule you state but not your desired output.
=============EDIT============
I am on 2010b and some time along the time line, the way unique works changed, you now need to add legacy if you are using any version above 2012b.
Sorry I forgot to mention this:
just change it to :
[ia ib] = unique(A,'legacy');
and it should work fine.

Is it possible to change the inequality behaviour of interp1 when using 'previous' or 'next'

Consider as examples:
interp1([0, 1], [2, 3], 0 , 'previous')
interp1([0, 1], [2, 3], 0 , 'next')
which produces
ans =
2
ans =
2
That is, in each respective case it finds the value in [0, 1] closest to and not exceeding 0 (respectively closest to and not below 0), then returns the corresponding value of [2,3]. I would like to change the second condition to "closest to and above 0", that is, it should return the same results as:
interp1([0, 1], [2, 3], 0.1 , 'previous')
interp1([0, 1], [2, 3], 0.1 , 'next')
which gives
ans =
2
ans =
3
In this case this works as 0.1 is a value in between [0, 1].