
adccommunitymod (AutomationDirect) asked a question.
Created Date: December 21,2016
Created By: alexb
**** This post has been imported from our legacy forum. Information in this post may be outdated and links contained in the post may no longer work.****
Hello everyone, This is a simple guide to connect the productivity controllers (P2000 & P3000) to Matlab using TCP/IP (or UDP/IP). It is achieved using the Custom Protocol over Ethernet (CPoE). Why Matlab? It's a great tool for data analysis, and it surely simplifies the design of controllers (especially PID), plus it is also widely available for students. In my case, I was able to tune a PI controller in 5 minutes. Softwares: CPoE is only available on the productivity programming software v2.4.0.16 (or later), make sure you are using the latest version. As for Matlab, I was using the Instrument Control Toolbox (version R2015), this toolbox was introduced in R2011a. Although, for those who don't have it, I think it could be also possible, see the conclusion remarks. Note: To see if you have the toolbox, type in the command: "tcpip " and press f1. If the tcpip object pops up, you have access to tcpip communications. P2000/3000 Setup: After this, you need to decide whether you want Matlab to be the client or server in the communication over TCP/IP. In my case, I decided I wanted Matlab to be the client but either way should work: If Matlab is the client, controller needs to be the slave. If Matlab is the server, controller needs to be the master. To setup the controller as the slave/master, go to the 'Hardware Configuration ', then go to the tab 'CPoE '. Add a 'CPoE Device ' by dragging and open up the properties by double clicking over the CPoE device icon (see pictures 1 and 2). This is where you setup the controller for TCP/IP or UDP/IP. Setting up the device properties: Device name: I decided to call it 'matlab_CPoE ' and the IP address: enter the IP of the computer running Matlab. Select how you want to run the CPoE, I decided to use TCP/IP - CPU as slave (Matlab as client). Enter a CPU port number from 1 to 65535, remember this port number for setting up Matlab. Enter a Boolean tag into the 'Enable ' field. For the outputs, you can name them how ever you want. I decided to use a Structure named Matlab. Basic Ladder Setup: The steps are: Enable the CPoE communication by setting the boolean tab in the device configuration. Check if connection is successful. Send data In my example, I wanted to send data to Matlab at 100ms interval, I also wanted the data to start sending as soon as Matlab connected. See picture #3 for my example ladder. Note: make sure in the Custom Protocol Ethernet (CPE) that you set your data length correctly. Matlab Basic Setup: Setting up Matlab is pretty simple, here is a basic script file: port = 4000 ; %same port as in the device config of CPoE P2000_ip = '192.168.0.150 '; t = tcpip(P2000,port); fopen(t); %starts TCP/IP communication % Send data back and forth here % do more stuff here fclose(t) % close TCP/IP delete(t) clear t Matlab Example: As I mentioned earlier, in my example, I want my controller to start sending data as soon as Matlab is connected. Matlab will need to start acquiring the data, so I decided to use a for loop to acquire 'X ' amount of time by knowing how fast the data is being sent (in my case 100msec). %tcp ip client, that means CPU in Slave port = 4000 ; t = tcpip(P2000,port); fopen(t); disp('starting ') %define sampling rate and time sampling_rate = 0.1; %100msec sampling_time = 20; % how long I want to capture nSize = sampling_time/sampling_rate; %array size to capture data %Initialize Arrays DO = zeros(nSize,1); Air = zeros(nSize,1); T = zeros(nSize,1); CD = zeros(nSize,1); V = zeros(nSize,1); Feed = zeros(nSize,1); MEBR = zeros(nSize,1); nData = 7; %number of data to be received by controller, i.e. the number of rows in the custom protocol block time = 0:sampling_rate:sampling_time-sampling_rate; %creating a time array for plotting for k = 1:nSize %wait for data to be available while(t.BytesAvailable == 0) end %read all data as floats (my specific application was all floats, I guess you would have to split your read depending on your data types temp = fread(t, nData, 'float32 '); %split the data accordingly, based on the order in the custom protocol block DO(k,1) = temp(1,1); Air(k,1) = temp(2,1); T(k,1) = temp(3,1); CD(k,1) = temp(4,1); V(k,1) = temp(5,1); Feed(k,1) = temp(6,1); MEBR(k,1) = temp(7,1); end fclose(t); delete(t); clear t Conclusion Remarks For those who don't have the Instrument Control Toolbox, I think you could replace the code for the tcpip object with tcpclient, it doesn't require additional toolboxes (as far as I can tell) but was introduced later, version R2014b. If you have any questions, let me know and I will try to help. Thank you AutomationDirect for CPoE. It's a great feature for academia since Matlab is very popular :D! I believe this guide could be applied similarly to LabVIEW and Arduino :rolleyes:. Alex
Created Date: December 21,2016
Created by: alexb
Hello everyone,
This is a simple guide to connect the productivity controllers (P2000 & P3000) to Matlab using TCP/IP (or UDP/IP). It is achieved using the Custom Protocol over Ethernet (CPoE).
Why Matlab? It's a great tool for data analysis, and it surely simplifies the design of controllers (especially PID), plus it is also widely available for students. In my case, I was able to tune a PI controller in 5 minutes.
Softwares:
CPoE is only available on the productivity programming software v2.4.0.16 (or later), make sure you are using the latest version.
As for Matlab, I was using the Instrument Control Toolbox (version R2015), this toolbox was introduced in R2011a. Although, for those who don't have it, I think it could be also possible, see the conclusion remarks.
Note: To see if you have the toolbox, type in the command: "tcpip " and press f1. If the tcpip object pops up, you have access to tcpip communications.
P2000/3000 Setup:
After this, you need to decide whether you want Matlab to be the client or server in the communication over TCP/IP. In my case, I decided I wanted Matlab to be the client but either way should work:
If Matlab is the client, controller needs to be the slave.
If Matlab is the server, controller needs to be the master.
To setup the controller as the slave/master, go to the 'Hardware Configuration ', then go to the tab 'CPoE '. Add a 'CPoE Device ' by dragging and open up the properties by double clicking over the CPoE device icon (see pictures 1 and 2). This is where you setup the controller for TCP/IP or UDP/IP.
Setting up the device properties:
Device name: I decided to call it 'matlab_CPoE ' and the
IP address: enter the IP of the computer running Matlab.
Select how you want to run the CPoE, I decided to use TCP/IP - CPU as slave (Matlab as client).
Enter a CPU port number from 1 to 65535, remember this port number for setting up Matlab.
Enter a Boolean tag into the 'Enable ' field.
For the outputs, you can name them how ever you want. I decided to use a Structure named Matlab.
Basic Ladder Setup:
The steps are:
Enable the CPoE communication by setting the boolean tab in the device configuration.
Check if connection is successful.
Send data
In my example, I wanted to send data to Matlab at 100ms interval, I also wanted the data to start sending as soon as Matlab connected.
See picture #3 for my example ladder.
Note: make sure in the Custom Protocol Ethernet (CPE) that you set your data length correctly.
Matlab Basic Setup:
Setting up Matlab is pretty simple, here is a basic script file:
port = 4000 ; %same port as in the device config of CPoE
P2000_ip = '192.168.0.150 ';
t = tcpip(P2000,port);
fopen(t); %starts TCP/IP communication
% Send data back and forth here
% do more stuff here
fclose(t) % close TCP/IP
delete(t)
clear t
Matlab Example:
As I mentioned earlier, in my example, I want my controller to start sending data as soon as Matlab is connected. Matlab will need to start acquiring the data, so I decided to use a for loop to acquire 'X ' amount of time by knowing how fast the data is being sent (in my case 100msec).
%tcp ip client, that means CPU in Slave
port = 4000 ;
t = tcpip(P2000,port);
fopen(t);
disp('starting ')
%define sampling rate and time
sampling_rate = 0.1; %100msec
sampling_time = 20; % how long I want to capture
nSize = sampling_time/sampling_rate; %array size to capture data
%Initialize Arrays
DO = zeros(nSize,1);
Air = zeros(nSize,1);
T = zeros(nSize,1);
CD = zeros(nSize,1);
V = zeros(nSize,1);
Feed = zeros(nSize,1);
MEBR = zeros(nSize,1);
nData = 7; %number of data to be received by controller, i.e. the number of rows in the custom protocol block
time = 0:sampling_rate:sampling_time-sampling_rate; %creating a time array for plotting
for k = 1:nSize
%wait for data to be available
while(t.BytesAvailable == 0)
end
%read all data as floats (my specific application was all floats, I guess you would have to split your read depending on your data types
temp = fread(t, nData, 'float32 ');
%split the data accordingly, based on the order in the custom protocol block
DO(k,1) = temp(1,1);
Air(k,1) = temp(2,1);
T(k,1) = temp(3,1);
CD(k,1) = temp(4,1);
V(k,1) = temp(5,1);
Feed(k,1) = temp(6,1);
MEBR(k,1) = temp(7,1);
end
fclose(t);
delete(t);
clear t
Conclusion Remarks
For those who don't have the Instrument Control Toolbox, I think you could replace the code for the tcpip object with tcpclient, it doesn't require additional toolboxes (as far as I can tell) but was introduced later, version R2014b.
If you have any questions, let me know and I will try to help.
Thank you AutomationDirect for CPoE. It's a great feature for academia since Matlab is very popular :D! I believe this guide could be applied similarly to LabVIEW and Arduino :rolleyes:.
Alex
Created Date: December 22,2016
Created by: kewakl
Maybe a Mod can have this worked into an example and placed with the Sample Programs on the AD site.