TCA9548A驱动多个MPU9250,只能读一个传感器数据-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 537|回复: 2

[未解决] TCA9548A驱动多个MPU9250,只能读一个传感器数据

[复制链接]
发表于 2022-6-26 15:19 | 显示全部楼层 |阅读模式
我在Arduino IDE上使用的是ESP32-S3-DevKitC-1这个板子.

我贴出来了5个GY-91,  1个TCA9548A  和 1个开发板的连线;
I2C扩展用的是1,3,4,5,6这五个;
ESP32设置SDA为18,SCL为19;

这个是代码(用扩展的1和3做测试):


#include <MPU9250_asukiaaa.h>
#include <Arduino.h>
#include "TCA9548A.h"


TCA9548A I2CMux;           
MPU9250_asukiaaa mySensor,mySensor2;

float aX, aY, aZ, aSqrt, gX, gY, gZ, mDirection, mX, mY, mZ;




void setup() {
  Serial.begin(115200);
  while(!Serial);
  Serial.println("started");

  
  Wire.setPins(18, 19);
  I2CMux.begin(Wire);             // Wire instance is passed to the library

  mySensor.setWire(&Wire);
  mySensor.beginAccel();
  mySensor.beginGyro();
  mySensor.beginMag();
  
  mySensor2.setWire(&Wire);
  mySensor2.beginAccel();
  mySensor2.beginGyro();
  mySensor2.beginMag();

}


void loop() {
  
  uint8_t sensorId, sensorId2;
  int result;
  
/////////////////////////////////mySensor///channel 1//////////////////////////////////////////////////////////////////////////////////////
  Serial.print("Opening << Channel: 1");
  I2CMux.openChannel(1);    // 开启通道1
  delay(200);
  Serial.println("Value: ---------------------------------");
  result = mySensor.readId(&sensorId);
  if (result == 0) {
    Serial.println("sensorId: " + String(sensorId));
  } else {
    Serial.println("Cannot read sensorId " + String(result));
  }
  result = mySensor.accelUpdate();
  if (result == 0) {
    aX = mySensor.accelX();
    aY = mySensor.accelY();
    aZ = mySensor.accelZ();
    aSqrt = mySensor.accelSqrt();
    Serial.println("accelX: " + String(aX));
    Serial.println("accelY: " + String(aY));
    Serial.println("accelZ: " + String(aZ));
    Serial.println("accelSqrt: " + String(aSqrt));
  } else {
    Serial.println("Cannod read accel values " + String(result));
  }
  result = mySensor.gyroUpdate();
  if (result == 0) {
    gX = mySensor.gyroX();
    gY = mySensor.gyroY();
    gZ = mySensor.gyroZ();
    Serial.println("gyroX: " + String(gX));
    Serial.println("gyroY: " + String(gY));
    Serial.println("gyroZ: " + String(gZ));
  } else {
    Serial.println("Cannot read gyro values " + String(result));
  }
  result = mySensor.magUpdate();
  if (result == 0) {
    mX = mySensor.magX();
    mY = mySensor.magY();
    mZ = mySensor.magZ();
    mDirection = mySensor.magHorizDirection();
    Serial.println("magX: " + String(mX));
    Serial.println("maxY: " + String(mY));
    Serial.println("magZ: " + String(mZ));
    Serial.println("horizontal direction: " + String(mDirection));
  } else {
    Serial.println("Cannot read mag values " + String(result));
  }
  Serial.println("at " + String(millis()) + "ms");
  Serial.println(""); // Add an empty line
  delay(500);
  Serial.print("Closing >> Channel: ");   
  Serial.println(1);
  I2CMux.closeChannel(1);   // 关闭通道1
  delay(300);


/////////////////////////////////mySensor2///channel 3//////////////////////////////////////////////////////////////////////////////////////
  Serial.print("Opening << Channel: 3");
  I2CMux.openChannel(3);    // 开启通道3
  delay(200);
  Serial.println("Value: ---------------------------------");
  result = mySensor2.readId(&sensorId2);
  if (result == 0) {
    Serial.println("sensorId2: " + String(sensorId));
  } else {
    Serial.println("Cannot read sensorId2 " + String(result));
  }
  result = mySensor2.accelUpdate();
  if (result == 0) {
    aX = mySensor2.accelX();
    aY = mySensor2.accelY();
    aZ = mySensor2.accelZ();
    aSqrt = mySensor2.accelSqrt();
    Serial.println("accelX: " + String(aX));
    Serial.println("accelY: " + String(aY));
    Serial.println("accelZ: " + String(aZ));
    Serial.println("accelSqrt: " + String(aSqrt));
  } else {
    Serial.println("Cannod read accel values " + String(result));
  }
  result = mySensor2.gyroUpdate();
  if (result == 0) {
    gX = mySensor2.gyroX();
    gY = mySensor2.gyroY();
    gZ = mySensor2.gyroZ();
    Serial.println("gyroX: " + String(gX));
    Serial.println("gyroY: " + String(gY));
    Serial.println("gyroZ: " + String(gZ));
  } else {
    Serial.println("Cannot read gyro values " + String(result));
  }
  result = mySensor2.magUpdate();
  if (result == 0) {
    mX = mySensor2.magX();
    mY = mySensor2.magY();
    mZ = mySensor2.magZ();
    mDirection = mySensor2.magHorizDirection();
    Serial.println("magX: " + String(mX));
    Serial.println("maxY: " + String(mY));
    Serial.println("magZ: " + String(mZ));
    Serial.println("horizontal direction: " + String(mDirection));
  } else {
    Serial.println("Cannot read mag values " + String(result));
  }
  Serial.println("at " + String(millis()) + "ms");
  Serial.println(""); // Add an empty line
  delay(500);
  Serial.print("Closing >> Channel: ");   
  Serial.println(3);
  I2CMux.closeChannel(3);   // 关闭通道3
  delay(300);

}



