Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
RunExperiment.m
00001 function output = RunExperiment(dest_ip,port,input,output_size,params) 00002 % RunExperiment 00003 % [output] = RunExperiment(dest_ip, port, input, output_size,params) 00004 % 00005 % Sends input to the FRDM board and processes output data 00006 % 00007 % INPUT PARAMETERS 00008 % dest_ip: (string) ip address of FRDM board 00009 % port: (int) port used by server on FRDM board 00010 % input: (float vector) input variables for the experiment 00011 % which are sent to the FRDM board 00012 % ouput_size: (int) number of floats to be sent back by the FRDM 00013 % board at each control step 00014 % params: (struct) (optional) contains arguments below 00015 % params.callback - callback function 00016 % params.timeout - timeout to end experiment (seconds) 00017 % 00018 % OUTPUT PARAMETERS 00019 % output: (float vector) matrix of all data recieved from FRDM 00020 % board during the experiment 00021 00022 if nargin == 4 00023 params = struct(); 00024 end 00025 if ~isfield(params,'callback') 00026 params.callback = 0; 00027 end 00028 00029 if ~isfield(params,'timeout') 00030 params.timeout = 2; 00031 end 00032 00033 00034 done = 0; 00035 00036 data = []; % output data 00037 % boolean to store if there is a callback 00038 has_callback = strcmp(class(params.callback) , 'function_handle'); 00039 00040 % event handler for packet reception 00041 function [] = recv(obj,es) 00042 if es.Data.DatagramLength == 1 00043 done = 1; 00044 return 00045 end 00046 % All packets must contains floats, which are of size 4 00047 % if the packet isn't divisible by 4, there is an error 00048 if mod(es.Data.DatagramLength,4*output_size) > 0 00049 fprintf(1,'Error bad data size %d expected mod %d\n',es.Data.DatagramLength,4*output_size); 00050 fread(obj, es.Data.DatagramLength); 00051 return 00052 end 00053 00054 d = fread(obj, es.Data.DatagramLength); 00055 d = typecast(uint8(d),'single'); 00056 %length(d) 00057 00058 num_data = length(d)/output_size; 00059 00060 d = reshape(d,output_size,num_data)'; 00061 data(end+1 : end+num_data,:) = d; 00062 00063 if (has_callback) 00064 params.callback(d); 00065 end 00066 end 00067 00068 % create port and bind handler 00069 u = udp(dest_ip,port); 00070 u.InputBufferSize = 1000000; 00071 u.DatagramReceivedFcn = @recv; 00072 u.DatagramTerminateMode = 'on'; 00073 00074 fopen(u); 00075 00076 % Send input data to mbed 00077 fwrite(u,typecast(single(input),'uint8')) 00078 00079 tic 00080 00081 % Wait for experiment to finish 00082 k = 0; 00083 pd = 0; 00084 pk = 0; 00085 while done == 0 00086 k = k+1; 00087 00088 if mod(k,20) == 0 00089 fprintf(' (%d bytes recieved, %d available)\n',u.ValuesReceived,u.BytesAvailable); 00090 else 00091 fprintf('.'); 00092 end 00093 00094 d = size(data,1); 00095 if d > pd 00096 pk = k; 00097 pd = d; 00098 elseif (k-pk) > params.timeout*10 00099 break 00100 end 00101 00102 pause(.1); 00103 end 00104 fprintf('\nExperiment finished\n'); 00105 toc 00106 00107 00108 fclose(u) 00109 delete(u) 00110 output = data; 00111 end
Generated on Sat Jul 16 2022 07:18:45 by
1.7.2