第一回 冬のスイッチ大勉強会 - XBee編 -

12
Switch_lecture ArduinoMicro + XBee + SerialCommunication

Transcript of 第一回 冬のスイッチ大勉強会 - XBee編 -

Page 1: 第一回 冬のスイッチ大勉強会 - XBee編 -

Switch_lecture

ArduinoMicro + XBee + SerialCommunication

Page 2: 第一回 冬のスイッチ大勉強会 - XBee編 -

とりあえず加速度。

ArduinoMicro

5V

GND

A0

A1

A2

1: Vdd

2: PSD

3: GND

5: SelfTest

6: Out X

7: Out Y

8: Out Z

KXM52-1050

Page 3: 第一回 冬のスイッチ大勉強会 - XBee編 -

Arduino sketch

BAUD_RATE : 9600

Device ID

Read analog values

Print to serial“id,x,y,z” + ‘\n’

int id = 0;

void setup(){ Serial.begin( 9600 );}

void loop(){ int x = analogRead( A0 ); int y = analogRead( A1 ); int z = analogRead( A2 ); Serial.print( id ); Serial.print( "," ); Serial.print( x ); Serial.print( "," ); Serial.print( y ); Serial.print( "," ); Serial.print( z ); Serial.println(); delay( 100 );}

Page 4: 第一回 冬のスイッチ大勉強会 - XBee編 -

Baud Rateボーレート

ボーbaud = 変調回数

9600bps = 1200Bps

9600bps = 1200 Character per sec

1Byte = 8bit = 1character( ASCII )

厳密には= bps ではない

Page 5: 第一回 冬のスイッチ大勉強会 - XBee編 -

AT Command

ATID : Network ID

ATCH : Channel

ATMY : My Address

ATDL : Send Address

ATBD : Baud Rate

ATWR : Write Settings

揃える

Page 6: 第一回 冬のスイッチ大勉強会 - XBee編 -

Communication

1 x 1

MY: 1DL: 0

MY: 2DL: 0

MY: 0DL: -

MY: 1DL: 0

Sender Receiver

MY: 0DL: -

2~ x 1

Sender Receiver

Page 7: 第一回 冬のスイッチ大勉強会 - XBee編 -

Add XBee

ArduinoMicro

3V

RX

TX

GND

3.3V

DOUT

DIN

GND

XBee

Transmit→

書き込み時は外す

Receive←

Page 8: 第一回 冬のスイッチ大勉強会 - XBee編 -

Arduino sketch( ArduinoMicro + XBee )

Serial → Serial1

Serial → Serial1

int id = 0;

void setup(){ Serial1.begin( 9600 );}

void loop(){ int x = analogRead( A0 ); int y = analogRead( A1 ); int z = analogRead( A2 ); Serial1.print( id ); Serial1.print( "," ); Serial1.print( x ); Serial1.print( "," ); Serial1.print( y ); Serial1.print( "," ); Serial1.print( z ); Serial1.println(); delay( 100 );}

Page 9: 第一回 冬のスイッチ大勉強会 - XBee編 -

Read Serial( openFrameworks )

ofApp.h ofApp.cpp

class ofApp : public ofBaseApp{public: ofSerial serial; string str; int index, x, y, z;};

void ofApp::setup(){ index = x = y = z = 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() == 4 ) { index = ofToInt( valStr.at( 0 ) ); x = ofToInt( valStr.at( 1 ) ); y = ofToInt( valStr.at( 2 ) ); z = ofToInt( valStr.at( 3 ) ); } str = ""; } else { str.push_back( c ); } }}

Page 10: 第一回 冬のスイッチ大勉強会 - XBee編 -

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 ) )x = ofToInt( valStr.at( 1 ) )y = ofToInt( valStr.at( 2 ) )z = ofToInt( valStr.at( 3 ) )

Page 11: 第一回 冬のスイッチ大勉強会 - XBee編 -

Get values from string

“0,524,535,751”

convert to int from string

index = ofToInt( valStr.at( 0 ) )x = ofToInt( valStr.at( 1 ) )y = ofToInt( valStr.at( 2 ) )z = ofToInt( valStr.at( 3 ) )

“0” “524” “535” “751”

id

string

string

x y z

string

int

vector< string > valStr = ofSplitString( str, "," )

Page 12: 第一回 冬のスイッチ大勉強会 - XBee編 -

Draw with sensor value

x = 0 ~ 1023

( float )x / 1023 = 0.0 ~ 1.0

( ( float )x / 1023 ) * ofGetWidth() = 0.0 ~ ofGetWidth()

( openFrameworks )

necessary to re-scale values

ofApp.cpp

void ofApp::draw(){ ofSetColor( 0 ); ofFill(); ofRect( 0, 0, ( ( float )x / 1023 ) * ofGetWidth(), 60 );}