Advertisement
blackswords

IMU3000 class

May 27th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. /*
  2.  * IMU3000.cpp
  3.  *
  4.  *  Created on: 27 mai 2012
  5.  *      Author: blackswords
  6.  */
  7.  
  8. #include "IMU3000.h"
  9.  
  10. HardWire *IMUWire;
  11.  
  12. IMU3000 IMU;
  13.  
  14. IMU3000::IMU3000() {
  15.  
  16.     FS = DEG_2000;
  17.  
  18.     LP = F_256Hz;
  19.  
  20. }
  21.  
  22. void IMU3000::Init(uint8 I2CPort, uint8 flags) {
  23.  
  24.     IMUWire = new HardWire(I2CPort, flags);
  25.  
  26.     IMUWire->begin();
  27.  
  28.     //Enable pass-through
  29.     WriteReg(IMU3000_ADDR, USER_CTRL, 0x08);
  30.  
  31.     // Configure the ADXL345
  32.     WriteReg(ADXL345_ADDR, POWER_CTL,0x08);
  33.  
  34.     // Set PLL with Gyro X reference
  35.     WriteReg(IMU3000_ADDR, PWR_MGM, 0x01);
  36.  
  37.     // Full scale range & Low Pass Filter
  38.     WriteReg(IMU3000_ADDR, DLPF_FS, FS << 3 | LP);
  39.  
  40.     calib.x = 0;
  41.     calib.y = 0;
  42.     calib.z = 0;
  43.  
  44. }
  45.  
  46. IMU3000::~IMU3000() {
  47.     IMUWire->~HardWire();
  48. }
  49.  
  50. void IMU3000::Update(void) {
  51.     GetGyro();
  52.     GetAcc();
  53. }
  54.  
  55. void IMU3000::GetGyro() {
  56.     uint8_t buff[6];
  57.  
  58.     BurstRead(IMU3000_ADDR, IMU3000_BURST_R_ADDR, 6, buff);
  59.  
  60. #ifdef AUTO_CALIB
  61.     gyro.x = (int16_t)(buff[0]<<8 | buff[1]);
  62.     gyro.y = (int16_t)(buff[2]<<8 | buff[3]);
  63.     gyro.z = (int16_t)(buff[4]<<8 | buff[5]);
  64. #else
  65.     gyro.x = (int16_t)(buff[0]<<8 | buff[1]) + calib.x;
  66.     gyro.y = (int16_t)(buff[2]<<8 | buff[3]) + calib.y;
  67.     gyro.z = (int16_t)(buff[4]<<8 | buff[5]) + calib.z;
  68. #endif
  69. }
  70.  
  71. void IMU3000::GetAcc() {
  72.  
  73.     BurstRead(ADXL345_ADDR, ADXL345_BURST_R_ADDR, 6, (uint8_t*)&acc);
  74.  
  75. }
  76.  
  77. void IMU3000::SetFullScaleRange(fullScale new_fs) {
  78.     FS = new_fs;
  79.  
  80.     // Full scale range
  81.     WriteReg(IMU3000_ADDR, DLPF_FS, FS << 3 | LP);
  82. }
  83.  
  84. void IMU3000::SetLowPassFilter(LPFilter new_lp) {
  85.     LP = new_lp;
  86.  
  87.     // Low Pass Filter
  88.     WriteReg(IMU3000_ADDR, DLPF_FS, FS << 3 | LP);
  89. }
  90.  
  91. void IMU3000::Calibration() {
  92. #ifdef AUTO_CALIB
  93.  
  94.     int32_t cal[3]={0};
  95.     uint8_t i;
  96.  
  97.     for(i = 0; i< 10; i++) {
  98.         GetGyro();
  99.         cal[0] += gyro.x;
  100.         cal[1] += gyro.y;
  101.         cal[2] += gyro.z;
  102.  
  103.         delay(200);
  104.     }
  105.  
  106.     calib.x = 0xFFFF - cal[0] / 10 + 1;
  107.     calib.y = 0xFFFF - cal[1] / 10 + 1;
  108.     calib.z = 0xFFFF - cal[2] / 10 + 1;
  109.  
  110.     char s[30];
  111.  
  112.     sprintf(s,"Cal  %5d %5d %5d",IMU.calib.x,IMU.calib.y,IMU.calib.z);
  113.     LCD::WriteText(10,110,s,Red,Black);
  114.  
  115.     WriteReg(IMU3000_ADDR, X_OFFS_USRH,(calib.x & 0xFF00) >> 8);
  116.     WriteReg(IMU3000_ADDR, X_OFFS_USRL,(calib.x & 0x00FF));
  117.  
  118.     WriteReg(IMU3000_ADDR, Y_OFFS_USRH,(calib.y & 0xFF00) >> 8);
  119.     WriteReg(IMU3000_ADDR, Y_OFFS_USRL,(calib.y & 0x00FF));
  120.  
  121.     WriteReg(IMU3000_ADDR, Z_OFFS_USRH,(calib.z & 0xFF00) >> 8);
  122.     WriteReg(IMU3000_ADDR, Z_OFFS_USRL,(calib.z & 0x00FF));
  123.  
  124.     uint8_t buff[6];
  125.  
  126.     BurstRead(IMU3000_ADDR, X_OFFS_USRH, 6, buff);
  127.  
  128.     calib.x = buff[0]<<8 | buff[1];
  129.     calib.y = buff[2]<<8 | buff[3];
  130.     calib.z = buff[4]<<8 | buff[5];
  131.  
  132. #else
  133.     calib.x = 28;
  134.     calib.y = 9;
  135.     calib.z = 18;
  136. #endif
  137.  
  138. }
  139.  
  140. void IMU3000::WriteReg(uint8_t address, uint8_t reg, uint8_t value) {
  141.     IMUWire->beginTransmission(address);
  142.     IMUWire->send(reg);
  143.     IMUWire->send(value);
  144.     IMUWire->endTransmission();
  145. }
  146.  
  147. uint8_t IMU3000::ReadReg(uint8_t address, uint8_t reg) {
  148.  
  149.     IMUWire->beginTransmission(address);
  150.     IMUWire->send(reg);
  151.     IMUWire->endTransmission();
  152.     IMUWire->requestFrom(address, 1);
  153.     return IMUWire->receive();
  154. }
  155.  
  156. void IMU3000::BurstWrite(uint8_t address, uint8_t first_reg, uint8_t nbytes, uint8_t *buff) {
  157.     uint8_t i;
  158.  
  159.     IMUWire->beginTransmission(address);
  160.     IMUWire->send(first_reg);
  161.  
  162.     for(i = 0; i < nbytes ;i++)
  163.         IMUWire->send(buff[i]);
  164.  
  165.     IMUWire->endTransmission();
  166. }
  167.  
  168. void IMU3000::BurstRead(uint8_t address, uint8_t first_reg, uint8_t nbytes, uint8_t *buff) {
  169.     uint8_t i;
  170.  
  171.     IMUWire->beginTransmission(address);
  172.     IMUWire->send(first_reg);
  173.     IMUWire->endTransmission();
  174.     IMUWire->requestFrom(address, nbytes);
  175.  
  176.     for(i = 0; i < nbytes ;i++)
  177.         buff[i] = IMUWire->receive();
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement