持梦追寻 发表于 2015-8-27 22:37:39

I2C与串口通讯的冲突

可以正常使用的APC220收发程序。多次测试没有问题。。。

但是将接收程序与mpu6050的滤波程序结合起来以后就得不到发送的值。大家帮忙看看,找找原因。
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.

This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2 requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").

Contact information
-------------------

Kristian Lauszus, TKJ Electronics
Web : http://www.tkjelectronics.com
e-mail : [email protected]
*/

#include <Wire.h> //mpu6050和arduino通过I2C通信,所以要包含这个库
#include <Kalman.h> //卡尔曼滤波函数
#define RESTRICT_PITCH
Kalman kalmanX; // 对X,Y轴创建卡尔曼实例
Kalman kalmanY;
double val,Val=0;
/* IMU Data */
double accX, accY, accZ;
double gyroX, gyroY, gyroZ;//从mpu6050读出的六个数据

double gyroXangle, gyroYangle; // Angle calculate using the gyro only
double compAngleX, compAngleY; // Calculated angle using a complementary filter
double kalAngleX, kalAngleY; // Calculated angle using a Kalman filter
double angleX, angleY, angle, angleZ; //finall angle
uint32_t timer;
uint8_t i2cData; // Buffer for I2C data

// TODO: Make calibration routine

void setup() {
Serial.begin(9600);
//if (Serial.available() > 0)
   //val = Serial.read();
Wire.begin();
#if ARDUINO >= 157
Wire.setClock(400000UL); // Set I2C frequency to 400kHz
#else
TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
#endif

i2cData = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz
i2cData = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling
i2cData = 0x00; // Set Gyro Full Scale Range to ±250deg/s
i2cData = 0x00; // Set Accelerometer Full Scale Range to ±2g
while (i2cWrite(0x19, i2cData, 4, false)); // Write to all four registers at once
while (i2cWrite(0x6B, 0x01, true)); // PLL with X axis gyroscope reference and disable sleep mode

while (i2cRead(0x75, i2cData, 1));
if (i2cData != 0x68) { // Read "WHO_AM_I" register
Serial.print(F("Error reading sensor"));
while (1);
}

delay(100); // Wait for sensor to stabilize

/* Set kalman and gyro starting angle */
while (i2cRead(0x3B, i2cData, 6));
accX = (i2cData << 8) | i2cData;
accY = (i2cData << 8) | i2cData;
accZ = (i2cData << 8) | i2cData;

// Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
// atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
// It is then converted from radians to degrees
#ifdef RESTRICT_PITCH // Eq. 25 and 26
double roll = atan2(accY, accZ) * RAD_TO_DEG;
double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
#else // Eq. 28 and 29
double roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
#endif

kalmanX.setAngle(roll); // Set starting angle
kalmanY.setAngle(pitch);
gyroXangle = roll;
gyroYangle = pitch;
compAngleX = roll;
compAngleY = pitch;

//Calibration();
timer = micros();
}

void loop() {
//if (Serial.available() > 0)
// val = Serial.read();
double ret(double x);
/* Update all the values */
while (i2cRead(0x3B, i2cData, 14));
accX = ((i2cData << 8) | i2cData);
accY = ((i2cData << 8) | i2cData);
accZ = ((i2cData << 8) | i2cData);
//tempRaw = (i2cData << 8) | i2cData;
gyroX = (i2cData << 8) | i2cData;
gyroY = (i2cData << 8) | i2cData;
gyroZ = (i2cData << 8) | i2cData;

double dt = (double)(micros() - timer) / 1000000; // Calculate delta time
timer = micros();

// Source: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf eq. 25 and eq. 26
// atan2 outputs the value of -π to π (radians) - see http://en.wikipedia.org/wiki/Atan2
// It is then converted from radians to degrees
#ifdef RESTRICT_PITCH // Eq. 25 and 26
double roll = atan2(accY, accZ) * RAD_TO_DEG;
double pitch = atan(-accX / sqrt(accY * accY + accZ * accZ)) * RAD_TO_DEG;
#else // Eq. 28 and 29
double roll = atan(accY / sqrt(accX * accX + accZ * accZ)) * RAD_TO_DEG;
double pitch = atan2(-accX, accZ) * RAD_TO_DEG;
#endif

double gyroXrate = gyroX / 131.0; // Convert to deg/s
double gyroYrate = gyroY / 131.0; // Convert to deg/s

#ifdef RESTRICT_PITCH
// This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
if ((roll < -90 && kalAngleX > 90) || (roll > 90 && kalAngleX < -90)) {
kalmanX.setAngle(roll);
compAngleX = roll;
kalAngleX = roll;
gyroXangle = roll;
} else
kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter

if (abs(kalAngleX) > 90)
gyroYrate = -gyroYrate; // Invert rate, so it fits the restriced accelerometer reading
kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt);
#else
// This fixes the transition problem when the accelerometer angle jumps between -180 and 180 degrees
if ((pitch < -90 && kalAngleY > 90) || (pitch > 90 && kalAngleY < -90)) {
kalmanY.setAngle(pitch);
compAngleY = pitch;
kalAngleY = pitch;
gyroYangle = pitch;
} else
kalAngleY = kalmanY.getAngle(pitch, gyroYrate, dt); // Calculate the angle using a Kalman filter

if (abs(kalAngleY) > 90)
gyroXrate = -gyroXrate; // Invert rate, so it fits the restriced accelerometer reading
kalAngleX = kalmanX.getAngle(roll, gyroXrate, dt); // Calculate the angle using a Kalman filter
#endif

