Upload
wataru-kani
View
740
Download
6
Embed Size (px)
Citation preview
Switch_lecture
FullColorLED & MPU-6050
FullColorLED
ArduinoMicro
5V
10 (PWM)
11 (PWM)
Common
9 (PWM) Red
Green
Blue
OSTA71A1D-A
Anode→
Cathode←150Ω
100Ω
100Ω
Cathode←
Cathode←
Arduino sketch
Set pin mode OUTPUT
Declare LED pins
Set LED color Red
Set LED function0 -> HIGH255 -> LOW(Anode Common)
const int ledPins[] = { 9, 10, 11 };const int nLedPins = 3;
void setup(){ for( int i = 0; i < nLedPins; ++i ) { pinMode( ledPins[ i ], OUTPUT ); } setLed( 255, 0, 0 );}
void loop(){}
void setLed( int _r, int _g, int _b ){ analogWrite( ledPins[ 0 ], 255 - _r ); analogWrite( ledPins[ 1 ], 255 - _g ); analogWrite( ledPins[ 2 ], 255 - _b );}
MPU-6050(GY-521)( I2C Interface )
ArduinoMicro
5V
GND
3 (SCL)
2 (SDA)
VCC
GND
SCL
SDA
MPU-6050
Arduino Library
Download “i2cdevlib” from
github.com/jrowberg/i2cdevlib
into “書類 / Arduino / Libraries /”
i2cdevlib / Arduino / I2Cdev andCopy
Paste
i2cdevlib / Arduino / MPU6050
Example
Arduino / Libraries / MPU6050 / Examples / MPU6050_raw / MPU6050_raw.ino
raw データを取得する Exampleここから不要な部分を削っていく。
Arduino sketch
MPU6050 object
Include libraries
Sensor values
InitializeSet full scale accel range 16GSet full scale gyro range 2000dps
Get acceleration valuesGet rotation values
#include "I2Cdev.h"#include "MPU6050.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #include "Wire.h"#endif
MPU6050 accelgyro;
int16_t ax, ay, az;int16_t gx, gy, gz;
int id = 0;
void setup(){ #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup( 400, true ); #endif Serial1.begin( 9600 );
accelgyro.initialize();
accelgyro.setFullScaleAccelRange( MPU6050_ACCEL_FS_16 );
accelgyro.setFullScaleGyroRange( MPU6050_GYRO_FS_2000 );}
void loop(){ accelgyro.getAcceleration( &ax, &ay, &az );
accelgyro.getRotation( &gx, &gy, &gz );
Serial1.print( id ); Serial1.print( "," ); Serial1.print( ax ); Serial1.print( "," ); Serial1.print( gz ); Serial1.println();
delay( 60 );}
( ArduinoMicro + XBee + MPU-6050 )
Arduino sketch
int16_t = -32768 ~ 32767
accelgyro.initialize()
accelgyro.setFullScaleAccelRange( MPU6050_ACCEL_FS_16 )
accelgyro.setFullScaleGyroRange( MPU6050_GYRO_FS_2000 )
signed 16bit
この中で
されているsetFullScaleAccelRange( MPU6050_ACCEL_FS_2 )setFullScaleGyroRange( MPU6050_GYRO_FS_250 )
( ArduinoMicro + XBee + MPU-6050 )
250dps 2000dps
Accel
2G 16G
Gyro
Arduino sketch( ArduinoMicro + XBee + MPU-6050 )
Address of “ax”
Memory
Address 0x00
ax
&ax
int16_tx
int16_t*
0x01 0x02 0x03 0x04
...
“MPU6050.h”
accelgyro.getAcceleration( &ax, &ay, &az )
void MPU6050::getAcceleration( int16_t* x, int16_t* y, int16_t* z )Pointer of “int16_t”
int16_t* x = &ax;assign address to pointer
Memory
Address 0x00
ax
*x
x
int16_tx
int16_t*
0x01 0x02 0x03 0x04
...
*x = -32768; ax = -32768;in automatically
ax -32768
Read Serial( openFrameworks )
ofApp.h ofApp.cpp
class ofApp : public ofBaseApp{public: ofSerial serial; string str; int index, ax, gz;};
void ofApp::setup(){ index = ax = gz = 0; str = ""; serial.listDevices(); serial.setup( 0, 9600 ); ofBackground( 255 );}
void ofApp::update(){ while( true ) { int c = serial.readByte(); if( c == OF_SERIAL_NO_DATA || c == OF_SERIAL_ERROR || c == 0 ) { break; } if( c == ‘\n’ ) { vector< string > valStr = ofSplitString( str, "," ); if( valStr.size() == 3 ) { index = ofToInt( valStr.at( 0 ) ); ax = abs( ofToInt( valStr.at( 1 ) ) ); gz = ofToInt( valStr.at( 2 ) ); } str = ""; } else { str.push_back( c ); } }}
Flow Chart
false
read 1 character
false
true
true
int c = serial.readByte()
c == OF_SERIAL_NO_DATAor
c == OF_SERIAL_ERRORor
c == 0
while( true )
break str.push_back( c )
c == ‘\n’
ofSplitString( str, "," )
str = ""
index = ofToInt( valStr.at( 0 ) )ax = abs( ofToInt( valStr.at( 1 ) ) )gz = ofToInt( valStr.at( 2 ) )
Get values from string
“0,-32768,32767”
convert to int from string
index = ofToInt( valStr.at( 0 ) )ax = abs( ofToInt( valStr.at( 1 ) ) )gz = ofToInt( valStr.at( 2 ) )
“0” “-32768” “32767”
id
string
string
ax gz
string
int
vector< string > valStr = ofSplitString( str, "," )
-32768 ~ 32767 0 ~ 32768
Draw with sensor value
x = 0 ~ 32768
( float )x / 32768 = 0.0 ~ 1.0
( ( float )x / 32768 ) * ofGetWidth() = 0.0 ~ ofGetWidth()
( openFrameworks )
necessary to re-scale values
ofApp.cpp
void ofApp::draw(){ ofSetColor( 0 ); ofFill(); ofRect( 0, 0, ( ( float )ax / 32768 ) * ofGetWidth(), 60 );}