简体   繁体   English

Arduino Matlab 串口通讯速度

[英]Arduino Matlab serial communication speed

I have some issues in a simple Arduino - Matlab (both 2014 and 2016) serial communication.我在简单的 Arduino - Matlab(2014 和 2016)串行通信中遇到了一些问题。 I have a simple Arduino sketch that collects values from a sensor and send them via serial.我有一个简单的 Arduino 草图,它从传感器收集值并通过串行发送它们。 Arduino waits for a character 'r' for starting the reading/sending procedure Arduino 等待字符 'r' 以启动读取/发送过程

void loop()
{    
  if(Serial.available()) 
  {
    cmd = Serial.read();
    if(cmd == 'r') 
    {
      while(1)
      {
        accelgyro.read();
        //acc
        raw_values[0] = accelgyro.a.x;
        raw_values[1] = accelgyro.a.y;
        raw_values[2] = accelgyro.a.z;    

        //gyro
        raw_values[3] = accelgyro.g.x;
        raw_values[4] = accelgyro.g.y;
        raw_values[5] = accelgyro.g.z;

        for (j=0; j<6; j++)
        {
            Serial.write (highByte(raw_values[j]));
            Serial.write (lowByte(raw_values[j]));
        }     
       delay(2);
      }
    }
  }
}

And the correspondent Matlab code:以及相应的Matlab代码:

Arduino = serial('COM6','BaudRate',115200);
fopen(Arduino);
flushinput(Arduino)

acqSize = 1000;

pause(2)

'start'
fwrite(Arduino,'r');
tStart = tic;

while( i <=acqSize)

    if(Arduino.BytesAvailable>packetSize-1)

        lastData = fread(Arduino,packetSize) ;
        raw_matrix(:,i) =  byteToInt(lastData);
        raw_matrix(7,i) = toc(tStart);
        tStart = tic;
        i=i+1
    end
    pause(0.001);
end

where packetsize is number of bytes sent per cycle from Arduino, ie, 12其中 packetsize 是每个周期从 Arduino 发送的字节数,即 12

The problem is that the speed is really low, I checked the time between two reading and what I obtain is depicted in the following pic问题是速度真的很慢,我检查了两次阅读之间的时间,得到的结果如下图所示在此处输入图片说明

I have a good speed except for these spikes that periodically occur.除了这些周期性出现的尖峰之外,我的速度很好。 In these cases the interval between two readings is greater than 0.1 s.在这些情况下,两次读数之间的间隔大于 0.1 秒。

Timing in Matlab is not vary accurate as the one on your Arduino. Matlab 中的时间与 Arduino 上的时间不一样。 Embedded hardware and small OSs (Operationg systems) like the one used for Arduino are hard real-time and can hold up the timing very accurate.嵌入式硬件和小型操作系统(操作系统)(例如用于 Arduino 的操作系统)是硬实时的,并且可以非常准确地保持计时。 However, Matlab needs to be run on OSs like Windows, Linux or so.但是,Matlab 需要在 Windows、Linux 等操作系统上运行。 These are not real-time OSs and the timing can not be accurate and predictable.这些不是实时操作系统,时间不能准确和可预测。 So the jitter in timing may increase if OS is busy doing something else.因此,如果操作系统忙于做其他事情,时序抖动可能会增加。 Also, "pause(0.001)" is not achievable even for a very strong CPU.此外,即使对于非常强大的 CPU,“暂停(0.001)”也无法实现。 If CPU consumption is not an issue for you, you can remove the "pause" or you can use a code that take the CPU and returns faster like this:如果 CPU 消耗对您来说不是问题,您可以删除“暂停”,或者您可以使用占用 CPU 并更快返回的代码,如下所示:

function delay(seconds)
  % function pause the program
  % seconds = delay time in seconds
  tic;
  while toc < seconds
  end
end

More discussion can be found here: Pause function in matlab for 1 millisecond更多讨论可以在这里找到: Pause function in matlab for 1ms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM