2016-05-04 23 views
0

私は何度も尋ねられている問題を認識していますが、多くのよく似た質問を読んだ後、私はまだこの問題を理解し解決できません。私は初心者のコーダーであり、まだ学んでおり、何日もこの問題を解決することができませんでした。uint8_t *からuint8_tへの変換が無効です。f ++

私はarduinoのデモコードライブラリを使用しており、C++ Atmel Studio 7(ATSAMD21ベースのカスタムボード用にコンパイル)でコンパイルしようとしています。ここに私の関連するコード(すべての無関係な部分を除去)である:

#include <Arduino.h> 
#include <Wire.h> 
#include "Kalman.h" // Source: https://github.com/TKJElectronics/KalmanFilter 
//Beginning of Auto generated function prototypes by Atmel Studio 
uint8_t i2cWrite(uint8_t registerAddress, uint8_t data, bool sendStop); 
uint8_t i2cWrite(uint8_t registerAddress, uint8_t data, uint8_t length, bool sendStop); 
uint8_t i2cRead(uint8_t registerAddress, uint8_t data, uint8_t nbytes); 
//End of Auto generated function prototypes by Atmel Studio 

#define RESTRICT_PITCH // Comment out to restrict roll to ±90deg instead - please read: http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf 

Kalman kalmanX; // Create the Kalman instances 
Kalman kalmanY; 

/* IMU Data */ 
double accX, accY, accZ; 
double gyroX, gyroY, gyroZ; 
int16_t tempRaw; 

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 

uint32_t timer; 
uint8_t i2cData[14]; // Buffer for I2C data 

// TODO: Make calibration routine 

#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL) 
    // Required for Serial on Zero based boards 
    #define Serial SERIAL_PORT_USBVIRTUAL 
#endif 

void setup() { 
    Serial.begin(115200); 
    Wire.begin(); 
    //TWBR = ((F_CPU/400000L) - 16)/2; // Set I2C frequency to 400kHz 

    i2cData[0] = 7; // Set the sample rate to 1000Hz - 8kHz/(7+1) = 1000Hz 
    i2cData[1] = 0x00; // Disable FSYNC and set 260 Hz Acc filtering, 256 Hz Gyro filtering, 8 KHz sampling 
    i2cData[2] = 0x00; // Set Gyro Full Scale Range to ±250deg/s 
    i2cData[3] = 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[0] != 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[0] << 8) | i2cData[1]; 
    accY = (i2cData[2] << 8) | i2cData[3]; 
    accZ = (i2cData[4] << 8) | i2cData[5]; 

    // 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; 

    timer = micros(); 
} 

void loop() { 
    /* Update all the values */ 
    while (i2cRead(0x3B, *i2cData, 14)); 
    accX = ((i2cData[0] << 8) | i2cData[1]); 
    accY = ((i2cData[2] << 8) | i2cData[3]); 
    accZ = ((i2cData[4] << 8) | i2cData[5]); 
    tempRaw = (i2cData[6] << 8) | i2cData[7]; 
    gyroX = (i2cData[8] << 8) | i2cData[9]; 
    gyroY = (i2cData[10] << 8) | i2cData[11]; 
    gyroZ = (i2cData[12] << 8) | i2cData[13]; 

    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; 
    //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; 

uint32_t time = millis(); 

    /* Print Data */ 
#if 1 // Set to 1 to activate 
    Serial.print(accX); Serial.print("\t"); 
    Serial.print(accY); Serial.print("\t"); 
    Serial.print(accZ); Serial.print("\t"); 

    Serial.print(gyroX); Serial.print("\t"); 
    Serial.print(gyroY); Serial.print("\t"); 
    Serial.print(gyroZ); Serial.print("\t"); 

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

    Serial.print("\t"); 
#endif 
#if 0 
    Serial.print(roll); Serial.print("\t"); 
    Serial.print(gyroXangle); Serial.print("\t"); 
    Serial.print(compAngleX); Serial.print("\t"); 
    Serial.print(kalAngleX); Serial.print("\t"); 

    Serial.print("\t"); 

    Serial.print(pitch); Serial.print("\t"); 
    Serial.print(gyroYangle); Serial.print("\t"); 
    Serial.print(compAngleY); Serial.print("\t"); 
    Serial.print(kalAngleY); Serial.print("\t"); 
#endif 
#if 1 // Set to 1 to print the temperature 
    Serial.print("\t"); 

    double temperature = (double)tempRaw/340.0 + 36.53; 
    Serial.print(temperature); Serial.print("\t"); 
#endif 

    Serial.print("\r\n"); 
    //delay(2); 
}  

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); // INVALID CONVERSION ERROR HERE 
} 

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[i] = Wire.read(); 
    else { 
     timeOutTimer = micros(); 
     while (((micros() - timeOutTimer) < I2C_TIMEOUT) && !Wire.available()); 
     if (Wire.available()) 
     data[i] = Wire.read(); 
     else { 
     Serial.println(F("i2cRead timeout")); 
     return 5; // This error value is not already taken by endTransmission 
     } 
    } 
    } 
    return 0; // Success 
} 

