필터 지우기
필터 지우기

Slow communication speed with Arduino

조회 수: 14 (최근 30일)
gdz
gdz 2022년 12월 23일
댓글: gdz 2022년 12월 25일
Hi,
I am using a MAX6675 to measure the temperature of thermocouple. I tried to measure the temperature in two ways. Measure ways: 1. Arduino IDE and 2. Matlab read temperature by using a MAX6675 connected to Arduino. Their execution time is quite different.
%Arduino
void loop() {
unsigned long timeBegin = micros();
float temperature = module.readCelsius();
unsigned long timeEnd = micros();
unsigned long duration = timeEnd- timeBegin;
Serial.print("Temperature: ");
serial.print(temperature);
Serial.println(F("c"));
serial.print("Duration time (us):");
serial.println(duration);delay(1000);
}
The exercution time for Arduino IDE is about 520 microseconds.
%Matlab read temperature by using a MAX6675 connected to Arduino
tic;
T=readCelcius(a,cs,sclk);
toc;
disp(T);
The Matlab elapsed time is 1.616992 seconds.
The execution speed of these two ways are different by 3100 times!! While they are using the same function readCelcius.
How can I make the speed of connecting Arduino to the Matlab execute much more faster?
Any suggestion will be appreciated. Thank you!

채택된 답변

Walter Roberson
Walter Roberson 2022년 12월 24일
To use readCelcius from MATLAB you need to have used arduino() first. The function then places a spiread() call on the arduino object and manipulates the results.
The arduino() interface works by having a command monitor on the arduino side, and the MATLAB side sends a command to the arduino side, which the arduino side has to interpret and place the appropriate commands locally, and then the arduino side has to package the results and send them back, and the MATLAB side has to decode and return appropriate results.
This requires multiple encoding and decoding steps, and requires two or more trips across the USB/serial interface, which involves a packetized command-and-response protocol.
This is, of course, much slower than native code on the arduino that can make direct calls.
I would not expect the slowdown to be as high as you are observing, but it is going to be notably slower.
You can reduce the time by having a custom sketch on the arduino side that listens for requests (anything i/o will do) and runs the spi commands directly and decodes to a temperature that you send back. This would reduce encoding and decoding time but not bus time.
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 12월 24일
It would be similar to what you already have, but you would add code that looped waiting for input, calling module.readCelsius() and writing out the response. To reduce decoding time, just send the temperature back without the text header. For technical reasons if you can get the response to be 4 bytes or fewer then the exchange can be more efficient (at least in theory)
gdz
gdz 2022년 12월 25일
you would add code that looped waiting for input, calling module.readCelsius() and writing out the response
Everything of the code is in Matlab now. Is this means I have to adding code in the arduino loop, that calling for module.readCelcius? Also the code in the arduino have to run first (module.readCelcius is reading now) and the matlab will read the reading on arduino side and display on Matlab.
However, this is different from understood. From my understanding, it is not able to run both arduino and matlab at the same time, because there is only one port for my PC.
Please correct me if I'm wrong.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 12월 24일
편집: Sulaymon Eshkabilov 2022년 12월 24일
Note that in Arduino IDE: delay() command does not give a fixed sampling time.
You had better use: millis()
You can also try to use Data Streamer MS Excel app to record your Arduino Data.
  댓글 수: 2
gdz
gdz 2022년 12월 24일
I have tried to replace micros() to millis(), millis() gives 0 or 1 only.
With the same function readCelcius, they both give a very different elaspse time.Weird....
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 12월 24일
Use Data Streamer MS Excel app to record your Arduino Data that gives very accurate data export from Arduino into MS Excel.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by