使用Matlab和HCI命令从ti sensortag获取加速度计数据

6kkfgxo0  于 2023-10-23  发布在  Matlab
关注(0)|答案(1)|浏览(174)

我正试图使用matlab从ti sensortag获取传感器数据,我的工作是基于github网站上发布的工作:
https://github.com/sid5291/SensorTag-Matlab
我试图获得加速度计数据,而不是温度和湿度数据(在原来的工作)
这是我的代码,matlab和sensortag之间的连接建立,

  • 当我请求温度信息时,结果是正确的,
  • 当我请求加速度计或陀螺仪数据时,结果错误
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'];  
CONNECT_TYPE = 2;

% Reference    http://processors.wiki.ti.com/images/a/a8/BLE_SensorTag_GATT_Server.pdf
GATT_AccOn  = ['01'; '92'; 'FD'; '05'; '00'; '00'; '34'; '00'; '01'];
GATT_AccOff = ['01'; '92'; 'FD'; '05'; '00'; '00'; '34'; '00'; '00'];
WRITE_TYPE  = 3;
GATT_AccRd  = ['01'; '8A'; 'FD'; '04'; '00'; '00'; '30' ;'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');

input('Press any key to continue');
HCI_TXRX(GAP_connect,CONNECT_TYPE);
disp('Going to Turn On Acc');
input('Press any key to continue');
HCI_TXRX(GATT_IRTAccOn,WRITE_TYPE);
disp('Going to Read from Sensors');
input('Press any key to continue');

while(1)
result = HCI_TXRX(GATT_AccRd,READ_TYPE);
x = hex2dec(result(1,:))/64;
y = hex2dec(result(2,:))/64;
z = hex2dec(result(3,:))/64;

char = input('Press any key to continue (x to exit)','s');
if(char == 'x')
    break;
end
 end

请问谁能帮我解决这个问题

k4ymrczo

k4ymrczo1#

我发现了错误:只需将加速度计服务的正确值设置为:“31”用于配置,“2D”用于阅读数据

clear all
 close all
 clf
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 http://processors.wiki.ti.com/images/a/a8/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

相关问题