This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Reading and displaying accelerometer data from sensortag cc2541/cc2540 using matlab

Other Parts Discussed in Thread: CC2540, CC2650

I'm using matlab to developp some application with ti-sensortag and cc2540 bluetooth dongle and my work is based on : https://github.com/sid5291/SensorTag-Matlab, and BleHealth Demo

so, the connection between matlab and the sensortag is established and i can send and recive data correctly, the broblem is that im using a loop (while(1)) to read data from accelerometer, for each iteration in the loop i open the serial port,  send commande and recive data, those operation take a long time (1 s for each iteration) so i'm asking if there is ather solution to get data faster and plot the in real time (like demo blehealth )

there is my code, if any one can help me to modify it to be more faster and to skip all warning in matlab: 

clear all
close all
clf


% HCI_TXRX used to send and recive data from serial port

function [result] = HCI_TXRX(data,type)
    ADDRESS_TYPE  = 1;
    if(nargin < 2)
        type = 0;
    end
    s = serial('COM3');
    set(s,'BaudRate',115200,'Timeout',0.2,'FlowControl','none','Parity','none');
    dec=hex2dec(data);
    fopen(s);
    fwrite(s,char(dec)');
    if(type == 1)
       temp_h = dec2hex(0,2);
       i = 1;
       while (1)
           temp = fread(s,1);
           if(isempty(temp))
               continue;
           end
           temp_h = [temp_h; dec2hex(temp,2)];
           i =i+1;
           if(strcmp(temp_h(i-1,:),'01') && strcmp(temp_h(i,:),'06'))% Discovery Done
               break;
           end
       end
    end
    tic;
    while(toc < 2)
    end
    [A,count] = fread(s);
    fclose(s);
    h=dec2hex(A);
    if(type==1)
        h = [temp_h;h];
    end
    %disp('Raw Data:')
    %Hex_disp(h);
    i = 1;
    while(i<=length(h))
        if(h(i,:) == '04')
                data_length = hex2dec(h(i+2,:));
                i = i+3;
                payload = h(i:(data_length+i-1),:);
                i = i + (data_length-1);
                %disp('Payload:')
                %Hex_disp(payload);
                if(payload(3,:) == '00')
                    % disp('Success')
                    if(type == 1)
                        if(strcmp(payload(1,:),'01') && strcmp(payload(2,:),'06')) % Discovery Done
                           % disp('Discovery Done')
                            devices = hex2dec(payload(4,:));
                            % fprintf('Number of Devices %d\n',devices);
                            % disp('Addresses');
                            size = length(payload);
                            for m = 1:devices
                                address = payload(size-5:size,:); % 6 byte MAC Address 
                               % fprintf('Device %d MAC Address(reversed):\n',m);
                               % Hex_disp(address,ADDRESS_TYPE)
                                size = size - 8;
                            end
                        end 
                    elseif(type == 4)
                        if(strcmp(payload(1,:),'0B') &&strcmp(payload(2,:),'05'))  % GATT_ReadRsp
                            result = payload((length(payload)-3):length(payload),:);
                            %disp('Raw Result')
                            %disp(result)
                        end
                    end
                elseif(payload(3,:)== '12')
                    disp('Not in Correct State')
                elseif(payload(3,:) == '11')
                    disp('Already Performing task')
                else
                    disp ('Error')
                end
        end
        i=i+1;
    end     
end

% function Hex_disp is used to display data 

function Hex_disp(data,type)
    if(nargin < 2)
        type = 0;
    end
    h_disp = ' ';
    for i=1:length(data)
         if(type == 0)
            h_disp=[h_disp data(i,:) ' '];
         elseif(type == 1)
             h_disp=[h_disp data(i,:) ':'];
         end    
    end
    disp(h_disp);
end



% main script :





GAP_initialise=['01';'00';'FE';'26';'08';'05';'00';'00';'00';'00';'00';'00';'00'
'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00'
'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'00';'01'
'00';'00';'00'];

GAP_DeviceDiscovery = ['01';'04';'FE';'03';'03';'01';'00'];

SCAN_TYPE = 1;
GAP_connect = ['01'; '09'; 'FE'; '09'; '00'; '00'; '00' ;'52';'DE'; 'AB' ;'29' ;'6A' ;'BC'];  %BC:6A:29:AC:50:46 (1325 Tag)
CONNECT_TYPE = 2;

% Reference processors.wiki.ti.com/.../BLE_SensorTag_GATT_Server.pdf

% 0x31 is the address of the acceloremeter config in the GATT server

GATT_AccOn = ['01'; '92'; 'FD'; '05'; '00'; '00'; '31'; '00'; '01'];
GATT_AccOff = ['01'; '92'; 'FD'; '05'; '00'; '00'; '31'; '00'; '00'];

% 0x31 is the address of the acceloremeter config in the GATT server


GATT_AccPerMin = ['01'; '92'; 'FD'; '05'; '00'; '00'; '34'; '00'; '0A'];
GATT_AccNoti = ['01'; '92'; 'FD'; '05'; '00'; '00'; '2E'; '00'; '01'];

WRITE_TYPE = 3;

% 0x2D is the address used to read the acceloremeter data in the GATT server

GATT_AccRd = ['01'; '8A'; 'FD'; '04'; '00'; '00'; '2D' ;'00' ]; 
READ_TYPE = 4;

disp('Going to Intialize');
HCI_TXRX(GAP_initialise);
disp('Going to Scan');
disp('Make Sure Led D1 is blinking on Sensor Tag');
disp('Wait for Scan To End');
input('Press any key to continue');
HCI_TXRX(GAP_DeviceDiscovery,SCAN_TYPE);
disp('Going to Connect to Sensor Tag');
disp('LED D1 will turn off when Connected, if doesnt there is an error');

HCI_TXRX(GAP_connect,CONNECT_TYPE);
disp('Going to Turn On Acc');

HCI_TXRX(GATT_AccOn,WRITE_TYPE);
HCI_TXRX(GATT_AccPerMin,WRITE_TYPE);
% HCI_TXRX(GATT_AccNoti,WRITE_TYPE);
disp('Going to Turn On Accelerometer');

i =1;
clf
while(1)
    result = HCI_TXRX(GATT_AccRd,READ_TYPE);
    
    % result(1,:) is not used (data from acc x:y:z  3 bytes )
    rawval = hex2dec([result(2,:); result(3,:); result(4,:)]);
    
    xval = rawval(1)/64; % calculate acc, unit g, range -2, +2
    yval = rawval(2)/64; 
    zval = rawval(3)/64; 
    
    % show data
    
    fprintf('Acc X: %f \n',xval);
    fprintf('Acc Y: %f \n',yval);
    fprintf('Acc Z: %f \n',zval);
    
    
    %char = input('Press any key to continue (x to exit)','s');
    
    
    % read 10 sample from accerometer 
    
    if(i == 100)
        break;
    end
    
    % update table of samples 
    
    graphe_x(i) = xval;
    graphe_y(i) = yval;
    graphe_z(i) = zval;
    
    % update graph with new samples
    figure(1), plot(graphe_x,'r'), hold on
    plot(graphe_y,'g'), hold on
    plot(graphe_z,'b')
    
    
    % increment counter 
    
    i = i + 1;
    
    
end

 

  • Hi,

    I don't know enough about matlab here, but it seems to me you at least can hoist the port opening out of the TX/RX function. Though, as it's a while 1 loop, possibly you will not be able to correctly close the port on termination. In any case that shouldn't take too long.

    The second reason it takes a long time could be the connection interval. The default should be 100ms, but maybe it's being changed for some reason. After GAP init, send for example "01 30 FE 03 15 28 00" and "01 30 FE 03 16 28 00" to set min/max connection interval to 40ms. (28:00 = 0x0028).

    You can also use BTool to connect, to determine if reading the acc characteristic takes as long manually as with the script. It's hard to say if it's connection parameters or the script itself that delays you here.

    Best regards,
    Aslak

  • Hi !

    Were you using the CC2650 SensorTag or the former version SensorTag.?

    Thanks !

    RH