ADXL345 GY-291 seltsame Maßnahmen

derzeit versuche ich mehrere ADXL345 an einen Arduino Nano anzuschließen. Ich habe das Sparkfun-Tutorial dazu befolgt ( https://www.sparkfun.com/tutorials/240 ). Um es kurz zu machen, ich habe zwei ADXL345 an den Arduino angeschlossen und kann die Maße daraus ablesen, aber die Werte scheinen völlig falsch zu sein.

Wenn ich also flach auf den Tisch lege, würde ich erwarten, dass die Maße ca. 0,0,1 g (x, y, z) betragen.

Wenn ich beide flach auf den Tisch lege bekomme ich:

SelectedChip: 9- -0.5460000038,-0.4679999828,-0.0077999997
SelectedChip: 10- -0.2262000083,-0.5460000038,-0.0077999997

Wenn ich beide Chips um 180 ° drehe, würde ich erwarten, dass die Maße ungefähr 0,0, -1 betragen. Aber ich bekomme:

SelectedChip: 9- -0.6006000041,-0.3977999877,-0.0077999997  <-- Z-Axis does not change at all with this one
SelectedChip: 10- -0.2574000120,-0.4368000030,2.9873998641

Wenn ich beide um 90 ° neige, erwarte ich, dass die Maße ungefähr 0,1,0 betragen. Aber ich bekomme:

SelectedChip: 9- -0.5460000038,-1.6223999023,-0.0077999997
SelectedChip: 10- -0.1949999904,-1.7081999778,-0.0546000003

Wenn ich sie schüttele bekomme ich folgendes:

SelectedChip: 9- -3.3539998531,-3.9935998916,-0.4835999965
SelectedChip: 10- 0.1715999984,-0.0077999997,-0.0077999997
SelectedChip: 9- -0.0311999988,-0.4368000030,-0.0077999997
SelectedChip: 10- -1.8017998695,2.4570000171,-0.0077999997
SelectedChip: 9- -0.2417999982,-0.6083999633,-0.0077999997
SelectedChip: 10- -0.9671999931,-3.4085998535,-0.2963999748
SelectedChip: 9- 0.7409999847,-0.6941999912,-0.0077999997
SelectedChip: 10- -0.7487999916,-3.9935998916,-0.1559999942

Mache ich etwas falsch? Oder denke ich falsch? Glaubst du, dass der Sensor richtig funktioniert? Ich meine, selbst wenn mein Code falsch wäre, sollte er für beide Sensoren falsch sein, oder?


Ich werde den Code auch hier hinzufügen, aber es ist hauptsächlich der Sparkfun-Beispielcode:

//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>

//Assign the Chip Select signal to pin 10.
//int CS=10;
int CS_1=10;
int CS_2=9;

//ADXL345 Register Addresses
#define DEVID   0x00  //Device ID Register
#define THRESH_TAP  0x1D  //Tap Threshold
#define OFSX    0x1E  //X-axis offset
#define OFSY    0x1F  //Y-axis offset
#define OFSZ    0x20  //Z-axis offset
#define DURATION  0x21  //Tap Duration
#define LATENT    0x22  //Tap latency
#define WINDOW    0x23  //Tap window
#define THRESH_ACT  0x24  //Activity Threshold
#define THRESH_INACT  0x25  //Inactivity Threshold
#define TIME_INACT  0x26  //Inactivity Time
#define ACT_INACT_CTL 0x27  //Axis enable control for activity and inactivity detection
#define THRESH_FF 0x28  //free-fall threshold
#define TIME_FF   0x29  //Free-Fall Time
#define TAP_AXES  0x2A  //Axis control for tap/double tap
#define ACT_TAP_STATUS  0x2B  //Source of tap/double tap
#define BW_RATE   0x2C  //Data rate and power mode control
#define POWER_CTL 0x2D  //Power Control Register
#define INT_ENABLE  0x2E  //Interrupt Enable Control
#define INT_MAP   0x2F  //Interrupt Mapping Control
#define INT_SOURCE  0x30  //Source of interrupts
#define DATA_FORMAT 0x31  //Data format control
#define DATAX0    0x32  //X-Axis Data 0
#define DATAX1    0x33  //X-Axis Data 1
#define DATAY0    0x34  //Y-Axis Data 0
#define DATAY1    0x35  //Y-Axis Data 1
#define DATAZ0    0x36  //Z-Axis Data 0
#define DATAZ1    0x37  //Z-Axis Data 1
#define FIFO_CTL  0x38  //FIFO control
#define FIFO_STATUS 0x39  //FIFO status



//This buffer will hold values read from the ADXL345 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
double xg, yg, zg;

//Convert the accelerometer value to G's. 
//With 10 bits measuring over a +/-4g range we can find how to convert by using the equation:
// Gs = Measurement Value * (G-range/(2^10)) or Gs = Measurement Value * (8/1024)
float gMultiplyer = (8/(2^10));

void setup(){ 
  //Initiate an SPI communication instance.
  SPI.begin();
  //Configure the SPI connection for the ADXL345.
  SPI.setDataMode(SPI_MODE3);
  //Create a serial connection to display the data on the terminal.
  Serial.begin(9600);
  
  //Set up the Chip Select pin to be an output from the Arduino.
  pinMode(CS_1, OUTPUT);
  pinMode(CS_2, OUTPUT);
  //Before communication starts, the Chip Select pin needs to be set high.
  digitalWrite(CS_1, HIGH);
  digitalWrite(CS_2, HIGH);
  
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeRegister(DATA_FORMAT, 0x01, CS_1);
  writeRegister(DATA_FORMAT, 0x01, CS_2);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeRegister(POWER_CTL, 0x08, CS_1);  //Measurement mode  
  writeRegister(POWER_CTL, 0x08, CS_2);  //Measurement mode  

  delay(1000);
}

void loop(){
  printValuesFor(CS_1, HIGH);
  printValuesFor(CS_2, HIGH);
  delay(10); 
}

void printValuesFor(int selectedChip, boolean printG){
    //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
  //The results of the read operation will get stored to the values[] buffer.
  readRegister(DATAX0, 6, values, selectedChip);

  //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
  //The X value is stored in values[0] and values[1].
  x = ((int)values[1]<<8)|(int)values[0];
  //The Y value is stored in values[2] and values[3].
  y = ((int)values[3]<<8)|(int)values[2];
  //The Z value is stored in values[4] and values[5].
  z = ((int)values[5]<<8)|(int)values[4];
  
  //Convert the accelerometer value to G's. 
  //With 10 bits measuring over a +/-4g range we can find how to convert by using the equation:
  // Gs = Measurement Value * (G-range/(2^10)) or Gs = Measurement Value * (8/1024)
  xg = x * 0.0078;
  yg = y * 0.0078;
  zg = z * 0.0078;
  
  //Print the results to the terminal.
  Serial.print("SelectedChip: ");
  Serial.print(selectedChip, DEC);
  Serial.print("- ");
  if(printG){
    Serial.print(xg, DEC);
    Serial.print(',');
    Serial.print(yg, DEC);
    Serial.print(',');
    Serial.println(zg, DEC);    
  }else{
    Serial.print(x, DEC);
    Serial.print(',');
    Serial.print(y, DEC);
    Serial.print(',');
    Serial.println(z, DEC);    
  }
}

//This function will write a value to a register on the ADXL345.
//Parameters:
//  char registerAddress - The register to write a value to
//  char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value, int selectedChip){
  //Set Chip Select pin low to signal the beginning of an SPI packet.
  digitalWrite(selectedChip, LOW);
  //Transfer the register address over SPI.
  SPI.transfer(registerAddress);
  //Transfer the desired register value over SPI.
  SPI.transfer(value);
  //Set the Chip Select pin high to signal the end of an SPI packet.
  digitalWrite(selectedChip, HIGH);
}

