matlabroboticswebots

How to pass data between files in Webots?


I want to pass data from a robot controller to the supervisor, but I can't seem to find a working solution. Both the codes are in Matlab. The controller should send a boolean to the supervisor when it encounters an object.

I tried TCP/IP and global values but none of them seem to work:

Controller code:

data_to_send = false; 

client_socket = tcpip('localhost', 12345);
fopen(client_socket);

while true
fwrite(client_socket, data_to_send, 'boolean');
end

fclose(client_socket);

Supervisor code:

server_socket = tcpip('0.0.0.0', 12345, 'NetworkRole', 'server');

fopen(server_socket);

while true 

while ~server_socket.BytesAvailable pause(0.1); end

received_data = fread(server_socket, server_socket.BytesAvailable, 'boolean');

disp(['Received data from the controller: ', num2str(received_data)]);

end 

fclose(server_socket);

Solution

  • There are mainly two options to make controllers communicate with each other in Webots:

    Alternatively, you may also use any standard inter-process communication (IPC) mechanism, such as files, sockets, shared memory, pipes, etc. But these are not specific to Webots.

    See also this answer.