Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 1:6a4c2f84180b, committed 2017-01-28
- Comitter:
- Gaku0606
- Date:
- Sat Jan 28 19:52:32 2017 +0000
- Parent:
- 0:d36bfb8300a2
- Child:
- 2:4c7bc164cc4d
- Commit message:
- ??????????
Changed in this revision
| mpu9250_i2c.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mpu9250_i2c.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/mpu9250_i2c.cpp Mon Jan 09 06:28:04 2017 +0000
+++ b/mpu9250_i2c.cpp Sat Jan 28 19:52:32 2017 +0000
@@ -22,7 +22,7 @@
_nine->frequency(Hz);
}
bool mpu9250::senserTest(){
- if(readReg(_addr, WHO_AM_I) == 0x71){
+ if(readReg(_addr, WHO_AM_I_MPU9250) == 0x71){
return true;
}
else return false;
@@ -106,7 +106,7 @@
setGyro(_500DPS);
}
-void mpu9250::getAcc(double *acc){
+template<typename T>void mpu9250::getAcc(T *acc){
char data[6];
short sign;
readReg(_addr, ACCEL_XOUT_H, data, 6);
@@ -117,14 +117,14 @@
sign = ((short)data[4] << 8) | (short)data[5];
acc[2] = (double)sign * acc_coef - acc_offset[2];
}
-void mpu9250::getAcc(double *ax, double *ay, double *az){
+template<typename T>void mpu9250::getAcc(T *ax, T *ay, T *az){
double acc[3];
getAcc(acc);
*ax = acc[0];
*ay = acc[1];
*az = acc[2];
}
-void mpu9250::getGyro(double *gyro){
+template<typename T>void mpu9250::getGyro(T *gyro){
char data[6];
short sign;
readReg(_addr, GYRO_XOUT_H, data, 6);
@@ -135,14 +135,14 @@
sign = ((short)data[4] << 8) | (short)data[5];
gyro[2] = (double)sign * gyro_coef - gyro_offset[2];
}
-void mpu9250::getGyro(double *gx, double *gy, double *gz){
+template<typename T>void mpu9250::getGyro(T *gx, T *gy, T *gz){
double gyro[3];
getGyro(gyro);
*gx = gyro[0];
*gy = gyro[1];
*gz = gyro[2];
}
-void mpu9250::getMag(double *mag){
+template<typename T>void mpu9250::getMag(T *mag){
char data[8];
short sign;
readReg(MAG_ADDR, ST1, data, 8);
@@ -153,14 +153,14 @@
sign = ((short)data[6] << 8) | (short)data[5];
mag[2] = (double)sign * -mag_coef - mag_offset[2];
}
-void mpu9250::getMag(double *mx, double *my, double *mz){
+template<typename T>void mpu9250::getMag(T *mx, T *my, T *mz){
double mag[3];
getMag(mag);
*mx = mag[0];
*my = mag[1];
*mz = mag[2];
}
-void mpu9250::getGyroAcc(double *imu){
+template<typename T>void mpu9250::getGyroAcc(T *imu){
char data[14];
short sign;
readReg(_addr, ACCEL_XOUT_H, data, 14);
@@ -182,4 +182,23 @@
void mpu9250::setAccLPF(A_BAND_WIDTH band){
writeReg(_addr, ACCEL_CONFIG2, band);
wait_us(70);
-}
\ No newline at end of file
+}
+
+template void mpu9250::getAcc<double>(double *ax, double *ay, double *az);
+template void mpu9250::getAcc<float>(float *ax, float *ay, float *az);
+template void mpu9250::getAcc<double>(double *acc);
+template void mpu9250::getAcc<float>(float *acc);
+
+template void mpu9250::getGyro<double>(double *gx, double *gy, double *gz);
+template void mpu9250::getGyro<float>(float *gx, float *gy, float *gz);
+template void mpu9250::getGyro<double>(double *gyro);
+template void mpu9250::getGyro<float>(float *gyro);
+
+template void mpu9250::getMag<double>(double *mx, double *my, double *mz);
+template void mpu9250::getMag<float>(float *mx, float *my, float *mz);
+template void mpu9250::getMag<double>(double *mag);
+template void mpu9250::getMag<float>(float *mag);
+
+template void mpu9250::getGyroAcc<double>(double *imu);//gx,gy,gz,ax,ay,az
+template void mpu9250::getGyroAcc<float>(float *imu);//gx,gy,gz,ax,ay,az
+
\ No newline at end of file
--- a/mpu9250_i2c.h Mon Jan 09 06:28:04 2017 +0000
+++ b/mpu9250_i2c.h Sat Jan 28 19:52:32 2017 +0000
@@ -1,6 +1,14 @@
#ifndef _MPU9250_I2C_H_
#define _MPU9250_I2C_H_
+/**
+ @file mpu9250.h
+ @bref mpu9250を比較的簡単に利用できるようにしたライブラリ
+ @note ローパスフィルタまわりの実装がまだです.外部でよろです!
+ @author Gaku MATSUMOTO
+*/
+
+
#define SLAVE_ADDR_LOW (0b1101000 << 1)//AD0 == LOW
#define SLAVE_ADDR_HIGH (0b1101001 << 1)//AD0 == HIGH
#define MAG_ADDR (0b0001100 << 1)
@@ -27,7 +35,7 @@
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
-#define WHO_AM_I 0x75 //0x71ならおk
+#define WHO_AM_I_MPU9250 0x75 //0x71ならおk
#define XG_OFFSET_H 0x13
#define XG_OFFSET_L 0x14
#define YG_OFFSET_H 0x15
@@ -96,36 +104,130 @@
class mpu9250{
public:
- mpu9250(I2C &_i2c, AD0 celect);
+
+ /**
+ @bref mpu9250インスタンスを生成する
+ @param _i2c メインプログラムで宣言したI2Cインスタンスのアドレス
+ @param celect AD0ピンがHIGHならAD0_HIGH,LOWならAD0_LOW
+ @note 第二引数なしだとAD0_HIGHになります.
+ */
+ mpu9250(I2C &_i2c, AD0 celect = AD0_HIGH);
I2C *_nine;
public:
void writeReg(char addr, char data);
void writeReg(char addr, char reg, char data);
char readReg(char addr, char reg);
void readReg(char addr, char start_reg, char* buff, char num);
+
+ /**
+ @bref 慣性センサと通信ができているか確認する
+ @return trueが返ってきたら成功,falseなら...
+ */
+
bool senserTest();
+ /**
+ @bref 地磁気センサと通信ができているか確認する
+ @return trueが返ってきたら成功,falseなら...
+ */
bool mag_senserTest();
- void setAcc(ACC_RANGE a_range);
- void setGyro(GYRO_RANGE g_range);
- void setMag(MAG_RATE rate);
+
+ /**
+ @bref 加速度センサのレンジを設定
+ @param a_range _2G, _4G, _8G, _16Gの中から選択
+ @note 引数無しで±4Gになる
+ */
+ void setAcc(ACC_RANGE a_range = _4G);
+
+ /**
+ @bref 角速度センサのレンジ設定
+ @param g_range _250DPS, _500DPS, _1000DPS, _2000DPSの中から選択
+ @note 引数無しで±500DPS
+ */
+ void setGyro(GYRO_RANGE g_range = _500DPS);
+
+ /**
+ @bref 地磁気センサのデータレート設定
+ @param rate _8HZ か _100HZを選択
+ @note あえて8Hzにする必要は無いと思います.
+ */
+ void setMag(MAG_RATE rate = _100HZ);
void init();
+
+ /**
+ @bref I2Cの通信速度を変更できます.余程のことがない限り使用しなくていいです・
+ */
void frequency(int Hz);
+
+ /**
+ @bref mpu9250のデジタルローパスフィルタの設定
+ @param band NO_USE, _460HZ, _184HZ, _92HZ, _41HZ, _20HZ, _10HZ, _5HZから選択
+ @note カットオフ周波数なのかサンプルレートなのかよく分かりません.正直効果が見られません
+ */
void setAccLPF(A_BAND_WIDTH band);
+
+ /**
+ @bref ゼロ点のずれを補正するオフセット値を設定する
+ @param ax,ay,az 加速度のオフセット
+ @param gx,gy,gz 角速度のオフセット
+ @param mx,my,mz 地磁気のオフセット
+ @note とても重要です.地磁気は定期的にキャリブレーションをしてください.ちなみに,これらの値は測定値より引かれています.
+ */
void setOffset(double ax, double ay, double az,
double gx, double gy, double gz,
double mx, double my, double mz);
-
- void getAcc(double *ax, double *ay, double *az);
- void getAcc(double *acc);
+ /**
+ @bref 加速度を取得します.
+ @param ax x軸方向の加速度[G]
+ @param ay y軸方向の加速度[G]
+ @param az z軸方向の加速度[G]
+ @note 型はfloat でも doubleでも構いません.
+ */
+ template<typename T>void getAcc(T *ax, T *ay, T *az);
+ /**
+ @bref 加速度を取得します.
+ @param acc 各軸方向の加速度[G],x,y,zの順
+ @note 型はfloat でも doubleでも構いません.
+ */
+ template<typename T>void getAcc(T *acc);
- void getGyro(double *gx, double *gy, double *gz);
- void getGyro(double *gyro);
+ /**
+ @bref 角速度を取得します.
+ @param gx x軸方向の角速度[degree/s]
+ @param gy y軸方向の角速度[degree/s]
+ @param gz z軸方向の角速度[degree/s]
+ @note 型はfloat でも doubleでも構いません.
+ */
+ template<typename T>void getGyro(T *gx, T *gy, T *gz);
+ /**
+ @bref 角速度を取得します.
+ @param gyro 各軸方向の角速度[degree/s], x,y,zの順
+ @note 型はfloat でも doubleでも構いません.
+ */
+ template<typename T>void getGyro(T *gyro);
- void getMag(double *mx, double *my, double *mz);
- void getMag(double *mag);
+ /**
+ @bref 磁束密度を取得します.
+ @param mx x軸方向の磁束密度[uT]
+ @param my y軸方向の磁束密度[uT]
+ @param mz z軸方向の磁束密度[uT]
+ @note 型はfloat でも doubleでも構いません.
+ */
+ template<typename T>void getMag(T *mx, T *my, T *mz);
+ /**
+ @bref 磁束密度を取得します.
+ @param mag 各軸方向の磁束密度[uT],x,y,zの順
+ @note 型はfloat でも doubleでも構いません.
+ */
+ template<typename T>void getMag(T *mag);
- void getGyroAcc(double *imu);//gx,gy,gz,ax,ay,az
+ /**
+ @bref 角速度と加速度を同時に取得します.
+ @param imu データを入れる配列,角速度[degree/s],加速度[G]の順
+ @note 配列数は6以上で
+ */
+ template<typename T>void getGyroAcc(T *imu);//gx,gy,gz,ax,ay,az
+private:
static char _addr;
static double acc_coef;//coefficient
static double gyro_coef;