2.74 MATLAB code for Interface to Nucleo
Experiment_Example_MATLAB.m@8:58d93d7564ed, 2020-08-27 (annotated)
- Committer:
- elijahsj
- Date:
- Thu Aug 27 16:00:16 2020 +0000
- Revision:
- 8:58d93d7564ed
- Parent:
- 7:3816d5dae909
- Child:
- 9:3d6b58589448
updated docs;
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 |
elijahsj | 7:3816d5dae909 | 3 | subplot(311) |
pwensing@mit.edu | 0:b568b8ff43f2 | 4 | h1 = plot([0],[0]); |
pwensing@mit.edu | 0:b568b8ff43f2 | 5 | h1.XData = []; h1.YData = []; |
Patrick Wensing |
4:41f9e216a12d | 6 | ylabel('Position (counts)'); |
pwensing@mit.edu | 0:b568b8ff43f2 | 7 | |
elijahsj | 7:3816d5dae909 | 8 | subplot(312) |
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)'); |
Patrick Wensing |
3:ba19438ac09e | 12 | |
elijahsj | 7:3816d5dae909 | 13 | subplot(313) |
elijahsj | 7:3816d5dae909 | 14 | h3 = plot([0],[0]); |
elijahsj | 7:3816d5dae909 | 15 | h3.XData = []; h3.YData = []; |
elijahsj | 8:58d93d7564ed | 16 | ylabel('Current (16 bit ADC counts)'); |
elijahsj | 7:3816d5dae909 | 17 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 18 | % This function will get called any time there is new data from |
pwensing@mit.edu | 0:b568b8ff43f2 | 19 | % the FRDM board. Data comes in blocks, rather than one at a time. |
pwensing@mit.edu | 0:b568b8ff43f2 | 20 | function my_callback(new_data) |
pwensing@mit.edu | 0:b568b8ff43f2 | 21 | t = new_data(:,1); % time |
pwensing@mit.edu | 0:b568b8ff43f2 | 22 | pos = new_data(:,2); % position |
pwensing@mit.edu | 0:b568b8ff43f2 | 23 | vel = new_data(:,3); % velocity |
elijahsj | 7:3816d5dae909 | 24 | curr = new_data(:,4); %current |
pwensing@mit.edu | 0:b568b8ff43f2 | 25 | N = length(pos); |
pwensing@mit.edu | 0:b568b8ff43f2 | 26 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 27 | h1.XData(end+1:end+N) = t; % Update subplot 1 |
pwensing@mit.edu | 0:b568b8ff43f2 | 28 | h1.YData(end+1:end+N) = pos; |
pwensing@mit.edu | 0:b568b8ff43f2 | 29 | h2.XData(end+1:end+N) = t; % Update subplot 2 |
pwensing@mit.edu | 0:b568b8ff43f2 | 30 | h2.YData(end+1:end+N) = vel; |
elijahsj | 7:3816d5dae909 | 31 | h3.XData(end+1:end+N) = t; % Update subplot 2 |
elijahsj | 7:3816d5dae909 | 32 | h3.YData(end+1:end+N) = curr; |
pwensing@mit.edu | 0:b568b8ff43f2 | 33 | end |
pwensing@mit.edu | 0:b568b8ff43f2 | 34 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 35 | frdm_ip = '192.168.1.100'; % FRDM board ip |
pwensing@mit.edu | 0:b568b8ff43f2 | 36 | frdm_port= 11223; % FRDM board port |
pwensing@mit.edu | 0:b568b8ff43f2 | 37 | params.callback = @my_callback; % callback function |
pwensing@mit.edu | 0:b568b8ff43f2 | 38 | params.timeout = 2; % end of experiment timeout |
pwensing@mit.edu | 0:b568b8ff43f2 | 39 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 40 | % The example program provided takes two arguments |
elijahsj | 7:3816d5dae909 | 41 | v1 = 0.5; % pwm applied for first second |
elijahsj | 8:58d93d7564ed | 42 | v2 = 0; % pwm applied for second second |
pwensing@mit.edu | 0:b568b8ff43f2 | 43 | input = [v1 v2]; % input sent to FRDM board |
elijahsj | 7:3816d5dae909 | 44 | output_size = 4; % number of outputs expected |
pwensing@mit.edu | 0:b568b8ff43f2 | 45 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 46 | output_data = RunExperiment(frdm_ip,frdm_port,input,output_size,params); |
Patrick Wensing |
3:ba19438ac09e | 47 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 48 | end |
pwensing@mit.edu | 0:b568b8ff43f2 | 49 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 50 | |
pwensing@mit.edu | 0:b568b8ff43f2 | 51 | |
elijahsj | 7:3816d5dae909 | 52 |