从uint8_t*到uint8_t fpermission c++的转换无效
Invalid Conversion from uint8_t* to uint8_t fpermissive c++
我意识到这个问题已经被问了很多次,但在读了很多类似的问题后,我仍然无法理解和解决这个问题。我是一个程序员新手,仍在学习,许多天来我一直无法解决这个问题。
我正在使用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("rn");
//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
}
上述代码在第195行Col 54:的i2cWrite函数中出现错误
从"uint8_t*{aka unsigned char*}"到"uint8 _t{akas unsigned char}"的转换无效-fpermission
请注意,我首先修改了上面的代码,并在第43、46、55、83行的i2cWrite/i2cRead数组中添加了一个*星号。如果我不加上这些,那么所有这些行也会出现同样的错误。由于原始代码没有这些*引用,也许我不应该添加这些指针。。。?
我正在努力学习指针和参考资料,但很难。就我的一生而言,我无法理解如何解决这个错误。我试过各种&但我一辈子都无法理解和纠正这个问题。我似乎无法理解我的代码是如何/在哪里试图将uint8_t*分配给uint8_t的。
根据其他主题,我是否需要对这些变量中的任何一个进行强制转换或使用volatile或const?我不这么认为,但我还是个初学者。
如果有人能为我指明正确的方向或帮助我理解解决方案,我将不胜感激。在Arduino中,我可以编译并运行这些代码,但不能在Atmel Studio中。非常感谢您的帮助。
编辑:我已经更新了代码并删除了注释,以便错误和行号与我的帖子相匹配。对第%s行的混乱表示歉意。
您需要在调用函数之前声明它。
在i2cWrite
的定义中,它看不到接收指针作为参数的重载的声明。因此,编译器假设您正在递归调用函数,其中有一个类型错误的参数。
这是一个问题:
uint8_t i2cWrite(uint8_t registerAddress, uint8_t data, bool sendStop) {
return i2cWrite(registerAddress, &data, 1, sendStop); // Returns 0 on success
}
如第一行所示,i2cWrite
采用3个参数:uint8_t
、uint8_t
和bool
。但是你用4个参数来调用它:uint8_t
,uint8_t *
,int
,bool
。
然后,声明另一个名为i2cWrite
的函数,该函数包含4个参数。这是不允许的。
这很难说,但我猜你希望这些函数有不同的名称,3参数的和4参数的。
感谢这两个答案。解决方案只是我的函数原型在一开始略有不同。第二个i2cWrite声明缺少derefence*星号。这就解决了问题。
- 从值小于256的uint16到uint8的Endian安全转换
- 将 Uint8(浮点AUDIO_F32)转换为int16_t(16 位 PCM)
- 错误:无法将'uint8* {aka unsigned int*}'转换为"常量emxArray_uint8_T*"?
- 将具有不同字段的结构转换为 uint8 的数组
- 如何将 uint8 列表转换为其相应的有符号值
- 如何将元素从 Swift 中的 [UnsafeMutablePointer<UInt8>] 转换为 C++ 中的 UInt8 *
- 从字符串转换为 uint8,反之亦然
- 将 UInt8 C++值转换为 C# 字节数组
- 警告缩小转换范围(uint32 到 uint8)
- 将 uint8 转换为范围为 0-1 的浮点数的最有效方法
- 将 Uint8 转换为无符号字符 RGB 值
- 将uint8的矢量转换为ascii十六进制字符串的更好方法
- 从std::string转换为uint8
- Jagged Uint8数组转换为C#字节
- 将"wstring"转换为"const UInt8 *"
- 我如何将bitset转换为字节/uint8数组
- 如何在c++中将UINT8转换为UINT32 ?
- 我如何将uint8_t (SDL中的Uint8)转换为字符串
- 将uint8 vector类型的元素转换为stringstream类型
- Node.js 6增加了将Buffer.from()转换为const UInt8*的功能