爲我的學校項目決定今年是否通過我必須使用MPU-6050和Arduino交付。 MPU通過I2C工作,並讓該部分工作。我可以得到價值觀及其聯繫。但是有一個問題,我的代碼似乎沒有得到正確的值!或者我正在處理他們錯誤。我會告訴你Arduino C代碼和我的代碼,也許有人知道我做錯了什麼。獲取/處理正確的值U從MPU-6050與Arduino交付
下面是Arduino的代碼輸出:
#include<Wire.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print("\n");
//Serial.print(" | GyZ = "); Serial.println(GyZ);
delay(100);
}
隨着輸出:
| AcX = 16676
| AcX = 16628
| AcX = 16740
| AcX = 16692
| AcX = 16660
| AcX = 16676
| AcX = 16780
| AcX = 16684
| AcX = 16612
| AcX = 16644
| AcX = 16588
現在,這似乎是罰款。但我的C++代碼看起來似乎不能正常工作。這裏是:
#include "hwlib.hpp"
#include <iomanip>
int main(void){
// kill the watchdog
WDT->WDT_MR = WDT_MR_WDDIS;
namespace target = hwlib::target;
// int16_t zaxis, yaxis;
byte adress = 0x68;
auto scl = target::pin_oc{ target::pins::scl };
auto sda = target::pin_oc{ target::pins::sda };
auto i2c_bus = hwlib::i2c_bus_bit_banged_scl_sda{ scl,sda };
for(;;){
byte data[8] = {0x3B};
i2c_bus.write(adress, data, 1);
i2c_bus.read(adress, data, 8);
hwlib::cout << (int16_t)data[0] << (int16_t)data[1] << (int16_t)data[2]<< (int16_t)data[3] << (int16_t)data[4] << (int16_t)data[5] << (int16_t)data[6] << (int16_t)data[7] << "\n";
hwlib::wait_ms(500);
}
}
對於想知道hwlib是什麼的人來說,它的圖書館是由學校提供的。所以,你看到的讀寫hwlib的方法,如果你想知道他們是怎麼看:現在
void write(fast_byte a, const byte data[], fast_byte n) override {
write_start();
write_byte(a << 1);
for(fast_byte i = 0; i < n; i++){
read_ack();
write_byte(data[ i ]);
}
read_ack();
write_stop();
}
//
void read(fast_byte a, byte data[], fast_byte n) override {
write_start();
write_byte((a << 1) | 0x01);
read_ack();
for(fast_byte i = 0; i < n; i++){
if(i > 0){
write_ack();
}
data[ i ] = read_byte();
}
write_stop();
}
,似乎它的數據寫入的字節數組,我與函數發送。隨着我的數據,並把它與hwlib :: cout在屏幕上。它的工作原理與普通std :: cout相似,但老師說要使用它。
輸出:
651601120216235240
65522552160248235192
65200120184235240
642280321100235240
64244010812423616
650255208132235224
654004018235224
641320280208235224
654255236102360
65480480200235224
我會繼續嘗試弄明白,因爲我不能失敗,這個項目哈哈,但如果有人有一個答案,他們能告訴我什麼即時做錯了,我應該怎麼這樣做會使我的一年,litterally。
提前謝謝!
這些值爲2個字節長,但一次輸出一個字節。這隻適用於十六進制,但不適用於小數。您還應該在值之間輸出一個空格以使其可讀。請注意,16640 = 0x4100和0x41 = 65,因此(至少)每行的前兩位數似乎是正確的。 –
@KarstenKoop對不起,我忘了回覆,你是對的,可能救了我的一年:)如果你可以把它作爲一個答案。我檢查它,所以問題出現,如答案 – Martacus