2.74 MATLAB code for Interface to Nucleo
Experiment_Example_MATLAB.m@0:b568b8ff43f2, 2015-08-12 (annotated)
- Committer:
- pwensing@mit.edu
- Date:
- Wed Aug 12 08:34:57 2015 -0400
- Revision:
- 0:b568b8ff43f2
- Child:
- 1:30408caaef92
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pwensing@mit.edu | 0:b568b8ff43f2 | 1 | function output_data = Experiment_Example_MATLAB() |
pwensing@mit.edu | 0:b568b8ff43f2 | 2 | figure(1); clf; % Create an empty figure to update later |
pwensing@mit.edu | 0:b568b8ff43f2 | 3 | subplot(211) |
pwensing@mit.edu | 0:b568b8ff43f2 | 4 | h1 = plot([0],[0]); |
pwensing@mit.edu | 0:b568b8ff43f2 | 5 | h1.XData = []; h1.YData = []; |
pwensing@mit.edu | 0:b568b8ff43f2 | 6 | ylabel('Posiotion (counts)'); |
pwensing@mit.edu | 0:b568b8ff43f2 | 7 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 8 | subplot(212) |
pwensing@mit.edu | 0:b568b8ff43f2 | 9 | h2 = plot([0],[0]); |
pwensing@mit.edu | 0:b568b8ff43f2 | 10 | h2.XData = []; h2.YData = []; |
pwensing@mit.edu | 0:b568b8ff43f2 | 11 | ylabel('Velocity (counts/s)'); |
pwensing@mit.edu | 0:b568b8ff43f2 | 12 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 13 | % This function will get called any time there is new data from |
pwensing@mit.edu | 0:b568b8ff43f2 | 14 | % the FRDM board. Data comes in blocks, rather than one at a time. |
pwensing@mit.edu | 0:b568b8ff43f2 | 15 | function my_callback(new_data) |
pwensing@mit.edu | 0:b568b8ff43f2 | 16 | t = new_data(:,1); % time |
pwensing@mit.edu | 0:b568b8ff43f2 | 17 | pos = new_data(:,2); % position |
pwensing@mit.edu | 0:b568b8ff43f2 | 18 | vel = new_data(:,3); % velocity |
pwensing@mit.edu | 0:b568b8ff43f2 | 19 | N = length(pos); |
pwensing@mit.edu | 0:b568b8ff43f2 | 20 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 21 | h1.XData(end+1:end+N) = t; % Update subplot 1 |
pwensing@mit.edu | 0:b568b8ff43f2 | 22 | h1.YData(end+1:end+N) = pos; |
pwensing@mit.edu | 0:b568b8ff43f2 | 23 | h2.XData(end+1:end+N) = t; % Update subplot 2 |
pwensing@mit.edu | 0:b568b8ff43f2 | 24 | h2.YData(end+1:end+N) = vel; |
pwensing@mit.edu | 0:b568b8ff43f2 | 25 | end |
pwensing@mit.edu | 0:b568b8ff43f2 | 26 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 27 | frdm_ip = '192.168.1.100'; % FRDM board ip |
pwensing@mit.edu | 0:b568b8ff43f2 | 28 | frdm_port= 11223; % FRDM board port |
pwensing@mit.edu | 0:b568b8ff43f2 | 29 | params.callback = @my_callback; % callback function |
pwensing@mit.edu | 0:b568b8ff43f2 | 30 | params.timeout = 2; % end of experiment timeout |
pwensing@mit.edu | 0:b568b8ff43f2 | 31 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 32 | % The example program provided takes two arguments |
pwensing@mit.edu | 0:b568b8ff43f2 | 33 | v1 = 1.0; % pwm applied for first second |
pwensing@mit.edu | 0:b568b8ff43f2 | 34 | v2 = 0.0; % pwm applied for second second |
pwensing@mit.edu | 0:b568b8ff43f2 | 35 | input = [v1 v2]; % input sent to FRDM board |
pwensing@mit.edu | 0:b568b8ff43f2 | 36 | output_size = 3; % number of outputs expected |
pwensing@mit.edu | 0:b568b8ff43f2 | 37 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 38 | output_data = RunExperiment(frdm_ip,frdm_port,input,output_size,params); |
pwensing@mit.edu | 0:b568b8ff43f2 | 39 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 40 | end |
pwensing@mit.edu | 0:b568b8ff43f2 | 41 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 42 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 43 |