How to setup two dependent functions in parallel?

7 vues (au cours des 30 derniers jours)
Previn Savaya
Previn Savaya le 1 Déc 2019
Hi,
I am working on a leader-follower robot system that has a camera setup at an intersection that is scanning for collision prone objects for my senior design.
Basically, I have two seperate, but dependent, functions that need to be ran in parallel. The robots will only be allowed to operate while the OK-2-Go flag is set to 1. If there is an object detected by the camera, the Ok-2-Go flag will be set to 0. The Ok-2-Go flag is defined and updated only in the camera function, but is used as a condition in the turtlebots function.
Here is what my main would look like in terms of this project (excluding the parallel processing stuff):
%% Main.m
% All other scripts can be converted to functions and executed here to
% simplify the process in combining the entire project together
%Startup: Used to setup and initialize wireless connections and variables
start();
%USB_Camera: Used to start reading intersection for objects
USB_Camera();
%Turtlebots will run a script to allow us to control the leader/follower system
Turtlebots();
%The USB_Camera and Turtlebots function needs to be ran at the same time, while transferring data from the USB_Camera to the Turtlebots.
My problem is that the camera function needs to be running continuously while the turtlebots function runs at the same time. I have read online and realized that parfor loops do not work as they only work for functions INDEPENDENT of one another. The other thing I've read up on was callbacks but I'm not sure if these will solve this specific issue. The last thing I read up on was parpools, but when I setup a test program, I keep getting errors. I cannot find any examples on how this parpool function should be setup.
Here is what my main function looks like:
%MAINCOMM
% defaultProfile = parallel.defaultClusterProfile;
% myCluster = parcluster(defaultProfile);
% parpool(myCluster);
delete(gcp('nocreate'));
mpiInit;
p = parpool(myCluster,2);
spmd
labindex
end
COMM2();
COMM1();
Here is what COMM1 looks like:
%COMM1
function [] = COMM1()
x = labReceive(1);
sum = x+10
end
Here is what COMM2 looks like:
%COMM2
function [] = COMM2()
x = 1;
labSend(x, 2);
end
Here is my errors when I run MAINCOMM:
>> MAINCOMM
Starting parallel pool (parpool) using the 'local' profile ...
Connected to the parallel pool (number of workers: 2).
Lab 1:
ans =
1
Lab 2:
ans =
2
Error using labSend
Destination 2 is out of range (valid range: 1 <= destination <= 1).
Error in COMM2 (line 5)
labSend(x, 2);
Error in MAINCOMM (line 14)
COMM2();
>>
Can anyone point me in the right direction or tell me what I'm doing wrong? Is this even the right path to take in terms of what I'm trying to do?
I appreciate all the help!

Réponse acceptée

Previn Savaya
Previn Savaya le 1 Déc 2019
Fixed it using this:
MAIN
%MAINCOMM
defaultProfile = parallel.defaultClusterProfile;
myCluster = parcluster(defaultProfile);
delete(gcp('nocreate'));
mpiInit;
p = parpool(myCluster,2);
spmd(2)
if (labindex == 1)
COMM2();
elseif (labindex == 2)
COMM1();
end
end
COMM 1
%MAINCOMM
defaultProfile = parallel.defaultClusterProfile;
myCluster = parcluster(defaultProfile);
delete(gcp('nocreate'));
mpiInit;
p = parpool(myCluster,2);
spmd(2)
if (labindex == 1)
COMM2();
elseif (labindex == 2)
COMM1();
end
end
COMM 2
%COMM2
function [] = COMM2()
x = 0;
tic
while toc<20
x = x+1;
labSend(x, labindex+1);
pause(1);
end
end

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by