Match real X, Y, Z, positions with calculated positions
1 view (last 30 days)
Show older comments
Hello, I am a student and I am using a Matlab code on my prototype project. I am using IMU sensors to track a tool's position. I have a 3 columns matrix data of positions x, y, z comes from IMU sensor. I also have 3 columns excel table of the positions x, y, z of objects that I need to add another column that includes for each row a scanning of the other table that comes from the sensor and tells matched or not matched. But since it cannot match exactly it needs an approximation such as +-2cm.
0 Comments
Accepted Answer
DGM
on 13 Jan 2022
Edited: DGM
on 13 Jan 2022
This is a generalized example of finding and evaluating distances. I'm assuming here that simple euclidean distance is an appropriate metric for the task.
Dtolerance = 0.5;
% example location data
[x y z] = ndgrid(1:4);
partloc = [x(:) y(:) z(:)];
toolloc = rand(100,3)*3+1;
% find closest part, evaluate distance
D = sqrt((toolloc(:,1)-partloc(:,1).').^2 + ...
(toolloc(:,2)-partloc(:,2).').^2 + ...
(toolloc(:,3)-partloc(:,3).').^2);
[Dmin closestpart] = min(D,[],2);
iscloseenough = Dmin <= Dtolerance;
% show results
[iscloseenough closestpart]
% plot the points; draw lines between tool and closest part
plot3(partloc(:,1),partloc(:,2),partloc(:,3),'o'); hold on
plot3(toolloc(:,1),toolloc(:,2),toolloc(:,3),'*');
axis equal
grid on
for k = 1:numel(closestpart)
a = toolloc(k,:);
b = partloc(closestpart(k),:);
if iscloseenough(k)
plot3([a(1) b(1)],[a(2) b(2)],[a(3) b(3)],'g')
else
plot3([a(1) b(1)],[a(2) b(2)],[a(3) b(3)],'r:')
end
end
2 Comments
DGM
on 18 Jan 2022
That's what the example does. It checks all sensor positions (toolloc) against all object positions (partloc). It finds the closest object by euclidean distance and tests whether the closest object is within a specified euclidean distance. The rest of the code is just visualization for sake of demonstration.
If you want to test axial distance instead of euclidean distance (the valid region being a cube instead of a sphere), then you can do that too.
Dtolerance = 0.3;
% example location data
[x y z] = ndgrid(1:4);
partloc = [x(:) y(:) z(:)];
toolloc = rand(100,3)*3+1;
% find closest part, evaluate distance
D = sqrt((toolloc(:,1)-partloc(:,1).').^2 + ...
(toolloc(:,2)-partloc(:,2).').^2 + ...
(toolloc(:,3)-partloc(:,3).').^2);
[Dmin closestpart] = min(D,[],2);
% is part within a tolerance box?
iscloseenough = all([abs(toolloc(:,1)-partloc(closestpart,1))<Dtolerance, ...
abs(toolloc(:,2)-partloc(closestpart,2))<Dtolerance, ...
abs(toolloc(:,3)-partloc(closestpart,3))<Dtolerance],2);
% show results
[iscloseenough closestpart]
% plot the points; draw lines between tool and closest part
plot3(partloc(:,1),partloc(:,2),partloc(:,3),'o'); hold on;
plot3(toolloc(:,1),toolloc(:,2),toolloc(:,3),'*');
axis equal
grid on
for k = 1:numel(closestpart)
a = toolloc(k,:);
b = partloc(closestpart(k),:);
if iscloseenough(k)
plot3([a(1) b(1)],[a(2) b(2)],[a(3) b(3)],'g')
else
plot3([a(1) b(1)],[a(2) b(2)],[a(3) b(3)],'r:')
end
end
Note that Dtolerance does not need to be scalar. It can also be specified as a three-element vector for per-axis tolerance. The logical test would need to address the corresponding elements of said vector, of course.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!