Code covered by the BSD License  

Highlights from
Shimmer MATLAB Instrument Driver

image thumbnail

Shimmer MATLAB Instrument Driver

by

 

04 May 2012 (Updated )

Object oriented solution for Shimmer sensor data capture directly into Matlab

plotandwriteexample(comPort, captureDuration, fileName)
function void = plotandwriteexample(comPort, captureDuration, fileName)

%PLOTANDWRITEXAMPLE - Demonstrate basic features of ShimmerHandleClass
%
%  PLOTANDWRITEEXAMPLE(COMPORT, CAPTUREDURATION, FILENAME) plots 3 
%  accelerometer signals, 3 gyroscope signals and 3 magnetometer signals,
%  from the Shimmer paired with COMPORT. The function 
%  will stream data for a fixed duration of time defined by the constant 
%  CAPTUREDURATION. The function also writes the data in a tab ddelimited 
%  format to the file defined in FILENAME.
%  NOTE: This example uses the method 'getdata' which is a more advanced 
%  alternative to the 'getuncalibrateddata' method in the beta release. 
%  The user is advised to use the updated method 'getdata'.  
%
%  SYNOPSIS: plotandwriteexample(comPort, captureDuration, fileName)
%
%  INPUT: comPort - String value defining the COM port number for Shimmer
%  INPUT: captureDuration - Numerical value defining the period of time 
%                           (in seconds) for which the function will stream 
%                           data from  the Shimmers.
%  INPUT : fileName - String value defining the name of the file that data 
%                     is written to in a comma delimited format.
%  OUTPUT: none
%
%  EXAMPLE: plotandwriteexample('7', 30, 'testdata.dat')
%
%  See also twoshimmerexample ShimmerHandleClass 


close all                                                                  % close all previous figures

% Note: these constants are only relevant to this examplescript and are not used
% by the ShimmerHandle Class
NO_SAMPLES_IN_PLOT = 500;                                                  % Number of samples that will be displayed in the plot at any one time
DELAY_PERIOD = 0.2;                                                        % A delay period of time in seconds between data read operations

%%

shimmer = ShimmerHandleClass(comPort);                                     % Define shimmer as a ShimmerHandle Class instance with comPort1
firsttime = true;

if (shimmer.connect)                                                       % TRUE if the shimmer connects
    
    % Define settings for shimmer
    shimmer.setsamplingrate(51.2);                                         % Set the shimmer sampling rate to 51.2Hz
    shimmer.setinternalboard('9DOF');                                      % Set the shimmer internal daughter board to '9DOF'
    shimmer.setenabledsensors('Gyro',1,'Mag',1,'Accel',1,'BattVolt',1);                                   % Enable the shimmer 1 gyroscope
    shimmer.setbattlimitwarning(3.4);                                      % This will cause the Shimmer LED to turn yellow when the battery voltage drops below 3.4, note that it is only triggered when the battery voltage is being monitored, and after the getdata command is executed to retrieve the battery data 
    shimmer.setaccelrange(0);                                              % Set the accelerometer range to 0 (+/- 1.5g)
    
    
          
    if (shimmer.start)                                                     % TRUE if the shimmer starts streaming
        
        plotData = [];                                               
        newData = [];
        
        h.figure1=figure('Name','Shimmer 1');                              % Create a handle to figure for plotting data from shimmer
       
        elapsedTime = 0;                                                   % Reset to 0    
        tic;                                                               % Start timer
        
        while (elapsedTime < captureDuration)            
                      
            pause(DELAY_PERIOD);                                           % Pause for this period of time on each iteration to allow data to arrive in the buffer
            
            [newData,signalNameArray,signalFormatArray,signalUnitArray] = shimmer.getdata('Time Stamp','c','Accelerometer','c','Gyroscope','c','Magnetometer','u','Battery Voltage','a');   % Read the latest data from shimmer data buffer, signalFormatArray defines the format of the data and signalUnitArray the unit
           
            if (firsttime==true) 
                signalNamesString = char(signalNameArray(1,1));                                           % Create a single string, signalNamesString
                signalFormatsString = char(signalFormatArray(1,1));                                       % Create a single string, signalFormatsString
                signalUnitsString = char(signalUnitArray(1,1));                                           % Create a single string, signalUnitsString
                for i= 2:length(signalNameArray)                                                          % which lists the names of the enabled 
                    tabbedNextSignalName = [char(9), char(signalNameArray(1,i))];                         % Add tab delimiter before signal name
                    signalNamesString = strcat(signalNamesString,tabbedNextSignalName);                   % Concatenate signal names delimited by a tab.
                 
                    firsttime=false;
                end

                dlmwrite(fileName, signalNamesString, '%s');    % Write the signalNamesString as the first row of the file

            end

            
            if ~isempty(newData)                                           % TRUE if new data has arrived
                
                dlmwrite(fileName, newData, '-append', 'delimiter', '\t'); % Append the new data to the file in a tab delimited format
                            
                plotData = [plotData; newData];                            % Update the plotDataBuffer with the new data

                set(0,'CurrentFigure',h.figure1);           
                subplot(2,2,1);                                            % Create subplot
                plot(plotData(:,1));                                       % Plot the time stamp data
                legend([signalFormatArray{1} ' ' signalNameArray{1} ' (' signalUnitArray{1} ')']);   
                

                subplot(2,2,2);                                            % Create subplot
                plot(plotData(:,[2 3 4]));                                 % Plot the accelerometer data
                legendName1=[signalFormatArray{2} ' ' signalNameArray{2} ' (' signalUnitArray{2} ')'];  
                legendName2=[signalFormatArray{3} ' ' signalNameArray{3} ' (' signalUnitArray{3} ')'];  
                legendName3=[signalFormatArray{4} ' ' signalNameArray{4} ' (' signalUnitArray{4} ')'];  
                legend(legendName1,legendName2,legendName3); % Add legend to plot
                

                subplot(2,2,3);                                            % Create subplot
                plot(plotData(:,[5 6 7]));                                 % Plot the gyroscope data
                legendName1=[signalFormatArray{5} ' ' signalNameArray{5} ' (' signalUnitArray{5} ')'];  
                legendName2=[signalFormatArray{6} ' ' signalNameArray{6} ' (' signalUnitArray{6} ')'];  
                legendName3=[signalFormatArray{7} ' ' signalNameArray{7} ' (' signalUnitArray{7} ')'];  
                legend(legendName1,legendName2,legendName3); % Add legend to plot
                
   
                subplot(2,2,4);                                            % Create subplot
                plot(plotData(:,[8 9 10]));                                % Plot the magnetometer data
                legendName1=[signalFormatArray{8} ' ' signalNameArray{8} ' (' signalUnitArray{8} ')'];  
                legendName2=[signalFormatArray{9} ' ' signalNameArray{9} ' (' signalUnitArray{9} ')'];  
                legendName3=[signalFormatArray{10} ' ' signalNameArray{10} ' (' signalUnitArray{10} ')'];  
                legend(legendName1,legendName2,legendName3); % Add legend to plot
            end
            
            elapsedTime = elapsedTime + toc;                               % Stop timer and add to elapsed time
            tic;                                                           % Start timer           
            
        end  
        
        elapsedTime = elapsedTime + toc;                                   % Stop timer
        fprintf('The percentage of received packets: %d \n',shimmer.getpercentageofpacketsreceived(plotData(:,1))); % Detect loss packets
        shimmer.stop;                                                      % Stop data streaming                                                    
       
    end 
    
    shimmer.disconnect;                                                    % Disconnect from shimmer
        
end


Contact us