Presentation is loading. Please wait.

Presentation is loading. Please wait.

Raspberry Pi project - 라즈베리파이로 핑퐁게임하기 - 1401582 신동윤 1401574 박지환.

Similar presentations


Presentation on theme: "Raspberry Pi project - 라즈베리파이로 핑퐁게임하기 - 1401582 신동윤 1401574 박지환."— Presentation transcript:

1 Raspberry Pi project - 라즈베리파이로 핑퐁게임하기 - 1401582 신동윤 1401574 박지환

2 프로젝트 소개 Game pi 라즈베리파이를 이용하여 게임즐기기

3 프로젝트 부품 라즈베리파이케이블 스위치 breadboard 아두이노 8*8 도트 매트릭스

4 프로젝트 내용 조작 버튼 python Serial 통신 RaspberryPi GPIO Port 도트 메트릭스 python Arduino Serial Port USB 를 통한

5 제작과정  라즈베리파이와 버튼 모듈 연결선과 브레드보드를 연결  버튼 작동에 관한 파이썬 소스 작동 후 터미널로 테스트  아두이노와 쉬프트 레지스터가 달린 8X8 도트 메트릭스를 연결  도트메트릭스의 각 LED 램프의 좌표값을 확인하면서 테스트한 후 도트메트릭스 소스들을 참조하여 아두이노 핑퐁게임 소스작성  라즈베리파이에 우노를 설치 후 USB 연결로 파이와 아두이노를 연결  아두이노와 라즈베리파이의 시리얼 통신 소스코드를 참고하여 핑퐁게임에 라즈베리파이의 버튼들 을 대응시킴  시리얼 통신으로 인한 오류와 반응속도가 많이 느린 문제등을 해결하기 위해 소스코드를 수정하며 테스트  테스트 이후 게임시작버튼과 종료버튼 그리고 도트메트릭스를 통한 그림그리기 등 추가 희망사항 등을 테스트하며 프로젝트 완성

6 Flow Chart

7 흐름도

8 라즈베리파이 아두이노 연동  라즈베리파이에 아두이노 설치 sudo apt-get install Arduino  시리얼 포트의 액세스 권한얻기 sudo usermod –a -G tty pi sudo usermod –a –G dialout pi  아두이노 실행 후 Tools ->Serial Port 에서 시리얼포트 선택 Tools -> Board 에서 보드선택 http://cafe.naver.com/openrt/2105

9 핑퐁게임 #include #define PIN_RedPlayer_Left 0 //1P q 버튼 #define PIN_RedPlayer_Right 1 #define PIN_BluePlayer_Right 3 // 2P 버튼 #define PIN_BluePlayer_Left 2 #define ROW_DATA // 열 주소값 ((row[0]<<7)|(row[1]<<6)|(row[2]<<5)|(row[3]<<4) |(row[4]<<3)|(row[5]<<2)|(row[6]<<1)|(row[7]<<0)) #define COL_DATA // 행 주소값 ((col[0]<<7)|(col[1]<<6)|(col[2]<<5)|(col[3]<<4)|(c ol[4]<<3)|(col[5]<<2)|(col[6]<<1)|(col[7]<<0)) int SER = 9; // 핀이 연결된 포트넘버 int RCK = 10; int SRCK = 11; unsigned int left = 0; unsigned int right = 0; unsigned int BlueButton_R = 0; unsigned int BlueButton_L = 0; unsigned int RedButton_R = 0; unsigned int RedButton_L = 0; int BlueScore = 0; int RedScore = 0; int angle = 0; int radians;

10 핑퐁게임 // 열과 행의 배열생성 int row[8] = {0, 0, 0, 0, 0, 0, 0, 0 }; int col[8] = { 0, 0, 0, 0, 0, 0, 0, 0}; byte screen[8] = {0, 0, 0, 0, 0, 0, 0, 0}; volatile byte screenRow = 0; volatile byte screenCol = 0; int _angle; int _px; int _py; int _w = 7; int _h = 7; int _wall[] = {3, 3}; int _count = 0; int _speed = 3; int _countPoints = 0; void setup() { Timer1.initialize(100); // 핀에 신호를 내보낸다 pinMode(RCK, OUTPUT); pinMode(SRCK, OUTPUT); pinMode(SER, OUTPUT); Timer1.attachInterrupt(doubleBuffer); // 신호를 가져온다 // attachInterrupt(0, restartGame, LOW); Serial.begin(115200); // 시리얼의 시작과 속도를 정의 }

