How to synchronize Arduino Due and Arduino Uno without data acquisition delay in Matlab??
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have interfaced Arduino due board and Arduino Uno boards with Matlab GUI to acquire data streams real time. Arduino due board is operated at 3.3V and sensor is connected via SDA/SCL and Arduino Uno is operated at 5V and analog sensors are connected to the device. When I run the GUI program, I get real time data from arduino due but data coming from Arduino Uno will get delayed and the delay time is increased with the progress.
How can I synchronize the 2 devices to get rid of delay???
0 Commenti
Risposte (1)
Mark Sherstan
il 22 Mar 2019
Could you just add your analog sensors to the due or the I2C device to the Uno so that all information is sent as one package?
Another solution is to introduce a "handshake". Basically you do a 1 for 1 data exchange. MATLAB sends some character and the Ardiuno will send a data value back and wont continue until both sets of data are acquired. This can slow down your data rate but is a good way to gaurntee you arent loosing data and that everything is synced.
Heres a quick example of what I am talking about:
MATLAB
% s is the serial port object...
fprintf(s, 'a'); % Send character to ardiuno
out = fscanf(s, '%40s\n'); % Read data from ardiuno
split = strsplit(out, ','); % Seperate data based off commas
% Continue to process...
ARDUINO
void setup(){
Serial.begin(115200);
Serial.setTimeout(3);
... set up
}
void loop(){
... data acquistion and processing
if (Serial.available() > 0) {
incomingString = Serial.readString();
if (incomingString == "a\n") {
Serial.print(data1); Serial.print(","); Serial.println(data2);
}
}
}
2 Commenti
Mark Sherstan
il 22 Mar 2019
Consider using a logic level converter for the I2C device so that you can work with 5V or going the other way you could make a voltage divider to ensure 3.3V is not exceeded.
Can you please post your code? How do you know there is no data loss? Are you sampling at the same rate on both devices? Can you use a handshake? A couple things you can implment if you havent already.
ARDUINO Sample Rate Stabilization:
// Variable Definition
... Other variables
long loopTimeMicroSec = 5000;
void setup() {
... Startup
// Reset the timer
timer = micros();
}
void loop() {
// Stabilize sampling rate
timeSync(loopTimeMicroSec);
... Rest of code
}
void timeSync(unsigned long deltaT){
// Calculate required delay to run at 200 Hz
unsigned long currTime = micros();
long timeToDelay = deltaT - (currTime - timer);
if (timeToDelay > 5000){
delay(timeToDelay / 1000);
delayMicroseconds(timeToDelay % 1000);
} else if (timeToDelay > 0){
delayMicroseconds(timeToDelay);
} else {}
timer = currTime + timeToDelay;
}
MATLAB Real Time Plotter:
function [] = realTimePlotter()
% Set up figure, get properties, and label
figure
h = animatedline;
ax = gca;
ax.YLim = [0 5];
xlabel('Time (s)')
ylabel('Output Y [units]')
% Start a timer
tic
startTime = toc;
% Loop for 20 secounds
while toc < 20
% Random data
v = randi(4);
% Get current time and add to animation
t = toc - startTime;
addpoints(h,t,v)
% Update axes
if t < 5
ax.XLim = [0 10];
drawnow
else
ax.XLim = [t-5 t+5];
drawnow
end
end
Vedere anche
Categorie
Scopri di più su MATLAB Support Package for Arduino Hardware in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!