Clear Filters
Clear Filters

How do I export data coming from a sensor?

2 views (last 30 days)
I have a robot that uses LiDAR and GPS to autonomously go to a waypoint (or will), and the devices I use, use telegraph communication. To accomplish this I am sending the comman, then in a while loop I am using fscanf to pick up the data that is coming in. Inside this loop the message is processed and parsed out to leave me with what I need.
Below is the example from the GPS code, I am pulling out the Latitude and Longitude values for waypoint navigation
fwrite(gps, 'unlogall');
fwrite(gps, char(13));
fwrite(gps, char(10));
fwrite(gps, 'log COM1 bestposa ontime .05');
fwrite(gps, char(13));
fwrite(gps, char(10));
fwrite(gps, 'log COM1 bestvela ontime .05');
fwrite(gps, char(13));
fwrite(gps, char(10));
while true
bestpos = fscanf(gps);
bestvel = fscanf(gps);
posbegin = strsplit(bestpos, ',');
messagebegin1 = strcmp(posbegin, '#BESTPOSA');
velbegin = strsplit(bestvel, ',');
velbegin1 = strcmp(velbegin, '#BESTVELA');
if all(messagebegin1 < 1) && all(velbegin1 <1)
bestpos = fscanf(gps);
bestvel = fscanf(gps);
else
posstring = strsplit(bestpos, ';');
velstring = strsplit(bestvel, ';');
headermessage = char(posstring(1,1));
header = strsplit(headermessage, ',');
velheadmes = char(velstring(1,1));
velheader = strsplit(velheadmes, ',');
data3 = char(posstring(1,2));
datamessage = strsplit(data3, ',');
veldata = char(velstring(1,2));
velmessage = strsplit(veldata, ',');
PosSolution = cell2mat(datamessage(1));
positiontype = cell2mat(datamessage(2));
Latency = cell2mat(velmessage(3));
HorizontSpeed = cell2mat(velmessage(5));
Lat = cell2mat(datamessage(3));
Long = cell2mat(datamessage(4));
end
end
How can I get the Lat and Long values outside of the while loop, updating at real time, so I can export them to other code to be integrated and processed to be then be used for motor commands. If that's possible, I also have other devices that will need to be integrated as well. I didn't want all 4 sensors to be run in the same code if possible. I have 3 serial, 1 TCPIP, which will be fed to a motor command "module"
Excuse my sloppy code I am fairly new to all of this.

Answers (1)

Tristan Yang
Tristan Yang on 3 Jan 2018
Please consider using event-driven to work with the data. For both TCPIP and Serial communication, it is possible to work with an asynchronous callback function to process/parse data. Please check the following documentation page for the ' BytesAvailableFcn ':
To export data, you may then consider concatenate the data in the instrument object (both TCPIP and serial objects have UserData that can be used to store persistent data) to save them and/or process with the following:
1. You may use a timer object to further process the received data (e.g. generate motor commands) every certain period of time using the current UserData in serial and TCPIP objects:
2. You may also define a new function and pass in the function handle to the TCPIP/Serial async callback functions, so that each time new data is arrived, you may trigger the further processing function from it. Please refer to code below for an example:
t = tcpip('rhost',rport);
s1 = serial('COM1');
s2 = serial('COM2');
s3 = serial('COM3');
fcnHandle = @furtherOperationOnFullData;
receivingDataTCPIP(fcnHandle)
function furtherOperationOnTCPData(data)
plot(data); % e.g plotting all the data
end
function receivingDataTCPIP(fcnHandle) % similar function for serials
t = tcpip('127.0.0.1', 5000);
t.BytesAvailableFcn = {@handleData, fcnHandle}; % pass along the function handle
fopen(t);
end
function handleData(src, fcnHandle) % src is the TCPIP object
if (isempty(src.UserData))
src.UserData = zeros(1000,1); % initializing, please customize
elseif (numel(src.UserData) < 1000) % this is only receiving the first 1000 data points, please modify this for your use case
src.UserData = [src.UserData, fread(t, src.BytesAvailable, 'int')]; % concatenate new data to old data
fcnHandle(src.UserData); % actually calling the function furtherOperationOnFullData when data is all received
else
fclose(t); % close the connection to stop the callback
end
end

Products

Community Treasure Hunt

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

Start Hunting!