venerdì 18 maggio 2012

Bussola Pitch Roll ed Accelerometro

 Ad integrazione del post precedente si possono leggere i valori di accelerazione triassiale coil seguente sketch

---------------------------------

#include <Wire.h>

#define ADDRESS 0x60        // Defines address of CMPS10


void setup(){
  Wire.begin();  // Conects I2C
  Serial.begin(9600);
}

void loop(){
   byte highByte, lowByte, fine;              

   char pitch, roll;                          
   int bearing;                              
   byte high_x,low_x,high_y,low_y,high_z,low_z;
   int acc_x,acc_y,acc_z;  
  
   Wire.beginTransmission(ADDRESS);           //starts communication with CMPS10
   Wire.send(2);                              //Sends the register we wish to start reading from
   Wire.endTransmission();

   Wire.requestFrom(ADDRESS, 4);              // Request 4 bytes from CMPS10
   while(Wire.available() < 4);               // Wait for bytes to become available
   highByte = Wire.receive();          
   lowByte = Wire.receive();           
   pitch = Wire.receive();             
   roll = Wire.receive();              
  
   bearing = ((highByte<<8)+lowByte)/10;      // Calculate full bearing
   fine = ((highByte<<8)+lowByte)%10;         // Calculate decimal place of bearing
  
   display_data(bearing, fine, pitch, roll);  // manda i dati alla seriale

   Wire.beginTransmission(ADDRESS);           //inizia lettura accelerometro
   Wire.send(16);                             
   Wire.endTransmission();

   Wire.requestFrom(ADDRESS, 6);              // Richiede i 6 byte dell'accelerazione
   while(Wire.available() < 6);              
   high_x = Wire.receive();          
   low_x = Wire.receive();           
   high_y = Wire.receive();          
   low_y = Wire.receive();           
   high_z = Wire.receive();          
   low_z = Wire.receive();           
  
   acc_x = ((high_x<<8)+low_x);      // Calcola accel_x
   acc_y = ((high_y<<8)+low_y);      // Calcola accel_y
   acc_z = ((high_z<<8)+low_z);      // Calcola accel_z

   display_acc(acc_x,acc_y,acc_z);
  
   delay(1000);
}

void display_data(int b, int f, int p, int r){    

 
  Serial.print("Bearing = ");                      

  Serial.print(b);                              
  Serial.print(".");
  Serial.println(f);
  Serial.print("Pitch = ");
  Serial.println(p);
  Serial.print("Roll = ");
  Serial.println(r);
}

void display_acc(int x, int y, int z){    //  manda a stampa le accelerazioni
 
  Serial.print("Acc.X = ");                     
  Serial.println(x);                              
  Serial.print("Acc.Y = ");
  Serial.println(y);
  Serial.print("Acc.Z = ");
  Serial.println(z);
}

Nessun commento:

Posta un commento

CH57x

Volevo costruirmi un controller fisico per VLC con almeno un comando rotativo (volevo utilizzare M5Dial) da usare mentre cerco di suonare pe...