Read continuous reading from Potentiometer and plot graph issues

2 visualizaciones (últimos 30 días)
aliff saad
aliff saad el 28 de Oct. de 2020
Comentada: aliff saad el 28 de Oct. de 2020
Hi everyone, I have an issue that i still can't figure it out and really need helps from experts. I try to read continuous reading from 2 potentiometers and plotted it in 1 figure. i tried to use animatedplot, however, i still don't find the best solutions for it. Thus, i stick to this easy (for me) but problematic code as below. The problem is, it runs very slow and after run about few minutes, the Matlab become crash. It give me "Matlab experienced unexpected error" for several times already and i think the code might be the cause. Can someone help and guide me,please?
clear all
clc
opengl('save', 'software');
a = arduino('COM3','Uno','Libraries','Servo'); %only needed if arduino not connected
NumberOfReadings = 100; %Set number of readings
analog1 = zeros(1,NumberOfReadings); %holds voltage reads
analog2 = zeros(1,NumberOfReadings); %holds voltage reads
while 1==1 % continually run
for index = 1:NumberOfReadings % run values 2 through 99
analog1(index) = readVoltage(a,'A3'); % Servo A
analog2(index) = readVoltage(a,'A1'); % Servo B
AllValues_A = analog1(index)+AllValues_A; %adds all values for test
AllValues_B = analog2(index)+AllValues_B; %
pause (.0005); %slow down read speed
%% VOLTAGE PLOT
subplot (1,2,1)
plot(analog1) %plot values
hold on
plot(analog2)
legend('Potentiometer A','Potentiometer B')
hold off
ylim([0 5]); %set y limits of plot
ylabel('Voltage'); %label Y axis
%% DISPLAY VALUE IN BIT
%AnalogRead1 = analog1(index) * (1023 / 5);
%AnalogRead2 = analog2(index) * (1023 / 5);
%fprintf('\n Potentiometer: A= %.2f B= %.2f ',AnalogRead1,AnalogRead2);
%% CHANGE OF ANGULAR DISPLACEMENT PLOT
AnalogRead_Hip = analog1(index) * (360 / 5);
AnalogRead_Knee = analog2(index) * (360 / 5);
subplot (1,2,2)
plot(index,AnalogRead_Hip,'Color','b','LineWidth',2);
hold on
plot(index,AnalogRead_Knee,'Color','k','LineWidth',2);
legend('Hip Angle','Knee Angle')
hold off
ylim([0 360]); %set y limits of plot
xlim([0 100]);
ylabel('Change of Angular Displacement'); %label Y axis
fprintf('\n Reading: A= %.2f B= %.2f ',AnalogRead_Hip,AnalogRead_Knee);
end
end % end while loop
  3 comentarios
aliff saad
aliff saad el 28 de Oct. de 2020
Hi Rohan, thanks for the reply.
For your first suggestion, i already tried to use the animatedline plot based on the examples and questions asked by others in Mathworks. However, i still can't find the issue. I attached together the code that i have plotted using animatedline.
clear all
clc
a = arduino('COM3','Uno','Libraries','Servo'); %only needed if arduino not connected
NumberOfReadings = 100; %Set number of readings
analog1 = zeros(1,NumberOfReadings); %holds voltage reads
analog2 = zeros(1,NumberOfReadings); %holds voltage reads
index = zeros(1,NumberOfReadings);
figure(1)
hplot1 = animatedline(index(1),analog1(1),'Color','b','LineWidth',2);
hold on
hplot2 = animatedline(index(1),analog2(1),'Color','m','LineWidth',2);
%% VOLTAGE PLOT
legend('Potentiometer A','Potentiometer B')
ylim([0 5]);xlim([0 NumberOfReadings]); %set y limits of plot
ylabel('Voltage'); %label Y axis
while 1==1
for index = 2:NumberOfReadings % run values 2 through 99
analog1(index) = readVoltage(a,'A0'); % Servo A
analog2(index) = readVoltage(a,'A1'); % Servo B
clearpoints(hplot1)
clearpoints(hplot2)
addpoints(hplot1,index,analog1(index));
addpoints(hplot2,index,analog2(index));
drawnow limitrate
pause(0.005)
end
end
for the second suggestion, i will try to remove it and find better solutions to make the code continuous. Thanks for the suggestions, im really appreciate it
aliff saad
aliff saad el 28 de Oct. de 2020
I think, the issue might come from repeating "readVoltage" that occur in every iteration. Moreover, the Matlab needs to read two values at the same time.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by