MPU9150 Magnetometer. Such a big problem
i have used code arduino playground.
code: [select]
// mpu-9150 accelerometer + gyro + compass + temperature
// -----------------------------
//
// arduino.cc user "frtrobotik" (tobias hübner)
//
//
// july 2013
// first version
//
// open source / public domain
//
// using arduino 1.0.1
// not work older version,
// since wire.endtransmission() uses parameter
// hold or release i2c bus.
//
// documentation:
// - invensense documents:
// - "mpu-9150 product specification revision 4.0",
// ps-mpu-9150a.pdf
// - "mpu-9150 register map , descriptions revision 4.0",
// rm-mpu-9150a-00.pdf
// - "mpu-9150 9-axis evaluation board user guide"
// an-mpu-9150evb-00.pdf
//
// accuracy 16-bits.
//
// parts copied mpu-6050 playground page.
// playground.arduino.cc/main/mpu-6050
// there more registervalues. here most
// nessecary ones started sensor.
#include <wire.h>
// register names according datasheet.
// according invensense document
// "mpu-9150 register map , descriptions revision 4.0",
#define mpu9150_self_test_x 0x0d // r/w
#define mpu9150_self_test_y 0x0e // r/w
#define mpu9150_self_test_x 0x0f // r/w
#define mpu9150_self_test_a 0x10 // r/w
#define mpu9150_smplrt_div 0x19 // r/w
#define mpu9150_config 0x1a // r/w
#define mpu9150_gyro_config 0x1b // r/w
#define mpu9150_accel_config 0x1c // r/w
#define mpu9150_ff_thr 0x1d // r/w
#define mpu9150_ff_dur 0x1e // r/w
#define mpu9150_mot_thr 0x1f // r/w
#define mpu9150_mot_dur 0x20 // r/w
#define mpu9150_zrmot_thr 0x21 // r/w
#define mpu9150_zrmot_dur 0x22 // r/w
#define mpu9150_fifo_en 0x23 // r/w
#define mpu9150_i2c_mst_ctrl 0x24 // r/w
#define mpu9150_i2c_slv0_addr 0x25 // r/w
#define mpu9150_i2c_slv0_reg 0x26 // r/w
#define mpu9150_i2c_slv0_ctrl 0x27 // r/w
#define mpu9150_i2c_slv1_addr 0x28 // r/w
#define mpu9150_i2c_slv1_reg 0x29 // r/w
#define mpu9150_i2c_slv1_ctrl 0x2a // r/w
#define mpu9150_i2c_slv2_addr 0x2b // r/w
#define mpu9150_i2c_slv2_reg 0x2c // r/w
#define mpu9150_i2c_slv2_ctrl 0x2d // r/w
#define mpu9150_i2c_slv3_addr 0x2e // r/w
#define mpu9150_i2c_slv3_reg 0x2f // r/w
#define mpu9150_i2c_slv3_ctrl 0x30 // r/w
#define mpu9150_i2c_slv4_addr 0x31 // r/w
#define mpu9150_i2c_slv4_reg 0x32 // r/w
#define mpu9150_i2c_slv4_do 0x33 // r/w
#define mpu9150_i2c_slv4_ctrl 0x34 // r/w
#define mpu9150_i2c_slv4_di 0x35 // r
#define mpu9150_i2c_mst_status 0x36 // r
#define mpu9150_int_pin_cfg 0x37 // r/w
#define mpu9150_int_enable 0x38 // r/w
#define mpu9150_int_status 0x3a // r
#define mpu9150_accel_xout_h 0x3b // r
#define mpu9150_accel_xout_l 0x3c // r
#define mpu9150_accel_yout_h 0x3d // r
#define mpu9150_accel_yout_l 0x3e // r
#define mpu9150_accel_zout_h 0x3f // r
#define mpu9150_accel_zout_l 0x40 // r
#define mpu9150_temp_out_h 0x41 // r
#define mpu9150_temp_out_l 0x42 // r
#define mpu9150_gyro_xout_h 0x43 // r
#define mpu9150_gyro_xout_l 0x44 // r
#define mpu9150_gyro_yout_h 0x45 // r
#define mpu9150_gyro_yout_l 0x46 // r
#define mpu9150_gyro_zout_h 0x47 // r
#define mpu9150_gyro_zout_l 0x48 // r
#define mpu9150_ext_sens_data_00 0x49 // r
#define mpu9150_ext_sens_data_01 0x4a // r
#define mpu9150_ext_sens_data_02 0x4b // r
#define mpu9150_ext_sens_data_03 0x4c // r
#define mpu9150_ext_sens_data_04 0x4d // r
#define mpu9150_ext_sens_data_05 0x4e // r
#define mpu9150_ext_sens_data_06 0x4f // r
#define mpu9150_ext_sens_data_07 0x50 // r
#define mpu9150_ext_sens_data_08 0x51 // r
#define mpu9150_ext_sens_data_09 0x52 // r
#define mpu9150_ext_sens_data_10 0x53 // r
#define mpu9150_ext_sens_data_11 0x54 // r
#define mpu9150_ext_sens_data_12 0x55 // r
#define mpu9150_ext_sens_data_13 0x56 // r
#define mpu9150_ext_sens_data_14 0x57 // r
#define mpu9150_ext_sens_data_15 0x58 // r
#define mpu9150_ext_sens_data_16 0x59 // r
#define mpu9150_ext_sens_data_17 0x5a // r
#define mpu9150_ext_sens_data_18 0x5b // r
#define mpu9150_ext_sens_data_19 0x5c // r
#define mpu9150_ext_sens_data_20 0x5d // r
#define mpu9150_ext_sens_data_21 0x5e // r
#define mpu9150_ext_sens_data_22 0x5f // r
#define mpu9150_ext_sens_data_23 0x60 // r
#define mpu9150_mot_detect_status 0x61 // r
#define mpu9150_i2c_slv0_do 0x63 // r/w
#define mpu9150_i2c_slv1_do 0x64 // r/w
#define mpu9150_i2c_slv2_do 0x65 // r/w
#define mpu9150_i2c_slv3_do 0x66 // r/w
#define mpu9150_i2c_mst_delay_ctrl 0x67 // r/w
#define mpu9150_signal_path_reset 0x68 // r/w
#define mpu9150_mot_detect_ctrl 0x69 // r/w
#define mpu9150_user_ctrl 0x6a // r/w
#define mpu9150_pwr_mgmt_1 0x6b // r/w
#define mpu9150_pwr_mgmt_2 0x6c // r/w
#define mpu9150_fifo_counth 0x72 // r/w
#define mpu9150_fifo_countl 0x73 // r/w
#define mpu9150_fifo_r_w 0x74 // r/w
#define mpu9150_who_am_i 0x75 // r
//mpu9150 compass
#define mpu9150_cmps_xout_l 0x4a // r
#define mpu9150_cmps_xout_h 0x4b // r
#define mpu9150_cmps_yout_l 0x4c // r
#define mpu9150_cmps_yout_h 0x4d // r
#define mpu9150_cmps_zout_l 0x4e // r
#define mpu9150_cmps_zout_h 0x4f // r
// i2c address 0x69 0x68 depends on wiring.
int mpu9150_i2c_address = 0x68;
here second part.
code: [select]
//variables our values can stored
int cmps[3];
int accl[3];
int gyro[3];
int temp;
void setup()
{
// initialize serial bus printing data.
serial.begin(9600);
// initialize 'wire' class i2c-bus.
wire.begin();
// clear 'sleep' bit start sensor.
mpu9150_writesensor(mpu9150_pwr_mgmt_1, 0);
mpu9150_setupcompass();
}
void loop()
{
// print sensor values sensor provides
// formated values x, y, , z in order for
// compass, gyro, acceleration. first value is
// temperature.
double dt = ( (double) mpu9150_readsensor(mpu9150_temp_out_l,mpu9150_temp_out_h) + 12412.0) / 340.0;
serial.print(dt);
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_cmps_xout_l,mpu9150_cmps_xout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_cmps_yout_l,mpu9150_cmps_yout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_cmps_zout_l,mpu9150_cmps_zout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_gyro_xout_l,mpu9150_gyro_xout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_gyro_yout_l,mpu9150_gyro_yout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_gyro_zout_l,mpu9150_gyro_zout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_accel_xout_l,mpu9150_accel_xout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_accel_yout_l,mpu9150_accel_yout_h));
serial.print(" ");
serial.print(mpu9150_readsensor(mpu9150_accel_zout_l,mpu9150_accel_zout_h));
serial.println();
delay(100);
}
//http://pansenti.wordpress.com/2013/03/26/pansentis-invensense-mpu-9150-software-for-arduino-is-now-on-github/
//thank pansenti setup code.
//i documented 1 later.
void mpu9150_setupcompass(){
mpu9150_i2c_address = 0x0c; //change adress compass
mpu9150_writesensor(0x0a, 0x00); //powerdownmode
mpu9150_writesensor(0x0a, 0x0f); //selftest
mpu9150_writesensor(0x0a, 0x00); //powerdownmode
mpu9150_i2c_address = 0x68; //change adress mpu
mpu9150_writesensor(0x24, 0x40); //wait data @ slave0
mpu9150_writesensor(0x25, 0x8c); //set i2c address @ slave0 @ 0x0c
mpu9150_writesensor(0x26, 0x02); //set reading @ slave 0 starts
mpu9150_writesensor(0x27, 0x88); //set offset @ start reading , enable
mpu9150_writesensor(0x28, 0x0c); //set i2c address @ slv1 @ 0x0c
mpu9150_writesensor(0x29, 0x0a); //set reading @ slave 1 starts
mpu9150_writesensor(0x2a, 0x81); //enable @ set length 1
mpu9150_writesensor(0x64, 0x01); //overvride register
mpu9150_writesensor(0x67, 0x03); //set delay rate
mpu9150_writesensor(0x01, 0x80);
mpu9150_writesensor(0x34, 0x04); //set i2c slv4 delay
mpu9150_writesensor(0x64, 0x00); //override register
mpu9150_writesensor(0x6a, 0x00); //clear usr setting
mpu9150_writesensor(0x64, 0x01); //override register
mpu9150_writesensor(0x6a, 0x20); //enable master i2c mode
mpu9150_writesensor(0x34, 0x13); //disable slv4
}
////////////////////////////////////////////////////////////
///////// i2c functions easier values ///////////
////////////////////////////////////////////////////////////
int mpu9150_readsensor(int addrl, int addrh){
wire.begintransmission(mpu9150_i2c_address);
wire.write(addrl);
wire.endtransmission(false);
wire.requestfrom(mpu9150_i2c_address, 1, true);
byte l = wire.read();
wire.begintransmission(mpu9150_i2c_address);
wire.write(addrh);
wire.endtransmission(false);
wire.requestfrom(mpu9150_i2c_address, 1, true);
byte h = wire.read();
return (int16_t)((h<<8)+l);
}
int mpu9150_readsensor(int addr){
wire.begintransmission(mpu9150_i2c_address);
wire.write(addr);
wire.endtransmission(false);
wire.requestfrom(mpu9150_i2c_address, 1, true);
return wire.read();
}
int mpu9150_writesensor(int addr,int data){
wire.begintransmission(mpu9150_i2c_address);
wire.write(addr);
wire.write(data);
wire.endtransmission(true);
return 1;
}
Arduino Forum > Using Arduino > Sensors > MPU9150 Magnetometer. Such a big problem
arduino
Comments
Post a Comment