上記のコードは、コル54ライン195にi2cWrite関数でエラーを返す:

「uint8_tの* {別名unsigned char型の*}から無効な変換'to' uint8_t {aka unsigned char} '-fpermissive

最初に上記のコードを修正し、行43,46,55,83のi2cWrite/i2cRead配列に*アスタリスクを追加したことに注意してください。これらのすべての行で同じ正確なエラーが発生します。元のコードはこれらの*参照を持たなかったかもしれないので、私はそれらのポインタを追加することを想定していませんでした...?

私はポインタとリファレンスについて学んでいますが、苦労しています。私の人生のために、私はこのエラーを解決する方法を理解できません。私は様々な&と*を試しましたが、私の人生はこの問題を理解して訂正することができません。私はちょうど私のコードがuint8_tをuint8_tに割り当てる方法と場所を理解しているようです。

他のトピックでは、これらの変数にvolatileまたはconstをキャストまたは使用する必要がありますか?私はそうは思わないが、もう一度私は初心者だ。

誰かが私を正しい方向に向けること、または解決策を理解するのに役立つことに非常に感謝します。 Arduinoではこのコードをコンパイルして実行できますが、Atmel Studioでは実行できません。どんな助けでも大歓迎です。

編集:エラーと行番号が投稿と一致するようにコードを更新してコメントを削除しました。行#に混乱の謝罪。

+0

1つはポインタであり、もう1つはポインタではありません。これらは互換性のない型であり、暗黙的な変換は適用できません。 –

+0

コードの行195は、複数行のコメントの途中で正しく表示されます。エラーの原因となっているコード行を強調表示できますか? –

+0

星を置く場所を推測するのは良いプログラムではありません。エラーメッセージが消えても、コードはまだ間違っている可能性があります –

答えて

1

関数を呼び出す前に宣言する必要があります。

i2cWriteの定義では、引数としてポインタを受け取るオーバーロードの宣言を見ることができません。そのため、コンパイラは関数を再帰的に呼び出すことを前提としています。引数の型が間違っています。

+0

ありがとうございますが、i2cWrite関数とi2cRead関数は5,6,7行目の最初に宣言していませんか?どんな助けでもThx。 – RogerPodacter

1

これは問題である:uint8_tuint8_t、そしてbool

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

最初の線で示すように、i2cWriteは3つの引数を取ります。しかし、あなたはそれを4つの引数:uint8_t,uint8_t *,int,boolと呼んでいます。

その後、4つの引数を取るi2cWriteという別の関数を宣言します。これは許可されていません。

私は、これらの関数が異なる3つの引数と4つの引数を持つ名前を持つことを望んでいると推測しています。

+0

私はこれらの機能を排除しましたが、エラーは残ります。 3つの変数を持つi2cWriteはすべてのインスタンスに対してi2cWrite1になりましたが、エラーはまだ発生しています。それは実際に行44 Col 35と行195 53で発生しました.uint8_t *からuint8_tへの無効な変換。 – RogerPodacter

+0

私はばかだ、あなたは正しい。あなたは私の宣言について考えさせてくれました。私は関数の宣言でderefernce *を忘れていました。それが追加され、コードはエラーなしでコンパイルされます。私の友人はとても感謝しています!私は答えとして選択します。 – RogerPodacter

0

両方の回答に感謝します。解決策は、私の最初の機能プロトタイプはわずかに異なっていました。2番目のi2cWrite宣言には、*アスタリスクがありませんでした。それはそれを直した。

関連する問題