这个是结果:

ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
modeIO, clock div:1
load:0x3fcd0108,len:0x43c
load:0x403b6000,len:0xbd0
load:0x403ba000,len:0x29c8
entry 0x403b61d8
started
Opening << Channel: 1Value: ---------------------------------
sensorId: 113
accelX: 0.25
accelY: 0.44
accelZ: 0.88
accelSqrt: 1.01
gyroX: -5.13
gyroY: -1.46
gyroZ: 0.37
magX: 188.28
maxY: 264.69
magZ: -1095.19
horizontal direction: 35.43
at 486ms

Closing >> Channel: 1
Opening << Channel: 3Value: ---------------------------------
Cannot read sensorId2 2
Cannod read accel values 2
Cannot read gyro values 2
Cannot read mag values 2
at 1789ms

Closing >> Channel: 3
Opening << Channel: 1Value: ---------------------------------
sensorId: 113
accelX: 0.25
accelY: 0.43
accelZ: 0.89
accelSqrt: 1.02
gyroX: -5.13
gyroY: -0.85
gyroZ: -0.67
magX: 187.08
maxY: 262.28
magZ: -1095.19
horizontal direction: 35.50
at 2804ms

Closing >> Channel: 1
Opening << Channel: 3Value: ---------------------------------
Cannot read sensorId2 2
Cannod read accel values 2
Cannot read gyro values 2
Cannot read mag values 2
at 4107ms

Closing >> Channel: 3




很无语啊,不知道怎么回事,读不了其他9250的数据

连线

连线
 楼主| 发表于 2022-6-26 15:20 | 显示全部楼层
有大神能帮忙看下吗?
或者有没有其他驱动多个MPU的方式?
新手脑阔疼了
发表于 2022-7-22 12:51 | 显示全部楼层
参考https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/wiring-and-test
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-29 23:36 , Processed in 0.085411 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表