- Check Serial Data Availability: Ensure that enough data is available before attempting to read.
- Validate Headers and Data: Add more checks to validate the headers and data bytes.
- Adjust Trigger Threshold: Make the trigger threshold configurable.
- Add Timeout: Add a timeout (using the 'pause' function) to avoid infinite loops in case the trigger condition is never met.
sometimes the function triggers when i trigger the sensor, sometimes it triggers without me triggering it, sometimes it won't trigger even through multiple attempts.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
hi, I am new to this and I have problem with my function. For some reason the software can't sense when I trigger the sensor or it triggers without me triggering the sensor. The result was so random and I couldn't find the problem. Thank you for reading this.
function [accelDataArray, accelDataArray2] = collectSamples(serialObject1, sampleNum, serialObject2, sampleNum2)
sampleCounter1 = 1;
accelDataArray = zeros(sampleNum, 1);
sampleCounter2 = 1;
accelDataArray2 = zeros(sampleNum2, 1);
triggered = false; % Initialize the trigger condition
% Wait for the trigger condition (preliminary loop)
disp('Waiting for trigger...');
%%fix 282 to 312
while ~triggered
if serialObject1.BytesAvailable < 20 || serialObject2.BytesAvailable < 20
continue;
end
header1 = fread(serialObject1, 1);
header2 = fread(serialObject2, 1);
if header1 ~= 85 || header2 ~= 85
fread(serialObject1, 1);
fread(serialObject2, 1);
continue;
end
dataBytes1 = fread(serialObject1, 19);
dataBytes2 = fread(serialObject2, 19);
if dataBytes1(1) ~= 97 || dataBytes2(1) ~= 97
continue;
end
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), 'int16')) / 32768 * 16;
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), 'int16')) / 32768 * 16;
if az > .1 || az2 > .1 % Trigger condition, adjust the threshold as needed
triggered = true;
disp('Triggered, now collecting samples...');
end
end
% Data collection loop
while sampleCounter1 <= sampleNum || sampleCounter2 <= sampleNum2
% Read data from sensor 1
if serialObject1.BytesAvailable >= 20
header1 = fread(serialObject1, 1);
if header1 == 85
dataBytes1 = fread(serialObject1, 19);
if dataBytes1(1) == 97
azL = dataBytes1(6);
azH = dataBytes1(7);
az = double(typecast(uint8([azL azH]), 'int16')) / 32768 * 16;
accelDataArray(sampleCounter1) = az;
sampleCounter1 = sampleCounter1 + 1;
end
end
end
% Read data from sensor 2
if serialObject2.BytesAvailable >= 20
header2 = fread(serialObject2, 1);
if header2 == 85
dataBytes2 = fread(serialObject2, 19);
if dataBytes2(1) == 97
azL2 = dataBytes2(6);
azH2 = dataBytes2(7);
az2 = double(typecast(uint8([azL2 azH2]), 'int16')) / 32768 * 16;
accelDataArray2(sampleCounter2) = az2;
sampleCounter2 = sampleCounter2 + 1;
end
end
end
end
end
0 Kommentare
Antworten (1)
Kautuk Raj
am 14 Aug. 2024
I can observe that you are experiencing inconsistent behaviour with your function, where it sometimes triggers the sensor correctly, sometimes triggers without any input, and sometimes fails to trigger despite multiple attempts.
This inconsistency could stem from several issues:
You can refer to the following resources from the MATLAB documentation:
Serial communication and availability: https://www.mathworks.com/help/matlab/serial-port-devices.html
I hope this helps with your query.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Workspace Variables and MAT-Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!