Keeping axes still, while transforming objects
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Julian Büchel
am 12 Sep. 2017
Kommentiert: Walter Roberson
am 13 Jun. 2020
Hi,
in this ( https://www.youtube.com/watch?v=FrpG-KP_Tg8 ) video at the end, a transformation is applied. I have adopted the code from the video, but somehow my axes get scaled too. This is my code:
clc;
close all;
clear all;
comPort = '/dev/cu.usbmodem1421';
if(~exist('serialFlag','var'));
[fsr.s,serialFlag] = setupSerial(comPort);
end
if(~exist('h','var')|| ~ishandle(h))
h = figure(1);
end
if(~exist('text1','var'))
text1 = uicontrol('Style','text', 'String', 'X: 0 degress',...
'pos',[450 100 100 25],'parent',h);
end
if(~exist('text2','var'))
text2 = uicontrol('Style','text', 'String', 'Y: 0 degress',...
'pos',[450 75 100 25],'parent',h);
end
if(~exist('button','var'))
button = uicontrol('Style','togglebutton', 'String', 'Stop & Close Serial Port',...
'pos',[0 0 200 25],'parent',h);
end
weights=[0 700 800 900 1000 1500 2000 2500 3000 4000 4999];
%Hier noch mehr hinzufuegen
m1 = zeros(length(weights),1);
length(weights)
%Read values for each weight and assign it
for i=2:length(weights)
mbox = msgbox(['Place ' num2str(weights(i)) ' grams on FSR.']); uiwait(mbox);
m1(i)= readFSR(fsr)
while (m1(i)<m1(i-1))
m1(i)=readFSR(fsr);
end
end
m=m1.';
P1=polyfit(m,weights,2)
%250 is the max weight. 250g. Change to 20 000!
myaxes = axes('xlim',[-20 20],'ylim',[-20 20],'zlim',[0 6]);
view(3);
grid on;
axis equal;
hold on;
%Draw sphere
[xsphere, ysphere, zsphere] = sphere();
h(1) = surface(xsphere,ysphere,zsphere);
combinedobject = hgtransform('parent',myaxes);
set(h,'parent',combinedobject)
drawnow
while (get(button,'Value')==0)
[voltage]=readFSR(fsr);
mass = polyval(P1,voltage); %in g
if(mass>0)
force = mass*9.81; %in newton
set(text1,'String',['Mass: ' num2str(mass) ' g']);
set(text2,'String',['Force: ' num2str(force) ' N']);
translation = makehgtform('translate',[0 0 mass/1000]); %in kg's
set(combinedobject,'matrix',translation);
%Scale sphere
scaling = makehgtform('scale', mass/10000);
set(combinedobject,'matrix',scaling);
set(combinedobject,'matrix',translation*scaling);
drawnow
end
pause(0.1);
end
closeSerial;
1 Kommentar
Walter Roberson
am 13 Jun. 2020
http://www.matlabarduino.org/force.html is the source of the tutorial
Akzeptierte Antwort
Tim Berk
am 19 Sep. 2017
I can't reproduce this as your script won't run on its own, but I think the problem is that
axis equal
Overwrites the limits stated in
myaxes = axes('xlim',[-20 20],'ylim',[-20 20],'zlim',[0 6]);
You can solve this by reversing the order in which the axes limits and "axis equal" option are called, i.e.
myaxes = axes;
view(3);
grid on;
axis equal;
axis([-20 20 -20 20 0 6])
hold on;
Cheers,
Tim
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!