Advertisement
Guest User

Untitled

a guest
Jun 20th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.54 KB | None | 0 0
  1. /* This file is part of the Razor AHRS Firmware */
  2.  
  3. // I2C code to read the sensors
  4.  
  5. // Sensor I2C addresses
  6. #define ACCEL_ADDRESS ((int) 0x53) // 0x53 = 0xA6 / 2
  7. #define MAGN_ADDRESS  ((int) 0x1E) // 0x1E = 0x3C / 2
  8. #define GYRO_ADDRESS  ((int) 0x68) // 0x68 = 0xD0 / 2
  9.  
  10. // Arduino backward compatibility macros
  11. #if ARDUINO >= 100
  12.   #define WIRE_SEND(b) Wire.write((byte) b)
  13.   #define WIRE_RECEIVE() Wire.read()
  14. #else
  15.   #define WIRE_SEND(b) Wire.send(b)
  16.   #define WIRE_RECEIVE() Wire.receive()
  17. #endif
  18.  
  19.  
  20. void I2C_Init()
  21. {
  22.   Wire.begin();
  23. }
  24.  
  25. void Accel_Init()
  26. {
  27.   Wire.beginTransmission(ACCEL_ADDRESS);
  28.   WIRE_SEND(0x2D);  // Power register
  29.   WIRE_SEND(0x08);  // Measurement mode
  30.   Wire.endTransmission();
  31.   delay(5);
  32.   Wire.beginTransmission(ACCEL_ADDRESS);
  33.   WIRE_SEND(0x31);  // Data format register
  34.   WIRE_SEND(0x08);  // Set to full resolution
  35.   Wire.endTransmission();
  36.   delay(5);
  37.  
  38.   // Because our main loop runs at 50Hz we adjust the output data rate to 50Hz (25Hz bandwidth)
  39.   Wire.beginTransmission(ACCEL_ADDRESS);
  40.   WIRE_SEND(0x2C);  // Rate
  41.   WIRE_SEND(0x09);  // Set to 50Hz, normal operation
  42.   Wire.endTransmission();
  43.   delay(5);
  44. }
  45.  
  46. // Reads x, y and z accelerometer registers
  47. void Read_Accel()
  48. {
  49.   int i = 0;
  50.   byte buff[6];
  51.  
  52.   Wire.beginTransmission(ACCEL_ADDRESS);
  53.   WIRE_SEND(0x32);  // Send address to read from
  54.   Wire.endTransmission();
  55.  
  56.   Wire.beginTransmission(ACCEL_ADDRESS);
  57.   Wire.requestFrom(ACCEL_ADDRESS, 6);  // Request 6 bytes
  58.   while(Wire.available())  // ((Wire.available())&&(i<6))
  59.   {
  60.     buff[i] = WIRE_RECEIVE();  // Read one byte
  61.     i++;
  62.   }
  63.   Wire.endTransmission();
  64.  
  65.   if (i == 6)  // All bytes received?
  66.   {
  67.     // No multiply by -1 for coordinate system transformation here, because of double negation:
  68.     // We want the gravity vector, which is negated acceleration vector.
  69.     accel[0] = (((int) buff[3]) << 8) | buff[2];  // X axis (internal sensor y axis)
  70.     accel[1] = (((int) buff[1]) << 8) | buff[0];  // Y axis (internal sensor x axis)
  71.     accel[2] = (((int) buff[5]) << 8) | buff[4];  // Z axis (internal sensor z axis)
  72.   }
  73.   else
  74.   {
  75.     num_accel_errors++;
  76.     if (output_errors) Serial.println("!ERR: reading accelerometer");
  77.   }
  78. }
  79.  
  80. void Magn_Init()
  81. {
  82.   Wire.beginTransmission(MAGN_ADDRESS);
  83.   WIRE_SEND(0x02);
  84.   WIRE_SEND(0x00);  // Set continuous mode (default 10Hz)
  85.   Wire.endTransmission();
  86.   delay(5);
  87.  
  88.   Wire.beginTransmission(MAGN_ADDRESS);
  89.   WIRE_SEND(0x00);
  90.   WIRE_SEND(0b00011000);  // Set 50Hz
  91.   Wire.endTransmission();
  92.   delay(5);
  93. }
  94.  
  95. void Read_Magn()
  96. {
  97.   int i = 0;
  98.   byte buff[6];
  99.  
  100.   Wire.beginTransmission(MAGN_ADDRESS);
  101.   WIRE_SEND(0x03);  // Send address to read from
  102.   Wire.endTransmission();
  103.  
  104.   Wire.beginTransmission(MAGN_ADDRESS);
  105.   Wire.requestFrom(MAGN_ADDRESS, 6);  // Request 6 bytes
  106.   while(Wire.available())  // ((Wire.available())&&(i<6))
  107.   {
  108.     buff[i] = WIRE_RECEIVE();  // Read one byte
  109.     i++;
  110.   }
  111.   Wire.endTransmission();
  112.  
  113.   if (i == 6)  // All bytes received?
  114.   {
  115. // 9DOF Razor IMU SEN-10125 using HMC5843 magnetometer
  116. #if HW__VERSION_CODE == 10125
  117.     // MSB byte first, then LSB; X, Y, Z
  118.     magnetom[0] = -1 * ((((int) buff[2]) << 8) | buff[3]);  // X axis (internal sensor -y axis)
  119.     magnetom[1] = -1 * ((((int) buff[0]) << 8) | buff[1]);  // Y axis (internal sensor -x axis)
  120.     magnetom[2] = -1 * ((((int) buff[4]) << 8) | buff[5]);  // Z axis (internal sensor -z axis)
  121. // 9DOF Razor IMU SEN-10736 using HMC5883L magnetometer
  122. #elif HW__VERSION_CODE == 10736
  123.     // MSB byte first, then LSB; Y and Z reversed: X, Z, Y
  124.     magnetom[0] = -1 * ((((int) buff[4]) << 8) | buff[5]);  // X axis (internal sensor -y axis)
  125.     magnetom[1] = -1 * ((((int) buff[0]) << 8) | buff[1]);  // Y axis (internal sensor -x axis)
  126.     magnetom[2] = -1 * ((((int) buff[2]) << 8) | buff[3]);  // Z axis (internal sensor -z axis)
  127. // 9DOF Sensor Stick SEN-10183 and SEN-10321 using HMC5843 magnetometer
  128. #elif (HW__VERSION_CODE == 10183) || (HW__VERSION_CODE == 10321)
  129.     // MSB byte first, then LSB; X, Y, Z
  130.     magnetom[0] = (((int) buff[0]) << 8) | buff[1];         // X axis (internal sensor x axis)
  131.     magnetom[1] = -1 * ((((int) buff[2]) << 8) | buff[3]);  // Y axis (internal sensor -y axis)
  132.     magnetom[2] = -1 * ((((int) buff[4]) << 8) | buff[5]);  // Z axis (internal sensor -z axis)
  133. // 9DOF Sensor Stick SEN-10724 using HMC5883L magnetometer
  134. #elif HW__VERSION_CODE == 10724
  135.     // MSB byte first, then LSB; Y and Z reversed: X, Z, Y
  136.     magnetom[0] = (((int) buff[0]) << 8) | buff[1];         // X axis (internal sensor x axis)
  137.     magnetom[1] = -1 * ((((int) buff[4]) << 8) | buff[5]);  // Y axis (internal sensor -y axis)
  138.     magnetom[2] = -1 * ((((int) buff[2]) << 8) | buff[3]);  // Z axis (internal sensor -z axis)
  139. #endif
  140.  
  141.   }
  142.   else
  143.   {
  144.     num_magn_errors++;
  145.     if (output_errors) Serial.println("!ERR: reading magnetometer");
  146.   }
  147. }
  148.  
  149. void Gyro_Init()
  150. {
  151.   // Power up reset defaults
  152.   Wire.beginTransmission(GYRO_ADDRESS);
  153.   WIRE_SEND(0x3E);
  154.   WIRE_SEND(0x80);
  155.   Wire.endTransmission();
  156.   delay(5);
  157.  
  158.   // Select full-scale range of the gyro sensors
  159.   // Set LP filter bandwidth to 42Hz
  160.   Wire.beginTransmission(GYRO_ADDRESS);
  161.   WIRE_SEND(0x16);
  162.   WIRE_SEND(0x1B);  // DLPF_CFG = 3, FS_SEL = 3
  163.   Wire.endTransmission();
  164.   delay(5);
  165.  
  166.   // Set sample rato to 50Hz
  167.   Wire.beginTransmission(GYRO_ADDRESS);
  168.   WIRE_SEND(0x15);
  169.   WIRE_SEND(0x0A);  //  SMPLRT_DIV = 10 (50Hz)
  170.   Wire.endTransmission();
  171.   delay(5);
  172.  
  173.   // Set clock to PLL with z gyro reference
  174.   Wire.beginTransmission(GYRO_ADDRESS);
  175.   WIRE_SEND(0x3E);
  176.   WIRE_SEND(0x00);
  177.   Wire.endTransmission();
  178.   delay(5);
  179. }
  180.  
  181. // Reads x, y and z gyroscope registers
  182. void Read_Gyro()
  183. {
  184.   int i = 0;
  185.   byte buff[6];
  186.  
  187.   Wire.beginTransmission(GYRO_ADDRESS);
  188.   WIRE_SEND(0x1D);  // Sends address to read from
  189.   Wire.endTransmission();
  190.  
  191.   Wire.beginTransmission(GYRO_ADDRESS);
  192.   Wire.requestFrom(GYRO_ADDRESS, 6);  // Request 6 bytes
  193.   while(Wire.available())  // ((Wire.available())&&(i<6))
  194.   {
  195.     buff[i] = WIRE_RECEIVE();  // Read one byte
  196.     i++;
  197.   }
  198.   Wire.endTransmission();
  199.  
  200.   if (i == 6)  // All bytes received?
  201.   {
  202.     gyro[0] = -1 * ((((int) buff[2]) << 8) | buff[3]);    // X axis (internal sensor -y axis)
  203.     gyro[1] = -1 * ((((int) buff[0]) << 8) | buff[1]);    // Y axis (internal sensor -x axis)
  204.     gyro[2] = -1 * ((((int) buff[4]) << 8) | buff[5]);    // Z axis (internal sensor -z axis)
  205.   }
  206.   else
  207.   {
  208.     num_gyro_errors++;
  209.     if (output_errors) Serial.println("!ERR: reading gyroscope");
  210.   }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement