バージョン情報
Arduino Nano , Arduino IDE 1.0.5 , processing 2.2.1 , windows 8.1
3軸加速度センサを使ってみた。
材料
加速度センサ:MPU6050
Arduino nano
参考にしたページ:こちら
回路
Arduino ソース
expampleのMPU6050_raw.inoをほぼそのまま使用。
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define LED_PIN 13
bool blinkState = false;
String str_out;
void setup() {
Wire.begin();
Serial.begin(38400);
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
// configure Arduino LED for
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
str_out = String(ax) + "," + String(ay) + "," + String(az) + "," + String(gx) + "," + String(gy) + "," + String(gz);
Serial.println(str_out);
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}
Processing ソース
加速度センサの値(ax,ay,az)の値を使ってBoxを表示させてみた。
cameraの使い方は前回の記事を参照。
シリアル通信の記事はこちらを参照。
import processing.serial.*;
PShape box1;
PVector v_center, v_camera;
Serial myPort;
int lf = 10;
String str_get_data = null;
String buf[];
int ax,ay,az;
void setup(){
size(500,500,P3D);
v_center = new PVector(0.0,0.0,0.0);
v_camera = new PVector(700.0,900.0,700.0);
box1 = createShape(BOX,100,100,100);
box1.translate(50,50,50);
myPort = new Serial(this, "COM5", 38400);
}
void draw(){
background(200);
//枠
noFill();
strokeWeight(1);
stroke(0,0,0);
translate(250, 250, 250);
box(500, 500, 500);
translate(-250, -250, -250);
//XYZ方向
strokeWeight(3);
stroke(255,0,0);
line(0,0,0,500,0,0);
stroke(0,255,0);
line(0,0,0,0,500,0);
stroke(0,0,255);
line(0,0,0,0,0,500);
//オブジェクト配置
shape(box1);
//カメラ配置
camera(v_camera.x, v_camera.y, v_camera.z, v_center.x,v_center.y,v_center.z, 0.0, 0.0, -1.0);
}
void serialEvent(Serial myPort){
str_get_data = myPort.readStringUntil(lf);
if (str_get_data != null){
str_get_data = trim(str_get_data); //改行コード取り除き
buf = split(str_get_data,",");
try{
fill(255,127,0);
stroke(0,0,0);
ax = int(buf[0])/100*2;
ay = int(buf[1])/100*2;
az = int(buf[2])/100*2;
box1 = createShape(BOX,ax,ay,az);
box1.translate(ax/2,ay/2,az/2);
} catch(Exception e) {
e.printStackTrace();
}
}
}




