Gyroskop (SD740)

 

/*
Gyroscope SD740 SPI connection 
 
 Version 1.0  Lauren <Diese E-Mail-Adresse ist vor Spambots geschützt! Zur Anzeige muss JavaScript eingeschaltet sein!
 >  
 
 Pin connection to the Arduino board:
 
 CSN         ->   digital pin 10(D10) 
 MOSI/SDA    ->   digital pin 11(D11)
 MISO/ADDR   ->   digital pin 12(D12)
 SCL         ->   digital pin 13(D13)
 
 Operating Supply Voltage           V    2.6 - 5% - 3.3 + 5%
 Full Scale Range (digital output) °/s  1024
 Sensitivity Accuracy               %    ± 5 
 Sensitivity Error Over Temperature %    ± 5
 Signal Update Rate                 KHz  10
 SPI Communication Speed            MHz  40
 
 Full scale range is factory defined. Please contact SensorDynamics for setting different ranges. Maximum full scale
 factor is 4096°/s
 Bandwidth is factory defined. Please contact SensorDynamics for setting different ranges. Maximum bandwidth is
 150Hz
 
 */


#include <SPI.h>

#define DataRegister 0x00
#define CS_Pin 10            //chip selection pin

char receiveData[128];        //data array
int x,y,z;
float xg,yg,zg;
float absHeading = 0;         // calculate the absolute heading
unsigned long iTime = 0;      

void setup(){

  Serial.begin(9600);

  SPI.begin();
  SPI.setDataMode(SPI_MODE0);

  pinMode(CS_Pin, OUTPUT);// init the chip selection pin
  digitalWrite(CS_Pin, HIGH);

  checkGyroStatus();
  
}

void checkGyroStatus(){

  readRegister(20,1,receiveData);
  Serial.print("check lock state:\t");
  Serial.println(receiveData[0],BIN);//bin; 110 means works well
  
  if(bitRead(receiveData[0],1) == 0 || bitRead(receiveData[0],2) == 0){
    Serial.println("Chip start error! You could reset the arduino board");
    while(1){
    }
  }

  readRegister(70,1,receiveData);
  Serial.print("check standby bit:\t");
  Serial.println(receiveData[0],BIN);//check bit0 0->normal 1->standby
  
}

void loop(){
  
  readRegister(DataRegister, 6, receiveData);

  x = ((int)receiveData[0]<<8)|(int)receiveData[1];
  y = ((int)receiveData[2]<<8)|(int)receiveData[3];
  z = ((int)receiveData[4]<<8)|(int)receiveData[5];

  float scale = 0.03125;// scale: 1024 / 2^15

  xg = x * scale;
  yg = y * scale;
  zg = z * scale;
  float pastTime = float(micros() - iTime) / float(1000000);
  absHeading += zg * pastTime;
  iTime = micros();

  Serial.print(x, DEC);
  Serial.print(',');
  Serial.print(y, DEC);
  Serial.print(',');
  Serial.print(z, DEC); 
  Serial.print((float)xg,2);
  Serial.print("\t");
  Serial.print((float)yg,2);
  Serial.print("\t");
  Serial.print((float)zg,2);
  Serial.println(" °/s");
  
  Serial.print("Heading:");
  Serial.println(absHeading);//Axis Z absolute heading 
  

  delay(1000);

}

void writeRegister(char registerAddress, char value){

  digitalWrite(CS_Pin, LOW);
  SPI.transfer(registerAddress);
  SPI.transfer(value);
  digitalWrite(CS_Pin, HIGH);

}


void readRegister(char registerAddress, int numBytes, char * values){

  char address = 0x80 | registerAddress;
  digitalWrite(CS_Pin, LOW);
  SPI.transfer(address);
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00);
  }
  digitalWrite(CS_Pin, HIGH);

}

 

Link zum dfrobot-Wiki

Anhänge:
Diese Datei herunterladen (SD740_datasheet_v0_4.pdf)Datenblatt SD740[ ]%2012-%09-%26 %1:%Sep%+02:00