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.
Experiment_Example_old_MATLAB.m@5:1497fccca82e, 2015-09-22 (annotated)
- Committer:
- Patrick Wensing
- Date:
- Tue Sep 22 09:08:00 2015 -0400
- Revision:
- 5:1497fccca82e
Added file for users of older MATLAB versions
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Patrick Wensing |
5:1497fccca82e | 1 | function output_data = Experiment_Example_MATLAB() |
| Patrick Wensing |
5:1497fccca82e | 2 | figure(1); clf; % Create an empty figure to update later |
| Patrick Wensing |
5:1497fccca82e | 3 | subplot(211) |
| Patrick Wensing |
5:1497fccca82e | 4 | h1 = plot([0],[0]); |
| Patrick Wensing |
5:1497fccca82e | 5 | |
| Patrick Wensing |
5:1497fccca82e | 6 | % h1 |
| Patrick Wensing |
5:1497fccca82e | 7 | set(h1,'XData',[],'YData', []); |
| Patrick Wensing |
5:1497fccca82e | 8 | ylabel('Position (counts)'); |
| Patrick Wensing |
5:1497fccca82e | 9 | subplot(212) |
| Patrick Wensing |
5:1497fccca82e | 10 | h2 = plot([0],[0]); |
| Patrick Wensing |
5:1497fccca82e | 11 | %h2.XData = []; h2.YData = []; |
| Patrick Wensing |
5:1497fccca82e | 12 | set(h2,'XData',[],'YData',[]); |
| Patrick Wensing |
5:1497fccca82e | 13 | ylabel('Velocity (counts/s)'); |
| Patrick Wensing |
5:1497fccca82e | 14 | |
| Patrick Wensing |
5:1497fccca82e | 15 | % This function will get called any time there is new data from |
| Patrick Wensing |
5:1497fccca82e | 16 | % the FRDM board. Data comes in blocks, rather than one at a time. |
| Patrick Wensing |
5:1497fccca82e | 17 | function my_callback(new_data) |
| Patrick Wensing |
5:1497fccca82e | 18 | t = new_data(:,1); % time |
| Patrick Wensing |
5:1497fccca82e | 19 | pos = new_data(:,2); % position |
| Patrick Wensing |
5:1497fccca82e | 20 | vel = new_data(:,3); % velocity |
| Patrick Wensing |
5:1497fccca82e | 21 | N = length(pos); |
| Patrick Wensing |
5:1497fccca82e | 22 | tt = get(h1,'XData'); |
| Patrick Wensing |
5:1497fccca82e | 23 | yy = get(h1,'YData'); |
| Patrick Wensing |
5:1497fccca82e | 24 | |
| Patrick Wensing |
5:1497fccca82e | 25 | tt(end+1:end+N) = t; % Update subplot 1 |
| Patrick Wensing |
5:1497fccca82e | 26 | yy(end+1:end+N) = pos; |
| Patrick Wensing |
5:1497fccca82e | 27 | set(h1,'XData',tt,'YData',yy); |
| Patrick Wensing |
5:1497fccca82e | 28 | |
| Patrick Wensing |
5:1497fccca82e | 29 | tt = get(h2,'XData'); |
| Patrick Wensing |
5:1497fccca82e | 30 | yy = get(h2,'YData'); |
| Patrick Wensing |
5:1497fccca82e | 31 | |
| Patrick Wensing |
5:1497fccca82e | 32 | tt(end+1:end+N) = t; % Update subplot 2 |
| Patrick Wensing |
5:1497fccca82e | 33 | yy(end+1:end+N) = vel; |
| Patrick Wensing |
5:1497fccca82e | 34 | |
| Patrick Wensing |
5:1497fccca82e | 35 | set(h2,'XData',tt,'YData',yy); |
| Patrick Wensing |
5:1497fccca82e | 36 | end |
| Patrick Wensing |
5:1497fccca82e | 37 | |
| Patrick Wensing |
5:1497fccca82e | 38 | frdm_ip = '192.168.1.100'; % FRDM board ip |
| Patrick Wensing |
5:1497fccca82e | 39 | frdm_port= 11223; % FRDM board port |
| Patrick Wensing |
5:1497fccca82e | 40 | params.callback = @my_callback; % callback function |
| Patrick Wensing |
5:1497fccca82e | 41 | params.timeout = 2; % end of experiment timeout |
| Patrick Wensing |
5:1497fccca82e | 42 | |
| Patrick Wensing |
5:1497fccca82e | 43 | % The example program provided takes two arguments |
| Patrick Wensing |
5:1497fccca82e | 44 | v1 = 1.0; % pwm applied for first second |
| Patrick Wensing |
5:1497fccca82e | 45 | v2 = 0.0; % pwm applied for second second |
| Patrick Wensing |
5:1497fccca82e | 46 | input = [v1 v2]; % input sent to FRDM board |
| Patrick Wensing |
5:1497fccca82e | 47 | output_size = 3; % number of outputs expected |
| Patrick Wensing |
5:1497fccca82e | 48 | |
| Patrick Wensing |
5:1497fccca82e | 49 | output_data = RunExperiment(frdm_ip,frdm_port,input,output_size,params); |
| Patrick Wensing |
5:1497fccca82e | 50 | |
| Patrick Wensing |
5:1497fccca82e | 51 | end |
| Patrick Wensing |
5:1497fccca82e | 52 | |
| Patrick Wensing |
5:1497fccca82e | 53 | |
| Patrick Wensing |
5:1497fccca82e | 54 |