gyroXangle += gyroXrate * dt; // Calculate gyro angle without any filter
gyroYangle += gyroYrate * dt;
angle += gyroZ * dt / 131.0;

gyroXangle += kalmanX.getRate() * dt; // Calculate gyro angle using the unbiased rate
gyroYangle += kalmanY.getRate() * dt;

compAngleX = 0.93 * (compAngleX + gyroXrate * dt) + 0.07 * roll; // Calculate the angle using a Complimentary filter
compAngleY = 0.93 * (compAngleY + gyroYrate * dt) + 0.07 * pitch;

// Reset the gyro angle when it has drifted too much
if (gyroXangle < -180 || gyroXangle > 180)
gyroXangle = kalAngleX;
if (gyroYangle < -180 || gyroYangle > 180)
gyroYangle = kalAngleY;

angleX = kalAngleX - 2.32;
angleY = kalAngleY + 0.38;
angleZ = angle + 5;
Val = ret(val);

/* Print Data */
#if 0 // Set to 1 to activate
Serial.print(accX); Serial.print(",");
Serial.print(accY); Serial.print(",");
Serial.print(accZ); Serial.print(",");
//Serial.print(tempRaw); Serial.print(",");
Serial.print(gyroX / 131.0); Serial.print(",");
Serial.print(gyroY / 131.0); Serial.print(",");
Serial.print(gyroZ / 131.0); Serial.print(",");

Serial.print("\t");
#endif
Serial.println(Val);// Serial.print(",");
//Serial.print(roll); Serial.print("\t");
//Serial.print(gyroXangle); Serial.print("\t");
//Serial.print(compAngleX); Serial.print("\t");
//Serial.print(angleX); Serial.print(",");

//Serial.print("\t");

//Serial.print(pitch); Serial.print("\t");
//Serial.print(gyroYangle); Serial.print("\t");
//Serial.print(compAngleY); Serial.print("\t");
//Serial.print(angleY); Serial.print(",");
//Serial.print(angleZ); Serial.println(",");

}
double ret(double x)
{
if(Serial.available()>0){
x=Serial.read();
}
return(x);
}
还有一个I2C的文件
const uint8_t IMUAddress = 0x68; // AD0 is logic low on the PCB
const uint16_t I2C_TIMEOUT = 1000; // Used to check for errors in I2C communication

uint8_t i2cWrite(uint8_t registerAddress, uint8_t data, bool sendStop) {
return i2cWrite(registerAddress, &data, 1, sendStop); // Returns 0 on success
}

uint8_t i2cWrite(uint8_t registerAddress, uint8_t *data, uint8_t length, bool sendStop) {
Wire.beginTransmission(IMUAddress);
Wire.write(registerAddress);
Wire.write(data, length);
uint8_t rcode = Wire.endTransmission(sendStop); // Returns 0 on success
if (rcode) {
    Serial.print(F("i2cWrite failed: "));
    Serial.println(rcode);
}
return rcode; // See: http://arduino.cc/en/Reference/WireEndTransmission
}

uint8_t i2cRead(uint8_t registerAddress, uint8_t *data, uint8_t nbytes) {
uint32_t timeOutTimer;
Wire.beginTransmission(IMUAddress);
Wire.write(registerAddress);
uint8_t rcode = Wire.endTransmission(false); // Don't release the bus
if (rcode) {
    Serial.print(F("i2cRead failed: "));
    Serial.println(rcode);
    return rcode; // See: http://arduino.cc/en/Reference/WireEndTransmission
}
Wire.requestFrom(IMUAddress, nbytes, (uint8_t)true); // Send a repeated start and then release the bus after reading
for (uint8_t i = 0; i < nbytes; i++) {
    if (Wire.available())
      data = Wire.read();
    else {
      timeOutTimer = micros();
      while (((micros() - timeOutTimer) < I2C_TIMEOUT) && !Wire.available());
      if (Wire.available())
      data = Wire.read();
      else {
      Serial.println(F("i2cRead timeout"));
      return 5; // This error value is not already taken by endTransmission
      }
    }
}
return 0; // Success
}

还有两个.h和.cpp文件,以及上面的程序我都做个打包压缩。

持梦追寻 发表于 2015-8-27 22:40:22

最后一张图片跟主题没有关系,手残了一下,不要意思。

zoologist 发表于 2015-8-28 09:52:17

化简程序吧,太长了不会有人给你看的

wetnt 发表于 2015-8-30 10:48:26

查延时吧,串口数据读取其实是内部中断控制的,程序块内耗费时间太长,串口就无法正常工作。

持梦追寻 发表于 2015-9-3 23:42:33

嗯,其实也这么想过,但是不知从何下手

wetnt 发表于 2015-9-15 10:13:21

所有delay()的地方都检查下,添加些串口检测之类的函数……

持梦追寻 发表于 2015-10-10 01:02:18

已经搞好了,没能及时回你,是我没搞清楚串口通讯的语法,:D:D

OHYE 发表于 2017-5-21 23:27:20

持梦追寻 发表于 2015-10-10 01:02
已经搞好了,没能及时回你,是我没搞清楚串口通讯的语法,

你咋解决的,能说一下吗?

xiao_y 发表于 2020-12-14 19:12:24

持梦追寻 发表于 2015-10-10 01:02
已经搞好了,没能及时回你,是我没搞清楚串口通讯的语法,

是怎么解决多块从机通信的问题的呢?
我的1主机2从机   从机1还正常,从机2就不正常了,
大神可以帮我看看我的I2C通信问题吗?谢谢了
传送门:https://www.geek-workshop.com/thread-39910-1-1.html
页: [1]
查看完整版本: I2C与串口通讯的冲突