Specifications

Software
141
used to initialize the module. The function Compass_Disconnect.m is used to close
and clear the serial port object at the end of the session as shown in Figure 5.15.
Figure 5.14 Excerpt from compass initialization code
Figure 5.15 Excerpt from compass disconnection code
5.4.3 Data Collection
Communication with the compass module uses ASCII based commands to set
preferences and acquire data. Two modes of operation are available either "query"
where the compass sends heading data after receiving a request command, or "auto
mode" where the compass continuously sends data with a short delay between sets.
A single query is initiated using Compass_GetReading.m (Figure 5.16) which sends
the ASCII code *Q using fprintf. Two query modes are possible, either direct query
(*Q) or averaged query (*A) where the compass automatically averages the last 20
samples before output. The data are then read from the serial port buffer with fscanf as
a comma separated text string of heading, pitch and roll in degrees
function Compass_Disconnect
global s_compass;
fclose(s_compass) % Disconnect and clean up serial port object
delete(s_compass)
clear s_compass
function Compass_Connect
global s_compass;
s_compass = serial('COM4','BaudRate',19200,... % Create Serial Port Object
'DataBits',8,...
'FlowControl','None',...
'Parity','none',...
'InputBufferSize',512,...
'StopBits',1,...
'ByteOrder', 'littleEndian',...
'ReadAsyncMode', 'continuous',...
'BytesAvailableFcnMode','terminator',...
'Terminator','CR/LF');
fopen(s_compass); % Connect to the device