Skip to content

Instantly share code, notes, and snippets.

@hatone
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatone/8034b336c047c80e81c3 to your computer and use it in GitHub Desktop.
Save hatone/8034b336c047c80e81c3 to your computer and use it in GitHub Desktop.
arduino
#include <Wire.h>
#define MPU6050_ACCEL_XOUT_H 0x3B // R
#define MPU6050_WHO_AM_I 0x75 // R
#define MPU6050_PWR_MGMT_1 0x6B // R/W
#define MPU6050_I2C_ADDRESS 0x68
typedef union accel_t_gyro_union{
struct{
uint8_t x_accel_h;
uint8_t x_accel_l;
uint8_t y_accel_h;
uint8_t y_accel_l;
uint8_t z_accel_h;
uint8_t z_accel_l;
uint8_t t_h;
uint8_t t_l;
uint8_t x_gyro_h;
uint8_t x_gyro_l;
uint8_t y_gyro_h;
uint8_t y_gyro_l;
uint8_t z_gyro_h;
uint8_t z_gyro_l;
}
reg;
struct{
int16_t x_accel;
int16_t y_accel;
int16_t z_accel;
int16_t temperature;
int16_t x_gyro;
int16_t y_gyro;
int16_t z_gyro;
}
value;
};
int ledR = 13;//ココ何番だろう?
int ledL = 13;//ココ何番だろう?
char lastStatus = '0';
String gpsStr="";
char x[5];
char y[5];
char z[5];
void setup(){
// シリアルポートを9600 bps[ビット/秒]で初期化
Serial1.begin(9600);
Wire.begin();
MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0);
}
void loop(){
int inputchar;
inputchar = Serial1.read();
if(inputchar != -1) {
switch(inputchar){
case 'R':
if(lastStatus != 'R') {
digitalWrite(ledL, LOW);
digitalWrite(ledR, HIGH);
lastStatus = 'R';
}
break;
case 'L':
if(lastStatus != 'L') {
digitalWrite(ledR, LOW);
digitalWrite(ledL, HIGH);
lastStatus = 'L';
}
break;
case 'S':
digitalWrite(ledR, HIGH);
digitalWrite(ledL, HIGH);
break;
}
}
float dT;
accel_t_gyro_union accel_t_gyro;
MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro));
uint8_t swap;
#define SWAP(x,y) swap = x; x = y; y = swap
SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l);
SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l);
SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l);
float gyro_x = accel_t_gyro.value.x_gyro / 131.0; //FS_SEL_0 131 LSB / (°/s)
float gyro_y = accel_t_gyro.value.y_gyro / 131.0;
float gyro_z = accel_t_gyro.value.z_gyro / 131.0;
gpsStr= String(gyro_x) + ":" + String(gyro_y) + ":" + String(gyro_z)+"EOL";
String apa= "hello";
Serial1.println(gpsStr);
delay(100);
}
// MPU6050_read
int MPU6050_read(int start, uint8_t *buffer, int size){
int i, n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start);
if (n != 1)
return (-10);
n = Wire.endTransmission(false); // hold the I2C-bus
if (n != 0)
return (n);
// Third parameter is true: relase I2C-bus after data is read.
Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
i = 0;
while(Wire.available() && i<size){
buffer[i++]=Wire.read();
}
if ( i != size)
return (-11);
return (0); // return : no error
}
// MPU6050_write
int MPU6050_write(int start, const uint8_t *pData, int size){
int n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start); // write the start address
if (n != 1)
return (-20);
n = Wire.write(pData, size); // write data bytes
if (n != size)
return (-21);
error = Wire.endTransmission(true); // release the I2C-bus
if (error != 0)
return (error);
return (0); // return : no error
}
// MPU6050_write_reg
int MPU6050_write_reg(int reg, uint8_t data){
int error;
error = MPU6050_write(reg, &data, 1);
return (error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment