프로세싱 & 확장키트 광운대학교로봇학부박광현
프로세싱
개요 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); stroke(r, g, b); stroke(r, g, b, a); stroke(#ff7a00); stroke(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); nofill();
정적스케치 (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); HALF_PI, PI, QUARTER_PI, TWO_PI strokeweight(4); fill(255, 0, 0); arc(100, 200, 80, 90, 0, HALF_PI); // x, y, width, height, start, stop 라디안 (radian)
기본형태 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); 0 부터시작 draw() 호출때마다 1 씩증가
밝기애니메이션 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)); random(end); random(start, end); end 는포함안됨
마우스 14 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);
마우스 15 size(600, 400); stroke(0, 0, 255); line(200, 200, mousex, mousey); void mousepressed() { background(255, 122, 0);
마우스 16 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);
마우스 17 mousebutton mouseclicked() mousedragged() mousemoved() mousepressed() mousereleased() mousewheel() mousex mousey pmousex pmousey
마우스 18 size(600, 400); background(255, 122, 0); stroke(0, 0, 255); void mousedragged() { line(pmousex, pmousey, mousex, mousey);
마우스 19 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);
키보드 20 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);
키보드 21 key keycode keypressed() keypressed keyreleased() keytyped()
화면저장 22 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);
응용프로그램만들기 23 파일 > 어플리케이션으로내보내기
햄스터
라이브러리사용 25 스케치 > 내부라이브러리... > hamster
기본동작 26 import org.roboid.robot.*; import processing.hamster.*; Hamster hamster; hamster = new Hamster(this); // don't forget 'draw' createhamster() 로변경예정 run() 으로변경예정 void control() { // move forward hamster.write(hamster.left_wheel, 50); hamster.write(hamster.right_wheel, 50); delay(500); // ms // 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.*; 27 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.*; 28 Hamster hamster; hamster = new Hamster(this); void control() { for(int i = 0; i < 10; ++i) { 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.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);
근접센서 import org.roboid.robot.*; import processing.hamster.*; 30 Hamster hamster; hamster = new Hamster(this); void repeat() { int proximity = hamster.read(hamster.left_proximity); if(proximity < 50) { hamster.write(hamster.left_wheel, 50); hamster.write(hamster.right_wheel, 50); else { hamster.write(hamster.left_wheel, -50); hamster.write(hamster.right_wheel, -50);
라인트레이서 import org.roboid.robot.*; import processing.hamster.*; 31 Hamster hamster; hamster = new Hamster(this); void repeat() { int leftfloor = hamster.read(hamster.left_floor); int rightfloor = hamster.read(hamster.right_floor); int diff = leftfloor - rightfloor; hamster.write(hamster.left_wheel, int(30 + diff * 0.4)); hamster.write(hamster.right_wheel, int(30 - diff * 0.4));
멀티로봇 import org.roboid.robot.*; import processing.hamster.*; 32 Hamster hamster1; Hamster hamster2; hamster1 = new Hamster(this); hamster2 = new Hamster(this); void repeat() { hamster1.write(hamster.left_wheel, 50); hamster1.write(hamster.right_wheel, 50); hamster2.write(hamster.left_wheel, -50); hamster2.write(hamster.right_wheel, 50); delay(500); hamster1.write(hamster.left_wheel, -50); hamster1.write(hamster.right_wheel, -50); hamster2.write(hamster.left_wheel, 50); hamster2.write(hamster.right_wheel, -50); delay(500);
햄스터 + 그래픽
손따라가기 1/2 import org.roboid.robot.*; import processing.hamster.*; 34 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); rect(30, 20, 30, leftproximity * 2); rect(130, 20, 30, rightproximity * 2);
손따라가기 2/2 35 void repeat() { leftproximity = hamster.read(hamster.left_proximity); rightproximity = hamster.read(hamster.right_proximity); // 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.*; 36 Hamster hamster; int centerx, centery; size(200,200); centerx = 100; centery = 100; hamster = new Hamster(this); background(255); fill(0); text("press a button to move..", 10, 16); ellipse(100,100, 30, 30); line(100,100, mousex, mousey);
마우스조종기 2/2 37 void repeat() { 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; hamster = new Hamster(this); 38 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);
확장보드
확장보드 40
외부확장포트 41 보조전원단자 3.7V 리튬폴리머전지 외부입출력단자 ( 포트 A, 포트 B) 디지털입력, ADC 입력디지털출력, 아날로그 (PWM) 출력아날로그서보제어출력
핀 / 소켓배치살펴보기 42 VCC GND 포트 A 포트 B 포트 A 포트 B
디지털입력 : 버튼을누르면삐소리가나요 43 저항값읽는방법
디지털입력 : 버튼을누르면삐소리가나요 44 PORT B PORT B PUSH BUTTON GND PUSH BUTTON GND 플로팅상태
디지털입력 : 버튼을누르면삐소리가나요 45 PORT B VCC PUSH BUTTON GND PORT B VCC PUSH BUTTON GND
디지털입력 : 버튼을누르면삐소리가나요 46 풀업저항 PORT B VCC 220 PUSH BUTTON GND PORT B VCC 220 PUSH BUTTON GND
디지털입력 : 버튼을누르면삐소리가나요 47 풀다운저항 PORT B VCC PUSH BUTTON 220 GND PORT B VCC PUSH BUTTON 220 GND
디지털입력 : 버튼을누르면삐소리가나요 48 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster; hamster = new Hamster(this); hamster.write(hamster.io_mode_b, Hamster.IO_MODE_DI); void repeat() { if(hamster.read(hamster.input_b) == 0) { hamster.write(hamster.buzzer, 1000); else { hamster.write(hamster.buzzer, 0);
디지털출력 : 반짝반짝 LED 를깜박여요 Active High 49 Active Low
디지털출력 : 반짝반짝 LED 를깜박여요 50 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);
아날로그입력 : 포텐셔미터를돌리면음높이가달라져요 51 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster; hamster = new Hamster(this); hamster.write(hamster.io_mode_a, Hamster.IO_MODE_ADC); void repeat() { int hz = hamster.read(hamster.input_a) * 10; hamster.write(hamster.buzzer, hz);
아날로그입력 : 빛을따라움직여요 52 import processing.hamster.*; import org.roboid.robot.*; Hamster hamster; hamster = new Hamster(this); hamster.write(hamster.io_mode_a, Hamster.IO_MODE_ADC); void repeat() { 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) > 100) { 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 촛불이바람에흔들려요 53 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.*; 54 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);
미션 55 햄스터조종기 (2 인 1 조 ) 첫번째햄스터의포텐셔미터를돌려서 두번째햄스터를조종하기 두번째햄스터는방향에따라 LED 깜박이기
수고하셨습니다. http://hamster.school akaii@kw.ac.kr