How to reach app object from worker when using parfeval

2 visualizzazioni (ultimi 30 giorni)
I am trying to contstruct a system which can control workproducts using image processing. I have created an app designed program. System has 2 different cameras at different position so triggers for cameras must be at different time. Also workproducts flows under the cameras with high speed. So simply I have used paralel processing. When a sensor (it is positioned to begining of the system) reads a value, it means workproduct is coming, a process function is called using parfeval and sensor continue to reading sensor's value. So synchronization is protected with this method. Simply the code is like that
function results = starting_func(app)
app.durdur = 0;
PLC_Baglanti(app)
while 1 && (app.durdur == 0)
try
value = PlcBoolean('DB50.DBX 0.0');
message = app.connection.ReadValues({value}); %readşng sensor
pause(0.01)
catch
msgbox('PLC bağlantısı koptu, başlat tuşuna tekrar basınız','error')
break
end
if app.durdur == 1
break
end
if message == 0
continue
else
app.ModDropDown.ItemsData=1:numel(app.ModDropDown.Items);
val = app.ModDropDown.Value;
app.TOPLAMSAYAEditField.Value = app.TOPLAMSAYAEditField.Value + 1;
sayac = app.TOPLAMSAYAEditField.Value;
switch val
case 1 %for normal process
F = parfeval(@process,1,app,sayac); % This line is important point
disp('normal mod')
try
outputs = fetchOutputs(F);
catch
outputs = 2;
app.NihaiSonuEditField.Value = 'No Measurement';
end
clear F
case 2 % mod control
%..
case 3 % mod calibration
%...
end
end
For each part process() function is called and work in backgroung thanks to paralel processing. process function is like that
function [fonk_basladi] = process(app,sayac)
fonk_basladi = 1; %just for control
pause(1) %waiting for workproduct comes to under the camera-1
frame_ust = getsnapshot(app.vidObject_Ust);
F(1) = parfeval(@ist1,4,frame_top); % Camera station 1
pause(2)
frame_yan = getsnapshot(app.vidObject_Yan); % Camera station 2
F(2) = parfeval(@ist2,4,frame_side);
pause(3)
try
outputs = fetchoutputs(f);
catch
outputs(1:2) = 2;
app.oklabel.text = 'No measurement';
app.oklabel.backgroundcolor = [1 0 0];
end
clear F
end
ist functions are for image processing and showing the result and captured images to main app screen. But workers can not reach app object from the backside. So ist function must be reach to app object in order to showing measurement results to main screen.
İst function is created for both controlling and showing results to main app screen.
function [deger,ok_nok_rework] = ist1(app)
% Çap ölçümleri Kısmı
F(1) = parfeval(@ic_cap_optimized,4,frame_ust,250);
F(2) = parfeval(@dis_cap_optimized_v3,8,frame_ust,250);
%[cap,maks,minn,egiklik] = ic_cap_optimized(frame_ust,250);
%% Piksel ölçüm değerini paralelden çekme
try
outputs = fetchOutputs(F);
catch
outputs(1:4) = 2;
app.OKLabel.Text = 'Ölçüm Alınamadı';
app.OKLabel.BackgroundColor = [1 0 0];
end
if isempty(F(1,1).OutputArguments{2}) || isempty(F(1,1).OutputArguments{1})
outputs(1:4) = 2;
app.OKLabel.Text = 'Ölçüm Alınamadı';
app.OKLabel.BackgroundColor = [1 0 0];
end
%% Gerçek Ölçüm Değerlerini Elde Etme, Tabloya Yazma ve Görüntüyü Ekranda Gösterme
for i=[1,2]
deger(i) = (app.UITable2.Data{i,2}*outputs(i) + app.UITable2.Data{i,3}); %
app.UITable.Data(i,1) = {deger(i)};
end
imshow(frame_ust, 'Parent', app.UIAxes);
%% Ölçüm sonuçlarını karşılaştırma
for i = [1,2]
if (deger(i)>(app.UITable.Data{i,3} + app.UITable2.Data{i,4})) && (deger(i)<(app.UITable.Data{i,4} - app.UITable2.Data{i,4}))
k(i,1) = 1;
elseif abs(deger(i)-parca_sinirlari(i,1)) >1
k(i,2) = 3;
else
k(i,3) = 2;
end
end
%% Kamera Ölçüm sonucunu ok-nok-rework ekrana yazma
ok_nok_rework = 3;
if sum(k(1:2,1)) == 2
app.OKLabel.Text = {'OK'};
ok_nok_rework = 1;
app.OKEditField.Value = app.OKEditField.Value + 1;
elseif sum(k(1:2,3)) > 0
app.OKLabel.Text = {'REWORK'};
ok_nok_rework = 3;
app.REWORKEditField.Value = app.REWORKEditField.Value + 1;
else
app.OKLabel.Text = {'NOK'};
ok_nok_rework = 2;
app.NOKEditField.Value = app.NOKEditField.Value + 1;
end
clear F;
end
But when F = parfeval(@process,1,app,sayac); is called I get "Warning: Unable to save App Designer app object. Save not supported for matlab.apps.AppBase objects. " warning .
So, is there an any solution to reach app object to show measurement result from parallel worker without obscuring to main program for synchronization?
Or any idea for creating this new algorithm ensuring that both show measurement data to main app at different time and works continuously?
Thanks.

Risposte (1)

Ayush Modi
Ayush Modi il 19 Gen 2024
Hi Ahmet,
The warning
"Warning: Unable to save App Designer app object. Save not supported for matlab.apps.AppBase objects."
occurs because Matlab is trying to serialize the app object when you pass it to the parfeval function. AppBase objects (which is what App Designer apps are based on) contain handles to UI components and other state that cannot be serialized or passed to workers in a parallel pool.
As a work around, you can pass only the data necessary for the computation to the parallel workers. Then, you can use afterEach or afterAll function to update the app UI based on the results from the workers. Here is an example to demonstrate how you can achieve this using afterEach function:
% ... your existing code ...
% Instead of passing app, pass only necessary data to process
F = parfeval(@process, 1, sayac, frameData); % frameData is the data needed
% Set up a callback to update the UI after the computation is done -
afterEach(F, @(results) updateUI(app, results));
Please refer to the following MathWorks documentation for more information on:
Hope this helps!

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by