프로세싱 (Processing) 광운대학교로봇학부박광현
기초
개요 3 2001년 MIT 미디어랩 Ben Fry와 Casey Reas 아티스트를위핚편리핚그래픽작성도구 자바기반 자바스크립트, 파이썬, 안드로이드,... 오픈소스
개요 4 프로세싱 : 프로세싱개발환경 (PDE) 함수모음 문법 커뮤니티 스케치 : 작성된프로그램 스케치북 : 스케치저장폴더
정적스케치 (Static Sketch) 5 line(10, 20, 80, 90); // x1, y1, x2, y2 size(600, 400); // width, height background(255); stroke(100); line(10, 20, 80, 90); background(gray); background(r, g, b); background(#ff7a00); background(0xff7a00); stroke(gray); stroke(gray, alpha); background(r, g, b); background(r, g, b, a); background(#ff7a00); background(0xffff7a00);
정적스케치 (Static Sketch) 6 size(600, 400); background(255, 122, 0); rect(10, 20, 80, 90); // x, y, width, height size(600, 400); background(255, 122, 0); nostroke(); rect(10, 20, 80, 90); size(600, 400); background(255, 122, 0); stroke(0, 0, 255); fill(255, 0, 0); rect(10, 20, 80, 90); size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); // pixel fill(255, 0, 0); rect(10, 20, 80, 90);
정적스케치 (Static Sketch) 7 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); fill(255, 0, 0); rect(10, 20, 80, 90, 10); // x, y, width, height, corner size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); fill(255, 0, 0); rect(10, 20, 80, 90, 10, 20, 30, 40); // x, y, w, h, tl, tr, br, bl
정적스케치 (Static Sketch) 8 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); fill(255, 0, 0); ellipse(100, 200, 80, 90); // x, y, width, height size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); fill(255, 0, 0); triangle(100, 20, 10, 100, 200, 100);// x1, y1, x2, y2, x3, y3 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); fill(255, 0, 0); point(100, 200);// x, y
정적스케치 (Static Sketch) 9 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); fill(255, 0, 0); quad(10, 20, 80, 90, 100, 200, 10, 100); // x1, y1, x2, y2, x3, y3, x4, y4 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); strokeweight(4); fill(255, 0, 0); arc(100, 200, 80, 90, 0, HALF_PI); // x, y, width, height, start, stop
기본형태 10
밝기애니메이션 11 size(600, 400); stroke(0, 0, 255); background(255, 122, 0); text("frame: " + framecount, 20, 20); fill(framecount % 256); rect(50, 50, 200, 200);
밝기애니메이션 12 size(600, 400); stroke(0, 0, 255); background(255, 122, 0); text("frame: " + framecount, 20, 20); pushstyle(); fill(framecount % 256); rect(50, 50, 200, 200); popstyle();
직선예술 13 size(600, 400); background(255); stroke(random(256), random(256), random(256)); line(random(width), random(height), random(width), random(height));
색상표 14 size(600, 400); background(255); nostroke(); colormode(hsb, 120); rectmode(center); translate(120, 30); for(int i = 0; i < 12; ++i) { fill(i * 10, 100, 119, 60); rect(0, 0, 30, 30); rotate(radians(30)); translate(40, 0);
마우스 15 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); line(200, 200, mousex, mousey); size(600, 400); stroke(0, 0, 255); background(255, 122, 0); line(200, 200, mousex, mousey);
마우스 16 size(600, 400); stroke(0, 0, 255); line(200, 200, mousex, mousey); void mousepressed() { background(255, 122, 0);
마우스 17 size(600, 400); stroke(0, 0, 255); line(200, 200, mousex, mousey); void mousepressed() { if(mousebutton == LEFT) background(255, 122, 0); else background(0, 128, 0);
마우스 18 mousebutton mouseclicked() mousedragged() mousemoved() mousepressed() mousereleased() mousewheel() mousex mousey pmousex pmousey
마우스 19 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); void mousedragged() { line(pmousex, pmousey, mousex, mousey);
마우스 20 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); background(255); fill(0, 255, 0); rect(mousex, mousey, 30, 30); size(600, 400); background(255, 122, 0); stroke(0, 0, 255); framerate(5); background(255); fill(0, 255, 0); rect(mousex, mousey, 30, 30);
키보드 21 size(600, 400); stroke(0, 0, 255); line(200, 200, mousex, mousey); void keypressed() { if(key == a ) background(255, 122, 0); else background(0, 128, 0);
키보드 22 key keycode keypressed() keypressed keyreleased() keytyped()
화면저장 23 size(600, 400); stroke(0, 0, 255); line(200, 200, mousex, mousey); void keypressed() { if(key == a ) saveframe("a.png"); else background(0, 128, 0);
응용프로그램만들기 24 파일 > 어플리케이션으로내보내기
햄스터
라이브러리사용 26 스케치 > 내부라이브러리... > hamster
기본동작 import org.roboid.robot.*; import processing.hamster.*; Hamster hamster; hamster = new Hamster(this); // don't forget 'draw' 27 void control() { // move forward hamster.write(hamster.left_wheel, 50); hamster.write(hamster.right_wheel, 50); delay(500); // move backward hamster.write(hamster.left_wheel, -50); hamster.write(hamster.right_wheel, -50); delay(500); // stop hamster.write(hamster.left_wheel, 0); hamster.write(hamster.right_wheel, 0); // disconnect hamster.dispose();
LED 켜기 import org.roboid.robot.*; import processing.hamster.*; 28 Hamster hamster; hamster = new Hamster(this); // dont' forget 'draw' void control() { hamster.write(hamster.left_led, Hamster.LED_RED); hamster.write(hamster.right_led, Hamster.LED_GREEN); delay(500); hamster.write(hamster.left_led, Hamster.LED_OFF); hamster.write(hamster.right_led, Hamster.LED_OFF); // disconnect hamster.dispose();
반복 import org.roboid.robot.*; import processing.hamster.*; 29 Hamster hamster; hamster = new Hamster(this); void repeat() { hamster.write(hamster.left_wheel, 50); hamster.write(hamster.right_wheel, 50); delay(500); hamster.write(hamster.left_wheel, -50); hamster.write(hamster.right_wheel, -50); delay(500); hamster.write(hamster.left_wheel, -50); hamster.write(hamster.right_wheel, 50); delay(500);
근접센서 import org.roboid.robot.*; import processing.hamster.*; 30 double note; new Hamster(this); void execute(robot hamster) { int proximity = hamster.read(hamster.left_proximity); if(proximity < 10) proximity = 0; note = (note * 9 + proximity) / 10; hamster.write(hamster.note, (int)note);
햄스터 + 그래픽
손따라가기 1/2 import org.roboid.robot.*; import processing.hamster.*; 32 Hamster hamster; int leftproximity; int rightproximity; size(200,200); nostroke(); hamster = new Hamster(this); background(255); fill(0); text("left: " + leftproximity, 28, 185); text("right: " + rightproximity, 125, 185); // draw bar graph rect(30, 20, 30, 150); rect(130, 20, 30, 150); fill(255); leftproximity = hamster.read(hamster.left_proximity); rightproximity = hamster.read(hamster.right_proximity); rect(30, 20, 30, leftproximity * 2); rect(130, 20, 30, rightproximity * 2);
손따라가기 2/2 33 void execute(robot hamster) { // left wheel if(leftproximity > 15) { hamster.write(hamster.left_wheel, (40 - leftproximity) * 4); else { hamster.write(hamster.left_wheel, 0); // right wheel if(rightproximity > 15) { hamster.write(hamster.right_wheel, (40 - rightproximity) * 4); else { hamster.write(hamster.right_wheel, 0);
마우스조종기 1/2 import org.roboid.robot.*; import processing.hamster.*; 34 int centerx, centery; size(200,200); new Hamster(this); centerx = 100; centery = 100; background(255); fill(0); text("press a button to move..", 10, 16); ellipse(100,100, 30, 30); line(100,100, mousex, mousey);
마우스조종기 2/2 35 void execute(robot hamster) { int dx = centerx - mousex; int dy = centery - mousey; hamster.write(hamster.left_wheel, 0); hamster.write(hamster.right_wheel, 0); if(!mousepressed) return; if(abs(dx) > 15 abs(dy) > 15) { if(dy < 0) { hamster.write(hamster.left_wheel, dy / 2 + dx / 2); hamster.write(hamster.right_wheel, dy / 2 - dx / 2); else { hamster.write(hamster.left_wheel, dy / 2 - dx / 2); hamster.write(hamster.right_wheel, dy / 2 + dx / 2);
키보드조종기 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster = new Hamster(this); 36 void keypressed() { if(key == CODED) { switch(keycode) { case UP: hamster.write(hamster.left_wheel, 30); hamster.write(hamster.right_wheel, 30); break; case DOWN: hamster.write(hamster.left_wheel, -30); hamster.write(hamster.right_wheel, -30); break; case LEFT: hamster.write(hamster.left_wheel, -30); hamster.write(hamster.right_wheel, 30); break; case RIGHT: hamster.write(hamster.left_wheel, 30); hamster.write(hamster.right_wheel, -30); break; else if(key == ' ') { hamster.write(hamster.left_wheel, 0); hamster.write(hamster.right_wheel, 0);
확장보드
확장보드 38
외부확장포트 39 보조전원단자 3.7V 리튬폴리머전지 외부입출력단자 ( 포트 A, 포트 B) 디지털입력, ADC 입력디지털출력, 아날로그 (PWM) 출력아날로그서보제어출력
핀 / 소켓배치살펴보기 40 VCC GND 포트 A 포트 B
디지털입력 : 버튼을누르면삐소리가나요 41 저항값읽는방법
디지털입력 : 버튼을누르면삐소리가나요 42 PORT B PORT B PUSH BUTTON GND PUSH BUTTON GND 플로팅상태
디지털입력 : 버튼을누르면삐소리가나요 43 PORT B VCC PUSH BUTTON GND PORT B VCC PUSH BUTTON GND
디지털입력 : 버튼을누르면삐소리가나요 44 풀업저항 PORT B VCC 220 PUSH BUTTON GND PORT B VCC 220 PUSH BUTTON GND
디지털입력 : 버튼을누르면삐소리가나요 45 풀다운저항 PORT B VCC PUSH BUTTON 220 GND PORT B VCC PUSH BUTTON 220 GND
디지털입력 : 버튼을누르면삐소리가나요 46 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster = new Hamster(this); hamster.write(hamster.io_mode_b, Hamster.IO_MODE_DI); void execute(robot hamster) { if(hamster.read(hamster.input_b) == 0) { hamster.write(hamster.buzzer, 1000); else { hamster.write(hamster.buzzer, 0); 추가활동 버튼을누르면햄스터로봇의 LED 에불이켜지고손을떼면꺼지도록해봅시다. 햄스터로봇이앞으로달려가다가버튼을누르면정지하도록해봅시다.
디지털출력 : 반짝반짝 LED 를깜박여요 Active High 47 Active Low
디지털출력 : 반짝반짝 LED 를깜박여요 48 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster; hamster = new Hamster(this); hamster.write(hamster.io_mode_a, Hamster.IO_MODE_DO); void repeat() { hamster.write(hamster.output_a, 1); delay(1000); hamster.write(hamster.output_a, 0); delay(1000); 추가활동앞으로달려가는햄스터로봇의앞을손으로막으면빨간색 LED 를깜박이면서뒤로후진하도록해봅시다.
아날로그입력 : 포텐셔미터를돌리면음높이가달라져요 49 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster = new Hamster(this); hamster.write(hamster.io_mode_a, Hamster.IO_MODE_ADC); void execute(robot hamster) { int hz = hamster.read(hamster.input_a) * 10; hamster.write(hamster.buzzer, hz);
아날로그입력 : 빛을따라움직여요 50 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster = new Hamster(this); hamster.write(hamster.io_mode_a, Hamster.IO_MODE_ADC); void execute(robot hamster) { if(hamster.read(hamster.light) > 180) { hamster.write(hamster.left_wheel, 30); hamster.write(hamster.right_wheel, 30); else if(hamster.read(hamster.input_a) > 180) { hamster.write(hamster.left_wheel, -30); hamster.write(hamster.right_wheel, -30); else { hamster.write(hamster.left_wheel, 0); hamster.write(hamster.right_wheel, 0);
PWM 출력 : LED 촛불이바람에흔들려요 51 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster; hamster = new Hamster(this); hamster.write(hamster.io_mode_a, Hamster.IO_MODE_PWM); hamster.write(hamster.io_mode_b, Hamster.IO_MODE_PWM); void repeat() { hamster.write(hamster.output_a, int(random(100, 256))); hamster.write(hamster.output_b, int(random(100, 256))); delay(int(random(0, 100)));
서보출력 : 햄스터로봇에게꼬리가생겼어요 import processing.hamster.*; import org.roboid.robot.*; 52 Hamster hamster; hamster = new Hamster(this); hamster.write(hamster.io_mode_a, Hamster.IO_MODE_SERVO); void repeat() { hamster.write(hamster.output_a, 10); delay(1000); hamster.write(hamster.output_a, 180); delay(1000);
수고하셨습니다. http://hamster.school akaii@kw.ac.kr