gpt4 book ai didi

c++ - 使用 Arduino Due 从 MPU-6050 获取/处理正确的值

转载 作者:行者123 更新时间:2023-11-28 05:38:25 26 4
gpt4 key购买 nike

对于决定我今年是否通过的学校项目,我必须使用 MPU-6050 和 Arduino Due。 MPU 通过 I2C 工作,我已经让那部分工作了。我可以获得值及其链接。但是有一个问题,我的代码似乎没有得到正确的值!或者我错误地处理了它们。我将向您展示 Arduino C 代码和我的代码,也许有人知道我做错了什么。

这是带有输出的 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 的 read 和 write 方法,如果你想知道它们的样子:

  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

我会继续努力解决这个问题,因为我不能让这个项目失败哈哈,但是如果有人有答案并且他们可以告诉我我做错了什么以及我应该如何做,那将让我的一年大放异彩。

提前谢谢你!

最佳答案

值的长度为 2 个字节,但您一次输出一个字节。这仅适用于十六进制,但不适用于小数。

请注意 16640=0x4100 和 0x41=65,因此(至少)每行的前两位数字是正确的。

关于c++ - 使用 Arduino Due 从 MPU-6050 获取/处理正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37720822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com