📄 readtracks.m
字号:
function tracks = ReadTracks(gps)
% ReadTracks Read all the track data from the GPS
global pid_command_data cmd_transfer_trk pid_xfer_cmplt pid_trk_hdr
global pid_ack_byte pid_nak_byte pid_trk_data
tracks = [];
% Tell the GPS to send the track data to us
WritePacket(gps, pid_command_data, cmd_transfer_trk, 'int16');
% Read standard beginning packet -- the count is the number of packets to
% follow. This stream of packets may consist of multiple tracks.
[id, packets] = ReadPacket(gps, 'int16');
% The latitude and longitude are stored on the GPS as "semicircles", where
% there are 2^31 semicircles in 180 degrees. We want to return the data as
% degrees, so compute a conversion factor.
semi2deg = (180 / (2^31));
% Until the transfer is complete
n = 0;
count = 0;
while (pid_xfer_cmplt ~= id)
count = count + 1;
[id, sz] = ReadPacketHeader(gps);
csum = -1;
disp(['Reading record ' num2str(count) ' of ' num2str(packets+1) '.']);
if (pid_trk_hdr == id)
% Resize the previous track, if necessary
if (n > 0)
tracks(n).waypoints = tracks(n).waypoints(1:w);
end
% Start a new track
n = n + 1;
w = 0;
% Read the header data, which consists of a numeric identifier and
% a null-terminated comment
tracks(n).display = fread(gps, 1);
tracks(n).color = fread(gps, 1);
term = get(gps, 'Terminator');
set(gps, 'Terminator', 0);
tracks(n).comment = fgets(gps);
set(gps, 'Terminator', term);
% Allocate space for the track packets
tracks(n).waypoints(packets) = struct('lat', [], 'long', [], 'time', [], ...
'alt', [], 'depth', [], 'newtrk', []);
% Compute the checksum
csum = ComputeChecksum(id, [tracks(n).display tracks(n).color tracks(n).comment 0]);
disp(['Starting new track. Name: "' tracks(n).comment '"']);
elseif (pid_trk_data == id)
% Read track log data into the current track
w = w+1;
% Read the waypoint data
[ tracks(n).waypoints(w).lat, d ] = ReadData(gps, id, 1, 'int32');
data = d;
tracks(n).waypoints(w).lat = tracks(n).waypoints(w).lat * semi2deg;
[ tracks(n).waypoints(w).long, d ] = ReadData(gps, id, 1, 'int32');
data = [ data d ];
tracks(n).waypoints(w).long = tracks(n).waypoints(w).long * semi2deg;
[ tracks(n).waypoints(w).time, d ] = ReadData(gps, id, 1, 'uint32');
data = [ data d ];
[ tracks(n).waypoints(w).alt, d ] = ReadData(gps, id, 1, 'float32');
data = [ data d ];
% Convert altitude from meters to feet
tracks(n).waypoints(w).alt = tracks(n).waypoints(w).alt * 3.28;
[ tracks(n).waypoints(w).depth, d ] = ReadData(gps, id, 1, 'float32');
data = [ data d ];
[ tracks(n).waypoints(w).newtrk, d ] = ReadData(gps, id, 1, 'uint8');
data = [ data d ];
% Skip three mysterious bytes
d = ReadData(gps, id, 3, 'uint8');
data = [ data d ];
% Compute the checksum
csum = ComputeChecksum(id, data);
elseif (pid_xfer_cmplt == id)
% Resize the current track
tracks(n).waypoints = tracks(n).waypoints(1:w);
% Read the data in the pid_xfer_cmplt packet
data = ReadData(gps, id, sz, 'uint8');
csum = ComputeChecksum(id, data);
else
WritePacket(gps, pid_nak_byte, id);
error(['Unexpected packet id ' num2str(id) ' while reading track log.']);
end
cksum = ReadPacketTerminator(gps);
if (csum ~= cksum)
WritePacket(gps, pid_nak_byte, id);
error(['Checksum mismatch in ReadTracks. Expected: ' num2str(csum) ', Received: ' num2str(cksum)]);
else
WritePacket(gps, pid_ack_byte, id);
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -