【问题标题】:Arduino Uno and Matlab serial communicationArduino Uno 和 Matlab 串行通信
【发布时间】:2014-07-22 14:21:07
【问题描述】:

我遇到了 Arduino Uno 和 Matlab 之间的问题。这个想法是在 Arduino 的模拟引脚上连接一个正弦波发生器,进行 A/D 转换并将结果发送到 Matlab 进行进一步处理(滤波和 FFT)。频率将在 10 到 20 Hz 之间变化。

第一个奇怪的事情是,Arduino 串行终端的值只能在 19200 波特率下看到,尽管在下面的代码中波特率定义为 9600。当我尝试更改终端的波特率时(回到 9600),我只看到垃圾值。

第二个奇怪的事情是,当我在 10 到 20 Hz 的间隔之间更改频率时,串行打印的值看起来不像是正弦信号。但是,当频率稳定在 20 Hz 或 10 Hz 时,输出是稳定的。

这是在 Arduino 上运行的代码:

int values;
float voltage;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
    values = analogRead(1); 
    float voltage = values * (1.0/1023);
    Serial.println(voltage, 3);
    delay(200);
  }     

这是在 Matlab 上运行的代码:

clc;
clear all;
close all;

s = serial('COM12');
set(s, 'InputBufferSize', 1024);                                   
set(s, 'FlowControl', 'none');
set(s, 'BaudRate', 19200);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout',4);

disp(get(s,'Name'));
prop(1)=(get(s,'BaudRate'));
prop(2)=(get(s,'DataBits')); 
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));

disp(['Port Setup Done!!',num2str(prop)]);
fopen(s); %opens the serial port

disp('Running');
buf_len = 1024;
index = 1:buf_len;
Raw = zeros(size(index));
Data = zeros(size(index));
Fs = 200;
T = 1/Fs;

while 1

    Raw = fscanf(s,'%f');
    disp(num2str(Raw));

    Data = [Data(2:end),Raw];
    subplot(2,1,1);
    plot(Data);
    xlabel('Number of Samples');
    ylabel('Amplitude');
    axis normal;
    drawnow;

    N = length(Data);
    f = [0:N/2]*Fs/N;
    FFT = 2*abs(fft(Data))/N;
    subplot(2,1,2);
    plot(f, FFT(1:N/2+1));
    xlabel('Frequency');
    ylabel('Amplitude |Xf|');
    axis normal;
    drawnow;

最后一个奇怪的事情是,尽管发生器产生 20 Hz 的正弦信号,但 FFT 图显示了不同频率(8 和 18 Hz)的信号。我想我应该看到一个 20 Hz 的信号。使用示波器验证发生器输出。

我希望有人帮我澄清这个问题。我很困惑,我在网上搜索了许多链接以查找 WEEKS,但还没有。原谅我这篇大文章。我试图提供尽可能多的信息。但是,如果有人想了解更多信息,或者我没有提到任何内容,请随时提问。

【问题讨论】:

    标签: matlab arduino


    【解决方案1】:

    你的程序有很多错误。

    1. 您为 arduino 和 PC 设置了不同的波特率。
    2. 你得到正确的波形了吗?你还没有提到它。
    3. 也发送硬件电路。
    4. 你可以试试这个 f = [0:N/2]*500/N; f=f/.05

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多