MATLABでのシリアルポートの値の表示と保存
13 views (last 30 days)
Show older comments
Arduinoで加速度ジャイロセンサを使い、
Serial.println(gx); %%これはArduinoコードの一部です
としてgxの値をMATLABでも表示させたく、
arduin = serialport('COM18',9600); %シリアルポートに接続
fopen(arduin);
fwrite(arduin,'a')
gx = zeros(100,1); %ここにデータを保存
uicontrol('String','Pause','Callback','jin=0;');
jin=1;
while(jin)
gx(end) = fscanf(arduin,'%d')
drawnow;
plot(gx)
gx(1:end-1)=gx(2:end);
title('mpu signal');
xlabel('time[s]');
ylabel('angle');
end
fclose(arduin);
% csvwrite('raw.txt',theta)
とすると、データをリアルタイムで表示することは出来るのですが、例えばArduinoのコードを
Serial.print(gy); Serial.print(" , "); %%これはArduinoコードの一部です
Serial.println(gx);
といった感じで2つのデータを表示、保存したい場合はどのようにコードを変更すればよいのでしょうか?
ご教授お願いいたします。
0 Comments
Answers (1)
Atsushi Ueno
on 1 Oct 2022
Edited: Atsushi Ueno
on 1 Oct 2022
gxを1列から2列に増やせば、新たな変数gyを作らなくても2つのデータを同時に扱えます。
arduin = serialport('COM18',9600); %シリアルポートに接続
fopen(arduin);
fwrite(arduin,'a')
gx = zeros(100,2); % ここにデータを保存(1列⇒2列に拡張)
uicontrol('String','Pause','Callback','jin=0;');
jin=1;
while(jin)
temp = fscanf(arduin,'%d,%d'); % 受信データは[gx; gy]のベクトルで受け取る見込み
gx(end,:) = temp'; % 転置したベクトル1行をgxの最後の行に追加する
drawnow;
plot(gx)
gx(1:end-1,:)=gx(2:end,:); % ここも変更
title('mpu signal');
xlabel('time[s]');
ylabel('angle');
end
fclose(arduin);
csvwrite('raw.txt',gx); % 保存はコメントアウトされたこのコードでOK
writematrix(gx,'raw.txt'); % R2022aなら、開発が続いているこっちの方が良いです
4 Comments
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!