EMG data acquisition with Arduino
6 views (last 30 days)
Show older comments
Heys guys , i need some help. I'm trying to comunicate Matlab with Arduino in a a more efficient way. I need to make an acquisition in real time of EMG data with 2 channels from an Arduino and Plot it. The data sampling frequency is 2000 Hz , I made an script that works like this : the matlab sends an number of samples to the arduino(between 0 and 200), then the arduino returns the same number to matlab . Then if the number recieved in matlab is the same as the number of samples , he receives the number of samples data in each channel. But it takes 8 seconds to plot 200 samples that is just 100ms of the data.
code :
= serial('COM5'); %assigns the object s to serial port
set(s, 'InputBufferSize', 512); %number of bytes in inout buffer
set(s, 'FlowControl', 'None');
set(s, 'BaudRate', 9600);
set(s, 'Parity', 'None');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',5000);
fopen(s); %opens the serial port
s.ReadAsyncMode = 'continuous';
amostras = 200;
figure(1)
tic
fprintf(s,int2str(amostras));
dado = fscanf(s,'%i');
if(dado(1) == amostras)
for i = 1:amostras
%
dados(1,i) = fscanf(s,'%f');
dados(2,i) = fscanf(s,'%f');
drawnow;
sb1 = subplot(2,1,1);
plot(dados(1,:))
sb2 = subplot(2,1,2);
plot(dados(2,:))
axis([sb1 sb2],[1 amostras -1.2e+04 2.5e+03]);
% pause(0.01 - toc);
end
end
dados = dados';
toc
With this code , it takes 8 seconds to plot the 200 samples , how can i make it work faster ? I'm thinking maybe its necessary to use the buffer , but how ?
the code of the arduino is :
if true
% int led = 13;
long emg[2][200]= {{-2915.1071,-2811.9843,-2769.226,-2741.5589,-2731.4981,-2766.7108,-2781.802,-2834.621,-2884.9248,-2967.9262,-3045.8971,-3171.6567,-3262.2036,-3320.053,-3304.9618,-3249.6276,-3118.8377,-2945.2894,-2681.1943,-2429.6751,-2147.9737,-1926.6368,-1730.4519,-1604.6923,-1483.9631,-1403.4769,-1328.0212,-1302.8693,-1277.7174,-1277.7174,-1312.93,-1436.1744,-1627.329,-1931.6672,-2308.9459,-2789.3476,-3299.9315,-3870.88,-4419.1918,-4962.4732,-5488.1482,-6061.6119,-6682.8643,-7321.7229,-7948.0057,-8461.1048,-8838.3835,-9029.5381,-9052.1748,-8868.5658,-8559.1972,-8134.1298,-7706.5473,-7281.4799,-6889.11,-6494.2249,-6119.4613,-5737.1522,-5369.9342,-4919.7149,-4487.1019,-4021.7915,-3619.3608,-3249.6276,-2970.4414,-2691.2551,-2429.6751,-2175.6408,-1964.3647,-1750.5734,-1554.3884,-1363.2339,-1207.292,-1081.5324,-996.0159,-882.8323,-779.7094,-641.3739,-513.0991,-402.4307,-314.399,-228.8824,-140.8507,-83.0013,-7.5456,72.9406,193.6698,284.2167,397.4003,445.1889,503.0383,533.2206,575.9789,613.7068,651.4346,669.041,706.7689,744.4967,794.8006,807.3765,852.65,877.8019,950.7424,998.5311,1046.3197,1043.8045,1043.8045,1033.7438,1036.259,1043.8045,1041.2893,1033.7438,1028.7134,1033.7438,1028.7134,1028.7134,1038.7742,1068.9565,1119.2603,1194.716,1250.0503,1307.8997,1350.6579,1398.4466,1408.5073,1426.1137,1393.4162,1398.4466,1400.9618,1438.6896,1438.6896,1436.1744,1395.9314,1348.1427,1285.2629,1209.8072,1089.078,996.0159,915.5298,887.8627,880.3171,887.8627,867.7411,857.6804,837.5588,809.8917,769.6486,721.86,689.1625,651.4346,628.7979,588.5548,570.9485,555.8574,568.4333,583.5245,621.2523,628.7979,633.8283,628.7979,658.9802,664.0106,689.1625,699.2233,747.0119,774.679,809.8917,784.7398,736.9512,661.4954,598.6156,543.2814,508.0687,467.8257,425.0674,372.2484,357.1572,309.3686,286.7319,261.5799,264.0951,251.5192,236.428,181.0938,62.8798,-65.395,-228.8824,-402.4307,-636.3435,-865.2259,-1094.1084,-1282.7477,-1426.1137,-1544.3277,-1622.2986,-1617.2682,-1589.6011,-1524.2061,-1483.9631,-1436.1744},{-973.3792,-1003.5615,-1036.259,-1121.7755,-1217.3528,-1348.1427,-1488.9935,-1670.0873,-1828.5443,-1976.9406,-2097.6698,-2168.0952,-2175.6408,-2140.4281,-2049.8812,-1939.2128,-1785.7861,-1644.9353,-1494.0238,-1360.7187,-1227.4135,-1136.8666,-1051.3501,-970.864,-928.1057,-877.8019,-860.1955,-832.5284,-855.1652,-885.3475,-940.6817,-985.9551,-1041.2893,-1081.5324,-1096.6236,-1071.4716,-1046.3197,-953.2576,-872.7715,-749.5271,-656.465,-513.0991,-392.3699,-201.2153,-30.1823,201.2153,402.4307,603.646,749.5271,850.1348,887.8627,875.2867,814.9221,747.0119,613.7068,485.432,316.9142,145.8811,-42.7583,-246.4888,-455.2497,-653.9498,-847.6196,-1018.6526,-1156.9882,-1307.8997,-1431.1441,-1569.4796,-1697.7544,-1856.2114,-2052.3964,-2215.8839,-2419.6144,-2648.4968,-2869.8337,-3103.7465,-3302.4467,-3473.4797,-3586.6633,-3659.6039,-3659.6039,-3641.9975,-3574.0874,-3483.5405,-3355.2657,-3234.5365,-3076.0794,-2900.016,-2774.2564,-2625.8601,-2464.8878,-2301.4004,-2115.2762,-1904.0001,-1670.0873,-1416.0529,-1149.4426,-880.3171,-606.1612,-339.5509,-93.0621,153.4267,384.8243,631.3131,809.8917,1028.7134,1154.473,1280.2326,1325.506,1358.2035,1335.5668,1315.4452,1250.0503,1207.292,1154.473,1131.8362,1101.6539,1101.6539,1104.1691,1126.8059,1169.5641,1252.5654,1300.3541,1370.7795,1411.0225,1428.6289,1413.5377,1345.6275,1257.5958,1126.8059,998.5311,872.7715,784.7398,724.3752,711.7992,689.1625,726.8904,754.5575,812.4069,842.5892,887.8627,892.893,900.4386,870.2563,832.5284,741.9815,686.6473,570.9485,498.0079,402.4307,337.0357,286.7319,281.7015,294.2774,334.5205,397.4003,492.9776,586.0397,701.7385,799.8309,910.4994,990.9855,1081.5324,1111.7147,1116.7451,1109.1995,1056.3805,990.9855,905.469,814.9221,734.436,691.6777,661.4954,646.4043,616.222,593.5852,540.7662,520.6447,480.4016,470.3408,533.2206,679.1017,855.1652,1151.9578,1466.3567,1810.938,2100.185,2329.0675,2447.2815,2500.1005,2444.7663,2334.0979,2198.2775,2075.0331,1964.3647,1863.757,1833.5747,1743.0278,1685.1784,1617.2682}};
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
String leStringSerial(){
String conteudo = "";
char caractere;
// Enquanto receber algo pela serial
while(Serial.available() > 0) {
// Lê byte da serial
caractere = Serial.read();
// Ignora caractere de quebra de linha
if (caractere != '\n'){
// Concatena valores
conteudo.concat(caractere);
}
// Aguarda buffer serial ler próximo caractere
delay(10);
}
Serial.println(conteudo);
return conteudo;
}
void loop() {
// Se receber algo pela serial
if (Serial.available() > 0){
// Lê toda string recebida
String recebido = leStringSerial();
int a = recebido.toInt();
if (a > 0 && a <= 200){
digitalWrite(led, HIGH);
for(int i=0;i< a;i++)
{
Serial.println(emg[0][i]);
Serial.println(emg[1][i]);
}
}
}
delay(100);
}
end
Can anyone help ?
2 Comments
Star Strider
on 16 Jul 2015
There aren’t many people experienced with Arduino on MATLAB Answers. Most who are are MathWorks folks.
You might consider contacting MathWorks Support directly.
Answers (0)
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!