11 핑퐁게임 void doubleBuffer() { row[screenRow] = 0; col[screenCol] = 1; screenCol++; if (screenCol >= 8) { screenCol = 0; screenRow++; if (screenRow >= 8){screenRow = 0;} } if ((screen[screenRow] >> screenCol) & B1 == B1) { row[screenRow] = 1; col[screenCol] = 0; digitalWrite(RCK, LOW); shiftOut(COL_DATA); shiftOut(ROW_DATA); digitalWrite(RCK, HIGH); } else { row[screenRow] = 0; col[screenCol] = 1; } void allOFF() { for (int i = 0; i < 8; i++) screen[i] = 0;} void on(byte row, byte column) { screen[column - 1] |= (B1 << (row - 1));} void off(byte row, byte column) { screen[column - 1] &= ~(B1 << (row - 1)); }

12 핑퐁게임 //1P 버튼을 읽어온다. void BlueButton(char b) { BlueButton_R = analogRead(PIN_BluePlayer_Right); BlueButton_L = analogRead(PIN_BluePlayer_Left); if (b=='1' ) { if (right > 5) {right = 6;} else { right ++; } delay(40); } else if (b=='2') { if (right < 1) { right = 0;} else { right --; } delay(40);} else { } //2P 버튼을 읽어온다. void RedButton(char b) { RedButton_R = analogRead(PIN_RedPlayer_Right); RedButton_L = analogRead(PIN_RedPlayer_Left); if (b=='5' ) { if (left > 5) { left = 6;} else { left ++;} delay(40); } else if (b=='6') { if (left < 1) { left = 0;} else { left --;} delay(40); } else { }

13 핑퐁게임 char i='Stop'; void calcWall() { if(i=='5'||i=='6') RedButton(i); if(i=='1'||i=='2') BlueButton(i); clearWall(); on(1, left + 1); on(1, left + 2); on(8, right + 1); on(8, right + 2); _wall[0] = left; _wall[1] = right; show(); } void clearWall() { for (int i = 0; i < 8; i++) screen[i] &= B01111110; } void clearGame() { for (int i = 0; i < 8; i++) screen[i] &= B10000001;} int count = 0; void loop() { delay(80); i=Serial.read(); Serial.println(i); if(i=='S'){ count=1; face(); // 웃는얼굴 테스트 reset(); // 리셋 } if(count==1){ calcWall(); enterFrameHandler(); } if(i=='T'){ clearD(); count=0; }

14 핑퐁게임 void show() { clearGame(); on(_px + 1, _py + 1); } //Collision check void checkCollision() { if (_px == _w - 1) { if (_angle == 315 || _angle == 0 || _angle == 45) { if (_py == _wall[1] || _py == _wall[1] + 1) { if (_angle == 0 && _py == _wall[1]) retorted(225); else if (_angle == 0 && _py == _wall[1] + 1) retorted(135); else if (_angle == 45 && _py == _wall[1]) retorted(135); else if (_angle == 45 && _py == _wall[1] + 1) retorted(180); else if (_angle == 315 && _py == _wall[1]) retorted(180); else if (_angle == 315 && _py == _wall[1] + 1) retorted(225); }

15 핑퐁게임 else if (_px == 1) { if (_angle == 225 || _angle == 180 || _angle == 135) { if (_py == _wall[0] || _py == _wall[0] + 1) { if (_angle == 180 && _py == _wall[0]) retorted(315); else if (_angle == 180 && _py == _wall[0] + 1) retorted(45); else if (_angle == 135 && _py == _wall[0]) retorted(45); else if (_angle == 135 && _py == _wall[0] + 1) retorted(0); else if (_angle == 225 && _py == _wall[0]) retorted(0); else if (_angle == 225 && _py == _wall[0] + 1) retorted(315); } if (_px == _w) { RedScore++; reset(); } else if (_px == 0) { BlueScore++; reset(); } else if (_py == _h) { if (_angle == 45) _angle = 315; else if (_angle == 135) _angle = 225; } else if (_py == 0) {

16 핑퐁게임 if (_angle == 225) _angle = 135; else if (_angle == 315) _angle = 45; } //Calculate the Angle increment void calcAngleIncrement() { if (_angle == 0 || _angle == 360) { _px += 1; } else if (_angle == 45) { _px += 1; _py += 1; } else if (_angle == 135) { _px -= 1; _py += 1; } else if (_angle == 180) { _px -= 1; } else if (_angle == 225) { _px -= 1; _py -= 1; } else if (_angle == 315) { _px += 1; _py -= 1; }

17 핑퐁게임 void shiftOut(byte myDataOut) { boolean pinState; // 쉬프트 레지스터를 읽어오고 데이터를 보낸다 digitalWrite(RCK, LOW); // 각각의 비트에 해당하는 데이터를 보낸다 for (int i = 0; i <= 7; i++) { digitalWrite(SRCK, LOW); if ( myDataOut & (1 << i) ) { pinState = HIGH; } else { pinState = LOW; } digitalWrite(SER, pinState); digitalWrite(SRCK, HIGH); digitalWrite(SER, LOW); } // 쉬프트를 멈춘다 digitalWrite(SRCK, LOW); } void clearD(){ for(int i=1;i<9;i++){ for(int j=1;j<9;j++){ off(i,j); }

18 조작버튼 제작 import serial import RPi.GPIO as GPIO import time //GPIO 모듈 준비 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) Up=21 Down=20 Start=4 A=19 B=26 GPIO.setup(21,GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.setup(20,GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.setup(4,GPIO.IN,pull_up_down=GPIO.PUD_UP) // 각 버튼에 GPIO 대응 GPIO.setup(19,GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.setup(26,GPIO.IN,pull_up_down=GPIO.PUD_UP) // 시리얼 통신을 위해 포트 지정 port= " /dev/ttyACM0“ serial=serial.Serial(port,115200) // 포트속도 결정 serialA.flushOutput() // 시리얼 통신 serialA.open() serialA.isOpen() x='0‘ count = 0 while count is not 2: // 정지신호가 올때까지

19 조작버튼 제작 S=GPIO.input(Start) if S is 0: if count is 0: x='S‘ serialA.write(x) x='0‘ time.sleep(5) count=1 elif count is 1: x='T‘ serialA.write(x) break if count is 1: U=GPIO.input(Up) D=GPIO.input(Down) A_B=GPIO.input(A) B_B=GPIO.input(B) if U is 0: x='1‘ if D is 0: x='2‘ if A_B is 0: x='5‘ if B_B is 0: x='6‘ serialA.write(x) time.sleep(0.5) GPIO.cleanup()


Download ppt "Raspberry Pi project - 라즈베리파이로 핑퐁게임하기 - 1401582 신동윤 1401574 박지환."

Similar presentations


Ads by Google