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

twoshimmerexamplelegacy(comPort1, comPort2, captureDuration)
function void = twoshimmerexamplelegacy(comPort1, comPort2, captureDuration)

%TWOSHIMMEREXAMPLELEGACY - Demonstrate basic features of ShimmerHandleClass
%
%  TWOSHIMMEREXAMPLELEGACY(COMPORT1, COMPORT2, CAPTUREDURATION) plots the 3 
%  accelerometer signals from the Shimmer paired with COMPORT1 and plots 
%  the 3 gyroscope signals from the Shimmer paired with COMPORT2 (assuming 
%  a 9DOF or Gyro daughter board is attached). The function will stream 
%  data for a fixed duration of time defined by the constant CAPTUREDURATION.
%  NOTE: This example uses the method 'getuncalibrateddata' which is a 
%  legacy method from the beta release. The user is advised to use the 
%  replacement method 'getdata' as an alternative (see twoshimmerexample.m).   
%
%  SYNOPSIS: twoshimmerexamplelegacy(comPort1, comPort2, captureDuration)
%
%  INPUT: comPort1 - string value defining the COM port numbers for Shimmer 1
%  INPUT: comPort2 - string value defining the COM port numbers for Shimmer 2
%  INPUT: captureDuration - numerical value defining the period of time 
%                        (in seconds) for which the function will stream 
%                        data from  the Shimmers.
%  OUTPUT: none
%
%  EXAMPLE: twoshimmerexamplelegacy('7','8',30)
%
%  See also plotandwriteexamplelegacy plotandwriteexample 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

%%

shimmer1 = ShimmerHandleClass(comPort1);                                   % Define shimmer1 as a ShimmerHandle Class instance with comPort1
shimmer2 = ShimmerHandleClass(comPort2);                                   % Define shimmer2 as a ShimmerHandle Class instance with comPort2