//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
//  char registerAddress - The register addresse to start the read sequence from.
//  int numBytes - The number of registers that should be read.
//  char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, char * values, int selectedChip){
  //Since we're performing a read operation, the most significant bit of the register address should be set.
  char address = 0x80 | registerAddress;
  //If we're doing a multi-byte read, bit 6 needs to be set as well.
  if(numBytes > 1)address = address | 0x40;
  
  //Set the Chip select pin low to start an SPI packet.
  digitalWrite(selectedChip, LOW);
  //Transfer the starting register address that needs to be read.
  SPI.transfer(address);
  //Continue to read registers until we've read the number specified, storing the results to the input buffer.
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00);
  }
  //Set the Chips Select pin high to end the SPI packet.
  digitalWrite(selectedChip, HIGH);
}
(a) EE.SE-Mitglieder möchten möglicherweise Ihre doppelte Frage in den Arduino-Foren auf dortige Aktualisierungen überprüfen ; (b) Ich würde das Testen auf ein Modul reduzieren und bekanntermaßen guten Code verwenden; Der SparkFun-Tutorial-Code, den Sie verlinkt haben, scheint in späteren Kommentaren "Korrekturen" zu haben, also würde ich in Ihrer Situation an anderer Stelle nach einfacherem Code (der Rohwerte anzeigt) suchen; (c) Ich bin besorgt, dass -0.0077999997so oft darüber berichtet wird – das sagt Ihnen wahrscheinlich etwas , obwohl ich ohne mehr Zeit nicht sagen kann, was ; (d) Versuchen Sie, den Selbsttest für jedes Modul auszuführen. HTH
@SamGibson vielen Dank für deine Antwort. (a) Tut mir leid. (b) Ja, ich habe mit einem Sensor angefangen, aber die Messwerte waren seltsam, also habe ich einen zweiten Sensor zum Vergleich hinzugefügt. Ich habe auch die Korrekturen in den Kommentaren ausprobiert und diese Bibliothek ( github.com/ograff/ADXL345---SP ) verwendet, die wiederum im Grunde der Sparkfun-Code mit der Korrektur ist. Ich bin mir nicht sicher, ob ich "bekanntermaßen guten Code" finden würde? (c) Ja, das ist im Wesentlichen digital 1 * 0.0078. Der Sensor gibt also immer 1 zurück oder mit der Korrektur gibt der Sensor immer 511 zurück. (d) Danke für den Hinweis, daran habe ich nicht gedacht.

Antworten (1)

Ok, ich habe das Problem gefunden. Die Gy-291, die ich gekauft habe, scheinen völlig unkalibriert zu sein. Ich habe sie mit einem Bereich von +-4 g verwendet. Aber gerade die z-Achse ist oft um ca. 8g daneben. Daher waren die Maßnahmen völlig seltsam. Ich bin auch auf die Adafruit-Bibliothek umgestiegen, da sie im Vergleich zum Sparkfun-Code die bessere Lösung zu sein scheint. Die Bibliothek finden Sie hier: https://github.com/adafruit/Adafruit_ADXL345/ .

Hoffe, das hilft jemandem mit dem gleichen Problem.