角速度+加速度_第1頁
角速度+加速度_第2頁
角速度+加速度_第3頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、#include #define Acc 0x1D#define Gyr 0x69#define Mag 0x1E#define Gry_offset -13 / 陀螺儀偏移量#define Gyr_Gain 0.07 / 滿量程2000dps時靈敏度(dps/digital)#define pi 3.14159float angleG;unsigned long timer = 0; / 采樣時間void setup() sensor_init(); / 配置傳感器 Serial.begin(19200); / 開啟串口以便監(jiān)視數(shù)據(jù) delay(1000); void loop() long

2、 o_timer = timer; / 上一次采樣時間(ms) float Y_Accelerometer = gDat(Acc, 1); / 獲取向前的加速度 float Z_Accelerometer = gDat(Acc, 2); / 獲取向下的加速度 float angleA = atan(Y_Accelerometer / Z_Accelerometer) * 180 / pi; / 根據(jù)加速度分量得到的角度(degree) timer = millis(); / 當前時間(ms) int dt = timer - o_timer; / 微分時間 angleG = angleG +

3、Gyr_Gain * (gDat(Gyr, 0) + Gry_offset) * dt / 1000; / 對角速度積分得到的角度(degree) Serial.print(timer); Serial.print(,); Serial.print(angleA, 6); Serial.print(,); Serial.print(angleG, 6); Serial.print(;); / 輸出數(shù)據(jù) delay(10); int gDat(int device, int axis) / 讀九軸姿態(tài)傳感器寄存器函數(shù)/ For Arduino, by 黑馬/ 調用參數(shù)表/ type device

4、 axis/ 0 1 2/ ADXL345 Acc x y z/ L3G4200D Gyr x y z/ HMC5883L Mag x z y/ Example/ 00 #include / 01 #define Acc 0x1D;/ 02 #define Gyr 0x69;/ 03 #define Mag 0x1E;/ 04/ 05 void setup() / 06 sensor_init();/ 07 delay(1000);/ 08 / 09/ 10 void loop() / 11 int Z-Gyroscope;/ 12 Z-Gyroscope = gDat(Gyr, 2);/ 1

5、3 delay(50);/ 14 int v; byte vL, vH, address; / 存放byte數(shù)值 if (device = Acc) address = 0x32; / ADXL345的讀數(shù)地址 if (device = Gyr) address = 0xA8; / L3G4200D的讀數(shù)地址 if (device = Mag) address = 0x03; / HMC5883L的讀數(shù)地址 address = address + axis * 2; / 數(shù)據(jù)偏移-坐標軸 Wire.beginTransmission(device); / 開始傳輸數(shù)據(jù) Wire.send(ad

6、dress); / 發(fā)送指針 Wire.requestFrom(device, 2); / 請求2 byte數(shù)據(jù) while(Wire.available() 2); / 成功獲取前等待 vL = Wire.receive(); vH = Wire.receive(); / 讀取數(shù)據(jù) Wire.endTransmission(); / 結束傳輸 if (device = Mag) v = (vL 8) | vH; else v = (vH 8) | vL; / 將byte數(shù)據(jù)合并為Int return v; / 返回讀書值void sensor_init() / 配置九軸姿態(tài)傳感器 write

7、Register(Acc, 0x2D, 0b00001000); / 測量模式 / 配置ADXL345 writeRegister(Gyr, 0x20, 0b00001111); / 設置睡眠模式、x, y, z軸使能 writeRegister(Gyr, 0x21, 0b00000000); / 選擇高通濾波模式和高通截止頻率 writeRegister(Gyr, 0x22, 0b00000000); / 設置中斷模式 writeRegister(Gyr, 0x23, 0b00110000); / 設置量程(2000dps)、自檢狀態(tài)、SPI模式 writeRegister(Gyr, 0x24, 0b00000000); / FIFO & 高通濾波 / 配置L3G4200D(2000 deg/sec) writeRegister(Mag, 0x02, 0x00); / 連續(xù)測量 / 配置HMC5883Lvoid writeRegister(int device, byte

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論