if (shimmer1.connect && shimmer2.connect)                                  % TRUE if both shimmer 1 and shimmer 2 connect
    
    % Define settings for shimmer1
    shimmer1.setinternalboard('None');                                     % Set the shimmer 1 internal daughter board to 'None'
    shimmer1.setenabledsensors('Accel',1);                                 % Enable the shimmer 1 accelerometer
    shimmer1.setsamplingrate(51.2);                                        % Set the shimmer 1 sampling rate to 51.2Hz
    
    iAccelXShimmer1 = shimmer1.getsignalindex('Accelerometer X');          % Determine the column index of the Accelerometer X-axis signal 
    iAccelYShimmer1 = shimmer1.getsignalindex('Accelerometer Y');          % Determine the column index of the Accelerometer Y-axis signal 
    iAccelZShimmer1 = shimmer1.getsignalindex('Accelerometer Z');          % Determine the column index of the Accelerometer Z-axis signal
  
    
    % Define settings for shimmer2
    shimmer2.setinternalboard('9DOF');                                                % Set the shimmer 2 internal daughter board to '9DOF', the default setting is 'None'
    shimmer2.setexternalboard('ExpBoard');                                                % Set the shimmer 2 external daughter board to 'AnEx', the default setting is 'None'
    shimmer2.setenabledsensors('Accel',1,'Gyro',1,'Mag',0','ExpBoard_A0',0,'ExpBoard_A7',0);  % Enable the shimmer 2 accelerometer and gyroscope, disable the magnetometer, AnEx_A0 and AnEX_A7
    shimmer2.setsamplingrate(102.4);                                                  % Set the shimmer 2 sampling rate to 102.4Hz
    
    iGyroXShimmer2 = shimmer2.getsignalindex('Gyroscope X');               % Determine the column index of the Gyroscope X-axis signal 
    iGyroYShimmer2 = shimmer2.getsignalindex('Gyroscope Y');               % Determine the column index of the Gyroscope Y-axis signal 
    iGyroZShimmer2 = shimmer2.getsignalindex('Gyroscope Z');               % Determine the column index of the Gyroscope Z-axis signal
    
    if (shimmer1.start && shimmer2.start)                                  % TRUE if both shimmers start streaming
        
        uncalibDataShimmer1 = []; 
        uncalibDataShimmer2 = [];
        
        h.figure1=figure('Name','Shimmer 1');                              % Create a handle to figure for plotting data from shimmer1
        h.figure2=figure('Name','Shimmer 2');                              % Create a handle to figure for plotting data from shimmer2
        
        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
            
            
            % Read and plot data for shimmer1
            uncalibDataShimmer1 = [uncalibDataShimmer1; shimmer1.getuncalibrateddata];    % Read the uncalibrated data for shimmer1 and add to previous data
                                                                                          % NOTE: The method 'getuncalibrateddata' is a legacy method from the beta release. 
                                                                                          %       The user is advised to use the replacement method 'getdata' as an alternative.
                                                                                          %       (see twoshimmerexample).    
              
                         
            if(length(uncalibDataShimmer1) > NO_SAMPLES_IN_PLOT)                 
               uncalibDataShimmer1=uncalibDataShimmer1((length(uncalibDataShimmer1) - NO_SAMPLES_IN_PLOT):end, :);   % Trim excess previous data from array for plotting purposes
            end
            
            accelDataShimmer1 = [uncalibDataShimmer1(:,iAccelXShimmer1), uncalibDataShimmer1(:,iAccelYShimmer1), uncalibDataShimmer1(:,iAccelZShimmer1)]; % Extract only the columns of accelerometer data
            
            set(0,'CurrentFigure',h.figure1);           
            plot(accelDataShimmer1);                                       % Plot the accelerometer data
            title('Shimmer 1 Accelerometer Data');                         % Add title to the plot
            axis([0 NO_SAMPLES_IN_PLOT 0 4095]);                           % Define min and max values for axis
            legend('Accel X','Accel Y','Accel Z')                          % Add legend to plot
           

             % Read and plot data for shimmer2            
            uncalibDataShimmer2 = [uncalibDataShimmer2; shimmer2.getuncalibrateddata];     % Read the uncalibrated data for shimmer2 and add to previous data
            
            if(length(uncalibDataShimmer2) > NO_SAMPLES_IN_PLOT)
               uncalibDataShimmer2 = uncalibDataShimmer2((length(uncalibDataShimmer2) - NO_SAMPLES_IN_PLOT):end, :);   % Trim excess previous data from array for plotting purposes
            end
            
            gyroDataShimmer2 = [uncalibDataShimmer2(:,iGyroXShimmer2), uncalibDataShimmer2(:,iGyroYShimmer2), uncalibDataShimmer2(:,iGyroZShimmer2)]; % Extract only the columns of gyroscope data
            
            set(0,'CurrentFigure',h.figure2);                     
            plot(gyroDataShimmer2);                                        % Plot the gyroscope data
            title('Shimmer 2 Gyroscope Data');                             % Add title to the plot
            axis([0 NO_SAMPLES_IN_PLOT 0 4095]);                           % Define min and max values for axis
            legend('Gyro X','Gyro Y','Gyro Z')                             % Add legend to plot
                       
            elapsedTime = elapsedTime + toc;                               % Stop timer and add to elapsed time
            tic;                                                           % Start timer
            
        end  
        
        elapsedTime = elapsedTime + toc;                                   % Stop timer
        
        shimmer1.stop;                                                     % Stop data streaming from shimmer1                                                    
        shimmer2.stop;                                                     % Stop data streaming from shimmer2
        
    else
        
        shimmer1.stop;                                                     % Stop data streaming from shimmer1 (if it has started streaming)                                                
        shimmer2.stop;                                                     % Stop data streaming from shimmer2 (if it has started streaming)  
        
    end            
    
    shimmer1.disconnect;                                                   % Disconnect from shimmer1
    shimmer2.disconnect;                                                   % Disconnect from shimmer2    
    
end

clear all;                                                                 % Remove all variables from memory

Contact us